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