/
blank_spec.rb
309 lines (265 loc) · 9.29 KB
/
blank_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::Blank, :config do
subject(:cop) { described_class.new(config) }
shared_examples 'offense' do |source, correction, message|
it 'registers an offense' do
inspect_source(source)
expect(cop.messages).to eq([message])
expect(cop.highlights).to eq([source])
end
it 'auto-corrects' do
new_source = autocorrect_source(source)
expect(new_source).to eq(correction)
end
end
context 'NilOrEmpty set to true' do
let(:cop_config) do
{ 'NilOrEmpty' => true }
end
it 'accepts checking nil?' do
expect_no_offenses('foo.nil?')
end
it 'accepts checking empty?' do
expect_no_offenses('foo.empty?')
end
it 'accepts checking nil? || empty? on different objects' do
expect_no_offenses('foo.nil? || bar.empty?')
end
# Bug: https://github.com/rubocop-hq/rubocop/issues/4171
it 'does not break when RHS of `or` is a naked falsiness check' do
expect_no_offenses('foo.empty? || bar')
end
it 'does not break when LHS of `or` is a naked falsiness check' do
expect_no_offenses('bar || foo.empty?')
end
# Bug: https://github.com/rubocop-hq/rubocop/issues/4814
it 'does not break when LHS of `or` is a send node with an arugment' do
expect_no_offenses('x(1) || something')
end
context 'nil or empty' do
it_behaves_like 'offense', 'foo.nil? || foo.empty?',
'foo.blank?',
'Use `foo.blank?` instead of `foo.nil? || foo.empty?`.'
it_behaves_like 'offense', 'nil? || empty?',
'blank?',
'Use `blank?` instead of `nil? || empty?`.'
it_behaves_like 'offense', 'foo == nil || foo.empty?',
'foo.blank?',
'Use `foo.blank?` instead of `foo == nil || foo.empty?`.'
it_behaves_like 'offense', 'nil == foo || foo.empty?',
'foo.blank?',
'Use `foo.blank?` instead of `nil == foo || foo.empty?`.'
it_behaves_like 'offense', '!foo || foo.empty?',
'foo.blank?',
'Use `foo.blank?` instead of `!foo || foo.empty?`.'
it_behaves_like 'offense', 'foo.nil? || !!foo.empty?',
'foo.blank?',
'Use `foo.blank?` instead of `foo.nil? || !!foo.empty?`.'
it_behaves_like 'offense', 'foo == nil || !!foo.empty?',
'foo.blank?',
'Use `foo.blank?` instead of ' \
'`foo == nil || !!foo.empty?`.'
it_behaves_like 'offense', 'nil == foo || !!foo.empty?',
'foo.blank?',
'Use `foo.blank?` instead of ' \
'`nil == foo || !!foo.empty?`.'
end
context 'checking all variable types' do
it_behaves_like 'offense', 'foo.bar.nil? || foo.bar.empty?',
'foo.bar.blank?',
'Use `foo.bar.blank?` instead of ' \
'`foo.bar.nil? || foo.bar.empty?`.'
it_behaves_like 'offense', 'FOO.nil? || FOO.empty?',
'FOO.blank?',
'Use `FOO.blank?` instead of `FOO.nil? || FOO.empty?`.'
it_behaves_like 'offense', 'Foo.nil? || Foo.empty?',
'Foo.blank?',
'Use `Foo.blank?` instead of `Foo.nil? || Foo.empty?`.'
it_behaves_like 'offense', 'Foo::Bar.nil? || Foo::Bar.empty?',
'Foo::Bar.blank?',
'Use `Foo::Bar.blank?` instead of ' \
'`Foo::Bar.nil? || Foo::Bar.empty?`.'
it_behaves_like 'offense', '@foo.nil? || @foo.empty?',
'@foo.blank?',
'Use `@foo.blank?` instead of `@foo.nil? || @foo.empty?`.'
it_behaves_like 'offense', '$foo.nil? || $foo.empty?',
'$foo.blank?',
'Use `$foo.blank?` instead of `$foo.nil? || $foo.empty?`.'
it_behaves_like 'offense', '@@foo.nil? || @@foo.empty?',
'@@foo.blank?',
'Use `@@foo.blank?` instead of ' \
'`@@foo.nil? || @@foo.empty?`.'
it_behaves_like 'offense', 'foo[bar].nil? || foo[bar].empty?',
'foo[bar].blank?',
'Use `foo[bar].blank?` instead of ' \
'`foo[bar].nil? || foo[bar].empty?`.'
it_behaves_like 'offense', 'foo(bar).nil? || foo(bar).empty?',
'foo(bar).blank?',
'Use `foo(bar).blank?` instead of ' \
'`foo(bar).nil? || foo(bar).empty?`.'
end
end
context 'NotPresent set to true' do
let(:cop_config) do
{ 'NotPresent' => true }
end
it_behaves_like 'offense', '!foo.present?',
'foo.blank?',
'Use `foo.blank?` instead of `!foo.present?`.'
it_behaves_like 'offense', 'not foo.present?',
'foo.blank?',
'Use `foo.blank?` instead of `not foo.present?`.'
it_behaves_like 'offense', '!present?',
'blank?',
'Use `blank?` instead of `!present?`.'
it 'accepts !present? if its in the body of a `blank?` method' do
expect_no_offenses('def blank?; !present? end')
end
end
context 'UnlessPresent set to true' do
let(:cop_config) do
{ 'UnlessPresent' => true }
end
it 'accepts modifier if present?' do
expect_no_offenses('something if foo.present?')
end
it 'accepts modifier unless blank?' do
expect_no_offenses('something unless foo.blank?')
end
it 'accepts normal if present?' do
expect_no_offenses(<<-RUBY.strip_indent)
if foo.present?
something
end
RUBY
end
it 'accepts normal unless blank?' do
expect_no_offenses(<<-RUBY.strip_indent)
unless foo.blank?
something
end
RUBY
end
it 'accepts elsif present?' do
expect_no_offenses(<<-RUBY.strip_indent)
if bar.present?
something
elsif bar.present?
something_else
end
RUBY
end
context 'modifier unless' do
context 'with a receiver' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
something unless foo.present?
^^^^^^^^^^^^^^^^^^^ Use `if foo.blank?` instead of `unless foo.present?`.
RUBY
end
it 'auto-corrects' do
new_source = autocorrect_source('something unless foo.present?')
expect(new_source).to eq('something if foo.blank?')
end
end
context 'without a receiver' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
something unless present?
^^^^^^^^^^^^^^^ Use `if blank?` instead of `unless present?`.
RUBY
end
it 'auto-corrects' do
new_source = autocorrect_source('something unless present?')
expect(new_source).to eq('something if blank?')
end
end
end
context 'normal unless present?' do
let(:source) do
<<-RUBY.strip_indent
unless foo.present?
something
end
RUBY
end
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
unless foo.present?
^^^^^^^^^^^^^^^^^^^ Use `if foo.blank?` instead of `unless foo.present?`.
something
end
RUBY
end
it 'auto-corrects' do
new_source = autocorrect_source(source)
expect(new_source).to eq(<<-RUBY.strip_indent)
if foo.blank?
something
end
RUBY
end
end
context 'unless present? with an else' do
let(:source) do
<<-RUBY.strip_indent
unless foo.present?
something
else
something_else
end
RUBY
end
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
unless foo.present?
^^^^^^^^^^^^^^^^^^^ Use `if foo.blank?` instead of `unless foo.present?`.
something
else
something_else
end
RUBY
end
it 'auto-corrects' do
new_source = autocorrect_source(source)
expect(new_source).to eq(<<-RUBY.strip_indent)
if foo.blank?
something
else
something_else
end
RUBY
end
end
end
context 'NilOrEmpty set to false' do
let(:cop_config) do
{ 'NilOrEmpty' => false,
'NotPresent' => true,
'UnlessPresent' => true }
end
it 'accepts checking nil? || empty?' do
expect_no_offenses('foo.nil? || foo.empty?')
end
end
context 'NotPresent set to false' do
let(:cop_config) do
{ 'NilOrEmpty' => true,
'NotPresent' => false,
'UnlessPresent' => true }
end
it 'accepts !present?' do
expect_no_offenses('!foo.present?')
end
end
context 'UnlessPresent set to false' do
let(:cop_config) do
{ 'NilOrEmpty' => true,
'NotPresent' => true,
'UnlessPresent' => false }
end
it 'accepts unless present?' do
expect_no_offenses('something unless foo.present?')
end
end
end