github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/fileHasCommentRule_test.go (about)

     1  package rules_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/yoheimuta/go-protoparser/v4/parser"
     8  	"github.com/yoheimuta/go-protoparser/v4/parser/meta"
     9  	"github.com/yoheimuta/protolint/internal/addon/rules"
    10  	"github.com/yoheimuta/protolint/linter/report"
    11  	"github.com/yoheimuta/protolint/linter/rule"
    12  )
    13  
    14  func TestFileHasCommentRule_Apply(t *testing.T) {
    15  	tests := []struct {
    16  		name         string
    17  		inputProto   *parser.Proto
    18  		wantFailures []report.Failure
    19  	}{
    20  		{
    21  			name: "no failures for proto starting with a doc comment",
    22  			inputProto: &parser.Proto{
    23  				ProtoBody: []parser.Visitee{
    24  					&parser.Syntax{
    25  						Comments: []*parser.Comment{
    26  							{
    27  								Raw: "// this file defines something-something.",
    28  							},
    29  						},
    30  					},
    31  				},
    32  			},
    33  		},
    34  		{
    35  			name: "a failure for proto without any doc comments",
    36  			inputProto: &parser.Proto{
    37  				ProtoBody: []parser.Visitee{
    38  					&parser.Syntax{
    39  						Meta: meta.Meta{
    40  							Pos: meta.Position{
    41  								Filename: "example.proto",
    42  								Offset:   150,
    43  								Line:     7,
    44  								Column:   15,
    45  							},
    46  						},
    47  					},
    48  				},
    49  			},
    50  			wantFailures: []report.Failure{
    51  				report.Failuref(
    52  					meta.Position{
    53  						Filename: "example.proto",
    54  						Offset:   150,
    55  						Line:     7,
    56  						Column:   15,
    57  					},
    58  					"FILE_HAS_COMMENT",
    59  					`File should start with a doc comment`,
    60  				),
    61  			},
    62  		},
    63  	}
    64  
    65  	for _, test := range tests {
    66  		test := test
    67  		t.Run(test.name, func(t *testing.T) {
    68  			rule := rules.NewFileHasCommentRule(rule.SeverityError)
    69  
    70  			got, err := rule.Apply(test.inputProto)
    71  			if err != nil {
    72  				t.Errorf("got err %v, but want nil", err)
    73  				return
    74  			}
    75  			if !reflect.DeepEqual(got, test.wantFailures) {
    76  				t.Errorf("got %v, but want %v", got, test.wantFailures)
    77  			}
    78  		})
    79  	}
    80  }