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