github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/rpcNamesCaseRule_test.go (about)

     1  package rules_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/yoheimuta/protolint/internal/linter/config"
     8  
     9  	"github.com/yoheimuta/go-protoparser/v4/parser"
    10  	"github.com/yoheimuta/go-protoparser/v4/parser/meta"
    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 TestRPCNamesCaseRule_Apply(t *testing.T) {
    17  	tests := []struct {
    18  		name            string
    19  		inputProto      *parser.Proto
    20  		inputConvention config.ConventionType
    21  		wantFailures    []report.Failure
    22  	}{
    23  		{
    24  			name: "no failures for proto without rpc",
    25  			inputProto: &parser.Proto{
    26  				ProtoBody: []parser.Visitee{
    27  					&parser.Service{},
    28  				},
    29  			},
    30  			inputConvention: config.ConventionLowerCamel,
    31  		},
    32  		{
    33  			name: "no failures for proto with valid rpc",
    34  			inputProto: &parser.Proto{
    35  				ProtoBody: []parser.Visitee{
    36  					&parser.Service{
    37  						ServiceBody: []parser.Visitee{
    38  							&parser.RPC{
    39  								RPCName: "rpcName",
    40  							},
    41  						},
    42  					},
    43  				},
    44  			},
    45  			inputConvention: config.ConventionLowerCamel,
    46  		},
    47  		{
    48  			name: "no failures for proto with valid rpc",
    49  			inputProto: &parser.Proto{
    50  				ProtoBody: []parser.Visitee{
    51  					&parser.Service{
    52  						ServiceBody: []parser.Visitee{
    53  							&parser.RPC{
    54  								RPCName: "RPC_NAME",
    55  							},
    56  						},
    57  					},
    58  				},
    59  			},
    60  			inputConvention: config.ConventionUpperSnake,
    61  		},
    62  		{
    63  			name: "no failures for proto with valid rpc",
    64  			inputProto: &parser.Proto{
    65  				ProtoBody: []parser.Visitee{
    66  					&parser.Service{
    67  						ServiceBody: []parser.Visitee{
    68  							&parser.RPC{
    69  								RPCName: "rpc_name",
    70  							},
    71  						},
    72  					},
    73  				},
    74  			},
    75  			inputConvention: config.ConventionLowerSnake,
    76  		},
    77  		{
    78  			name: "failures for proto with the option of LowerCamelCase",
    79  			inputProto: &parser.Proto{
    80  				ProtoBody: []parser.Visitee{
    81  					&parser.Service{
    82  						ServiceBody: []parser.Visitee{
    83  							&parser.RPC{
    84  								RPCName: "RPCName",
    85  								Meta: meta.Meta{
    86  									Pos: meta.Position{
    87  										Filename: "example.proto",
    88  										Offset:   100,
    89  										Line:     5,
    90  										Column:   10,
    91  									},
    92  								},
    93  							},
    94  							&parser.RPC{
    95  								RPCName: "RPC_NAME",
    96  								Meta: meta.Meta{
    97  									Pos: meta.Position{
    98  										Filename: "example.proto",
    99  										Offset:   200,
   100  										Line:     10,
   101  										Column:   20,
   102  									},
   103  								},
   104  							},
   105  							&parser.RPC{
   106  								RPCName: "rpc_name",
   107  								Meta: meta.Meta{
   108  									Pos: meta.Position{
   109  										Filename: "example.proto",
   110  										Offset:   300,
   111  										Line:     20,
   112  										Column:   30,
   113  									},
   114  								},
   115  							},
   116  						},
   117  					},
   118  				},
   119  			},
   120  			inputConvention: config.ConventionLowerCamel,
   121  			wantFailures: []report.Failure{
   122  				report.Failuref(
   123  					meta.Position{
   124  						Filename: "example.proto",
   125  						Offset:   100,
   126  						Line:     5,
   127  						Column:   10,
   128  					},
   129  					"RPC_NAMES_CASE",
   130  					`RPC name "RPCName" must be LowerCamelCase`,
   131  				),
   132  				report.Failuref(
   133  					meta.Position{
   134  						Filename: "example.proto",
   135  						Offset:   200,
   136  						Line:     10,
   137  						Column:   20,
   138  					},
   139  					"RPC_NAMES_CASE",
   140  					`RPC name "RPC_NAME" must be LowerCamelCase`,
   141  				),
   142  				report.Failuref(
   143  					meta.Position{
   144  						Filename: "example.proto",
   145  						Offset:   300,
   146  						Line:     20,
   147  						Column:   30,
   148  					},
   149  					"RPC_NAMES_CASE",
   150  					`RPC name "rpc_name" must be LowerCamelCase`,
   151  				),
   152  			},
   153  		},
   154  		{
   155  			name: "failures for proto with the option of UpperSnakeCase",
   156  			inputProto: &parser.Proto{
   157  				ProtoBody: []parser.Visitee{
   158  					&parser.Service{
   159  						ServiceBody: []parser.Visitee{
   160  							&parser.RPC{
   161  								RPCName: "RPCName",
   162  								Meta: meta.Meta{
   163  									Pos: meta.Position{
   164  										Filename: "example.proto",
   165  										Offset:   100,
   166  										Line:     5,
   167  										Column:   10,
   168  									},
   169  								},
   170  							},
   171  							&parser.RPC{
   172  								RPCName: "rpcName",
   173  								Meta: meta.Meta{
   174  									Pos: meta.Position{
   175  										Filename: "example.proto",
   176  										Offset:   200,
   177  										Line:     10,
   178  										Column:   20,
   179  									},
   180  								},
   181  							},
   182  							&parser.RPC{
   183  								RPCName: "rpc_name",
   184  								Meta: meta.Meta{
   185  									Pos: meta.Position{
   186  										Filename: "example.proto",
   187  										Offset:   300,
   188  										Line:     20,
   189  										Column:   30,
   190  									},
   191  								},
   192  							},
   193  						},
   194  					},
   195  				},
   196  			},
   197  			inputConvention: config.ConventionUpperSnake,
   198  			wantFailures: []report.Failure{
   199  				report.Failuref(
   200  					meta.Position{
   201  						Filename: "example.proto",
   202  						Offset:   100,
   203  						Line:     5,
   204  						Column:   10,
   205  					},
   206  					"RPC_NAMES_CASE",
   207  					`RPC name "RPCName" must be UpperSnakeCase`,
   208  				),
   209  				report.Failuref(
   210  					meta.Position{
   211  						Filename: "example.proto",
   212  						Offset:   200,
   213  						Line:     10,
   214  						Column:   20,
   215  					},
   216  					"RPC_NAMES_CASE",
   217  					`RPC name "rpcName" must be UpperSnakeCase`,
   218  				),
   219  				report.Failuref(
   220  					meta.Position{
   221  						Filename: "example.proto",
   222  						Offset:   300,
   223  						Line:     20,
   224  						Column:   30,
   225  					},
   226  					"RPC_NAMES_CASE",
   227  					`RPC name "rpc_name" must be UpperSnakeCase`,
   228  				),
   229  			},
   230  		},
   231  		{
   232  			name: "failures for proto with the option of LowerSnakeCase",
   233  			inputProto: &parser.Proto{
   234  				ProtoBody: []parser.Visitee{
   235  					&parser.Service{
   236  						ServiceBody: []parser.Visitee{
   237  							&parser.RPC{
   238  								RPCName: "RPCName",
   239  								Meta: meta.Meta{
   240  									Pos: meta.Position{
   241  										Filename: "example.proto",
   242  										Offset:   100,
   243  										Line:     5,
   244  										Column:   10,
   245  									},
   246  								},
   247  							},
   248  							&parser.RPC{
   249  								RPCName: "rpcName",
   250  								Meta: meta.Meta{
   251  									Pos: meta.Position{
   252  										Filename: "example.proto",
   253  										Offset:   200,
   254  										Line:     10,
   255  										Column:   20,
   256  									},
   257  								},
   258  							},
   259  							&parser.RPC{
   260  								RPCName: "RPC_NAME",
   261  								Meta: meta.Meta{
   262  									Pos: meta.Position{
   263  										Filename: "example.proto",
   264  										Offset:   300,
   265  										Line:     20,
   266  										Column:   30,
   267  									},
   268  								},
   269  							},
   270  						},
   271  					},
   272  				},
   273  			},
   274  			inputConvention: config.ConventionLowerSnake,
   275  			wantFailures: []report.Failure{
   276  				report.Failuref(
   277  					meta.Position{
   278  						Filename: "example.proto",
   279  						Offset:   100,
   280  						Line:     5,
   281  						Column:   10,
   282  					},
   283  					"RPC_NAMES_CASE",
   284  					`RPC name "RPCName" must be LowerSnakeCase`,
   285  				),
   286  				report.Failuref(
   287  					meta.Position{
   288  						Filename: "example.proto",
   289  						Offset:   200,
   290  						Line:     10,
   291  						Column:   20,
   292  					},
   293  					"RPC_NAMES_CASE",
   294  					`RPC name "rpcName" must be LowerSnakeCase`,
   295  				),
   296  				report.Failuref(
   297  					meta.Position{
   298  						Filename: "example.proto",
   299  						Offset:   300,
   300  						Line:     20,
   301  						Column:   30,
   302  					},
   303  					"RPC_NAMES_CASE",
   304  					`RPC name "RPC_NAME" must be LowerSnakeCase`,
   305  				),
   306  			},
   307  		},
   308  	}
   309  
   310  	for _, test := range tests {
   311  		test := test
   312  		t.Run(test.name, func(t *testing.T) {
   313  			rule := rules.NewRPCNamesCaseRule(rule.SeverityError, test.inputConvention)
   314  
   315  			got, err := rule.Apply(test.inputProto)
   316  			if err != nil {
   317  				t.Errorf("got err %v, but want nil", err)
   318  				return
   319  			}
   320  			if !reflect.DeepEqual(got, test.wantFailures) {
   321  				t.Errorf("got %v, but want %v", got, test.wantFailures)
   322  			}
   323  		})
   324  	}
   325  }