github.com/googleapis/api-linter@v1.65.2/rules/aip0158/response_plural_first_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 aip0158 16 17 import ( 18 "testing" 19 20 "github.com/googleapis/api-linter/rules/internal/testutils" 21 ) 22 23 func TestResponsePluralFirstField(t *testing.T) { 24 tests := []struct { 25 testName string 26 MessageName string 27 problems testutils.Problems 28 }{ 29 {"Valid", "student_profiles", testutils.Problems{}}, 30 {"InvalidWrongSuffix", "student_profile", testutils.Problems{{Suggestion: "student_profiles"}}}, 31 {"ValidLatin", "cacti", testutils.Problems{}}, 32 {"InvalidLatin", "cactuses", testutils.Problems{{Suggestion: "cacti"}}}, 33 {"ValidNonstandard", "people", testutils.Problems{}}, 34 {"InvalidNonstandard", "persons", testutils.Problems{{Suggestion: "people"}}}, 35 } 36 for _, test := range tests { 37 t.Run(test.testName, func(t *testing.T) { 38 // Create the proto message. 39 f := testutils.ParseProto3Tmpl(t, ` 40 message Profile { 41 string name = 1; 42 } 43 44 message ListStudentProfilesResponse { 45 repeated Profile {{.MessageName}} = 1; 46 string next_page_token = 2; 47 } 48 `, test) 49 50 // Run the lint rule and establish we get the correct problems. 51 problems := responsePluralFirstField.Lint(f) 52 if diff := test.problems.SetDescriptor(f.GetMessageTypes()[1].FindFieldByNumber(1)).Diff(problems); diff != "" { 53 t.Errorf(diff) 54 } 55 }) 56 } 57 58 tests = []struct { 59 testName string 60 MessageName string 61 problems testutils.Problems 62 }{ 63 {"ValidResourcePlural", "library_books", testutils.Problems{}}, 64 {"InvalidResourcePlural", "books", testutils.Problems{{Suggestion: "library_books"}}}, 65 } 66 for _, test := range tests { 67 t.Run(test.testName, func(t *testing.T) { 68 // Create the proto message. 69 f := testutils.ParseProto3Tmpl(t, ` 70 import "google/api/resource.proto"; 71 72 message LibraryBook { 73 option (google.api.resource) = { 74 type: "example.com/LibraryBook" 75 pattern: "libraryBooks/{libraryBook}" 76 singular: "libraryBook" 77 plural: "libraryBooks" 78 }; 79 string name = 1; 80 } 81 82 message ListLibraryBooksResponse { 83 repeated LibraryBook {{.MessageName}} = 1; 84 string next_page_token = 2; 85 } 86 `, test) 87 88 // Run the lint rule and establish we get the correct problems. 89 problems := responsePluralFirstField.Lint(f) 90 if diff := test.problems.SetDescriptor(f.GetMessageTypes()[1].FindFieldByNumber(1)).Diff(problems); diff != "" { 91 t.Errorf(diff) 92 } 93 }) 94 } 95 96 t.Run("ValidNoFields", func(t *testing.T) { 97 f := testutils.ParseProto3Tmpl(t, ` 98 message ListStudentProfilesResponse {} 99 `, nil) 100 problems := responsePluralFirstField.Lint(f) 101 if diff := (testutils.Problems{}).Diff(problems); diff != "" { 102 t.Error(diff) 103 } 104 }) 105 }