github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/fieldNamesExcludePrepositionsRule_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 TestFieldNamesExcludePrepositionsRule_Apply(t *testing.T) {
    17  	tests := []struct {
    18  		name              string
    19  		inputProto        *parser.Proto
    20  		inputPrepositions []string
    21  		inputExcludes     []string
    22  		wantFailures      []report.Failure
    23  	}{
    24  		{
    25  			name: "no failures for proto without fields",
    26  			inputProto: &parser.Proto{
    27  				ProtoBody: []parser.Visitee{
    28  					&parser.Enum{},
    29  				},
    30  			},
    31  		},
    32  		{
    33  			name: "no failures for proto with valid field names",
    34  			inputProto: &parser.Proto{
    35  				ProtoBody: []parser.Visitee{
    36  					&parser.Service{},
    37  					&parser.Message{
    38  						MessageBody: []parser.Visitee{
    39  							&parser.Field{
    40  								FieldName: "error_reason",
    41  							},
    42  							&parser.Field{
    43  								FieldName: "failure_time_cpu_usage",
    44  							},
    45  							&parser.MapField{
    46  								MapName: "song_name2",
    47  							},
    48  							&parser.Oneof{
    49  								OneofFields: []*parser.OneofField{
    50  									{
    51  										FieldName: "song_name3",
    52  									},
    53  								},
    54  							},
    55  						},
    56  					},
    57  				},
    58  			},
    59  		},
    60  		{
    61  			name: "failures for proto with invalid field names",
    62  			inputProto: &parser.Proto{
    63  				ProtoBody: []parser.Visitee{
    64  					&parser.Message{
    65  						MessageBody: []parser.Visitee{
    66  							&parser.Field{
    67  								FieldName: "reason_for_error",
    68  								Meta: meta.Meta{
    69  									Pos: meta.Position{
    70  										Filename: "example.proto",
    71  										Offset:   100,
    72  										Line:     5,
    73  										Column:   10,
    74  									},
    75  								},
    76  							},
    77  							&parser.Field{
    78  								FieldName: "cpu_usage_at_time_of_failure",
    79  								Meta: meta.Meta{
    80  									Pos: meta.Position{
    81  										Filename: "example.proto",
    82  										Offset:   200,
    83  										Line:     10,
    84  										Column:   20,
    85  									},
    86  								},
    87  							},
    88  							&parser.MapField{
    89  								MapName: "name_of_song",
    90  								Meta: meta.Meta{
    91  									Pos: meta.Position{
    92  										Filename: "example.proto",
    93  										Offset:   210,
    94  										Line:     14,
    95  										Column:   30,
    96  									},
    97  								},
    98  							},
    99  							&parser.Oneof{
   100  								OneofFields: []*parser.OneofField{
   101  									{
   102  										FieldName: "name_of_song2",
   103  										Meta: meta.Meta{
   104  											Pos: meta.Position{
   105  												Filename: "example.proto",
   106  												Offset:   300,
   107  												Line:     21,
   108  												Column:   45,
   109  											},
   110  										},
   111  									},
   112  								},
   113  							},
   114  						},
   115  					},
   116  				},
   117  			},
   118  			wantFailures: []report.Failure{
   119  				report.Failuref(
   120  					meta.Position{
   121  						Filename: "example.proto",
   122  						Offset:   100,
   123  						Line:     5,
   124  						Column:   10,
   125  					},
   126  					"FIELD_NAMES_EXCLUDE_PREPOSITIONS",
   127  					`Field name "reason_for_error" should not include a preposition "for"`,
   128  				),
   129  				report.Failuref(
   130  					meta.Position{
   131  						Filename: "example.proto",
   132  						Offset:   200,
   133  						Line:     10,
   134  						Column:   20,
   135  					},
   136  					"FIELD_NAMES_EXCLUDE_PREPOSITIONS",
   137  					`Field name "cpu_usage_at_time_of_failure" should not include a preposition "at"`,
   138  				),
   139  				report.Failuref(
   140  					meta.Position{
   141  						Filename: "example.proto",
   142  						Offset:   200,
   143  						Line:     10,
   144  						Column:   20,
   145  					},
   146  					"FIELD_NAMES_EXCLUDE_PREPOSITIONS",
   147  					`Field name "cpu_usage_at_time_of_failure" should not include a preposition "of"`,
   148  				),
   149  				report.Failuref(
   150  					meta.Position{
   151  						Filename: "example.proto",
   152  						Offset:   210,
   153  						Line:     14,
   154  						Column:   30,
   155  					},
   156  					"FIELD_NAMES_EXCLUDE_PREPOSITIONS",
   157  					`Field name "name_of_song" should not include a preposition "of"`,
   158  				),
   159  				report.Failuref(
   160  					meta.Position{
   161  						Filename: "example.proto",
   162  						Offset:   300,
   163  						Line:     21,
   164  						Column:   45,
   165  					},
   166  					"FIELD_NAMES_EXCLUDE_PREPOSITIONS",
   167  					`Field name "name_of_song2" should not include a preposition "of"`,
   168  				),
   169  			},
   170  		},
   171  		{
   172  			name: "failures for proto with invalid field names, but field name including the excluded keyword is no problem",
   173  			inputProto: &parser.Proto{
   174  				ProtoBody: []parser.Visitee{
   175  					&parser.Message{
   176  						MessageBody: []parser.Visitee{
   177  							&parser.Field{
   178  								FieldName: "end_of_support_version",
   179  							},
   180  							&parser.MapField{
   181  								MapName: "end_of_support_version",
   182  							},
   183  							&parser.Oneof{
   184  								OneofFields: []*parser.OneofField{
   185  									{
   186  										FieldName: "end_of_support_version",
   187  									},
   188  								},
   189  							},
   190  							&parser.Field{
   191  								FieldName: "version_of_support_end",
   192  								Meta: meta.Meta{
   193  									Pos: meta.Position{
   194  										Filename: "example.proto",
   195  										Offset:   200,
   196  										Line:     10,
   197  										Column:   20,
   198  									},
   199  								},
   200  							},
   201  						},
   202  					},
   203  				},
   204  			},
   205  			inputExcludes: []string{
   206  				"end_of_support",
   207  			},
   208  			wantFailures: []report.Failure{
   209  				report.Failuref(
   210  					meta.Position{
   211  						Filename: "example.proto",
   212  						Offset:   200,
   213  						Line:     10,
   214  						Column:   20,
   215  					},
   216  					"FIELD_NAMES_EXCLUDE_PREPOSITIONS",
   217  					`Field name "version_of_support_end" should not include a preposition "of"`,
   218  				),
   219  			},
   220  		},
   221  	}
   222  
   223  	for _, test := range tests {
   224  		test := test
   225  		t.Run(test.name, func(t *testing.T) {
   226  			rule := rules.NewFieldNamesExcludePrepositionsRule(rule.SeverityError, test.inputPrepositions, test.inputExcludes)
   227  
   228  			got, err := rule.Apply(test.inputProto)
   229  			if err != nil {
   230  				t.Errorf("got err %v, but want nil", err)
   231  				return
   232  			}
   233  			if !reflect.DeepEqual(got, test.wantFailures) {
   234  				t.Errorf("got %v, but want %v", got, test.wantFailures)
   235  			}
   236  		})
   237  	}
   238  }