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

     1  // Copyright 2023 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  package aip0123
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/googleapis/api-linter/rules/internal/testutils"
    20  )
    21  
    22  func TestResourcePlural(t *testing.T) {
    23  	for _, test := range []struct {
    24  		name     string
    25  		Plural   string
    26  		problems testutils.Problems
    27  	}{
    28  		{
    29  			"Valid",
    30  			`plural: "bookShelves"`,
    31  			nil,
    32  		},
    33  		{
    34  			"InvalidMissing",
    35  			``,
    36  			testutils.Problems{{
    37  				Message: "Resources should declare plural",
    38  			}},
    39  		},
    40  		{
    41  			"InvalidUpperCamel",
    42  			`plural: "BookShelves"`,
    43  			testutils.Problems{{
    44  				Message: "Resource plural should be lowerCamelCase",
    45  			}},
    46  		},
    47  		{
    48  			"InvalidDash",
    49  			`plural: "Book-Shelves"`,
    50  			testutils.Problems{{
    51  				Message: "Resource plural should be lowerCamelCase",
    52  			}},
    53  		},
    54  	} {
    55  		t.Run(test.name, func(t *testing.T) {
    56  			f := testutils.ParseProto3Tmpl(t, `
    57  			import "google/api/resource.proto";
    58  			message Book {
    59  				option (google.api.resource) = {
    60  					type: "library.googleapis.com/BookShelf"
    61  					pattern: "publishers/{publisher}/bookShelves/{book_shelf}"
    62  					{{.Plural}}
    63  				};
    64  				string name = 1;
    65  			}
    66  			`, test)
    67  			m := f.GetMessageTypes()[0]
    68  			if diff := test.problems.SetDescriptor(m).Diff(resourcePlural.Lint(f)); diff != "" {
    69  				t.Error(diff)
    70  			}
    71  		})
    72  	}
    73  }