forked from rubocop/rubocop
/
format_parameter_mismatch_spec.rb
304 lines (255 loc) · 9.31 KB
/
format_parameter_mismatch_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::FormatParameterMismatch do
subject(:cop) { described_class.new }
shared_examples 'variables' do |variable|
it 'does not register an offense for % called on a variable' do
expect_no_offenses(<<~RUBY)
#{variable} = '%s'
#{variable} % [foo]
RUBY
end
it 'does not register an offense for format called on a variable' do
expect_no_offenses(<<~RUBY)
#{variable} = '%s'
format(#{variable}, foo)
RUBY
end
it 'does not register an offense for format called on a variable' do
expect_no_offenses(<<~RUBY)
#{variable} = '%s'
sprintf(#{variable}, foo)
RUBY
end
end
it_behaves_like 'variables', 'CONST'
it_behaves_like 'variables', 'var'
it_behaves_like 'variables', '@var'
it_behaves_like 'variables', '@@var'
it_behaves_like 'variables', '$var'
it 'registers an offense when calling Kernel.format ' \
'and the fields do not match' do
expect_offense(<<~RUBY)
Kernel.format("%s %s", 1)
^^^^^^ Number of arguments (1) to `format` doesn't match the number of fields (2).
RUBY
end
it 'registers an offense when calling Kernel.sprintf ' \
'and the fields do not match' do
expect_offense(<<~RUBY)
Kernel.sprintf("%s %s", 1)
^^^^^^^ Number of arguments (1) to `sprintf` doesn't match the number of fields (2).
RUBY
end
it 'registers an offense when there are less arguments than expected' do
expect_offense(<<~RUBY)
format("%s %s", 1)
^^^^^^ Number of arguments (1) to `format` doesn't match the number of fields (2).
RUBY
end
it 'registers an offense when there are more arguments than expected' do
expect_offense(<<~RUBY)
format("%s %s", 1, 2, 3)
^^^^^^ Number of arguments (3) to `format` doesn't match the number of fields (2).
RUBY
end
it 'does not register an offense when arguments and fields match' do
expect_no_offenses('format("%s %d %i", 1, 2, 3)')
end
it 'correctly ignores double percent' do
expect_no_offenses("format('%s %s %% %s %%%% %%%%%% %%5B', 1, 2, 3)")
end
it 'constants do not register offenses' do
expect_no_offenses('format(A_CONST, 1, 2, 3)')
end
it 'registers offense with sprintf' do
expect_offense(<<~RUBY)
sprintf("%s %s", 1, 2, 3)
^^^^^^^ Number of arguments (3) to `sprintf` doesn't match the number of fields (2).
RUBY
end
it 'correctly parses different sprintf formats' do
expect_no_offenses('sprintf("%020x%+g:% g %%%#20.8x %#.0e", 1, 2, 3, 4, 5)')
end
it 'registers an offense for String#%' do
expect_offense(<<~RUBY)
"%s %s" % [1, 2, 3]
^ Number of arguments (3) to `String#%` doesn't match the number of fields (2).
RUBY
end
it 'does not register offense for `String#%` when arguments, fields match' do
expect_no_offenses('"%s %s" % [1, 2]')
end
it 'does not register an offense when single argument is a hash' do
expect_no_offenses('puts "%s" % {"a" => 1}')
end
it 'does not register an offense when single argument is not an array' do
expect_no_offenses('puts "%s" % CONST')
end
context 'when splat argument is present' do
it 'does not register an offense when args count is less than expected' do
expect_no_offenses('sprintf("%s, %s, %s", 1, *arr)')
end
context 'when args count is more than expected' do
it 'registers an offense for `#%`' do
expect_offense(<<~RUBY)
puts "%s, %s, %s" % [1, 2, 3, 4, *arr]
^ Number of arguments (5) to `String#%` doesn't match the number of fields (3).
RUBY
end
it 'registers an offense for `#format`' do
expect_no_offenses(<<~RUBY)
puts format("%s, %s, %s", 1, 2, 3, 4, *arr)
RUBY
end
it 'registers an offense for `#sprintf`' do
expect_no_offenses(<<~RUBY)
puts sprintf("%s, %s, %s", 1, 2, 3, 4, *arr)
RUBY
end
end
end
context 'when multiple arguments are called for' do
context 'and a single variable argument is passed' do
it 'does not register an offense' do
# the variable could evaluate to an array
expect_no_offenses('puts "%s %s" % var')
end
end
context 'and a single send node is passed' do
it 'does not register an offense' do
expect_no_offenses('puts "%s %s" % ("ab".chars)')
end
end
end
context 'when using (digit)$ flag' do
it 'does not register an offense' do
expect_no_offenses("format('%1$s %2$s', 'foo', 'bar')")
end
it 'does not register an offense when match between the maximum value ' \
'specified by (digit)$ flag and the number of arguments' do
expect_no_offenses("format('%1$s %1$s', 'foo')")
end
it 'registers an offense when mismatch between the maximum value ' \
'specified by (digit)$ flag and the number of arguments' do
expect_offense(<<~RUBY)
format('%1$s %2$s', 'foo', 'bar', 'baz')
^^^^^^ Number of arguments (3) to `format` doesn't match the number of fields (2).
RUBY
end
end
context 'when format is not a string literal' do
it 'does not register an offense' do
expect_no_offenses('puts str % [1, 2]')
end
end
context 'when format is invalid' do
it 'registers an offense' do
expect_offense(<<~RUBY)
format('%s %2$s', 'foo', 'bar')
^^^^^^ Format string is invalid because formatting sequence types (numbered, named or unnumbered) are mixed.
RUBY
end
end
# Regression: https://github.com/rubocop-hq/rubocop/issues/3869
context 'when passed an empty array' do
it 'does not register an offense' do
expect_no_offenses("'%' % []")
end
end
# Regression: https://github.com/rubocop-hq/rubocop/issues/8115
context 'when argument itself contains format characters and ' \
'formats in format string and argument are not equal' do
it 'ignores argument formatting' do
expect_no_offenses(%{format('%<t>s', t: '%d')})
end
end
it 'ignores percent right next to format string' do
expect_no_offenses('format("%0.1f%% percent", 22.5)')
end
it 'accepts an extra argument for dynamic width' do
expect_no_offenses('format("%*d", max_width, id)')
end
it 'registers an offense if extra argument for dynamic width not given' do
expect_offense(<<~RUBY)
format("%*d", id)
^^^^^^ Number of arguments (1) to `format` doesn't match the number of fields (2).
RUBY
end
it 'accepts an extra arg for dynamic width with other preceding flags' do
expect_no_offenses('format("%0*x", max_width, id)')
end
it 'does not register an offense argument is the result of a message send' do
expect_no_offenses('format("%s", "a b c".gsub(" ", "_"))')
end
it 'does not register an offense when using named parameters' do
expect_no_offenses('"foo %{bar} baz" % { bar: 42 }')
end
it 'identifies correctly digits for spacing in format' do
expect_no_offenses('"duration: %10.fms" % 42')
end
it 'finds faults even when the string looks like a HEREDOC' do
# heredocs are ignored at the moment
expect_offense(<<~RUBY)
format("<< %s bleh", 1, 2)
^^^^^^ Number of arguments (2) to `format` doesn't match the number of fields (1).
RUBY
end
it 'does not register an offense for sprintf with splat argument' do
expect_no_offenses('sprintf("%d%d", *test)')
end
it 'does not register an offense for format with splat argument' do
expect_no_offenses('format("%d%d", *test)')
end
context 'on format with %{} interpolations' do
context 'and 1 argument' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
params = { y: '2015', m: '01', d: '01' }
puts format('%{y}-%{m}-%{d}', params)
RUBY
end
end
context 'and multiple arguments' do
it 'registers an offense' do
expect_offense(<<~RUBY)
params = { y: '2015', m: '01', d: '01' }
puts format('%{y}-%{m}-%{d}', 2015, 1, 1)
^^^^^^ Number of arguments (3) to `format` doesn't match the number of fields (1).
RUBY
end
end
end
context 'on format with %<> interpolations' do
context 'and 1 argument' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
params = { y: '2015', m: '01', d: '01' }
puts format('%<y>d-%<m>d-%<d>d', params)
RUBY
end
end
context 'and multiple arguments' do
it 'registers an offense' do
expect_offense(<<~RUBY)
params = { y: '2015', m: '01', d: '01' }
puts format('%<y>d-%<m>d-%<d>d', 2015, 1, 1)
^^^^^^ Number of arguments (3) to `format` doesn't match the number of fields (1).
RUBY
end
end
end
context 'with wildcard' do
it 'does not register an offense for width' do
expect_no_offenses('format("%*d", 10, 3)')
end
it 'does not register an offense for precision' do
expect_no_offenses('format("%.*f", 2, 20.19)')
end
it 'does not register an offense for width and precision' do
expect_no_offenses('format("%*.*f", 10, 3, 20.19)')
end
it 'does not register an offense for multiple wildcards' do
expect_no_offenses('format("%*.*f %*.*f", 10, 2, 20.19, 5, 1, 11.22)')
end
end
end