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

     1  package rules_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/yoheimuta/go-protoparser/v4/parser/meta"
     8  
     9  	"github.com/yoheimuta/go-protoparser/v4/parser"
    10  
    11  	"github.com/yoheimuta/protolint/internal/addon/rules"
    12  	"github.com/yoheimuta/protolint/linter/report"
    13  	"github.com/yoheimuta/protolint/linter/rule"
    14  )
    15  
    16  func TestEnumsHaveCommentRule_Apply(t *testing.T) {
    17  	tests := []struct {
    18  		name                         string
    19  		inputProto                   *parser.Proto
    20  		inputShouldFollowGolangStyle bool
    21  		wantFailures                 []report.Failure
    22  	}{
    23  		{
    24  			name: "no failures for proto without enum",
    25  			inputProto: &parser.Proto{
    26  				ProtoBody: []parser.Visitee{},
    27  			},
    28  		},
    29  		{
    30  			name: "no failures for proto including valid enums with comments",
    31  			inputProto: &parser.Proto{
    32  				ProtoBody: []parser.Visitee{
    33  					&parser.Enum{
    34  						EnumName: "EnumName",
    35  						Comments: []*parser.Comment{
    36  							{
    37  								Raw: "// a enum name.",
    38  							},
    39  						},
    40  					},
    41  					&parser.Enum{
    42  						EnumName: "EnumName2",
    43  						InlineComment: &parser.Comment{
    44  							Raw: "// a enum name.",
    45  						},
    46  					},
    47  					&parser.Enum{
    48  						EnumName: "EnumName3",
    49  						InlineCommentBehindLeftCurly: &parser.Comment{
    50  							Raw: "// a enum name.",
    51  						},
    52  					},
    53  				},
    54  			},
    55  		},
    56  		{
    57  			name: "no failures for proto including valid enums with Golang style comments",
    58  			inputProto: &parser.Proto{
    59  				ProtoBody: []parser.Visitee{
    60  					&parser.Enum{
    61  						EnumName: "EnumName",
    62  						Comments: []*parser.Comment{
    63  							{
    64  								Raw: "// EnumName is a enum name.",
    65  							},
    66  						},
    67  					},
    68  				},
    69  			},
    70  			inputShouldFollowGolangStyle: true,
    71  		},
    72  		{
    73  			name: "failures for proto with invalid enums",
    74  			inputProto: &parser.Proto{
    75  				ProtoBody: []parser.Visitee{
    76  					&parser.Enum{
    77  						EnumName: "EnumName",
    78  						Meta: meta.Meta{
    79  							Pos: meta.Position{
    80  								Filename: "example.proto",
    81  								Offset:   150,
    82  								Line:     7,
    83  								Column:   15,
    84  							},
    85  						},
    86  					},
    87  				},
    88  			},
    89  			wantFailures: []report.Failure{
    90  				report.Failuref(
    91  					meta.Position{
    92  						Filename: "example.proto",
    93  						Offset:   150,
    94  						Line:     7,
    95  						Column:   15,
    96  					},
    97  					"ENUMS_HAVE_COMMENT",
    98  					`Enum "EnumName" should have a comment`,
    99  				),
   100  			},
   101  		},
   102  		{
   103  			name: "failures for proto with invalid enums without Golang style comments",
   104  			inputProto: &parser.Proto{
   105  				ProtoBody: []parser.Visitee{
   106  					&parser.Enum{
   107  						EnumName: "EnumName",
   108  						Comments: []*parser.Comment{
   109  							{
   110  								Raw: "// a enum name.",
   111  							},
   112  						},
   113  						Meta: meta.Meta{
   114  							Pos: meta.Position{
   115  								Filename: "example.proto",
   116  								Offset:   150,
   117  								Line:     7,
   118  								Column:   15,
   119  							},
   120  						},
   121  					},
   122  				},
   123  			},
   124  			inputShouldFollowGolangStyle: true,
   125  			wantFailures: []report.Failure{
   126  				report.Failuref(
   127  					meta.Position{
   128  						Filename: "example.proto",
   129  						Offset:   150,
   130  						Line:     7,
   131  						Column:   15,
   132  					},
   133  					"ENUMS_HAVE_COMMENT",
   134  					`Enum "EnumName" should have a comment of the form "// EnumName ..."`,
   135  				),
   136  			},
   137  		},
   138  		{
   139  			name: "failures for proto with invalid enums without Golang style comments due to inline",
   140  			inputProto: &parser.Proto{
   141  				ProtoBody: []parser.Visitee{
   142  					&parser.Enum{
   143  						EnumName: "EnumName",
   144  						InlineComment: &parser.Comment{
   145  							Raw: "// EnumName is special.",
   146  						},
   147  					},
   148  					&parser.Enum{
   149  						EnumName: "EnumName2",
   150  						InlineCommentBehindLeftCurly: &parser.Comment{
   151  							Raw: "// EnumName2 is special.",
   152  						},
   153  					},
   154  				},
   155  			},
   156  			inputShouldFollowGolangStyle: true,
   157  			wantFailures: []report.Failure{
   158  				report.Failuref(
   159  					meta.Position{},
   160  					"ENUMS_HAVE_COMMENT",
   161  					`Enum "EnumName" should have a comment of the form "// EnumName ..."`,
   162  				),
   163  				report.Failuref(
   164  					meta.Position{},
   165  					"ENUMS_HAVE_COMMENT",
   166  					`Enum "EnumName2" should have a comment of the form "// EnumName2 ..."`,
   167  				),
   168  			},
   169  		},
   170  	}
   171  
   172  	for _, test := range tests {
   173  		test := test
   174  		t.Run(test.name, func(t *testing.T) {
   175  			rule := rules.NewEnumsHaveCommentRule(rule.SeverityError, test.inputShouldFollowGolangStyle)
   176  
   177  			got, err := rule.Apply(test.inputProto)
   178  			if err != nil {
   179  				t.Errorf("got err %v, but want nil", err)
   180  				return
   181  			}
   182  			if !reflect.DeepEqual(got, test.wantFailures) {
   183  				t.Errorf("got %v, but want %v", got, test.wantFailures)
   184  			}
   185  		})
   186  	}
   187  }