forked from rubocop/rubocop
/
negated_if_else_condition_spec.rb
229 lines (206 loc) · 5.34 KB
/
negated_if_else_condition_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::NegatedIfElseCondition, :config do
it 'registers an offense and corrects when negating condition with `!` for `if-else`' do
expect_offense(<<~RUBY)
if !x
^^^^^ Invert the negated condition and swap the if-else branches.
do_something
else
do_something_else
end
RUBY
expect_correction(<<~RUBY)
if x
do_something_else
else
do_something
end
RUBY
end
it 'registers an offense and corrects when negating condition with `not` for `if-else`' do
expect_offense(<<~RUBY)
if not x
^^^^^^^^ Invert the negated condition and swap the if-else branches.
do_something
else
do_something_else
end
RUBY
expect_correction(<<~RUBY)
if x
do_something_else
else
do_something
end
RUBY
end
it 'registers an offense and corrects when negating condition with `not` for ternary' do
expect_offense(<<~RUBY)
!x ? do_something : do_something_else
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Invert the negated condition and swap the ternary branches.
RUBY
expect_correction(<<~RUBY)
x ? do_something_else : do_something
RUBY
end
shared_examples 'negation method' do |method, inverted_method|
it "registers an offense and corrects when negating condition with `#{method}` for `if-else`" do
expect_offense(<<~RUBY, method: method)
if x %{method} y
^^^^^^{method}^^ Invert the negated condition and swap the if-else branches.
do_something
else
do_something_else
end
RUBY
expect_correction(<<~RUBY)
if x #{inverted_method} y
do_something_else
else
do_something
end
RUBY
end
it "registers an offense and corrects when negating condition with `#{method}` for ternary" do
expect_offense(<<~RUBY, method: method)
x %{method} y ? do_something : do_something_else
^^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Invert the negated condition and swap the ternary branches.
RUBY
expect_correction(<<~RUBY)
x #{inverted_method} y ? do_something_else : do_something
RUBY
end
end
it_behaves_like('negation method', '!=', '==')
it_behaves_like('negation method', '!~', '=~')
it 'registers an offense and corrects nested `if-else` with negated condition' do
expect_offense(<<~RUBY)
if !x
^^^^^ Invert the negated condition and swap the if-else branches.
do_something
else
if !y
^^^^^ Invert the negated condition and swap the if-else branches.
do_something_else_1
else
do_something_else_2
end
end
RUBY
expect_correction(<<~RUBY)
if x
if y
do_something_else_2
else
do_something_else_1
end
else
do_something
end
RUBY
end
it 'registers an offense when using negated condition and `if` branch body is empty' do
expect_offense(<<~RUBY)
if !condition.nil?
^^^^^^^^^^^^^^^^^^ Invert the negated condition and swap the if-else branches.
else
foo = 42
end
RUBY
expect_correction(<<~RUBY)
if condition.nil?
foo = 42
end
RUBY
end
it 'moves comments to correct branches during autocorrect' do
expect_offense(<<~RUBY)
if !condition.nil?
^^^^^^^^^^^^^^^^^^ Invert the negated condition and swap the if-else branches.
# part B
# and foo is 39
foo = 39
else
# part A
# and foo is 42
foo = 42
end
RUBY
expect_correction(<<~RUBY)
if condition.nil?
# part A
# and foo is 42
foo = 42
else
# part B
# and foo is 39
foo = 39
end
RUBY
end
it 'works with comments and multiple statements' do
expect_offense(<<~RUBY)
if !condition.nil?
^^^^^^^^^^^^^^^^^^ Invert the negated condition and swap the if-else branches.
# part A
# and foo is 1 and bar is 2
foo = 1
bar = 2
else
# part B
# and foo is 3 and bar is 4
foo = 3
bar = 4
end
RUBY
expect_correction(<<~RUBY)
if condition.nil?
# part B
# and foo is 3 and bar is 4
foo = 3
bar = 4
else
# part A
# and foo is 1 and bar is 2
foo = 1
bar = 2
end
RUBY
end
it 'does not register an offense when negating condition for `if-elsif`' do
expect_no_offenses(<<~RUBY)
if !x
do_something
elsif !y
do_something_else
else
do_another_thing
end
RUBY
end
it 'does not register an offense when not whe whole condition is negated' do
expect_no_offenses(<<~RUBY)
if !x && y
do_something
else
do_another_thing
end
RUBY
end
it 'does not register an offense when `if` with `!!` condition' do
expect_no_offenses(<<~RUBY)
if !!x
do_something
else
do_another_thing
end
RUBY
end
it 'does not register an offense when `if` with negated condition has no `else` branch' do
expect_no_offenses(<<~RUBY)
if !x
do_something
end
RUBY
end
end