reek 4.4.0 → 4.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/CONTRIBUTING.md +41 -4
- data/README.md +15 -3
- data/defaults.reek +1 -1
- data/docs/Basic-Smell-Options.md +2 -2
- data/docs/Code-Smells.md +4 -0
- data/docs/How-To-Write-New-Detectors.md +116 -0
- data/docs/How-reek-works-internally.md +3 -4
- data/docs/Instance-Variable-Assumption.md +134 -0
- data/docs/Simulated-Polymorphism.md +1 -1
- data/docs/Smell-Suppression.md +6 -6
- data/docs/{style-guide.md → Style-Guide.md} +0 -0
- data/docs/Unused-Private-Method.md +1 -1
- data/docs/YAML-Reports.md +0 -18
- data/features/configuration_files/directory_specific_directives.feature +4 -4
- data/features/configuration_files/unused_private_method.feature +2 -2
- data/features/samples.feature +122 -117
- data/features/smells/subclassed_from_core_class.feature +1 -1
- data/lib/reek/code_comment.rb +13 -4
- data/lib/reek/context/code_context.rb +1 -0
- data/lib/reek/examiner.rb +24 -27
- data/lib/reek/smells/class_variable.rb +1 -1
- data/lib/reek/smells/control_parameter.rb +1 -1
- data/lib/reek/smells/data_clump.rb +1 -1
- data/lib/reek/smells/duplicate_method_call.rb +1 -1
- data/lib/reek/smells/feature_envy.rb +1 -1
- data/lib/reek/smells/instance_variable_assumption.rb +1 -1
- data/lib/reek/smells/prima_donna_method.rb +1 -1
- data/lib/reek/smells/repeated_conditional.rb +1 -1
- data/lib/reek/smells/smell_detector.rb +5 -14
- data/lib/reek/smells/smell_repository.rb +1 -5
- data/lib/reek/smells/smell_warning.rb +6 -8
- data/lib/reek/smells/subclassed_from_core_class.rb +1 -1
- data/lib/reek/smells/uncommunicative_variable_name.rb +22 -12
- data/lib/reek/smells/unused_private_method.rb +1 -1
- data/lib/reek/spec.rb +2 -2
- data/lib/reek/spec/should_reek_of.rb +12 -8
- data/lib/reek/version.rb +1 -1
- data/spec/reek/code_comment_spec.rb +13 -5
- data/spec/reek/examiner_spec.rb +2 -2
- data/spec/reek/smells/attribute_spec.rb +91 -78
- data/spec/reek/smells/boolean_parameter_spec.rb +72 -64
- data/spec/reek/smells/class_variable_spec.rb +81 -68
- data/spec/reek/smells/control_parameter_spec.rb +101 -141
- data/spec/reek/smells/data_clump_spec.rb +94 -149
- data/spec/reek/smells/duplicate_method_call_spec.rb +98 -85
- data/spec/reek/smells/feature_envy_spec.rb +164 -183
- data/spec/reek/smells/instance_variable_assumption_spec.rb +51 -147
- data/spec/reek/smells/irresponsible_module_spec.rb +153 -170
- data/spec/reek/smells/long_parameter_list_spec.rb +44 -88
- data/spec/reek/smells/long_yield_list_spec.rb +41 -41
- data/spec/reek/smells/manual_dispatch_spec.rb +36 -18
- data/spec/reek/smells/module_initialize_spec.rb +31 -33
- data/spec/reek/smells/nested_iterators_spec.rb +189 -183
- data/spec/reek/smells/nil_check_spec.rb +48 -37
- data/spec/reek/smells/prima_donna_method_spec.rb +41 -26
- data/spec/reek/smells/repeated_conditional_spec.rb +75 -87
- data/spec/reek/smells/smell_warning_spec.rb +7 -0
- data/spec/reek/smells/subclassed_from_core_class_spec.rb +37 -112
- data/spec/reek/smells/too_many_constants_spec.rb +109 -199
- data/spec/reek/smells/too_many_instance_variables_spec.rb +105 -128
- data/spec/reek/smells/too_many_methods_spec.rb +38 -62
- data/spec/reek/smells/too_many_statements_spec.rb +69 -45
- data/spec/reek/smells/uncommunicative_method_name_spec.rb +16 -29
- data/spec/reek/smells/uncommunicative_module_name_spec.rb +24 -37
- data/spec/reek/smells/uncommunicative_parameter_name_spec.rb +55 -60
- data/spec/reek/smells/uncommunicative_variable_name_spec.rb +108 -95
- data/spec/reek/smells/unused_parameters_spec.rb +73 -49
- data/spec/reek/smells/unused_private_method_spec.rb +97 -50
- data/spec/reek/smells/utility_function_spec.rb +130 -188
- data/spec/reek/spec/should_reek_of_spec.rb +2 -2
- metadata +6 -7
- data/lib/reek/cli/warning_collector.rb +0 -27
- data/spec/reek/cli/warning_collector_spec.rb +0 -25
- data/spec/reek/smells/smell_detector_shared.rb +0 -29
@@ -1,250 +1,231 @@
|
|
1
1
|
require_relative '../../spec_helper'
|
2
2
|
require_lib 'reek/smells/feature_envy'
|
3
|
-
require_lib 'reek/examiner'
|
4
|
-
require_relative 'smell_detector_shared'
|
5
3
|
|
6
|
-
# TODO: Bring specs in line with specs for other detectors
|
7
4
|
RSpec.describe Reek::Smells::FeatureEnvy do
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
it 'should not report vcall with no argument' do
|
14
|
-
expect('def simple() func; end').not_to reek_of(:FeatureEnvy)
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'should not report single use' do
|
18
|
-
expect('def no_envy(arga) arga.barg(@item) end').not_to reek_of(:FeatureEnvy)
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'should not report return value' do
|
22
|
-
expect('def no_envy(arga) arga.barg(@item); arga end').not_to reek_of(:FeatureEnvy)
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'should ignore global variables' do
|
26
|
-
expect('def no_envy() $s2.to_a; $s2[@item] end').not_to reek_of(:FeatureEnvy)
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'should not report class methods' do
|
30
|
-
expect('def simple() self.class.new.flatten_merge(self) end').
|
31
|
-
not_to reek_of(:FeatureEnvy)
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'should not report single use of an ivar' do
|
35
|
-
expect('def no_envy() @item.to_a end').not_to reek_of(:FeatureEnvy)
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'should not report returning an ivar' do
|
39
|
-
expect('def no_envy() @item.to_a; @item end').not_to reek_of(:FeatureEnvy)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'should not report ivar usage in a parameter' do
|
43
|
-
expect('def no_envy() @item.price + tax(@item) - savings(@item) end').
|
44
|
-
not_to reek_of(:FeatureEnvy)
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'should not report single use of an lvar' do
|
48
|
-
expect('def no_envy() lv = @item; lv.to_a end').not_to reek_of(:FeatureEnvy)
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'should not report returning an lvar' do
|
52
|
-
expect('def no_envy() lv = @item; lv.to_a; lv end').not_to reek_of(:FeatureEnvy)
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'ignores lvar usage in a parameter' do
|
56
|
-
expect('def no_envy() lv = @item; lv.price + tax(lv) - savings(lv); end').
|
57
|
-
not_to reek_of(:FeatureEnvy)
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'ignores multiple ivars' do
|
61
|
-
src = <<-EOS
|
62
|
-
def func
|
63
|
-
@other.a
|
64
|
-
@other.b
|
65
|
-
@nother.c
|
66
|
-
@nother.d
|
5
|
+
it 'reports the right values' do
|
6
|
+
src = <<-EOS
|
7
|
+
class Alfa
|
8
|
+
def bravo(charlie)
|
9
|
+
(charlie.delta - charlie.echo) * foxtrot
|
67
10
|
end
|
68
|
-
|
69
|
-
|
70
|
-
|
11
|
+
end
|
12
|
+
EOS
|
13
|
+
|
14
|
+
expect(src).to reek_of(:FeatureEnvy,
|
15
|
+
lines: [3, 3],
|
16
|
+
context: 'Alfa#bravo',
|
17
|
+
message: "refers to 'charlie' more than self (maybe move it to another class?)",
|
18
|
+
source: 'string',
|
19
|
+
name: 'charlie')
|
71
20
|
end
|
72
21
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
def
|
77
|
-
|
22
|
+
it 'does count all occurences' do
|
23
|
+
src = <<-EOS
|
24
|
+
class Alfa
|
25
|
+
def bravo(charlie)
|
26
|
+
(charlie.delta - charlie.echo) * foxtrot
|
78
27
|
end
|
79
|
-
|
80
|
-
|
28
|
+
|
29
|
+
def golf(hotel)
|
30
|
+
(hotel.india + hotel.juliett) * kilo
|
31
|
+
end
|
32
|
+
end
|
33
|
+
EOS
|
34
|
+
|
35
|
+
expect(src).to reek_of(:FeatureEnvy,
|
36
|
+
lines: [3, 3],
|
37
|
+
name: 'charlie')
|
38
|
+
expect(src).to reek_of(:FeatureEnvy,
|
39
|
+
lines: [7, 7],
|
40
|
+
name: 'hotel')
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'does not report use of self' do
|
44
|
+
expect('def alfa; self.to_s + self.to_i; end').not_to reek_of(:FeatureEnvy)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'does not report vcall with no argument' do
|
48
|
+
expect('def alfa; bravo; end').not_to reek_of(:FeatureEnvy)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'does not report single use' do
|
52
|
+
expect('def alfa(bravo); bravo.charlie(@delta); end').not_to reek_of(:FeatureEnvy)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'does not report return value' do
|
56
|
+
expect('def alfa(bravo); bravo.charlie(@delta); bravo; end').not_to reek_of(:FeatureEnvy)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'does ignore global variables' do
|
60
|
+
expect('def alfa; $bravo.to_a; $bravo[@charlie]; end').not_to reek_of(:FeatureEnvy)
|
81
61
|
end
|
82
62
|
|
83
|
-
it '
|
63
|
+
it 'does not report class methods' do
|
64
|
+
expect('def alfa; self.class.bravo(self); end').not_to reek_of(:FeatureEnvy)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'does not report single use of an ivar' do
|
68
|
+
expect('def alfa; @bravo.to_a; end').not_to reek_of(:FeatureEnvy)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'does not report returning an ivar' do
|
72
|
+
expect('def alfa; @bravo.to_a; @bravo; end').not_to reek_of(:FeatureEnvy)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'does not report ivar usage in a parameter' do
|
76
|
+
expect('def alfa; @bravo.charlie + delta(@bravo) - echo(@bravo) end').
|
77
|
+
not_to reek_of(:FeatureEnvy)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'does not report single use of an lvar' do
|
81
|
+
expect('def alfa; bravo = @charlie; bravo.to_a; end').not_to reek_of(:FeatureEnvy)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'does not report returning an lvar' do
|
85
|
+
expect('def alfa; bravo = @charlie; bravo.to_a; lv end').not_to reek_of(:FeatureEnvy)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'ignores lvar usage in a parameter' do
|
89
|
+
expect('def alfa; bravo = @item; bravo.charlie + delta(bravo) - echo(bravo); end').
|
90
|
+
not_to reek_of(:FeatureEnvy)
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'ignores multiple ivars' do
|
84
94
|
src = <<-EOS
|
85
|
-
def
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
95
|
+
def func
|
96
|
+
@alfa.charlie
|
97
|
+
@alfa.delta
|
98
|
+
|
99
|
+
@bravo.echo
|
100
|
+
@bravo.foxtrot
|
101
|
+
end
|
102
|
+
EOS
|
103
|
+
|
104
|
+
expect(src).not_to reek_of(:FeatureEnvy)
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'report highest affinity' do
|
108
|
+
src = <<-EOS
|
109
|
+
def alfa
|
110
|
+
bravo = @charlie
|
111
|
+
delta = 0
|
112
|
+
delta += bravo.echo
|
113
|
+
delta += bravo.foxtrot
|
114
|
+
delta *= 1.15
|
91
115
|
end
|
92
116
|
EOS
|
93
|
-
|
94
|
-
expect(src).
|
117
|
+
|
118
|
+
expect(src).to reek_of(:FeatureEnvy, name: 'delta')
|
119
|
+
expect(src).not_to reek_of(:FeatureEnvy, name: 'bravo')
|
95
120
|
end
|
96
121
|
|
97
122
|
it 'should report multiple affinities' do
|
98
123
|
src = <<-EOS
|
99
|
-
def
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
124
|
+
def alfa
|
125
|
+
bravo = @charlie
|
126
|
+
delta = 0
|
127
|
+
delta += bravo.echo
|
128
|
+
delta += bravo.foxtrot
|
104
129
|
end
|
105
130
|
EOS
|
106
|
-
|
107
|
-
expect(src).to reek_of(:FeatureEnvy, name: '
|
131
|
+
|
132
|
+
expect(src).to reek_of(:FeatureEnvy, name: 'delta')
|
133
|
+
expect(src).to reek_of(:FeatureEnvy, name: 'bravo')
|
108
134
|
end
|
109
135
|
|
110
|
-
it '
|
111
|
-
|
112
|
-
def
|
113
|
-
@
|
114
|
-
@
|
136
|
+
it 'is not be fooled by duplication' do
|
137
|
+
src = <<-EOS
|
138
|
+
def alfa(bravo)
|
139
|
+
@charlie.delta(bravo.echo)
|
140
|
+
@foxtrot.delta(bravo.echo)
|
115
141
|
end
|
116
|
-
|
142
|
+
EOS
|
143
|
+
|
144
|
+
expect(src).to reek_only_of(:DuplicateMethodCall)
|
117
145
|
end
|
118
146
|
|
119
|
-
it '
|
120
|
-
|
121
|
-
def
|
122
|
-
|
123
|
-
|
147
|
+
it 'does not count local calls' do
|
148
|
+
src = <<-EOS
|
149
|
+
def alfa(bravo)
|
150
|
+
@charlie.delta(bravo.echo)
|
151
|
+
@foxtrot.delta(bravo.echo)
|
124
152
|
end
|
125
|
-
|
153
|
+
EOS
|
154
|
+
|
155
|
+
expect(src).to reek_only_of(:DuplicateMethodCall)
|
126
156
|
end
|
127
157
|
|
128
|
-
it '
|
129
|
-
|
130
|
-
def
|
131
|
-
|
132
|
-
|
158
|
+
it 'reports many calls to lvar' do
|
159
|
+
src = <<-EOS
|
160
|
+
def alfa
|
161
|
+
bravo = @charlie
|
162
|
+
bravo.delta + bravo.echo
|
133
163
|
end
|
134
|
-
|
135
|
-
end
|
164
|
+
EOS
|
136
165
|
|
137
|
-
|
138
|
-
expect('def func() other.a; other.b; nother.c end').not_to reek_of(:FeatureEnvy)
|
166
|
+
expect(src).to reek_only_of(:FeatureEnvy)
|
139
167
|
end
|
140
168
|
|
141
169
|
it 'counts =~ as a call' do
|
142
170
|
src = <<-EOS
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
171
|
+
def alfa(bravo)
|
172
|
+
charlie(bravo.delta)
|
173
|
+
bravo =~ /charlie/
|
174
|
+
end
|
147
175
|
EOS
|
176
|
+
|
148
177
|
expect(src).to reek_of :FeatureEnvy
|
149
178
|
end
|
150
179
|
|
151
180
|
it 'counts += as a call' do
|
152
181
|
src = <<-EOS
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
182
|
+
def alfa(bravo)
|
183
|
+
charlie(bravo.delta)
|
184
|
+
bravo += 1
|
185
|
+
end
|
157
186
|
EOS
|
187
|
+
|
158
188
|
expect(src).to reek_of :FeatureEnvy
|
159
189
|
end
|
160
190
|
|
161
191
|
it 'counts ivar assignment as call to self' do
|
162
192
|
src = <<-EOS
|
163
|
-
|
164
|
-
|
193
|
+
def foo
|
194
|
+
bravo = charlie(1, 2)
|
165
195
|
|
166
|
-
|
167
|
-
|
168
|
-
|
196
|
+
@delta = bravo.echo
|
197
|
+
@foxtrot = bravo.golf
|
198
|
+
end
|
169
199
|
EOS
|
200
|
+
|
170
201
|
expect(src).not_to reek_of :FeatureEnvy
|
171
202
|
end
|
172
203
|
|
173
204
|
it 'counts self references correctly' do
|
174
205
|
src = <<-EOS
|
175
|
-
def
|
176
|
-
|
177
|
-
self[
|
178
|
-
self[
|
206
|
+
def alfa(bravo)
|
207
|
+
bravo.keys.each do |charlie|
|
208
|
+
self[charlie] += 3
|
209
|
+
self[charlie] = 4
|
179
210
|
end
|
180
211
|
self
|
181
212
|
end
|
182
213
|
EOS
|
183
|
-
expect(src).not_to reek_of(:FeatureEnvy)
|
184
|
-
end
|
185
214
|
|
186
|
-
|
187
|
-
ruby = <<-EOS
|
188
|
-
def report
|
189
|
-
unless @report
|
190
|
-
@report = Report.new
|
191
|
-
cf = SmellConfig.new
|
192
|
-
cf = cf.load_local(@dir) if @dir
|
193
|
-
ContextBuilder.new(@report, cf.smell_listeners).check_source(@source)
|
194
|
-
end
|
195
|
-
@report
|
196
|
-
end
|
197
|
-
EOS
|
198
|
-
expect(ruby).not_to reek_of(:FeatureEnvy)
|
215
|
+
expect(src).not_to reek_of(:FeatureEnvy)
|
199
216
|
end
|
200
217
|
|
201
218
|
it 'interprets << correctly' do
|
202
|
-
|
203
|
-
def
|
204
|
-
if @
|
205
|
-
|
219
|
+
src = <<-EOS
|
220
|
+
def alfa(bravo)
|
221
|
+
if @charlie
|
222
|
+
bravo.delta(self)
|
206
223
|
else
|
207
|
-
|
224
|
+
bravo << self
|
208
225
|
end
|
209
226
|
end
|
210
227
|
EOS
|
211
228
|
|
212
|
-
expect(
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
RSpec.describe Reek::Smells::FeatureEnvy do
|
217
|
-
let(:detector) { build(:smell_detector, smell_type: :FeatureEnvy) }
|
218
|
-
|
219
|
-
it_should_behave_like 'SmellDetector'
|
220
|
-
|
221
|
-
context 'when a smell is reported' do
|
222
|
-
let(:receiver) { 'other' }
|
223
|
-
|
224
|
-
let(:warning) do
|
225
|
-
src = <<-EOS
|
226
|
-
def envious(other)
|
227
|
-
#{receiver}.call
|
228
|
-
self.do_nothing
|
229
|
-
#{receiver}.other
|
230
|
-
#{receiver}.fred
|
231
|
-
end
|
232
|
-
EOS
|
233
|
-
Reek::Examiner.new(src, filter_by_smells: ['FeatureEnvy']).smells.first
|
234
|
-
end
|
235
|
-
|
236
|
-
it_should_behave_like 'common fields set correctly'
|
237
|
-
|
238
|
-
it 'reports the name' do
|
239
|
-
expect(warning.parameters[:name]).to eq(receiver)
|
240
|
-
end
|
241
|
-
|
242
|
-
it 'reports the lines' do
|
243
|
-
expect(warning.lines).to eq([2, 4, 5])
|
244
|
-
end
|
245
|
-
|
246
|
-
it 'has the right message' do
|
247
|
-
expect(warning.message).to eq('refers to other more than self (maybe move it to another class?)')
|
248
|
-
end
|
229
|
+
expect(src).not_to reek_of(:FeatureEnvy)
|
249
230
|
end
|
250
231
|
end
|
@@ -3,104 +3,59 @@ require_relative '../../spec_helper'
|
|
3
3
|
require_lib 'reek/smells/instance_variable_assumption'
|
4
4
|
|
5
5
|
RSpec.describe Reek::Smells::InstanceVariableAssumption do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
@a
|
13
|
-
end
|
14
|
-
end
|
15
|
-
EOS
|
16
|
-
|
17
|
-
expect(src).to reek_of(:InstanceVariableAssumption, lines: [1])
|
6
|
+
it 'reports the right values' do
|
7
|
+
src = <<-EOS
|
8
|
+
class Alfa
|
9
|
+
def bravo
|
10
|
+
@charlie
|
11
|
+
end
|
18
12
|
end
|
19
|
-
|
20
|
-
|
21
|
-
context 'smell parameters' do
|
22
|
-
it 'should report the lines' do
|
23
|
-
src = <<-EOS
|
24
|
-
class Dummy
|
25
|
-
def test
|
26
|
-
@a
|
27
|
-
end
|
28
|
-
end
|
29
|
-
EOS
|
13
|
+
EOS
|
30
14
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
class Dummy
|
39
|
-
def test
|
40
|
-
@a
|
41
|
-
end
|
42
|
-
end
|
43
|
-
EOS
|
15
|
+
expect(src).to reek_of(:InstanceVariableAssumption,
|
16
|
+
lines: [1],
|
17
|
+
context: 'Alfa',
|
18
|
+
message: "assumes too much for instance variable '@charlie'",
|
19
|
+
source: 'string',
|
20
|
+
assumption: :@charlie)
|
21
|
+
end
|
44
22
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
message_a = 'assumes too much for instance variable @a'
|
52
|
-
message_b = 'assumes too much for instance variable @b'
|
53
|
-
|
54
|
-
src = <<-EOS
|
55
|
-
class Dummy
|
56
|
-
def test
|
57
|
-
[@a, @b]
|
58
|
-
end
|
59
|
-
end
|
60
|
-
EOS
|
23
|
+
it 'does count all occurences' do
|
24
|
+
src = <<-EOS
|
25
|
+
class Alfa
|
26
|
+
def bravo
|
27
|
+
@charlie
|
28
|
+
end
|
61
29
|
|
62
|
-
|
63
|
-
|
30
|
+
def delta
|
31
|
+
@echo
|
32
|
+
end
|
64
33
|
end
|
65
34
|
|
66
|
-
|
67
|
-
message_a = 'assumes too much for instance variable @a'
|
68
|
-
message_b = 'assumes too much for instance variable @b'
|
69
|
-
message_c = 'assumes too much for instance variable @c'
|
70
|
-
|
71
|
-
src = <<-EOS
|
72
|
-
class Dummy
|
73
|
-
def test
|
74
|
-
[@a, @a, @b, @c]
|
75
|
-
end
|
76
|
-
|
77
|
-
def retest
|
78
|
-
@c
|
79
|
-
end
|
80
|
-
end
|
81
|
-
EOS
|
35
|
+
EOS
|
82
36
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
37
|
+
expect(src).to reek_of(:InstanceVariableAssumption,
|
38
|
+
lines: [1],
|
39
|
+
assumption: :@charlie)
|
40
|
+
expect(src).to reek_of(:InstanceVariableAssumption,
|
41
|
+
lines: [1],
|
42
|
+
assumption: :@echo)
|
88
43
|
end
|
89
44
|
|
90
45
|
it 'should not report an empty class' do
|
91
46
|
src = <<-EOS
|
92
|
-
class
|
47
|
+
class Alfa
|
93
48
|
end
|
94
49
|
EOS
|
95
50
|
|
96
51
|
expect(src).not_to reek_of(:InstanceVariableAssumption)
|
97
52
|
end
|
98
53
|
|
99
|
-
it '
|
54
|
+
it 'does not report when lazy initializing' do
|
100
55
|
src = <<-EOS
|
101
|
-
class
|
102
|
-
def
|
103
|
-
@
|
56
|
+
class Alfa
|
57
|
+
def bravo
|
58
|
+
@charlie ||= 1
|
104
59
|
end
|
105
60
|
end
|
106
61
|
EOS
|
@@ -108,88 +63,37 @@ RSpec.describe Reek::Smells::InstanceVariableAssumption do
|
|
108
63
|
expect(src).not_to reek_of(:InstanceVariableAssumption)
|
109
64
|
end
|
110
65
|
|
111
|
-
it 'should report when making instance variable assumption' do
|
112
|
-
src = <<-EOS
|
113
|
-
class Dummy
|
114
|
-
def test
|
115
|
-
@a
|
116
|
-
end
|
117
|
-
end
|
118
|
-
EOS
|
119
|
-
|
120
|
-
expect(src).to reek_of(:InstanceVariableAssumption)
|
121
|
-
end
|
122
|
-
|
123
66
|
it 'reports variable even if others are initialized' do
|
124
67
|
src = <<-EOS
|
125
|
-
class
|
68
|
+
class Alfa
|
126
69
|
def initialize
|
127
|
-
@
|
70
|
+
@bravo = 1
|
128
71
|
end
|
129
72
|
|
130
|
-
def
|
131
|
-
[@
|
73
|
+
def charlie
|
74
|
+
[@bravo, @delta]
|
132
75
|
end
|
133
76
|
end
|
134
77
|
EOS
|
135
78
|
|
136
|
-
expect(src).to reek_of(:InstanceVariableAssumption)
|
79
|
+
expect(src).to reek_of(:InstanceVariableAssumption, assumption: :@delta)
|
137
80
|
end
|
138
81
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
@a
|
145
|
-
end
|
146
|
-
|
147
|
-
class Dummiest
|
148
|
-
end
|
149
|
-
end
|
150
|
-
EOS
|
151
|
-
|
152
|
-
expect(src).to reek_of(:InstanceVariableAssumption, context: 'Dummy')
|
153
|
-
end
|
154
|
-
|
155
|
-
it 'should report even if outer class initialize the variable' do
|
156
|
-
src = <<-EOS
|
157
|
-
class Dummy
|
158
|
-
def initialize
|
159
|
-
@a = 1
|
160
|
-
end
|
161
|
-
|
162
|
-
class Dummiest
|
163
|
-
def test
|
164
|
-
@a
|
165
|
-
end
|
166
|
-
end
|
82
|
+
it 'reports inner class even if outer class initializes the variable' do
|
83
|
+
src = <<-EOS
|
84
|
+
class Alfa
|
85
|
+
def initialize
|
86
|
+
@bravo = 1
|
167
87
|
end
|
168
|
-
EOS
|
169
|
-
|
170
|
-
expect(src).to reek_of(:InstanceVariableAssumption, context: 'Dummy::Dummiest')
|
171
|
-
end
|
172
88
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
def initialize
|
177
|
-
@a = 1
|
178
|
-
end
|
179
|
-
|
180
|
-
class Dummiest
|
181
|
-
def initialize
|
182
|
-
@b = 1
|
183
|
-
end
|
184
|
-
|
185
|
-
def test
|
186
|
-
@c
|
187
|
-
end
|
89
|
+
class Charlie
|
90
|
+
def delta
|
91
|
+
@bravo
|
188
92
|
end
|
189
93
|
end
|
190
|
-
|
94
|
+
end
|
95
|
+
EOS
|
191
96
|
|
192
|
-
|
193
|
-
end
|
97
|
+
expect(src).to reek_of(:InstanceVariableAssumption, context: 'Alfa::Charlie')
|
194
98
|
end
|
195
99
|
end
|