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