github.com/googleapis/api-linter@v1.65.2/rules/aip0234/response_resource_field_test.go (about) 1 // Copyright 2019 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 aip0234 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 Field string 29 problems testutils.Problems 30 problemDesc func(m *desc.MessageDescriptor) desc.Descriptor 31 }{ 32 { 33 testName: "Valid", 34 Field: "repeated Book books", 35 problems: testutils.Problems{}, 36 }, 37 { 38 testName: "FieldIsNotRepeated", 39 Field: "Book book", 40 problems: testutils.Problems{{Message: "repeated"}}, 41 }, 42 { 43 testName: "MissingField", 44 Field: "string response", 45 problems: testutils.Problems{{Message: "no \"Book\" type field"}}, 46 problemDesc: func(m *desc.MessageDescriptor) desc.Descriptor { 47 return m 48 }, 49 }, 50 } 51 52 // Run each test individually. 53 for _, test := range tests { 54 t.Run(test.testName, func(t *testing.T) { 55 file := testutils.ParseProto3Tmpl(t, ` 56 message BatchUpdateBooksResponse { 57 {{.Field}} = 1; 58 } 59 message Book { 60 }`, test) 61 62 m := file.GetMessageTypes()[0] 63 64 // Determine the descriptor that a failing test will attach to. 65 var problemDesc desc.Descriptor = m.GetFields()[0] 66 if test.problemDesc != nil { 67 problemDesc = test.problemDesc(m) 68 } 69 70 problems := responseResourceField.Lint(file) 71 if diff := test.problems.SetDescriptor(problemDesc).Diff(problems); diff != "" { 72 t.Errorf(diff) 73 } 74 }) 75 } 76 }