github.com/googleapis/api-linter@v1.65.2/rules/aip0192/has_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 TestFieldHasComments(t *testing.T) {
    24  	for _, tst := range []struct {
    25  		testName string
    26  		Comment  string
    27  		problems testutils.Problems
    28  	}{
    29  		{"Valid", "This is the title.", nil},
    30  		{"Invalid", "", testutils.Problems{{Message: `Missing comment over`}}},
    31  		{"InvalidInternal", "(-- Internal only comment --)", testutils.Problems{{Message: `Missing comment over`}}},
    32  	} {
    33  		file := testutils.ParseProto3Tmpl(t, `
    34  		// This is a book.
    35  		message Book {
    36  			// The resource name.
    37  			string name = 1;
    38  			// {{ .Comment }}
    39  			string title = 2;
    40  		}
    41  	`, tst)
    42  		problems := tst.problems.SetDescriptor(file.GetMessageTypes()[0].GetFields()[1])
    43  		if diff := problems.Diff(hasComments.Lint(file)); diff != "" {
    44  			t.Errorf("%s: got(+),want(-):\n%s", tst.testName, diff)
    45  		}
    46  	}
    47  }
    48  
    49  func TestMessageHasComments(t *testing.T) {
    50  	for _, tst := range []struct {
    51  		testName string
    52  		Comment  string
    53  		problems testutils.Problems
    54  	}{
    55  		{"Valid", "This is a book.", nil},
    56  		{"Invalid", "", testutils.Problems{{Message: `Missing comment over`}}},
    57  		{"InvalidInternal", "(-- Internal only comment --)", testutils.Problems{{Message: `Missing comment over`}}},
    58  	} {
    59  		file := testutils.ParseProto3Tmpl(t, `
    60  		// {{ .Comment }}
    61  		message Book {
    62  			// The resource name.
    63  			string name = 1;
    64  		}
    65  	`, tst)
    66  		problems := tst.problems.SetDescriptor(file.GetMessageTypes()[0])
    67  		if diff := problems.Diff(hasComments.Lint(file)); diff != "" {
    68  			t.Errorf("%s: got(+),want(-):\n%s", tst.testName, diff)
    69  		}
    70  	}
    71  }