github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/serviceNamesEndWithRule_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/report" 12 "github.com/yoheimuta/protolint/linter/rule" 13 ) 14 15 func TestValidServiceNamesEndWithRule_Apply(t *testing.T) { 16 validTestCase := struct { 17 name string 18 inputProto *parser.Proto 19 wantFailures []report.Failure 20 }{ 21 name: "no failures for proto with valid service names", 22 inputProto: &parser.Proto{ 23 ProtoBody: []parser.Visitee{ 24 &parser.Service{ 25 ServiceName: "SomeServiceService", 26 }, 27 &parser.Service{ 28 ServiceName: "AnotherService", 29 }, 30 }, 31 }, 32 } 33 34 t.Run(validTestCase.name, func(t *testing.T) { 35 rule := rules.NewServiceNamesEndWithRule(rule.SeverityError, "Service") 36 37 _, err := rule.Apply(validTestCase.inputProto) 38 if err != nil { 39 t.Errorf("got err %v, but want nil", err) 40 return 41 } 42 }) 43 } 44 45 func TestInvalidServiceNamesEndWithRule_Apply(t *testing.T) { 46 invalidTestCase := struct { 47 name string 48 inputProto *parser.Proto 49 wantFailures []report.Failure 50 }{ 51 name: "failures for proto with invalid service names", 52 inputProto: &parser.Proto{ 53 ProtoBody: []parser.Visitee{ 54 &parser.Service{ 55 ServiceName: "SomeThing", 56 }, 57 &parser.Service{ 58 ServiceName: "AnotherThing", 59 }, 60 }, 61 }, 62 wantFailures: []report.Failure{ 63 report.Failuref(meta.Position{}, "SERVICE_NAMES_END_WITH", `Service name "SomeThing" must end with Service`), 64 report.Failuref(meta.Position{}, "SERVICE_NAMES_END_WITH", `Service name "AnotherThing" must end with Service`), 65 }, 66 } 67 68 t.Run(invalidTestCase.name, func(t *testing.T) { 69 rule := rules.NewServiceNamesEndWithRule(rule.SeverityError, "Service") 70 71 got, err := rule.Apply(invalidTestCase.inputProto) 72 if err != nil { 73 t.Errorf("got err %v, but want nil", err) 74 return 75 } 76 if !reflect.DeepEqual(got, invalidTestCase.wantFailures) { 77 t.Errorf("got %v, but want %v", got, invalidTestCase.wantFailures) 78 } 79 }) 80 }