github.com/googleapis/api-linter@v1.65.2/rules/aip0216/nesting_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 aip0216
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestNesting(t *testing.T) {
    24  	t.Run("Nested", func(t *testing.T) {
    25  		f := testutils.ParseProto3String(t, `
    26  			message Foo {
    27  				enum State {
    28  					STATE_UNSPECIFIED = 0;
    29  				}
    30  			}
    31  		`)
    32  		if diff := (testutils.Problems{}).Diff(nesting.Lint(f)); diff != "" {
    33  			t.Errorf(diff)
    34  		}
    35  	})
    36  	t.Run("NotNested", func(t *testing.T) {
    37  		tests := []struct {
    38  			name        string
    39  			MessageName string
    40  			EnumName    string
    41  			PackageStmt string
    42  			problems    testutils.Problems
    43  		}{
    44  			{"ShouldNest", "Foo", "FooState", "package test;", testutils.Problems{{Message: "Nest"}}},
    45  			{"ShouldNestNoPkg", "Foo", "FooState", "", testutils.Problems{{Message: "Nest"}}},
    46  			{"ValidNoFoo", "Bar", "FooState", "package test;", testutils.Problems{}},
    47  		}
    48  		for _, test := range tests {
    49  			t.Run(test.name, func(t *testing.T) {
    50  				f := testutils.ParseProto3Tmpl(t, `
    51  					{{.PackageStmt}}
    52  					message {{.MessageName}} {}
    53  					enum {{.EnumName}} {
    54  						UNSPECIFIED = 0;
    55  					}
    56  				`, test)
    57  				e := f.GetEnumTypes()[0]
    58  				if diff := test.problems.SetDescriptor(e).Diff(nesting.Lint(f)); diff != "" {
    59  					t.Errorf(diff)
    60  				}
    61  			})
    62  		}
    63  	})
    64  }