github.com/googleapis/api-linter@v1.65.2/rules/aip0126/unspecified_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 aip0126
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestUnspecified(t *testing.T) {
    24  	tests := []struct {
    25  		testName  string
    26  		EnumName  string
    27  		ValueName string
    28  		problems  testutils.Problems
    29  	}{
    30  		{"Valid", "BookFormat", "BOOK_FORMAT_UNSPECIFIED", testutils.Problems{}},
    31  		{"ValidWithNum", "Ipv6Format", "IPV6_FORMAT_UNSPECIFIED", nil},
    32  		{"ValidUnknown", "BookFormat", "UNKNOWN", nil},
    33  		{"InvalidNoPrefix", "BookFormat", "UNSPECIFIED", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}},
    34  		{"InvalidWrongSuffix", "BookFormat", "BOOK_FORMAT_UNKNOWN", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}},
    35  		{"InvalidWithNum", "Ipv6Format", "IPV6FORMAT_UNSPECIFIED", testutils.Problems{{Suggestion: "IPV6_FORMAT_UNSPECIFIED"}}},
    36  	}
    37  	for _, test := range tests {
    38  		t.Run(test.testName, func(t *testing.T) {
    39  			// Create the proto with the enum.
    40  			f := testutils.ParseProto3Tmpl(t, `
    41  				enum {{.EnumName}} {
    42  					{{.ValueName}} = 0;
    43  					HARDBACK = 1;
    44  					PAPERBACK = 2;
    45  				}
    46  			`, test)
    47  
    48  			// Run the lint rule and establish we get the correct problems.
    49  			problems := unspecified.Lint(f)
    50  			if diff := test.problems.SetDescriptor(f.GetEnumTypes()[0].GetValues()[0]).Diff(problems); diff != "" {
    51  				t.Errorf(diff)
    52  			}
    53  		})
    54  	}
    55  	for _, test := range tests {
    56  		t.Run(test.testName, func(t *testing.T) {
    57  			// Create the proto with the enum.
    58  			f := testutils.ParseProto3Tmpl(t, `
    59  				enum {{.EnumName}} {
    60  					option allow_alias = true;
    61  					HARDBACK = 0;
    62  					{{.ValueName}} = 0;
    63  					PAPERBACK = 1;
    64  				}
    65  			`, test)
    66  
    67  			// Run the lint rule and establish we get the correct problems.
    68  			problems := unspecified.Lint(f)
    69  			if diff := test.problems.SetDescriptor(f.GetEnumTypes()[0].GetValues()[0]).Diff(problems); diff != "" {
    70  				t.Errorf(diff)
    71  			}
    72  		})
    73  	}
    74  }