/
word_array_spec.rb
566 lines (472 loc) · 15 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# 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
expect_correction(<<~RUBY)
%w(one two three)
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
expect_correction(<<~RUBY)
%w(one two three)
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
expect_correction(<<~RUBY)
%w(foo bar foo-bar)
RUBY
end
context 'when the default external encoding is UTF-8' do
around do |example|
orig_encoding = Encoding.default_external
Encoding.default_external = Encoding::UTF_8
example.run
Encoding.default_external = orig_encoding
end
it 'registers an offense for arrays of unicode word characters' do
expect_offense(<<~RUBY, wide: '中文网')
["ВУЗ", "вуз", "%{wide}"]
^^^^^^^^^^^^^^^^^{wide}^^ Use `%w` or `%W` for an array of words.
RUBY
expect_correction(<<~RUBY)
%w(ВУЗ вуз 中文网)
RUBY
end
end
context 'when the default external encoding is US-ASCII' do
around do |example|
orig_encoding = Encoding.default_external
Encoding.default_external = Encoding::US_ASCII
example.run
Encoding.default_external = orig_encoding
end
it 'registers an offense for arrays of unicode word characters' do
expect_offense(<<~RUBY, wide: '中文网')
["ВУЗ", "вуз", "%{wide}"]
^^^^^^^^^^^^^^^^^{wide}^^ Use `%w` or `%W` for an array of words.
RUBY
expect_correction(<<~'RUBY')
%W(\u0412\u0423\u0417 \u0432\u0443\u0437 \u4E2D\u6587\u7F51)
RUBY
end
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
expect_correction(<<~'RUBY')
%W(one \n)
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
expect_correction(<<~'RUBY')
%W(one\n hi\tthere)
RUBY
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
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
expect_correction(<<~RUBY)
foo(%w(bar baz)) { qux }
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
expect_offense(<<~RUBY)
['one', %q(two), 'three']
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
expect_correction(<<~RUBY)
%w(one two three)
RUBY
end
it 'auto-corrects an array with one element' do
expect_offense(<<~RUBY)
['one']
^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
expect_correction(<<~RUBY)
%w(one)
RUBY
end
it 'auto-corrects an array of words and character constants' do
expect_offense(<<~'RUBY')
[%|one|, %Q(two), ?\n, ?\t]
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
expect_correction(<<~'RUBY')
%W(one two \n \t)
RUBY
end
it 'keeps the line breaks in place after auto-correct' do
expect_offense(<<~RUBY)
['one',
^^^^^^^ Use `%w` or `%W` for an array of words.
'two', 'three']
RUBY
expect_correction(<<~RUBY)
%w(one
two three)
RUBY
end
it 'auto-corrects an array of words in multiple lines' do
expect_offense(<<-RUBY)
[
^ Use `%w` or `%W` for an array of words.
"foo",
"bar",
"baz"
]
RUBY
expect_correction(<<-RUBY)
%w(
foo
bar
baz
)
RUBY
end
it 'auto-corrects an array of words using partial newlines' do
expect_offense(<<-RUBY)
["foo", "bar", "baz",
^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
"boz", "buz",
"biz"]
RUBY
expect_correction(<<-RUBY)
%w(foo bar baz
boz buz
biz)
RUBY
end
it 'detects right value of MinSize to use for --auto-gen-config' do
expect_offense(<<~RUBY)
['one', 'two', 'three']
^^^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
%w(a b c d)
RUBY
expect_correction(<<~RUBY)
%w(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
expect_offense(<<~RUBY)
['one', 'two', 'three']
^^^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
%w(a b)
RUBY
expect_correction(<<~RUBY)
%w(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 `expect_offense`; 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
it 'registers an offense for a %w() array containing spaces' do
expect_offense(<<~'RUBY')
%w(one\ two three\ four)
^^^^^^^^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
RUBY
expect_correction(<<~RUBY)
['one two', 'three four']
RUBY
end
it 'does not register an offense for a %w() array containing non word characters' do
expect_no_offenses(<<~RUBY)
%w(% %i %I %q %Q %r %s %w %W %x)
RUBY
end
it 'corrects properly when there is an extra trailing comma' do
expect_offense(<<~RUBY)
A = ["one", "two",]
^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
expect_correction(<<~RUBY)
A = %w(one two)
RUBY
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 'does not register an offense for arrays of strings with spaces' do
expect_no_offenses("['foo bar', 'baz quux']")
end
it 'registers an offense for a %w() array' do
expect_offense(<<~RUBY)
%w(one two three)
^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
RUBY
expect_correction(<<~RUBY)
['one', 'two', 'three']
RUBY
end
it 'autocorrects a %w() array which uses single quotes' do
expect_offense(<<~RUBY)
%w(one's two's three's)
^^^^^^^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
RUBY
expect_correction(<<~RUBY)
["one's", "two's", "three's"]
RUBY
end
it 'autocorrects a %W() array which uses escapes' do
expect_offense(<<~'RUBY')
%W(\n \t \b \v \f)
^^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
RUBY
expect_correction(<<~'RUBY')
["\n", "\t", "\b", "\v", "\f"]
RUBY
end
it 'autocorrects a %w() array which uses string with hyphen' do
expect_offense(<<~RUBY)
%w(foo bar foo-bar)
^^^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
RUBY
expect_correction(<<~RUBY)
['foo', 'bar', 'foo-bar']
RUBY
end
it 'autocorrects a %W() array which uses string with hyphen' do
expect_offense(<<~'RUBY')
%W(foo bar #{foo}-bar)
^^^^^^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
RUBY
expect_correction(<<~'RUBY')
['foo', 'bar', "#{foo}-bar"]
RUBY
end
it 'autocorrects a %W() array which uses string interpolation' do
expect_offense(<<~'RUBY')
%W(#{foo}bar baz)
^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
RUBY
expect_correction(<<~'RUBY')
["#{foo}bar", 'baz']
RUBY
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
expect_correction(<<~RUBY)
%w(a@example.com b@example.com)
RUBY
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 { expect_no_offenses('') }.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
expect_offense(<<~'RUBY')
["\x1f\x1e", "hello"]
^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
expect_correction(<<~'RUBY')
%W(\u001F\u001E hello)
RUBY
end
it 'uses %w for strings which only appear to have an escape' do
expect_offense(<<~'RUBY')
['hi\tthere', 'again\n']
^^^^^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
expect_correction(<<~'RUBY')
%w(hi\tthere again\n)
RUBY
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
expect_no_offenses(<<~RUBY)
['hi there', 'something\telse']
RUBY
end
it "doesn't break when words contain delimiters" do
expect_offense(<<~RUBY)
[')', ']', '(']
^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
expect_correction(<<~'RUBY')
%w(\) ] \()
RUBY
end
context 'when PreferredDelimiters is specified' do
let(:other_cops) do
{
'Style/PercentLiteralDelimiters' => {
'PreferredDelimiters' => {
'default' => '[]'
}
}
}
end
it 'autocorrects an array with delimiters' do
expect_offense(<<~RUBY)
[')', ']', '(', '[']
^^^^^^^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY
expect_correction(<<~'RUBY')
%w[) \] ( \[]
RUBY
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