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

     1  // Copyright 2021 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 aip4232
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestRepeatedFields(t *testing.T) {
    24  	tests := []struct {
    25  		testName  string
    26  		FirstSig  string
    27  		SecondSig string
    28  		problems  testutils.Problems
    29  	}{
    30  		{"Valid", "name,paperback_only,book.editions", "name,editions", nil},
    31  		{"InvalidFirstSignature", "name,book.shelves.shelf,paperback_only", "name,editions", testutils.Problems{{Message: "only the last"}}},
    32  		{"InvalidSecondSignature", "name,book.editions", "name,book.shelves.shelf,paperback_only", testutils.Problems{{Message: "only the last"}}},
    33  		{"BothInvalid", "name,book.shelves.shelf,paperback_only", "name,book.shelves.shelf,paperback_only", testutils.Problems{{Message: "only the last"}, {Message: "only the last"}}},
    34  	}
    35  
    36  	for _, test := range tests {
    37  		t.Run(test.testName, func(t *testing.T) {
    38  			f := testutils.ParseProto3Tmpl(t, `
    39  				import "google/api/client.proto";
    40  				service Library {
    41  					rpc ArchiveBook(ArchiveBookRequest) returns (ArchiveBookResponse) {
    42  						option (google.api.method_signature) = "{{.FirstSig}}";
    43  						option (google.api.method_signature) = "{{.SecondSig}}";
    44  					}
    45  				}
    46  				message ArchiveBookRequest {
    47  					string name = 1;
    48  
    49  					bool paperback_only = 2;
    50  
    51  					Book book = 3;
    52  				}
    53  				message Book {
    54  					string name = 1;
    55  
    56  					repeated int32 editions = 2;
    57  
    58  					repeated Shelf shelves = 3;
    59  				}
    60  				message Shelf {
    61  					string shelf = 1;
    62  				}
    63  				message ArchiveBookResponse {}
    64  			`, test)
    65  			method := f.GetServices()[0].GetMethods()[0]
    66  			if diff := test.problems.SetDescriptor(method).Diff(repeatedFields.Lint(f)); diff != "" {
    67  				t.Error(diff)
    68  			}
    69  		})
    70  	}
    71  }