github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/proto3GroupsAvoidRule_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 "github.com/yoheimuta/protolint/internal/addon/rules" 10 "github.com/yoheimuta/protolint/linter/autodisable" 11 "github.com/yoheimuta/protolint/linter/report" 12 "github.com/yoheimuta/protolint/linter/rule" 13 ) 14 15 func TestProto3GroupsAvoidRule_Apply(t *testing.T) { 16 tests := []struct { 17 name string 18 inputProto *parser.Proto 19 wantFailures []report.Failure 20 }{ 21 { 22 name: "no failures for proto without groups", 23 inputProto: &parser.Proto{ 24 ProtoBody: []parser.Visitee{ 25 &parser.Message{}, 26 }, 27 }, 28 }, 29 { 30 name: "no failures for proto with required field names for proto2", 31 inputProto: &parser.Proto{ 32 Syntax: &parser.Syntax{ 33 ProtobufVersion: "proto2", 34 }, 35 ProtoBody: []parser.Visitee{ 36 &parser.Service{}, 37 &parser.Message{ 38 MessageBody: []parser.Visitee{ 39 &parser.GroupField{ 40 GroupName: "song_Name", 41 }, 42 &parser.GroupField{ 43 GroupName: "song.name", 44 }, 45 }, 46 }, 47 }, 48 }, 49 }, 50 { 51 name: "failures for proto with required field names for proto3", 52 inputProto: &parser.Proto{ 53 Syntax: &parser.Syntax{ 54 ProtobufVersion: "proto3", 55 }, 56 ProtoBody: []parser.Visitee{ 57 &parser.Message{ 58 MessageBody: []parser.Visitee{ 59 &parser.GroupField{ 60 GroupName: "song_Name", 61 Meta: meta.Meta{ 62 Pos: meta.Position{ 63 Filename: "example.proto", 64 Offset: 100, 65 Line: 5, 66 Column: 10, 67 }, 68 }, 69 }, 70 &parser.GroupField{ 71 GroupName: "song.name", 72 Meta: meta.Meta{ 73 Pos: meta.Position{ 74 Filename: "example.proto", 75 Offset: 200, 76 Line: 10, 77 Column: 20, 78 }, 79 }, 80 }, 81 }, 82 }, 83 }, 84 }, 85 wantFailures: []report.Failure{ 86 report.Failuref( 87 meta.Position{ 88 Filename: "example.proto", 89 Offset: 100, 90 Line: 5, 91 Column: 10, 92 }, 93 "PROTO3_GROUPS_AVOID", 94 `Group "song_Name" should be avoided for proto3`, 95 ), 96 report.Failuref( 97 meta.Position{ 98 Filename: "example.proto", 99 Offset: 200, 100 Line: 10, 101 Column: 20, 102 }, 103 "PROTO3_GROUPS_AVOID", 104 `Group "song.name" should be avoided for proto3`, 105 ), 106 }, 107 }, 108 } 109 110 for _, test := range tests { 111 test := test 112 t.Run(test.name, func(t *testing.T) { 113 rule := rules.NewProto3GroupsAvoidRule(rule.SeverityError, autodisable.Noop) 114 115 got, err := rule.Apply(test.inputProto) 116 if err != nil { 117 t.Errorf("got err %v, but want nil", err) 118 return 119 } 120 if !reflect.DeepEqual(got, test.wantFailures) { 121 t.Errorf("got %v, but want %v", got, test.wantFailures) 122 } 123 }) 124 } 125 } 126 127 func TestProto3GroupsAvoidRule_Apply_disable(t *testing.T) { 128 tests := []struct { 129 name string 130 inputFilename string 131 inputPlacementType autodisable.PlacementType 132 wantFilename string 133 }{ 134 { 135 name: "do nothing in case of no violations", 136 inputFilename: "valid.proto", 137 wantFilename: "valid.proto", 138 }, 139 { 140 name: "insert disable:next comments", 141 inputFilename: "invalid.proto", 142 inputPlacementType: autodisable.Next, 143 wantFilename: "disable_next.proto", 144 }, 145 { 146 name: "insert disable:this comments", 147 inputFilename: "invalid.proto", 148 inputPlacementType: autodisable.ThisThenNext, 149 wantFilename: "disable_this.proto", 150 }, 151 } 152 153 for _, test := range tests { 154 test := test 155 t.Run(test.name, func(t *testing.T) { 156 r := rules.NewProto3GroupsAvoidRule(rule.SeverityError, test.inputPlacementType) 157 testApplyFix(t, r, test.inputFilename, test.wantFilename) 158 }) 159 } 160 }