/
redundant_self_assignment_branch_spec.rb
236 lines (206 loc) · 6.01 KB
/
redundant_self_assignment_branch_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::RedundantSelfAssignmentBranch, :config do
it 'registers and corrects an offense when self-assigning redundant else ternary branch' do
expect_offense(<<~RUBY)
foo = condition ? bar : foo
^^^ Remove the self-assignment branch.
RUBY
expect_correction(<<~RUBY)
foo = bar if condition
RUBY
end
it 'registers and corrects an offense when self-assigning redundant if ternary branch' do
expect_offense(<<~RUBY)
foo = condition ? foo : bar
^^^ Remove the self-assignment branch.
RUBY
expect_correction(<<~RUBY)
foo = bar unless condition
RUBY
end
it 'registers and corrects an offense when self-assigning redundant else branch' do
expect_offense(<<~RUBY)
foo = if condition
bar
else
foo
^^^ Remove the self-assignment branch.
end
RUBY
expect_correction(<<~RUBY)
foo = if condition
bar
end
RUBY
end
it 'registers and corrects an offense when self-assigning redundant if branch' do
expect_offense(<<~RUBY)
foo = if condition
foo
^^^ Remove the self-assignment branch.
else
bar
end
RUBY
expect_correction(<<~RUBY)
foo = unless condition
bar
end
RUBY
end
it 'registers and corrects an offense when self-assigning redundant else branch and multiline if branch' do
expect_offense(<<~RUBY)
foo = if condition
bar
baz
else
foo
^^^ Remove the self-assignment branch.
end
RUBY
expect_correction(<<~RUBY)
foo = if condition
bar
baz
end
RUBY
end
it 'registers and corrects an offense when self-assigning redundant else branch and multiline else branch' do
expect_offense(<<~RUBY)
foo = if condition
foo
^^^ Remove the self-assignment branch.
else
bar
baz
end
RUBY
expect_correction(<<~RUBY)
foo = unless condition
bar
baz
end
RUBY
end
it 'registers and corrects an offense when self-assigning redundant else branch and empty if branch' do
expect_offense(<<~RUBY)
foo = if condition
else
foo
^^^ Remove the self-assignment branch.
end
RUBY
expect_correction(<<~RUBY)
foo = if condition
end
RUBY
end
it 'registers and corrects an offense when self-assigning redundant else branch and empty else branch' do
expect_offense(<<~RUBY)
foo = if condition
foo
^^^ Remove the self-assignment branch.
else
end
RUBY
expect_correction(<<~RUBY)
foo = unless condition
end
RUBY
end
it 'registers and corrects an offense when self-assigning redundant else ternary branch for ivar' do
expect_offense(<<~RUBY)
@foo = condition ? @bar : @foo
^^^^ Remove the self-assignment branch.
RUBY
expect_correction(<<~RUBY)
@foo = @bar if condition
RUBY
end
it 'registers and corrects an offense when self-assigning redundant else ternary branch for cvar' do
expect_offense(<<~RUBY)
@@foo = condition ? @@bar : @@foo
^^^^^ Remove the self-assignment branch.
RUBY
expect_correction(<<~RUBY)
@@foo = @@bar if condition
RUBY
end
it 'registers and corrects an offense when self-assigning redundant else ternary branch for gvar' do
expect_offense(<<~RUBY)
$foo = condition ? $bar : $foo
^^^^ Remove the self-assignment branch.
RUBY
expect_correction(<<~RUBY)
$foo = $bar if condition
RUBY
end
it 'does not register an offense when not self-assigning redundant branches' do
expect_no_offenses(<<~RUBY)
foo = condition ? bar : baz
RUBY
end
it 'does not register an offense when using only if branch' do
expect_no_offenses(<<~RUBY)
foo = if condition
bar
end
RUBY
end
it 'does not register an offense when multi assignment' do
expect_no_offenses(<<~RUBY)
foo, bar = baz
RUBY
end
# Ignore method calls as they can have side effects. In other words, it may be unsafe detection.
it 'does not register an offense when lhs is not variable' do
expect_no_offenses(<<~RUBY)
foo.do_something = condition ? foo.do_something : bar.do_something
RUBY
end
it 'registers and corrects an offense when using `elsif` and self-assigning the value of `then` branch' do
expect_offense(<<~RUBY)
foo = if condition
foo
^^^ Remove the self-assignment branch.
elsif another_condtion
bar
else
baz
end
RUBY
expect_correction(<<~RUBY)
foo = if another_condtion
bar
else
baz
end
RUBY
end
# It may be possible to extend it to register an offense in future.
# auto-correction test patterns should be considered and implemented.
it 'registers and corrects an offense when using `elsif` and self-assigning the value of `elsif` branch' do
expect_no_offenses(<<~RUBY)
foo = if condition
bar
elsif another_condtion
foo
else
baz
end
RUBY
end
# It may be possible to extend it to register an offense in future.
# auto-correction test patterns should be considered and implemented.
it 'registers and corrects an offense when using `elsif` and self-assigning the value of `else` branch' do
expect_no_offenses(<<~RUBY)
foo = if condition
bar
elsif another_condtion
baz
else
foo
end
RUBY
end
end