forked from rubocop/rubocop
/
symbol_array_spec.rb
261 lines (218 loc) · 6.53 KB
/
symbol_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::SymbolArray, :config do
before do
# Reset data which is shared by all instances of SymbolArray
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) { { 'MinSize' => 0, 'EnforcedStyle' => 'percent' } }
it 'registers an offense for arrays of symbols' do
expect_offense(<<~RUBY)
[:one, :two, :three]
^^^^^^^^^^^^^^^^^^^^ Use `%i` or `%I` for an array of symbols.
RUBY
expect_correction(<<~RUBY)
%i(one two three)
RUBY
end
it 'autocorrects arrays of one symbol' do
expect_offense(<<~RUBY)
[:one]
^^^^^^ Use `%i` or `%I` for an array of symbols.
RUBY
expect_correction(<<~RUBY)
%i(one)
RUBY
end
it 'autocorrects arrays of symbols with embedded newlines and tabs' do
expect_offense(<<~RUBY, tab: "\t")
[:"%{tab}", :"two
^^^^{tab}^^^^^^^^ Use `%i` or `%I` for an array of symbols.
", :three]
RUBY
expect_correction(<<~'RUBY')
%I(\t two\n three)
RUBY
end
it 'autocorrects arrays of symbols with new line' do
expect_offense(<<~RUBY)
[:one,
^^^^^^ Use `%i` or `%I` for an array of symbols.
:two, :three,
:four]
RUBY
expect_correction(<<~RUBY)
%i(one
two three
four)
RUBY
end
it 'uses %I when appropriate' do
expect_offense(<<~'RUBY')
[:"\t", :"\n", :three]
^^^^^^^^^^^^^^^^^^^^^^ Use `%i` or `%I` for an array of symbols.
RUBY
expect_correction(<<~'RUBY')
%I(\t \n three)
RUBY
end
it "doesn't break when a symbol contains )" do
expect_offense(<<~RUBY)
[:one, :")", :three, :"(", :"]", :"["]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `%i` or `%I` for an array of symbols.
RUBY
expect_correction(<<~'RUBY')
%i(one \) three \( ] [)
RUBY
end
it 'does not register an offense for array with non-syms' do
expect_no_offenses('[:one, :two, "three"]')
end
it 'does not register an offense for array starting with %i' do
expect_no_offenses('%i(one two three)')
end
it 'does not register an offense if symbol contains whitespace' do
expect_no_offenses('[:one, :two, :"space here"]')
end
it 'registers an offense in a non-ambiguous block context' do
expect_offense(<<~RUBY)
foo([:bar, :baz]) { qux }
^^^^^^^^^^^^ Use `%i` or `%I` for an array of symbols.
RUBY
expect_correction(<<~RUBY)
foo(%i(bar baz)) { qux }
RUBY
end
it 'detects right value for MinSize to use for --auto-gen-config' do
expect_offense(<<~RUBY)
[:one, :two, :three]
^^^^^^^^^^^^^^^^^^^^ Use `%i` or `%I` for an array of symbols.
%i(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 `%i` or `%I` for an array of symbols.
%i(a b)
RUBY
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
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)
[:one, :")", :three, :"(", :"]", :"["]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `%i` or `%I` for an array of symbols.
RUBY
expect_correction(<<~'RUBY')
%i[one ) three ( \] \[]
RUBY
end
it 'autocorrects an array in multiple lines' do
expect_offense(<<-RUBY)
[
^ Use `%i` or `%I` for an array of symbols.
:foo,
:bar,
:baz
]
RUBY
expect_correction(<<-RUBY)
%i[
foo
bar
baz
]
RUBY
end
it 'autocorrects an array using partial newlines' do
expect_offense(<<-RUBY)
[:foo, :bar, :baz,
^^^^^^^^^^^^^^^^^^ Use `%i` or `%I` for an array of symbols.
:boz, :buz,
:biz]
RUBY
expect_correction(<<-RUBY)
%i[foo bar baz
boz buz
biz]
RUBY
end
end
end
context 'when EnforcedStyle is brackets' do
let(:cop_config) { { 'EnforcedStyle' => 'brackets', 'MinSize' => 0 } }
it 'does not register an offense for arrays of symbols' do
expect_no_offenses('[:one, :two, :three]')
end
it 'registers an offense for array starting with %i' do
expect_offense(<<~RUBY)
%i(one two three)
^^^^^^^^^^^^^^^^^ Use `[:one, :two, :three]` for an array of symbols.
RUBY
expect_correction(<<~RUBY)
[:one, :two, :three]
RUBY
end
it 'autocorrects an array starting with %i' do
expect_offense(<<~RUBY)
%i(one @two $three four-five)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `[:one, :@two, :$three, :'four-five']` for an array of symbols.
RUBY
expect_correction(<<~RUBY)
[:one, :@two, :$three, :'four-five']
RUBY
end
it 'autocorrects multiline %i array' do
expect_offense(<<~RUBY)
%i(
^^^ Use an array literal `[...]` for an array of symbols.
one
two
three
)
RUBY
expect_correction(<<~RUBY)
[
:one,
:two,
:three
]
RUBY
end
it 'autocorrects an array has interpolations' do
expect_offense(<<~'RUBY')
%I(#{foo} #{foo}bar foo#{bar} foo)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `[:"#{foo}", :"#{foo}bar", :"foo#{bar}", :foo]` for an array of symbols.
RUBY
expect_correction(<<~'RUBY')
[:"#{foo}", :"#{foo}bar", :"foo#{bar}", :foo]
RUBY
end
end
context 'with non-default MinSize' do
let(:cop_config) { { 'MinSize' => 2, 'EnforcedStyle' => 'percent' } }
it 'does not autocorrect array of one symbol if MinSize > 1' do
expect_no_offenses('[:one]')
end
end
end