rpcNamesUpperCamelCaseRule_test.go•5.11 kB
package rules_test
import (
"reflect"
"testing"
"github.com/yoheimuta/go-protoparser/v4/parser"
"github.com/yoheimuta/go-protoparser/v4/parser/meta"
"github.com/yoheimuta/protolint/internal/addon/rules"
"github.com/yoheimuta/protolint/linter/autodisable"
"github.com/yoheimuta/protolint/linter/report"
"github.com/yoheimuta/protolint/linter/rule"
)
func TestRPCNamesUpperCamelCaseRule_Apply(t *testing.T) {
tests := []struct {
name string
inputProto *parser.Proto
wantFailures []report.Failure
}{
{
name: "no failures for proto without rpc",
inputProto: &parser.Proto{
ProtoBody: []parser.Visitee{
&parser.Service{},
},
},
},
{
name: "no failures for proto with valid rpc",
inputProto: &parser.Proto{
ProtoBody: []parser.Visitee{
&parser.Service{
ServiceBody: []parser.Visitee{
&parser.RPC{
RPCName: "RPCName",
},
},
},
},
},
},
{
name: "failures for proto with LowerCamelCase",
inputProto: &parser.Proto{
ProtoBody: []parser.Visitee{
&parser.Service{
ServiceBody: []parser.Visitee{
&parser.RPC{
RPCName: "rpcName",
Meta: meta.Meta{
Pos: meta.Position{
Filename: "example.proto",
Offset: 100,
Line: 5,
Column: 10,
},
},
},
},
},
},
},
wantFailures: []report.Failure{
report.Failuref(
meta.Position{
Filename: "example.proto",
Offset: 100,
Line: 5,
Column: 10,
},
"RPC_NAMES_UPPER_CAMEL_CASE",
string(rule.SeverityError),
`RPC name "rpcName" must be UpperCamelCase like "RpcName"`,
),
},
},
{
name: "a failure for proto with SnakeCase",
inputProto: &parser.Proto{
ProtoBody: []parser.Visitee{
&parser.Service{
ServiceBody: []parser.Visitee{
&parser.RPC{
RPCName: "RPC_name",
Meta: meta.Meta{
Pos: meta.Position{
Filename: "example.proto",
Offset: 100,
Line: 5,
Column: 10,
},
},
},
},
},
},
},
wantFailures: []report.Failure{
report.Failuref(
meta.Position{
Filename: "example.proto",
Offset: 100,
Line: 5,
Column: 10,
},
"RPC_NAMES_UPPER_CAMEL_CASE",
string(rule.SeverityError),
`RPC name "RPC_name" must be UpperCamelCase like "RPCName"`,
),
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
rule := rules.NewRPCNamesUpperCamelCaseRule(rule.SeverityError, false, autodisable.Noop)
got, err := rule.Apply(test.inputProto)
if err != nil {
t.Errorf("got err %v, but want nil", err)
return
}
if !reflect.DeepEqual(got, test.wantFailures) {
t.Errorf("got %v, but want %v", got, test.wantFailures)
}
})
}
}
func TestRPCNamesUpperCamelCaseRule_Apply_fix(t *testing.T) {
tests := []struct {
name string
inputFilename string
wantFilename string
}{
{
name: "no fix for a correct proto",
inputFilename: "upperCamelCase.proto",
wantFilename: "upperCamelCase.proto",
},
{
name: "fix for an incorrect proto",
inputFilename: "invalid.proto",
wantFilename: "upperCamelCase.proto",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
r := rules.NewRPCNamesUpperCamelCaseRule(rule.SeverityError, true, autodisable.Noop)
testApplyFix(t, r, test.inputFilename, test.wantFilename)
})
}
}
func TestRPCNamesUpperCamelCaseRule_Apply_disable(t *testing.T) {
tests := []struct {
name string
inputFilename string
inputPlacementType autodisable.PlacementType
wantFilename string
}{
{
name: "do nothing in case of no violations",
inputFilename: "upperCamelCase.proto",
wantFilename: "upperCamelCase.proto",
},
{
name: "insert disable:next comments",
inputFilename: "invalid.proto",
inputPlacementType: autodisable.Next,
wantFilename: "disable_next.proto",
},
{
name: "insert disable:this comments",
inputFilename: "invalid.proto",
inputPlacementType: autodisable.ThisThenNext,
wantFilename: "disable_this.proto",
},
{
name: "apply nothing to already disabled lines",
inputFilename: "disable_this.proto",
inputPlacementType: autodisable.ThisThenNext,
wantFilename: "disable_this.proto",
},
{
name: "apply nothing to already disabled lines behind a left curly. See #368",
inputFilename: "disable_this_left_curly.proto",
inputPlacementType: autodisable.ThisThenNext,
wantFilename: "disable_this_left_curly.proto",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
r := rules.NewRPCNamesUpperCamelCaseRule(rule.SeverityError, true, test.inputPlacementType)
testApplyFix(t, r, test.inputFilename, test.wantFilename)
})
}
}