github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/enumFieldNamesUpperSnakeCaseRule_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  
    10  	"github.com/yoheimuta/protolint/internal/addon/rules"
    11  	"github.com/yoheimuta/protolint/linter/autodisable"
    12  	"github.com/yoheimuta/protolint/linter/report"
    13  	"github.com/yoheimuta/protolint/linter/rule"
    14  )
    15  
    16  func TestEnumFieldNamesUpperSnakeCaseRule_Apply(t *testing.T) {
    17  	tests := []struct {
    18  		name         string
    19  		inputProto   *parser.Proto
    20  		wantFailures []report.Failure
    21  	}{
    22  		{
    23  			name: "no failures for proto without enum fields",
    24  			inputProto: &parser.Proto{
    25  				ProtoBody: []parser.Visitee{
    26  					&parser.Enum{},
    27  				},
    28  			},
    29  		},
    30  		{
    31  			name: "no failures for proto with valid enum field names",
    32  			inputProto: &parser.Proto{
    33  				ProtoBody: []parser.Visitee{
    34  					&parser.Service{},
    35  					&parser.Enum{
    36  						EnumBody: []parser.Visitee{
    37  							&parser.EnumField{
    38  								Ident: "FIRST_VALUE",
    39  							},
    40  							&parser.EnumField{
    41  								Ident: "SECOND_VALUE",
    42  							},
    43  						},
    44  					},
    45  				},
    46  			},
    47  		},
    48  		{
    49  			name: "failures for proto with invalid enum field names",
    50  			inputProto: &parser.Proto{
    51  				ProtoBody: []parser.Visitee{
    52  					&parser.Enum{
    53  						EnumBody: []parser.Visitee{
    54  							&parser.EnumField{
    55  								Ident: "fIRST_VALUE",
    56  								Meta: meta.Meta{
    57  									Pos: meta.Position{
    58  										Filename: "example.proto",
    59  										Offset:   100,
    60  										Line:     5,
    61  										Column:   10,
    62  									},
    63  								},
    64  							},
    65  							&parser.EnumField{
    66  								Ident: "secondValue",
    67  								Meta: meta.Meta{
    68  									Pos: meta.Position{
    69  										Filename: "example.proto",
    70  										Offset:   200,
    71  										Line:     10,
    72  										Column:   20,
    73  									},
    74  								},
    75  							},
    76  						},
    77  					},
    78  				},
    79  			},
    80  			wantFailures: []report.Failure{
    81  				report.Failuref(
    82  					meta.Position{
    83  						Filename: "example.proto",
    84  						Offset:   100,
    85  						Line:     5,
    86  						Column:   10,
    87  					},
    88  					"ENUM_FIELD_NAMES_UPPER_SNAKE_CASE",
    89  					`EnumField name "fIRST_VALUE" must be CAPITALS_WITH_UNDERSCORES like "FIRST_VALUE"`,
    90  				),
    91  				report.Failuref(
    92  					meta.Position{
    93  						Filename: "example.proto",
    94  						Offset:   200,
    95  						Line:     10,
    96  						Column:   20,
    97  					},
    98  					"ENUM_FIELD_NAMES_UPPER_SNAKE_CASE",
    99  					`EnumField name "secondValue" must be CAPITALS_WITH_UNDERSCORES like "SECOND_VALUE"`,
   100  				),
   101  			},
   102  		},
   103  	}
   104  
   105  	for _, test := range tests {
   106  		test := test
   107  		t.Run(test.name, func(t *testing.T) {
   108  			rule := rules.NewEnumFieldNamesUpperSnakeCaseRule(rule.SeverityError, false, autodisable.Noop)
   109  
   110  			got, err := rule.Apply(test.inputProto)
   111  			if err != nil {
   112  				t.Errorf("got err %v, but want nil", err)
   113  				return
   114  			}
   115  			if !reflect.DeepEqual(got, test.wantFailures) {
   116  				t.Errorf("got %v, but want %v", got, test.wantFailures)
   117  			}
   118  		})
   119  	}
   120  }
   121  
   122  func TestEnumFieldNamesUpperSnakeCaseRule_Apply_fix(t *testing.T) {
   123  	tests := []struct {
   124  		name          string
   125  		inputFilename string
   126  		wantFilename  string
   127  	}{
   128  		{
   129  			name:          "no fix for a correct proto",
   130  			inputFilename: "upperSnakeCase.proto",
   131  			wantFilename:  "upperSnakeCase.proto",
   132  		},
   133  		{
   134  			name:          "fix for an incorrect proto",
   135  			inputFilename: "invalid.proto",
   136  			wantFilename:  "upperSnakeCase.proto",
   137  		},
   138  	}
   139  
   140  	for _, test := range tests {
   141  		test := test
   142  		t.Run(test.name, func(t *testing.T) {
   143  			r := rules.NewEnumFieldNamesUpperSnakeCaseRule(rule.SeverityError, true, autodisable.Noop)
   144  			testApplyFix(t, r, test.inputFilename, test.wantFilename)
   145  		})
   146  	}
   147  }
   148  
   149  func TestEnumFieldNamesUpperSnakeCaseRule_Apply_disable(t *testing.T) {
   150  	tests := []struct {
   151  		name               string
   152  		inputFilename      string
   153  		inputPlacementType autodisable.PlacementType
   154  		wantFilename       string
   155  	}{
   156  		{
   157  			name:          "do nothing in case of no violations",
   158  			inputFilename: "upperSnakeCase.proto",
   159  			wantFilename:  "upperSnakeCase.proto",
   160  		},
   161  		{
   162  			name:               "insert disable:next comments",
   163  			inputFilename:      "invalid.proto",
   164  			inputPlacementType: autodisable.Next,
   165  			wantFilename:       "disable_next.proto",
   166  		},
   167  		{
   168  			name:               "insert disable:this comments",
   169  			inputFilename:      "invalid.proto",
   170  			inputPlacementType: autodisable.ThisThenNext,
   171  			wantFilename:       "disable_this.proto",
   172  		},
   173  	}
   174  
   175  	for _, test := range tests {
   176  		test := test
   177  		t.Run(test.name, func(t *testing.T) {
   178  			r := rules.NewEnumFieldNamesUpperSnakeCaseRule(rule.SeverityError, true, test.inputPlacementType)
   179  			testApplyFix(t, r, test.inputFilename, test.wantFilename)
   180  		})
   181  	}
   182  }