github.com/googleapis/api-linter@v1.65.2/rules/aip0140/abbreviations_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 aip0140
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/googleapis/api-linter/rules/internal/testutils"
    22  	"github.com/jhump/protoreflect/desc"
    23  	"github.com/stoewer/go-strcase"
    24  )
    25  
    26  func TestAbbreviations(t *testing.T) {
    27  	ruleGroups := []struct {
    28  		name     string
    29  		tmpl     string
    30  		caseFunc func(string) string
    31  		descFunc func(*desc.FileDescriptor) desc.Descriptor
    32  	}{
    33  		{
    34  			name:     "Field",
    35  			tmpl:     `message Book { string {{.Name}} = 1; }`,
    36  			caseFunc: strcase.SnakeCase,
    37  			descFunc: func(f *desc.FileDescriptor) desc.Descriptor {
    38  				return f.GetMessageTypes()[0].GetFields()[0]
    39  			},
    40  		},
    41  		{
    42  			name:     "Message",
    43  			tmpl:     `message {{.Name}} {}`,
    44  			caseFunc: strcase.UpperCamelCase,
    45  			descFunc: func(f *desc.FileDescriptor) desc.Descriptor {
    46  				return f.GetMessageTypes()[0]
    47  			},
    48  		},
    49  		{
    50  			name:     "Service",
    51  			tmpl:     `service {{.Name}} {}`,
    52  			caseFunc: strcase.UpperCamelCase,
    53  			descFunc: func(f *desc.FileDescriptor) desc.Descriptor {
    54  				return f.GetServices()[0]
    55  			},
    56  		},
    57  		{
    58  			name: "Method",
    59  			tmpl: `
    60  				service Library {
    61  					rpc {{.Name}}(Request) returns (Response);
    62  				}
    63  
    64  				message Request {}
    65  				message Response {}
    66  			`,
    67  			caseFunc: strcase.UpperCamelCase,
    68  			descFunc: func(f *desc.FileDescriptor) desc.Descriptor {
    69  				return f.GetServices()[0].GetMethods()[0]
    70  			},
    71  		},
    72  		{
    73  			name:     "Enum",
    74  			tmpl:     `enum {{.Name}} { UNSPECIFIED = 0; }`,
    75  			caseFunc: strcase.UpperCamelCase,
    76  			descFunc: func(f *desc.FileDescriptor) desc.Descriptor {
    77  				return f.GetEnumTypes()[0]
    78  			},
    79  		},
    80  		{
    81  			name: "EnumValue",
    82  			tmpl: `enum Thing { {{.Name}} = 0; }`,
    83  			caseFunc: func(s string) string {
    84  				return strings.ToUpper(strcase.SnakeCase(s))
    85  			},
    86  			descFunc: func(f *desc.FileDescriptor) desc.Descriptor {
    87  				return f.GetEnumTypes()[0].GetValues()[0]
    88  			},
    89  		},
    90  	}
    91  	for _, group := range ruleGroups {
    92  		t.Run(group.name, func(t *testing.T) {
    93  			for _, test := range buildTests(group.caseFunc) {
    94  				t.Run(test.Name, func(t *testing.T) {
    95  					// Build the file descriptor, and retrieve the descriptor we
    96  					// expect any problems to be attached to.
    97  					file := testutils.ParseProto3Tmpl(t, group.tmpl, test)
    98  					d := group.descFunc(file)
    99  
   100  					// Establish that we get the problems we expect.
   101  					problems := abbreviations.Lint(file)
   102  					if diff := test.problems.SetDescriptor(d).Diff(problems); diff != "" {
   103  						t.Errorf(diff)
   104  					}
   105  				})
   106  			}
   107  		})
   108  	}
   109  }
   110  
   111  type abbvTest struct {
   112  	Name     string
   113  	problems testutils.Problems
   114  }
   115  
   116  func buildTests(caseFunc func(string) string) []abbvTest {
   117  	return []abbvTest{
   118  		{caseFunc("book_configuration"), testutils.Problems{{Suggestion: caseFunc("book_config")}}},
   119  		{caseFunc("book_config"), testutils.Problems{}},
   120  		{caseFunc("book_identifier"), testutils.Problems{{Suggestion: caseFunc("book_id")}}},
   121  		{caseFunc("book_id"), testutils.Problems{}},
   122  		{caseFunc("book_information"), testutils.Problems{{Suggestion: caseFunc("book_info")}}},
   123  		{caseFunc("book_info"), testutils.Problems{}},
   124  		{caseFunc("book_specification"), testutils.Problems{{Suggestion: caseFunc("book_spec")}}},
   125  		{caseFunc("book_spec"), testutils.Problems{}},
   126  		{caseFunc("book_statistics"), testutils.Problems{{Suggestion: caseFunc("book_stats")}}},
   127  		{caseFunc("book_stats"), testutils.Problems{}},
   128  		{caseFunc("informational_book"), testutils.Problems{}},
   129  	}
   130  }