github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/rpcNamesUpperCamelCaseRule_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 TestRPCNamesUpperCamelCaseRule_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 rpc", 24 inputProto: &parser.Proto{ 25 ProtoBody: []parser.Visitee{ 26 &parser.Service{}, 27 }, 28 }, 29 }, 30 { 31 name: "no failures for proto with valid rpc", 32 inputProto: &parser.Proto{ 33 ProtoBody: []parser.Visitee{ 34 &parser.Service{ 35 ServiceBody: []parser.Visitee{ 36 &parser.RPC{ 37 RPCName: "RPCName", 38 }, 39 }, 40 }, 41 }, 42 }, 43 }, 44 { 45 name: "failures for proto with LowerCamelCase", 46 inputProto: &parser.Proto{ 47 ProtoBody: []parser.Visitee{ 48 &parser.Service{ 49 ServiceBody: []parser.Visitee{ 50 &parser.RPC{ 51 RPCName: "rpcName", 52 Meta: meta.Meta{ 53 Pos: meta.Position{ 54 Filename: "example.proto", 55 Offset: 100, 56 Line: 5, 57 Column: 10, 58 }, 59 }, 60 }, 61 }, 62 }, 63 }, 64 }, 65 wantFailures: []report.Failure{ 66 report.Failuref( 67 meta.Position{ 68 Filename: "example.proto", 69 Offset: 100, 70 Line: 5, 71 Column: 10, 72 }, 73 "RPC_NAMES_UPPER_CAMEL_CASE", 74 `RPC name "rpcName" must be UpperCamelCase like "RpcName"`, 75 ), 76 }, 77 }, 78 { 79 name: "a failure for proto with SnakeCase", 80 inputProto: &parser.Proto{ 81 ProtoBody: []parser.Visitee{ 82 &parser.Service{ 83 ServiceBody: []parser.Visitee{ 84 &parser.RPC{ 85 RPCName: "RPC_name", 86 Meta: meta.Meta{ 87 Pos: meta.Position{ 88 Filename: "example.proto", 89 Offset: 100, 90 Line: 5, 91 Column: 10, 92 }, 93 }, 94 }, 95 }, 96 }, 97 }, 98 }, 99 wantFailures: []report.Failure{ 100 report.Failuref( 101 meta.Position{ 102 Filename: "example.proto", 103 Offset: 100, 104 Line: 5, 105 Column: 10, 106 }, 107 "RPC_NAMES_UPPER_CAMEL_CASE", 108 `RPC name "RPC_name" must be UpperCamelCase like "RPCName"`, 109 ), 110 }, 111 }, 112 } 113 114 for _, test := range tests { 115 test := test 116 t.Run(test.name, func(t *testing.T) { 117 rule := rules.NewRPCNamesUpperCamelCaseRule(rule.SeverityError, false, autodisable.Noop) 118 119 got, err := rule.Apply(test.inputProto) 120 if err != nil { 121 t.Errorf("got err %v, but want nil", err) 122 return 123 } 124 if !reflect.DeepEqual(got, test.wantFailures) { 125 t.Errorf("got %v, but want %v", got, test.wantFailures) 126 } 127 }) 128 } 129 } 130 131 func TestRPCNamesUpperCamelCaseRule_Apply_fix(t *testing.T) { 132 tests := []struct { 133 name string 134 inputFilename string 135 wantFilename string 136 }{ 137 { 138 name: "no fix for a correct proto", 139 inputFilename: "upperCamelCase.proto", 140 wantFilename: "upperCamelCase.proto", 141 }, 142 { 143 name: "fix for an incorrect proto", 144 inputFilename: "invalid.proto", 145 wantFilename: "upperCamelCase.proto", 146 }, 147 } 148 149 for _, test := range tests { 150 test := test 151 t.Run(test.name, func(t *testing.T) { 152 r := rules.NewRPCNamesUpperCamelCaseRule(rule.SeverityError, true, autodisable.Noop) 153 testApplyFix(t, r, test.inputFilename, test.wantFilename) 154 }) 155 } 156 } 157 158 func TestRPCNamesUpperCamelCaseRule_Apply_disable(t *testing.T) { 159 tests := []struct { 160 name string 161 inputFilename string 162 inputPlacementType autodisable.PlacementType 163 wantFilename string 164 }{ 165 { 166 name: "do nothing in case of no violations", 167 inputFilename: "upperCamelCase.proto", 168 wantFilename: "upperCamelCase.proto", 169 }, 170 { 171 name: "insert disable:next comments", 172 inputFilename: "invalid.proto", 173 inputPlacementType: autodisable.Next, 174 wantFilename: "disable_next.proto", 175 }, 176 { 177 name: "insert disable:this comments", 178 inputFilename: "invalid.proto", 179 inputPlacementType: autodisable.ThisThenNext, 180 wantFilename: "disable_this.proto", 181 }, 182 { 183 name: "apply nothing to already disabled lines", 184 inputFilename: "disable_this.proto", 185 inputPlacementType: autodisable.ThisThenNext, 186 wantFilename: "disable_this.proto", 187 }, 188 { 189 name: "apply nothing to already disabled lines behind a left curly. See #368", 190 inputFilename: "disable_this_left_curly.proto", 191 inputPlacementType: autodisable.ThisThenNext, 192 wantFilename: "disable_this_left_curly.proto", 193 }, 194 } 195 196 for _, test := range tests { 197 test := test 198 t.Run(test.name, func(t *testing.T) { 199 r := rules.NewRPCNamesUpperCamelCaseRule(rule.SeverityError, true, test.inputPlacementType) 200 testApplyFix(t, r, test.inputFilename, test.wantFilename) 201 }) 202 } 203 }