|
| 1 | +package rule |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mgechev/revive/lint" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAddConstantRule_Configure(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + arguments lint.Arguments |
| 15 | + wantErr error |
| 16 | + wantList allowList |
| 17 | + wantStrLitLimit int |
| 18 | + }{ |
| 19 | + { |
| 20 | + |
| 21 | + name: "no arguments", |
| 22 | + arguments: lint.Arguments{}, |
| 23 | + wantErr: nil, |
| 24 | + wantList: allowList{ |
| 25 | + kindINT: {}, |
| 26 | + kindFLOAT: {}, |
| 27 | + kindSTRING: {}, |
| 28 | + }, |
| 29 | + wantStrLitLimit: 2, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "valid arguments", |
| 33 | + arguments: lint.Arguments{ |
| 34 | + map[string]any{ |
| 35 | + "allowFloats": "1.0,2.0", |
| 36 | + "allowInts": "1,2", |
| 37 | + "allowStrs": "a,b", |
| 38 | + "maxLitCount": "3", |
| 39 | + "ignoreFuncs": "fmt.Println,fmt.Printf", |
| 40 | + }, |
| 41 | + }, |
| 42 | + wantErr: nil, |
| 43 | + wantList: allowList{ |
| 44 | + kindFLOAT: {"1.0": true, "2.0": true}, |
| 45 | + kindINT: {"1": true, "2": true}, |
| 46 | + kindSTRING: {"a": true, "b": true}, |
| 47 | + }, |
| 48 | + wantStrLitLimit: 3, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "valid lowercased arguments", |
| 52 | + arguments: lint.Arguments{ |
| 53 | + map[string]any{ |
| 54 | + "allowfloats": "1.0,2.0", |
| 55 | + "allowints": "1,2", |
| 56 | + "allowstrs": "a,b", |
| 57 | + "maxlitcount": "3", |
| 58 | + "ignorefuncs": "fmt.Println,fmt.Printf", |
| 59 | + }, |
| 60 | + }, |
| 61 | + wantErr: nil, |
| 62 | + wantList: allowList{ |
| 63 | + kindFLOAT: {"1.0": true, "2.0": true}, |
| 64 | + kindINT: {"1": true, "2": true}, |
| 65 | + kindSTRING: {"a": true, "b": true}, |
| 66 | + }, |
| 67 | + wantStrLitLimit: 3, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "valid kebab-cased arguments", |
| 71 | + arguments: lint.Arguments{ |
| 72 | + map[string]any{ |
| 73 | + "allow-floats": "1.0,2.0", |
| 74 | + "allow-ints": "1,2", |
| 75 | + "allow-strs": "a,b", |
| 76 | + "max-lit-count": "3", |
| 77 | + "ignore-funcs": "fmt.Println,fmt.Printf", |
| 78 | + }, |
| 79 | + }, |
| 80 | + wantErr: nil, |
| 81 | + wantList: allowList{ |
| 82 | + kindFLOAT: {"1.0": true, "2.0": true}, |
| 83 | + kindINT: {"1": true, "2": true}, |
| 84 | + kindSTRING: {"a": true, "b": true}, |
| 85 | + }, |
| 86 | + wantStrLitLimit: 3, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "unrecognized key", |
| 90 | + arguments: lint.Arguments{ |
| 91 | + map[string]any{ |
| 92 | + "unknownKey": "someValue", |
| 93 | + }, |
| 94 | + }, |
| 95 | + wantErr: nil, |
| 96 | + wantList: allowList{ |
| 97 | + kindINT: {}, |
| 98 | + kindFLOAT: {}, |
| 99 | + kindSTRING: {}, |
| 100 | + }, |
| 101 | + wantStrLitLimit: 2, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "invalid argument type", |
| 105 | + arguments: lint.Arguments{ |
| 106 | + "invalid_argument", |
| 107 | + }, |
| 108 | + wantErr: errors.New("invalid argument to the add-constant rule, expecting a k,v map. Got string"), |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "invalid allowFloats value", |
| 112 | + arguments: lint.Arguments{ |
| 113 | + map[string]any{ |
| 114 | + "allowFloats": 123, |
| 115 | + }, |
| 116 | + }, |
| 117 | + wantErr: errors.New("invalid argument to the add-constant rule, string expected. Got '123' (int)"), |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "invalid maxLitCount value: not a string", |
| 121 | + arguments: lint.Arguments{ |
| 122 | + map[string]any{ |
| 123 | + "maxLitCount": 123, |
| 124 | + }, |
| 125 | + }, |
| 126 | + wantErr: errors.New("invalid argument to the add-constant rule, expecting string representation of an integer. Got '123' (int)"), |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "invalid maxLitCount value: not an int", |
| 130 | + arguments: lint.Arguments{ |
| 131 | + map[string]any{ |
| 132 | + "maxLitCount": "abc", |
| 133 | + }, |
| 134 | + }, |
| 135 | + wantErr: errors.New("invalid argument to the add-constant rule, expecting string representation of an integer. Got 'abc'"), |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "invalid ignoreFuncs value: not a string", |
| 139 | + arguments: lint.Arguments{ |
| 140 | + map[string]any{ |
| 141 | + "ignoreFuncs": 123, |
| 142 | + }, |
| 143 | + }, |
| 144 | + wantErr: errors.New("invalid argument to the ignoreFuncs parameter of add-constant rule, string expected. Got '123' (int)"), |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "invalid ignoreFuncs value: empty string", |
| 148 | + arguments: lint.Arguments{ |
| 149 | + map[string]any{ |
| 150 | + "ignoreFuncs": " ", |
| 151 | + }, |
| 152 | + }, |
| 153 | + wantErr: errors.New("invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty"), |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "invalid ignoreFuncs value: wrong regexp", |
| 157 | + arguments: lint.Arguments{ |
| 158 | + map[string]any{ |
| 159 | + "ignoreFuncs": "(", |
| 160 | + }, |
| 161 | + }, |
| 162 | + wantErr: errors.New(`invalid argument to the ignoreFuncs parameter of add-constant rule: regexp "(" does not compile: error parsing regexp: missing closing ): ` + "`(`"), |
| 163 | + }, |
| 164 | + } |
| 165 | + |
| 166 | + for _, tt := range tests { |
| 167 | + t.Run(tt.name, func(t *testing.T) { |
| 168 | + var rule AddConstantRule |
| 169 | + |
| 170 | + err := rule.Configure(tt.arguments) |
| 171 | + |
| 172 | + if tt.wantErr != nil { |
| 173 | + if err == nil { |
| 174 | + t.Errorf("unexpected error: got = nil, want = %v", tt.wantErr) |
| 175 | + return |
| 176 | + } |
| 177 | + if err.Error() != tt.wantErr.Error() { |
| 178 | + t.Errorf("unexpected error: got = %v, want = %v", err, tt.wantErr) |
| 179 | + } |
| 180 | + return |
| 181 | + } |
| 182 | + if err != nil { |
| 183 | + t.Errorf("unexpected error: got = %v, want = nil", err) |
| 184 | + } |
| 185 | + if !reflect.DeepEqual(rule.allowList, tt.wantList) { |
| 186 | + t.Errorf("unexpected allowList: got = %v, want %v", rule.allowList, tt.wantList) |
| 187 | + } |
| 188 | + if rule.strLitLimit != tt.wantStrLitLimit { |
| 189 | + t.Errorf("unexpected strLitLimit: got = %v, want %v", rule.strLitLimit, tt.wantStrLitLimit) |
| 190 | + } |
| 191 | + }) |
| 192 | + } |
| 193 | +} |
0 commit comments