This repository has been archived by the owner on Feb 19, 2024. It is now read-only.
forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_array_spec.rb
401 lines (338 loc) · 11.9 KB
/
word_array_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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::WordArray, :config do
before do
# Reset data which is shared by all instances of WordArray
described_class.largest_brackets = -Float::INFINITY
end
let(:other_cops) do
{
'Style/PercentLiteralDelimiters' => {
'PreferredDelimiters' => {
'default' => '()'
}
}
}
end
context 'when EnforcedStyle is percent' do
let(:cop_config) do
{ 'MinSize' => 0,
'WordRegex' => /\A(?:\p{Word}|\p{Word}-\p{Word}|\n|\t)+\z/,
'EnforcedStyle' => 'percent' }
end
it 'registers an offense for arrays of single quoted strings' do
expect_offense(<<~RUBY)
['one', 'two', 'three']
^^^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
end
it 'registers an offense for arrays of double quoted strings' do
expect_offense(<<~RUBY)
["one", "two", "three"]
^^^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
end
it 'registers an offense for arrays of strings containing hyphens' do
expect_offense(<<~RUBY)
['foo', 'bar', 'foo-bar']
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
end
it 'registers an offense for arrays of unicode word characters' do
expect_offense(<<~RUBY)
["ВУЗ", "вуз", "中文网"]
^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
end
it 'registers an offense for arrays with character constants' do
expect_offense(<<~'RUBY')
["one", ?\n]
^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
end
it 'uses %W when autocorrecting strings with embedded newlines and tabs' do
expect_offense(<<~RUBY)
["one
^^^^^ Use `%w` or `%W` for an array of words.
", "hi\tthere"]
RUBY
expect_correction(<<~'RUBY')
%W(one\n hi\tthere)
RUBY
end
it 'registers an offense for strings with newline and tab escapes' do
expect_offense(<<~'RUBY')
["one\n", "hi\tthere"]
^^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
end
it 'uses %W when autocorrecting strings with newlines and tabs' do
new_source = autocorrect_source(%(["one\\n", "hi\\tthere"]))
expect(new_source).to eq('%W(one\\n hi\\tthere)')
end
it 'does not register an offense for array of non-words' do
expect_no_offenses('["one space", "two", "three"]')
end
it 'does not register an offense for array containing non-string' do
expect_no_offenses('["one", "two", 3]')
end
it 'does not register an offense for array starting with %w' do
expect_no_offenses('%w(one two three)')
end
it 'does not register an offense for array with empty strings' do
expect_no_offenses('["", "two", "three"]')
end
it 'does not register an offense on non-word strings' do
expect_no_offenses("['-', '----']")
end
# Bug: https://github.com/rubocop-hq/rubocop/issues/4481
it 'does not register an offense in an ambiguous block context' do
expect_no_offenses('foo ["bar", "baz"] { qux }')
end
it 'registers an offense in a non-ambiguous block context' do
expect_offense(<<~RUBY)
foo(['bar', 'baz']) { qux }
^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
end
it 'does not register offense for array with allowed number of strings' do
cop_config['MinSize'] = 4
expect_no_offenses('["one", "two", "three"]')
end
it 'does not register an offense for an array with comments in it' do
expect_no_offenses(<<~RUBY)
[
"foo", # comment here
"bar", # this thing was done because of a bug
"baz" # do not delete this line
]
RUBY
end
it 'registers an offense for an array with comments outside of it' do
expect_offense(<<~RUBY)
[
^ Use `%w` or `%W` for an array of words.
"foo",
"bar",
"baz"
] # test
RUBY
expect_correction(<<~RUBY)
%w(
foo
bar
baz
) # test
RUBY
end
it 'auto-corrects an array of words' do
new_source = autocorrect_source("['one', %q(two), 'three']")
expect(new_source).to eq('%w(one two three)')
end
it 'auto-corrects an array with one element' do
new_source = autocorrect_source("['one']")
expect(new_source).to eq('%w(one)')
end
it 'auto-corrects an array of words and character constants' do
new_source = autocorrect_source('[%|one|, %Q(two), ?\n, ?\t]')
expect(new_source).to eq('%W(one two \n \t)')
end
it 'keeps the line breaks in place after auto-correct' do
new_source = autocorrect_source(<<~RUBY)
['one',
'two', 'three']
RUBY
expect(new_source).to eq(<<~RUBY)
%w(one
two three)
RUBY
end
it 'auto-corrects an array of words in multiple lines' do
new_source = autocorrect_source(<<-RUBY)
[
"foo",
"bar",
"baz"
]
RUBY
expect(new_source).to eq(<<-RUBY)
%w(
foo
bar
baz
)
RUBY
end
it 'auto-corrects an array of words using partial newlines' do
new_source = autocorrect_source(<<-RUBY)
["foo", "bar", "baz",
"boz", "buz",
"biz"]
RUBY
expect(new_source).to eq(<<-RUBY)
%w(foo bar baz
boz buz
biz)
RUBY
end
it 'detects right value of MinSize to use for --auto-gen-config' do
inspect_source(<<~RUBY)
['one', 'two', 'three']
%w(a b c d)
RUBY
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'percent',
'MinSize' => 4)
end
it 'detects when the cop must be disabled to avoid offenses' do
inspect_source(<<~RUBY)
['one', 'two', 'three']
%w(a b)
RUBY
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end
it "doesn't fail in wacky ways when multiple cop instances are used" do
# Regression test for GH issue #2740
cop1 = described_class.new(config)
cop2 = described_class.new(config)
RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
# Don't use `inspect_source`; it resets `config_to_allow_offenses` each
# time, which suppresses the bug we are checking for
_investigate(cop1, parse_source("['g', 'h']"))
_investigate(cop2, parse_source('%w(a b c)'))
expect(cop2.config_to_allow_offenses).to eq('EnforcedStyle' => 'percent',
'MinSize' => 3)
end
end
context 'when EnforcedStyle is array' do
let(:cop_config) do
{ 'MinSize' => 0,
'WordRegex' => /\A(?:\p{Word}|\p{Word}-\p{Word}|\n|\t)+\z/,
'EnforcedStyle' => 'brackets' }
end
it 'does not register an offense for arrays of single quoted strings' do
expect_no_offenses("['one', 'two', 'three']")
end
it 'does not register an offense for arrays of double quoted strings' do
expect_no_offenses('["one", "two", "three"]')
end
it 'does not register an offense for arrays of strings with hyphens' do
expect_no_offenses("['foo', 'bar', 'foo-bar']")
end
it 'registers an offense for a %w() array' do
expect_offense(<<~RUBY)
%w(one two three)
^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
RUBY
end
it 'auto-corrects a %w() array' do
new_source = autocorrect_source('%w(one two three)')
expect(new_source).to eq("['one', 'two', 'three']")
end
it 'autocorrects a %w() array which uses single quotes' do
new_source = autocorrect_source("%w(one's two's three's)")
expect(new_source).to eq('["one\'s", "two\'s", "three\'s"]')
end
it 'autocorrects a %W() array which uses escapes' do
new_source = autocorrect_source('%W(\\n \\t \\b \\v \\f)')
expect(new_source).to eq('["\n", "\t", "\b", "\v", "\f"]')
end
it 'autocorrects a %w() array which uses string with hyphen' do
new_source = autocorrect_source('%w(foo bar foo-bar)')
expect(new_source).to eq("['foo', 'bar', 'foo-bar']")
end
it 'autocorrects a %W() array which uses string with hyphen' do
new_source = autocorrect_source('%W(foo bar #{foo}-bar)')
expect(new_source).to eq("['foo', 'bar', \"#\{foo}-bar\"]")
end
it 'autocorrects a %W() array which uses string interpolation' do
new_source = autocorrect_source('%W(#{foo}bar baz)')
expect(new_source).to eq('["#{foo}bar", \'baz\']')
end
it "doesn't fail on strings which are not valid UTF-8" do
# Regression test, see GH issue 2671
expect_no_offenses(<<~'RUBY')
["\xC0",
"\xC2\x4a",
"\xC2\xC2",
"\x4a\x82",
"\x82\x82",
"\xe1\x82\x4a",
]
RUBY
end
it "doesn't fail with `encoding: binary" do
expect_no_offenses(<<~'RUBY')
# -*- encoding: binary -*-
["\xC0"] # Invalid as UTF-8
['a'] # Valid as UTF-8 and ASCII
["あ"] # Valid as UTF-8
RUBY
end
end
context 'with a custom WordRegex configuration' do
let(:cop_config) { { 'MinSize' => 0, 'WordRegex' => /\A[\w@.]+\z/ } }
it 'registers an offense for arrays of email addresses' do
expect_offense(<<~RUBY)
['a@example.com', 'b@example.com']
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
end
it 'auto-corrects an array of email addresses' do
new_source = autocorrect_source("['a@example.com', 'b@example.com']")
expect(new_source).to eq('%w(a@example.com b@example.com)')
end
end
context 'when the WordRegex configuration is not a Regexp' do
let(:cop_config) { { 'WordRegex' => 'just_a_string' } }
it 'still parses the code without raising an error' do
expect { inspect_source('') }.not_to raise_error
end
end
context 'with a WordRegex configuration which accepts almost anything' do
let(:cop_config) { { 'MinSize' => 0, 'WordRegex' => /\S+/ } }
it 'uses %W when autocorrecting strings with non-printable chars' do
new_source = autocorrect_source('["\x1f\x1e", "hello"]')
expect(new_source).to eq('%W(\u001F\u001E hello)')
end
it 'uses %w for strings which only appear to have an escape' do
new_source = autocorrect_source("['hi\\tthere', 'again\\n']")
expect(new_source).to eq('%w(hi\\tthere again\\n)')
end
end
context 'with a treacherous WordRegex configuration' do
let(:cop_config) { { 'MinSize' => 0, 'WordRegex' => /[\w \[\]()]/ } }
it "doesn't break when words contain whitespace" do
new_source = autocorrect_source("['hi there', 'something\telse']")
expect(new_source).to eq("['hi there', 'something\telse']")
end
it "doesn't break when words contain delimiters" do
new_source = autocorrect_source("[')', ']', '(']")
expect(new_source).to eq('%w(\\) ] \\()')
end
context 'when PreferredDelimiters is specified' do
let(:other_cops) do
{
'Style/PercentLiteralDelimiters' => {
'PreferredDelimiters' => {
'default' => '[]'
}
}
}
end
it 'autocorrects an array with delimiters' do
new_source = autocorrect_source("[')', ']', '(', '[']")
expect(new_source).to eq('%w[) \\] ( \\[]')
end
end
end
context 'with non-default MinSize' do
let(:cop_config) do
{ 'MinSize' => 2,
'WordRegex' => /\A(?:\p{Word}|\p{Word}-\p{Word}|\n|\t)+\z/,
'EnforcedStyle' => 'percent' }
end
it 'does not autocorrects arrays of one symbol if MinSize > 1' do
expect_no_offenses('["one"]')
end
end
end