/
disjunctive_assignment_in_constructor_spec.rb
118 lines (108 loc) · 2.81 KB
/
disjunctive_assignment_in_constructor_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
# frozen_string_literal: true
RSpec.describe(
RuboCop::Cop::Lint::DisjunctiveAssignmentInConstructor,
:config
) do
subject(:cop) { described_class.new(config) }
context 'empty constructor' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
class Banana
def initialize
end
end
RUBY
end
end
context 'constructor does not have disjunctive assignment' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
class Banana
def initialize
@delicious = true
end
end
RUBY
end
end
context 'constructor has disjunctive assignment' do
context 'LHS is lvar' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
class Banana
def initialize
delicious ||= true
end
end
RUBY
end
end
context 'LHS is ivar' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
class Banana
def initialize
@delicious ||= true
^^^ Unnecessary disjunctive assignment. Use plain assignment.
end
end
RUBY
expect_correction(<<~RUBY)
class Banana
def initialize
@delicious = true
end
end
RUBY
end
context 'constructor calls super after assignment' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
class Banana
def initialize
@delicious ||= true
^^^ Unnecessary disjunctive assignment. Use plain assignment.
super
end
end
RUBY
expect_correction(<<~RUBY)
class Banana
def initialize
@delicious = true
super
end
end
RUBY
end
end
context 'constructor calls super before disjunctive assignment' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
class Banana
def initialize
super
@delicious ||= true
end
end
RUBY
end
end
context 'constructor calls any method before disjunctive assignment' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
class Banana
def initialize
# With the limitations of static analysis, it's very difficult
# to determine, after this method call, whether the disjunctive
# assignment is necessary or not.
absolutely_any_method
@delicious ||= true
end
end
RUBY
end
end
end
end
end