github.com/googleapis/api-linter@v1.65.2/rules/aip0233/request_requests_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 aip0233 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 TestRequestRequestsField(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 CreateBookRequest requests = 1;", 35 problems: testutils.Problems{}, 36 }, 37 { 38 testName: "Invalid-MissingRequestsField", 39 Field: "string parent = 1;", 40 problems: testutils.Problems{{Message: `no "requests" field`}}, 41 }, 42 { 43 testName: "Invalid-RequestsFieldIsNotRepeated", 44 Field: "CreateBookRequest requests = 1;", 45 problems: testutils.Problems{{Message: "repeated"}}, 46 problemDesc: func(m *desc.MessageDescriptor) desc.Descriptor { 47 return m.FindFieldByName("requests") 48 }, 49 }, 50 { 51 testName: "Invalid-RequestsFieldWrongType", 52 Field: "repeated int32 requests = 1;", 53 problems: testutils.Problems{{ 54 Suggestion: "CreateBookRequest", 55 }}, 56 problemDesc: func(m *desc.MessageDescriptor) desc.Descriptor { 57 return m.FindFieldByName("requests") 58 }, 59 }, 60 { 61 testName: "Invalid-RequestsNotRepeatedWrongType", 62 Field: "int32 requests = 1;", 63 problems: testutils.Problems{ 64 {Message: "repeated"}, 65 { 66 Suggestion: "CreateBookRequest", 67 }, 68 }, 69 problemDesc: func(m *desc.MessageDescriptor) desc.Descriptor { 70 return m.FindFieldByName("requests") 71 }, 72 }, 73 } 74 75 // Run each test individually. 76 for _, test := range tests { 77 t.Run(test.testName, func(t *testing.T) { 78 file := testutils.ParseProto3Tmpl(t, ` 79 message BatchCreateBooksRequest { 80 {{.Field}} 81 } 82 83 message CreateBookRequest {} 84 `, test) 85 86 m := file.GetMessageTypes()[0] 87 88 // Determine the descriptor that a failing test will attach to. 89 var problemDesc desc.Descriptor = m 90 if test.problemDesc != nil { 91 problemDesc = test.problemDesc(m) 92 } 93 94 problems := requestRequestsField.Lint(file) 95 if diff := test.problems.SetDescriptor(problemDesc).Diff(problems); diff != "" { 96 t.Errorf(diff) 97 } 98 }) 99 } 100 }