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