github.com/googleapis/api-linter@v1.65.2/rules/aip0235/response_resource_field_test.go (about)

     1  // Copyright 2020 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aip0235
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  	"github.com/jhump/protoreflect/desc"
    22  )
    23  
    24  func TestResponseResourceField(t *testing.T) {
    25  	// Set up the testing permutations.
    26  	tests := []struct {
    27  		testName    string
    28  		Message     string
    29  		Field       string
    30  		problems    testutils.Problems
    31  		problemDesc func(m *desc.MessageDescriptor) desc.Descriptor
    32  	}{
    33  		{
    34  			testName: "Valid",
    35  			Message:  "BatchDeleteBooksResponse",
    36  			Field:    "repeated Book books",
    37  			problems: testutils.Problems{},
    38  		},
    39  		{
    40  			testName: "FieldIsNotRepeated",
    41  			Message:  "BatchDeleteBooksResponse",
    42  			Field:    "Book book",
    43  			problems: testutils.Problems{{Message: "repeated"}},
    44  		},
    45  		{
    46  			testName: "MissingField",
    47  			Message:  "BatchDeleteBooksResponse",
    48  			Field:    "string response",
    49  			problems: testutils.Problems{{Message: `no "Book" type field`}},
    50  			problemDesc: func(m *desc.MessageDescriptor) desc.Descriptor {
    51  				return m
    52  			},
    53  		},
    54  		{
    55  			testName: "IrrelevantMessage",
    56  			Message:  "DeleteBookResponse",
    57  			Field:    "Book books",
    58  			problems: testutils.Problems{},
    59  		},
    60  	}
    61  
    62  	// Run each test individually.
    63  	for _, test := range tests {
    64  		t.Run(test.testName, func(t *testing.T) {
    65  			file := testutils.ParseProto3Tmpl(t, `
    66  				message {{.Message}} {
    67  					{{.Field}} = 1;
    68  				}
    69  				message Book {
    70  				}`, test)
    71  
    72  			m := file.GetMessageTypes()[0]
    73  
    74  			// Determine the descriptor that a failing test will attach to.
    75  			var problemDesc desc.Descriptor = m.GetFields()[0]
    76  			if test.problemDesc != nil {
    77  				problemDesc = test.problemDesc(m)
    78  			}
    79  
    80  			problems := responseResourceField.Lint(file)
    81  			if diff := test.problems.SetDescriptor(problemDesc).Diff(problems); diff != "" {
    82  				t.Errorf(diff)
    83  			}
    84  		})
    85  	}
    86  }