github.com/googleapis/api-linter@v1.65.2/rules/aip0192/only_leading_comments_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 aip0192
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestOnlyLeadingComments(t *testing.T) {
    24  	tests := []struct {
    25  		testName string
    26  		Detached string
    27  		Trailing string
    28  		problems testutils.Problems
    29  	}{
    30  		{"ValidNone", "", "", testutils.Problems{}},
    31  		{"ValidInternalDetached", "// (-- detached --)", "", testutils.Problems{}},
    32  		{"ValidInternalTrailing", "", "// (-- trailing --)", testutils.Problems{}},
    33  		{"ValidInternalTrailingNoClose", "", "// (-- trailing", testutils.Problems{}},
    34  		{
    35  			"ValidInternalBoth",
    36  			"// (-- detached --)",
    37  			"// (-- trailing --)",
    38  			testutils.Problems{},
    39  		},
    40  		{
    41  			"ValidInternalBothDouble",
    42  			"// (-- detached --)\n// (-- detached --)",
    43  			"// (-- trailing --)\n// (-- trailing --)",
    44  			testutils.Problems{},
    45  		},
    46  		{"InvalidDetached", "// detached", "", testutils.Problems{{Message: "empty lines"}}},
    47  		{"InvalidTrailing", "", "// trailing", testutils.Problems{{Message: "trailing"}}},
    48  		{
    49  			"InvalidBoth",
    50  			"// detached",
    51  			"// trailing",
    52  			testutils.Problems{{Message: "trailing"}, {Message: "empty lines"}},
    53  		},
    54  		{
    55  			"InvalidPartialDetachedInternalFirst",
    56  			"// (-- detached --) detached",
    57  			"",
    58  			testutils.Problems{{Message: "empty lines"}},
    59  		},
    60  		{
    61  			"InvalidPartialDetachedExternalFirst",
    62  			"// detached (-- detached --)",
    63  			"",
    64  			testutils.Problems{{Message: "empty lines"}},
    65  		},
    66  		{
    67  			"InvalidPartialDetachedSandwich",
    68  			"// (-- detached --) detached (-- detached --)",
    69  			"",
    70  			testutils.Problems{{Message: "empty lines"}},
    71  		},
    72  	}
    73  	for _, test := range tests {
    74  		t.Run(test.testName, func(t *testing.T) {
    75  			file := testutils.ParseProto3Tmpl(t, `
    76  				{{.Detached}}
    77  
    78  				// The book.
    79  				message Book {
    80  					{{.Trailing}}
    81  
    82  					string name = 1;
    83  				}
    84  				
    85  			`, test)
    86  			problems := onlyLeadingComments.Lint(file)
    87  			if diff := test.problems.SetDescriptor(file.GetMessageTypes()[0]).Diff(problems); diff != "" {
    88  				t.Errorf(diff)
    89  			}
    90  		})
    91  	}
    92  }