forked from rubocop/rubocop
/
format_string_token_spec.rb
316 lines (253 loc) · 9.52 KB
/
format_string_token_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
305
306
307
308
309
310
311
312
313
314
315
316
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::FormatStringToken, :config do
let(:enforced_style) { :annotated }
let(:ignored_methods) { [] }
let(:cop_config) do
{
'EnforcedStyle' => enforced_style,
'SupportedStyles' => %i[annotated template unannotated],
'MaxUnannotatedPlaceholdersAllowed' => 0,
'IgnoredMethods' => ignored_methods
}
end
shared_examples 'maximum allowed unannotated' do |token|
context 'when MaxUnannotatedPlaceholdersAllowed is 1' do
before { cop_config['MaxUnannotatedPlaceholdersAllowed'] = 1 }
it 'does not register offenses for single unannotated' do
expect_no_offenses("format('%#{token}', foo)")
end
it 'registers offense for dual unannotated' do
expect_offense(<<~RUBY)
format('%#{token} %s', foo, bar)
^^ Prefer [...]
^^ Prefer [...]
RUBY
end
end
context 'when MaxUnannotatedPlaceholdersAllowed is 2' do
before { cop_config['MaxUnannotatedPlaceholdersAllowed'] = 2 }
it 'does not register offenses for single unannotated' do
expect_no_offenses("format('%#{token}', foo)")
end
it 'does not register offenses for dual unannotated' do
expect_no_offenses("format('%#{token} %s', foo, bar)")
end
end
end
shared_examples 'enforced styles for format string tokens' do |token|
template = '%{template}'
annotated = "%<named>#{token}"
context 'when enforced style is annotated' do
let(:enforced_style) { :annotated }
it 'registers offenses for template style' do
expect_offense(<<~RUBY, annotated: annotated, template: template)
<<-HEREDOC
foo %{annotated} + bar %{template}
_{annotated} ^{template} Prefer annotated tokens [...]
HEREDOC
RUBY
expect_no_corrections
end
it 'supports dynamic string with interpolation' do
expect_offense(<<~'RUBY', annotated: annotated, template: template)
"a#{b}%{annotated} c#{d}%{template} e#{f}"
_{annotated} ^{template} Prefer annotated tokens [...]
RUBY
expect_no_corrections
end
it 'sets the enforced style to annotated after inspecting "%<a>s"' do
expect_no_offenses('"%<a>s"')
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'annotated')
end
it 'configures the enforced style to template after inspecting "%{a}"' do
expect_offense(<<~RUBY)
"%{a}"
^^^^ Prefer annotated tokens [...]
RUBY
expect_no_corrections
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'template')
end
it_behaves_like 'maximum allowed unannotated', token
end
context 'when enforced style is template' do
let(:enforced_style) { :template }
it 'registers offenses for annotated style' do
expect_offense(<<~RUBY, annotated: annotated, template: template)
<<-HEREDOC
foo %{template} + bar %{annotated}
_{template} ^{annotated} Prefer template tokens [...]
HEREDOC
RUBY
expect_no_corrections
end
it 'supports dynamic string with interpolation' do
expect_offense(<<~'RUBY', annotated: annotated, template: template)
"a#{b}%{template} c#{d}%{annotated} e#{f}"
_{template} ^{annotated} Prefer template tokens [...]
RUBY
expect_no_corrections
end
it 'sets the enforced style to annotated after inspecting "%<a>s"' do
expect_offense(<<~RUBY)
"%<a>s"
^^^^^ Prefer template tokens [...]
RUBY
expect_no_corrections
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'annotated')
end
it 'configures the enforced style to template after inspecting "%{a}"' do
expect_no_offenses('"%{a}"')
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'template')
end
it_behaves_like 'maximum allowed unannotated', token
end
end
it 'ignores percent escapes' do
expect_no_offenses("format('%<hit_rate>6.2f%%', hit_rate: 12.34)")
end
it 'ignores xstr' do
expect_no_offenses('`echo "%s %<annotated>s %{template}"`')
end
it 'ignores regexp' do
expect_no_offenses('/foo bar %u/')
end
it 'ignores `%r` regexp' do
expect_no_offenses('%r{foo bar %u}')
end
%i[strptime strftime].each do |method_name|
it "ignores time format (when used as argument to #{method_name})" do
expect_no_offenses(<<~RUBY)
Time.#{method_name}('2017-12-13', '%Y-%m-%d')
RUBY
end
end
it 'ignores time format when it is stored in a variable' do
expect_no_offenses(<<~RUBY)
time_format = '%Y-%m-%d'
Time.strftime('2017-12-13', time_format)
RUBY
end
it 'ignores time format and unrelated `format` method using' do
expect_no_offenses(<<~RUBY)
Time.now.strftime('%Y-%m-%d-%H-%M-%S')
format
RUBY
end
it 'handles dstrs' do
expect_offense(<<~'RUBY')
"c#{b}%{template}"
^^^^^^^^^^^ Prefer annotated tokens (like `%<foo>s`) over template tokens (like `%{foo}`).
RUBY
expect_no_corrections
end
it 'ignores http links' do
expect_no_offenses(<<~RUBY)
'https://ru.wikipedia.org/wiki/%D0%90_'\
'(%D0%BA%D0%B8%D1%80%D0%B8%D0%BB%D0%BB%D0%B8%D1%86%D0%B0)'
RUBY
end
it 'ignores placeholder arguments' do
expect_no_offenses(<<~RUBY)
format(
'%<day>s %<start>s-%<end>s',
day: open_house.starts_at.strftime('%a'),
start: open_house.starts_at.strftime('%l'),
end: open_house.ends_at.strftime('%l %p').strip
)
RUBY
end
it 'works inside hashes' do
expect_offense(<<~RUBY)
{ bar: format('%{foo}', foo: 'foo') }
^^^^^^ Prefer annotated tokens (like `%<foo>s`) over template tokens (like `%{foo}`).
RUBY
expect_no_corrections
end
it 'supports flags and modifiers' do
expect_offense(<<~RUBY)
format('%-20s %-30s', 'foo', 'bar')
^^^^^ Prefer annotated tokens (like `%<foo>s`) over unannotated tokens (like `%s`).
^^^^^ Prefer annotated tokens (like `%<foo>s`) over unannotated tokens (like `%s`).
RUBY
expect_no_corrections
end
it 'ignores __FILE__' do
expect_no_offenses('__FILE__')
end
it_behaves_like 'enforced styles for format string tokens', 'A'
it_behaves_like 'enforced styles for format string tokens', 'B'
it_behaves_like 'enforced styles for format string tokens', 'E'
it_behaves_like 'enforced styles for format string tokens', 'G'
it_behaves_like 'enforced styles for format string tokens', 'X'
it_behaves_like 'enforced styles for format string tokens', 'a'
it_behaves_like 'enforced styles for format string tokens', 'b'
it_behaves_like 'enforced styles for format string tokens', 'c'
it_behaves_like 'enforced styles for format string tokens', 'd'
it_behaves_like 'enforced styles for format string tokens', 'e'
it_behaves_like 'enforced styles for format string tokens', 'f'
it_behaves_like 'enforced styles for format string tokens', 'g'
it_behaves_like 'enforced styles for format string tokens', 'i'
it_behaves_like 'enforced styles for format string tokens', 'o'
it_behaves_like 'enforced styles for format string tokens', 'p'
it_behaves_like 'enforced styles for format string tokens', 's'
it_behaves_like 'enforced styles for format string tokens', 'u'
it_behaves_like 'enforced styles for format string tokens', 'x'
context 'when enforced style is annotated' do
let(:enforced_style) { :annotated }
it 'gives a helpful error message' do
expect_offense(<<~RUBY)
"%{foo}"
^^^^^^ Prefer annotated tokens (like `%<foo>s`) over template tokens (like `%{foo}`).
RUBY
expect_no_corrections
end
context 'when `IgnoredMethods: redirect`' do
let(:ignored_methods) { ['redirect'] }
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
redirect("%{foo}")
RUBY
end
it 'does not register an offense for value in nested structure' do
expect_no_offenses(<<~RUBY)
redirect("%{foo}", bye: "%{foo}")
RUBY
end
it 'registers an offense for different method call within ignored method' do
expect_offense(<<~RUBY)
redirect("%{foo}", bye: foo("%{foo}"))
^^^^^^ Prefer annotated tokens (like `%<foo>s`) over template tokens (like `%{foo}`).
RUBY
end
end
context 'when `IgnoredMethods: []`' do
let(:ignored_methods) { [] }
it 'registers an offense' do
expect_offense(<<~RUBY)
redirect("%{foo}")
^^^^^^ Prefer annotated tokens (like `%<foo>s`) over template tokens (like `%{foo}`).
RUBY
end
end
end
context 'when enforced style is template' do
let(:enforced_style) { :template }
it 'gives a helpful error message' do
expect_offense(<<~RUBY)
"%<foo>d"
^^^^^^^ Prefer template tokens (like `%{foo}`) over annotated tokens (like `%<foo>s`).
RUBY
expect_no_corrections
end
end
context 'when enforced style is unannotated' do
let(:enforced_style) { :unannotated }
it 'gives a helpful error message' do
expect_offense(<<~RUBY)
"%{foo}"
^^^^^^ Prefer unannotated tokens (like `%s`) over template tokens (like `%{foo}`).
RUBY
expect_no_corrections
end
end
end