forked from rubocop/rubocop
/
literal_in_interpolation_spec.rb
338 lines (286 loc) · 11.3 KB
/
literal_in_interpolation_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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::LiteralInInterpolation, :config do
it 'accepts empty interpolation' do
expect_no_offenses('"this is #{a} silly"')
end
it 'accepts interpolation of xstr' do
expect_no_offenses('"this is #{`a`} silly"')
end
it 'accepts interpolation of irange where endpoints are not literals' do
expect_no_offenses('"this is an irange: #{var1..var2}"')
end
it 'accepts interpolation of erange where endpoints are not literals' do
expect_no_offenses('"this is an erange: #{var1...var2}"')
end
shared_examples 'literal interpolation' do |literal, expected = literal|
it "registers an offense for #{literal} in interpolation " \
'and removes interpolation around it' do
expect_offense(<<~'RUBY', literal: literal)
"this is the #{%{literal}}"
^{literal} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
"this is the #{expected}"
RUBY
end
it "removes interpolation around #{literal} when there is more text" do
expect_offense(<<~'RUBY', literal: literal)
"this is the #{%{literal}} literally"
^{literal} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
"this is the #{expected} literally"
RUBY
end
it "removes interpolation around multiple #{literal}" do
expect_offense(<<~'RUBY', literal: literal)
"some #{%{literal}} with #{%{literal}} too"
^{literal} Literal interpolation detected.
_{literal} ^{literal} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
"some #{expected} with #{expected} too"
RUBY
end
context 'when there is non-literal and literal interpolation' do
context 'when literal interpolation is before non-literal' do
it 'only removes interpolation around literal' do
expect_offense(<<~'RUBY', literal: literal)
"this is #{%{literal}} with #{a} now"
^{literal} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
"this is #{expected} with \#{a} now"
RUBY
end
end
context 'when literal interpolation is after non-literal' do
it 'only removes interpolation around literal' do
expect_offense(<<~'RUBY', literal: literal)
"this is #{a} with #{%{literal}} now"
^{literal} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
"this is \#{a} with #{expected} now"
RUBY
end
end
end
it "registers an offense only for final #{literal} in interpolation" do
expect_offense(<<~'RUBY', literal: literal)
"this is the #{%{literal};%{literal}}"
_{literal} ^{literal} Literal interpolation detected.
RUBY
end
end
it_behaves_like('literal interpolation', 1)
it_behaves_like('literal interpolation', -1)
it_behaves_like('literal interpolation', '1_123', '1123')
it_behaves_like('literal interpolation', '123_456_789_123_456_789', '123456789123456789')
it_behaves_like('literal interpolation', '1.2e-3', '0.0012')
it_behaves_like('literal interpolation', '0xaabb', '43707')
it_behaves_like('literal interpolation', '0o377', '255')
it_behaves_like('literal interpolation', 2.0)
it_behaves_like('literal interpolation', '[]', '[]')
it_behaves_like('literal interpolation', '["a", "b"]', '[\"a\", \"b\"]')
it_behaves_like('literal interpolation', '{"a" => "b"}', '{\"a\" => \"b\"}')
it_behaves_like('literal interpolation', true)
it_behaves_like('literal interpolation', false)
it_behaves_like('literal interpolation', 'nil', '')
it_behaves_like('literal interpolation', ':symbol', 'symbol')
it_behaves_like('literal interpolation', ':"symbol"', 'symbol')
it_behaves_like('literal interpolation', 1..2)
it_behaves_like('literal interpolation', 1...2)
it_behaves_like('literal interpolation', '%w[]', '[]')
it_behaves_like('literal interpolation', '%w[v1]', '[\"v1\"]')
it_behaves_like('literal interpolation', '%w[v1 v2]', '[\"v1\", \"v2\"]')
it_behaves_like('literal interpolation', '%i[s1 s2]', '[\"s1\", \"s2\"]')
it_behaves_like('literal interpolation', '%I[s1 s2]', '[\"s1\", \"s2\"]')
it_behaves_like('literal interpolation', '%i[s1 s2]', '[\"s1\", \"s2\"]')
it_behaves_like('literal interpolation', '%i[ s1 s2 ]', '[\"s1\", \"s2\"]')
shared_examples 'literal interpolation in words literal' do |prefix|
let(:word) { 'interpolation' }
it "accepts interpolation of a string literal with space in #{prefix}[]" do
expect_no_offenses(<<~RUBY)
#{prefix}[\#{\"this interpolation\"} is significant]
RUBY
end
it "accepts interpolation of a symbol literal with space in #{prefix}[]" do
expect_no_offenses(<<~RUBY)
#{prefix}[\#{:\"this interpolation\"} is significant]
RUBY
end
it "accepts interpolation of an array literal containing a string with space in #{prefix}[]" do
expect_no_offenses(<<~RUBY)
#{prefix}[\#{[\"this interpolation\"]} is significant]
RUBY
end
it "accepts interpolation of an array literal containing a symbol with space in #{prefix}[]" do
expect_no_offenses(<<~RUBY)
#{prefix}[\#{[:\"this interpolation\"]} is significant]
RUBY
end
it "removes interpolation of a string literal without space in #{prefix}[]" do
expect_offense(<<~'RUBY', prefix: prefix, literal: word.inspect)
%{prefix}[this #{%{literal}} is not significant]
_{prefix} ^{literal} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
#{prefix}[this #{word} is not significant]
RUBY
end
it "removes interpolation of a symbol literal without space in #{prefix}[]" do
expect_offense(<<~'RUBY', prefix: prefix, literal: word.to_sym.inspect)
%{prefix}[this #{%{literal}} is not significant]
_{prefix} ^{literal} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
#{prefix}[this #{word} is not significant]
RUBY
end
it "removes interpolation of an array containing a string literal without space in #{prefix}[]" do
expect_offense(<<~'RUBY', prefix: prefix, literal: [word].inspect)
%{prefix}[this #{%{literal}} is not significant]
_{prefix} ^{literal} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
#{prefix}[this #{[word].inspect.gsub(/"/, '\"')} is not significant]
RUBY
end
it "removes interpolation of an array containing a symbol literal without space in #{prefix}[]" do
expect_offense(<<~'RUBY', prefix: prefix, literal: [word.to_sym].inspect)
%{prefix}[this #{%{literal}} is not significant]
_{prefix} ^{literal} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
#{prefix}[this #{[word.to_sym].inspect} is not significant]
RUBY
end
end
it_behaves_like('literal interpolation in words literal', '%W')
it_behaves_like('literal interpolation in words literal', '%I')
it 'handles nested interpolations when autocorrecting' do
expect_offense(<<~'RUBY')
"this is #{"#{1}"} silly"
^ Literal interpolation detected.
RUBY
# next iteration fixes this
expect_correction(<<~'RUBY', loop: false)
"this is #{"1"} silly"
RUBY
end
shared_examples 'special keywords' do |keyword|
it "accepts strings like #{keyword}" do
expect_no_offenses(<<~RUBY)
%("this is \#{#{keyword}} silly")
RUBY
end
it "registers an offense and autocorrects interpolation after #{keyword}" do
expect_offense(<<~'RUBY', keyword: keyword)
"this is the #{%{keyword}} #{1}"
_{keyword} ^ Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
"this is the \#{#{keyword}} 1"
RUBY
end
end
it_behaves_like('special keywords', '__FILE__')
it_behaves_like('special keywords', '__LINE__')
it_behaves_like('special keywords', '__END__')
it_behaves_like('special keywords', '__ENCODING__')
shared_examples 'non-special string literal interpolation' do |string|
it "registers an offense for #{string} and removes the interpolation " \
"and quotes around #{string}" do
expect_offense(<<~'RUBY', string: string)
"this is the #{%{string}}"
^{string} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
"this is the #{string.gsub(/'|"/, '')}"
RUBY
end
end
it_behaves_like('non-special string literal interpolation', %('foo'))
it_behaves_like('non-special string literal interpolation', %("foo"))
it 'handles double quotes in single quotes when autocorrecting' do
expect_offense(<<~'RUBY')
"this is #{'"'} silly"
^^^ Literal interpolation detected.
RUBY
expect_correction(<<~'RUBY')
"this is \" silly"
RUBY
end
it 'handles backslash in single quotes when autocorrecting' do
expect_offense(<<~'RUBY')
x = "ABC".gsub(/(A)(B)(C)/, "D#{'\2'}F")
^^^^ Literal interpolation detected.
"this is #{'\n'} silly"
^^^^ Literal interpolation detected.
"this is #{%q(\n)} silly"
^^^^^^ Literal interpolation detected.
RUBY
expect_correction(<<~'RUBY')
x = "ABC".gsub(/(A)(B)(C)/, "D\\2F")
"this is \\n silly"
"this is \\n silly"
RUBY
end
it 'handles backslash in double quotes when autocorrecting' do
expect_offense(<<~'RUBY')
"this is #{"\n"} silly"
^^^^ Literal interpolation detected.
"this is #{%(\n)} silly"
^^^^^ Literal interpolation detected.
"this is #{%Q(\n)} silly"
^^^^^^ Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
"this is#{trailing_whitespace}
silly"
"this is#{trailing_whitespace}
silly"
"this is#{trailing_whitespace}
silly"
RUBY
end
it 'does not register an offense when space literal at the end of heredoc line' do
expect_no_offenses(<<~RUBY)
<<~HERE
Line with explicit space literal at the end. \#{' '}
HERE
RUBY
end
context 'in string-like contexts' do
let(:literal) { '42' }
let(:expected) { '42' }
it 'removes interpolation in symbols' do
expect_offense(<<~'RUBY', literal: literal)
:"this is the #{%{literal}}"
^{literal} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
:"this is the #{expected}"
RUBY
end
it 'removes interpolation in backticks' do
expect_offense(<<~'RUBY', literal: literal)
`this is the #{%{literal}}`
^{literal} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
\`this is the #{expected}\`
RUBY
end
it 'removes interpolation in regular expressions' do
expect_offense(<<~'RUBY', literal: literal)
/this is the #{%{literal}}/
^{literal} Literal interpolation detected.
RUBY
expect_correction(<<~RUBY)
/this is the #{expected}/
RUBY
end
end
end