|
| 1 | +// Copyright 2025 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 aip0160 |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/googleapis/api-linter/lint" |
| 22 | + "github.com/googleapis/api-linter/rules/internal/testutils" |
| 23 | +) |
| 24 | + |
| 25 | +func TestFiltersFieldName(t *testing.T) { |
| 26 | + tests := []struct { |
| 27 | + testName string |
| 28 | + FieldType string |
| 29 | + FieldName string |
| 30 | + MethodName string |
| 31 | + wantErr bool |
| 32 | + }{ |
| 33 | + {"Valid", "string", "filter", "ListBooks", false}, |
| 34 | + {"Valid different native type List method", "bytes", "filter", "ListBooks", false}, |
| 35 | + {"Valid different message type List method", "BookFilters", "filter", "ListBooks", false}, |
| 36 | + {"Valid different native type custom method", "bytes", "filter", "SearchBooks", false}, |
| 37 | + {"Valid different message type custom method", "BookFilters", "filter", "SearchBooks", false}, |
| 38 | + {"Invalid list method", "string", "filters", "ListBooks", true}, |
| 39 | + {"Invalid custom method", "string", "filters", "SearchBooks", true}, |
| 40 | + } |
| 41 | + for _, test := range tests { |
| 42 | + t.Run(test.testName, func(t *testing.T) { |
| 43 | + file := testutils.ParseProto3Tmpl(t, ` |
| 44 | + service Library { |
| 45 | + rpc {{.MethodName}}({{.MethodName}}Request) returns ({{.MethodName}}Response) {} |
| 46 | + rpc GetBook(GetBookRequest) returns (Book) {} |
| 47 | + } |
| 48 | + message {{.MethodName}}Request { |
| 49 | + {{.FieldType}} {{.FieldName}} = 1; |
| 50 | + } |
| 51 | + message {{.MethodName}}Response {} |
| 52 | +
|
| 53 | + message GetBookRequest {} |
| 54 | + message Book { string filters = 1; } |
| 55 | +
|
| 56 | + message BookFilters {} |
| 57 | + `, test) |
| 58 | + field := file.GetMessageTypes()[0].GetFields()[0] |
| 59 | + wantProblems := testutils.Problems{} |
| 60 | + if test.wantErr { |
| 61 | + wantProblems = append(wantProblems, lint.Problem{ |
| 62 | + Message: fmt.Sprintf(`"string filter", not "%s %s`, test.FieldType, test.FieldName), |
| 63 | + Suggestion: "filter", |
| 64 | + Descriptor: field, |
| 65 | + }) |
| 66 | + } |
| 67 | + if diff := wantProblems.Diff(filterFieldName.Lint(file)); diff != "" { |
| 68 | + t.Error(diff) |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
0 commit comments