reek 4.4.0 → 4.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/CONTRIBUTING.md +41 -4
  4. data/README.md +15 -3
  5. data/defaults.reek +1 -1
  6. data/docs/Basic-Smell-Options.md +2 -2
  7. data/docs/Code-Smells.md +4 -0
  8. data/docs/How-To-Write-New-Detectors.md +116 -0
  9. data/docs/How-reek-works-internally.md +3 -4
  10. data/docs/Instance-Variable-Assumption.md +134 -0
  11. data/docs/Simulated-Polymorphism.md +1 -1
  12. data/docs/Smell-Suppression.md +6 -6
  13. data/docs/{style-guide.md → Style-Guide.md} +0 -0
  14. data/docs/Unused-Private-Method.md +1 -1
  15. data/docs/YAML-Reports.md +0 -18
  16. data/features/configuration_files/directory_specific_directives.feature +4 -4
  17. data/features/configuration_files/unused_private_method.feature +2 -2
  18. data/features/samples.feature +122 -117
  19. data/features/smells/subclassed_from_core_class.feature +1 -1
  20. data/lib/reek/code_comment.rb +13 -4
  21. data/lib/reek/context/code_context.rb +1 -0
  22. data/lib/reek/examiner.rb +24 -27
  23. data/lib/reek/smells/class_variable.rb +1 -1
  24. data/lib/reek/smells/control_parameter.rb +1 -1
  25. data/lib/reek/smells/data_clump.rb +1 -1
  26. data/lib/reek/smells/duplicate_method_call.rb +1 -1
  27. data/lib/reek/smells/feature_envy.rb +1 -1
  28. data/lib/reek/smells/instance_variable_assumption.rb +1 -1
  29. data/lib/reek/smells/prima_donna_method.rb +1 -1
  30. data/lib/reek/smells/repeated_conditional.rb +1 -1
  31. data/lib/reek/smells/smell_detector.rb +5 -14
  32. data/lib/reek/smells/smell_repository.rb +1 -5
  33. data/lib/reek/smells/smell_warning.rb +6 -8
  34. data/lib/reek/smells/subclassed_from_core_class.rb +1 -1
  35. data/lib/reek/smells/uncommunicative_variable_name.rb +22 -12
  36. data/lib/reek/smells/unused_private_method.rb +1 -1
  37. data/lib/reek/spec.rb +2 -2
  38. data/lib/reek/spec/should_reek_of.rb +12 -8
  39. data/lib/reek/version.rb +1 -1
  40. data/spec/reek/code_comment_spec.rb +13 -5
  41. data/spec/reek/examiner_spec.rb +2 -2
  42. data/spec/reek/smells/attribute_spec.rb +91 -78
  43. data/spec/reek/smells/boolean_parameter_spec.rb +72 -64
  44. data/spec/reek/smells/class_variable_spec.rb +81 -68
  45. data/spec/reek/smells/control_parameter_spec.rb +101 -141
  46. data/spec/reek/smells/data_clump_spec.rb +94 -149
  47. data/spec/reek/smells/duplicate_method_call_spec.rb +98 -85
  48. data/spec/reek/smells/feature_envy_spec.rb +164 -183
  49. data/spec/reek/smells/instance_variable_assumption_spec.rb +51 -147
  50. data/spec/reek/smells/irresponsible_module_spec.rb +153 -170
  51. data/spec/reek/smells/long_parameter_list_spec.rb +44 -88
  52. data/spec/reek/smells/long_yield_list_spec.rb +41 -41
  53. data/spec/reek/smells/manual_dispatch_spec.rb +36 -18
  54. data/spec/reek/smells/module_initialize_spec.rb +31 -33
  55. data/spec/reek/smells/nested_iterators_spec.rb +189 -183
  56. data/spec/reek/smells/nil_check_spec.rb +48 -37
  57. data/spec/reek/smells/prima_donna_method_spec.rb +41 -26
  58. data/spec/reek/smells/repeated_conditional_spec.rb +75 -87
  59. data/spec/reek/smells/smell_warning_spec.rb +7 -0
  60. data/spec/reek/smells/subclassed_from_core_class_spec.rb +37 -112
  61. data/spec/reek/smells/too_many_constants_spec.rb +109 -199
  62. data/spec/reek/smells/too_many_instance_variables_spec.rb +105 -128
  63. data/spec/reek/smells/too_many_methods_spec.rb +38 -62
  64. data/spec/reek/smells/too_many_statements_spec.rb +69 -45
  65. data/spec/reek/smells/uncommunicative_method_name_spec.rb +16 -29
  66. data/spec/reek/smells/uncommunicative_module_name_spec.rb +24 -37
  67. data/spec/reek/smells/uncommunicative_parameter_name_spec.rb +55 -60
  68. data/spec/reek/smells/uncommunicative_variable_name_spec.rb +108 -95
  69. data/spec/reek/smells/unused_parameters_spec.rb +73 -49
  70. data/spec/reek/smells/unused_private_method_spec.rb +97 -50
  71. data/spec/reek/smells/utility_function_spec.rb +130 -188
  72. data/spec/reek/spec/should_reek_of_spec.rb +2 -2
  73. metadata +6 -7
  74. data/lib/reek/cli/warning_collector.rb +0 -27
  75. data/spec/reek/cli/warning_collector_spec.rb +0 -25
  76. data/spec/reek/smells/smell_detector_shared.rb +0 -29
@@ -1,98 +1,111 @@
1
1
  require_relative '../../spec_helper'
2
2
  require_lib 'reek/smells/class_variable'
3
- require_lib 'reek/context/module_context'
4
- require_relative 'smell_detector_shared'
5
3
 
6
4
  RSpec.describe Reek::Smells::ClassVariable do
7
- let(:class_variable) { '@@things' }
8
- let(:detector) { build(:smell_detector, smell_type: :ClassVariable) }
5
+ it 'reports the right values' do
6
+ src = <<-EOS
7
+ class Alfa
8
+ @@bravo = 5
9
+ end
10
+ EOS
9
11
 
10
- it_should_behave_like 'SmellDetector'
12
+ expect(src).to reek_of(:ClassVariable,
13
+ lines: [2],
14
+ context: 'Alfa',
15
+ message: "declares the class variable '@@bravo'",
16
+ source: 'string',
17
+ name: '@@bravo')
18
+ end
11
19
 
12
- context 'with no class variables' do
13
- it 'records nothing in the class' do
14
- exp = sexp(:class, :Fred)
15
- expect(detector.sniff(Reek::Context::CodeContext.new(nil, exp))).to be_empty
16
- end
20
+ it 'does count all class variables' do
21
+ src = <<-EOS
22
+ class Alfa
23
+ @@bravo = 42
24
+ @@charlie = 99
25
+ end
26
+ EOS
17
27
 
18
- it 'records nothing in the module' do
19
- exp = sexp(:module, :Fred)
20
- expect(detector.sniff(Reek::Context::CodeContext.new(nil, exp))).to be_empty
21
- end
28
+ expect(src).to reek_of(:ClassVariable, name: '@@bravo')
29
+ expect(src).to reek_of(:ClassVariable, name: '@@charlie')
22
30
  end
23
31
 
24
- context 'with one class variable' do
25
- shared_examples_for 'one variable found' do
26
- let(:ast) { Reek::Source::SourceCode.from(src).syntax_tree }
27
- let(:smells) { detector.sniff(Reek::Context::CodeContext.new(nil, ast)) }
28
-
29
- it 'records only that class variable' do
30
- expect(smells.length).to eq(1)
32
+ it 'does not report class instance variables' do
33
+ src = <<-EOS
34
+ class Alfa
35
+ @bravo = 42
31
36
  end
37
+ EOS
32
38
 
33
- it 'records the variable name' do
34
- expect(smells[0].parameters[:name]).to eq(class_variable)
35
- end
36
- end
39
+ expect(src).to_not reek_of(:ClassVariable)
40
+ end
37
41
 
38
- ['class', 'module'].each do |scope|
39
- context "declared in a #{scope}" do
40
- let(:src) { "#{scope} Fred; #{class_variable} = {}; end" }
41
- it_should_behave_like 'one variable found'
42
- end
42
+ context 'with no class variables' do
43
+ it 'records nothing in the class' do
44
+ src = <<-EOS
45
+ class Alfa
46
+ def bravo; end
47
+ end
48
+ EOS
43
49
 
44
- context "used in a #{scope}" do
45
- let(:src) { "#{scope} Fred; def jim() #{class_variable} = {}; end; end" }
46
- it_should_behave_like 'one variable found'
47
- end
50
+ expect(src).to_not reek_of(:ClassVariable)
51
+ end
48
52
 
49
- context "indexed in a #{scope}" do
50
- let(:src) { "#{scope} Fred; def jim() #{class_variable}[mash] = {}; end; end" }
51
- it_should_behave_like 'one variable found'
52
- end
53
+ it 'records nothing in the module' do
54
+ src = <<-EOS
55
+ module Alfa
56
+ def bravo; end
57
+ end
58
+ EOS
53
59
 
54
- context "declared and used in a #{scope}" do
55
- let(:src) do
56
- <<-EOS
57
- #{scope} Fred
58
- #{class_variable} = {}
59
- def jim() #{class_variable} = {}; end
60
+ expect(src).to_not reek_of(:ClassVariable)
61
+ end
62
+ end
63
+
64
+ ['class', 'module'].each do |scope|
65
+ context "Scoped to #{scope}" do
66
+ context 'set in a method' do
67
+ it 'reports correctly' do
68
+ src = <<-EOS
69
+ #{scope} Alfa
70
+ def bravo
71
+ @@charlie = {}
72
+ end
60
73
  end
61
74
  EOS
75
+
76
+ expect(src).to reek_of(:ClassVariable)
62
77
  end
63
- it_should_behave_like 'one variable found'
64
78
  end
65
79
 
66
- context "used twice in a #{scope}" do
67
- let(:src) do
68
- <<-EOS
69
- #{scope} Fred
70
- def jeff() #{class_variable} = {}; end
71
- def jim() #{class_variable} = {}; end
80
+ context 'used in a method' do
81
+ it 'reports correctly' do
82
+ src = <<-EOS
83
+ #{scope} Alfa
84
+ def bravo
85
+ puts @@charlie
86
+ end
72
87
  end
73
88
  EOS
89
+
90
+ expect(src).to reek_of(:ClassVariable)
74
91
  end
75
- it_should_behave_like 'one variable found'
76
92
  end
77
- end
78
- end
79
93
 
80
- context 'when a smell is reported' do
81
- let(:warning) do
82
- src = <<-EOS
83
- module Fred
84
- #{class_variable} = {}
85
- end
86
- EOS
87
- ctx = Reek::Context::CodeContext.new(nil, Reek::Source::SourceCode.from(src).syntax_tree)
88
- detector.sniff(ctx).first
89
- end
94
+ context "set in #{scope} and used in a method" do
95
+ it 'reports correctly' do
96
+ src = <<-EOS
97
+ #{scope} Alfa
98
+ @@bravo = 42
90
99
 
91
- it_should_behave_like 'common fields set correctly'
100
+ def charlie
101
+ puts @@bravo
102
+ end
103
+ end
104
+ EOS
92
105
 
93
- it 'reports the correct values' do
94
- expect(warning.parameters[:name]).to eq(class_variable)
95
- expect(warning.lines).to eq([2])
106
+ expect(src).to reek_of(:ClassVariable)
107
+ end
108
+ end
96
109
  end
97
110
  end
98
111
  end
@@ -1,292 +1,252 @@
1
1
  require_relative '../../spec_helper'
2
2
  require_lib 'reek/smells/control_parameter'
3
- require_relative 'smell_detector_shared'
4
3
 
5
4
  RSpec.describe Reek::Smells::ControlParameter do
6
- let(:detector) { build(:smell_detector, smell_type: :ControlParameter) }
5
+ it 'reports the right values' do
6
+ src = <<-EOS
7
+ def alfa(bravo)
8
+ bravo ? true : false
9
+ end
10
+ EOS
11
+
12
+ expect(src).to reek_of(:ControlParameter,
13
+ lines: [2],
14
+ context: 'alfa',
15
+ message: "is controlled by argument 'bravo'",
16
+ source: 'string',
17
+ argument: 'bravo')
18
+ end
19
+
20
+ it 'does count all occurences' do
21
+ src = <<-EOS
22
+ def alfa(bravo, charlie)
23
+ bravo ? true : false
24
+ charlie ? true : false
25
+ end
26
+ EOS
7
27
 
8
- it_should_behave_like 'SmellDetector'
28
+ expect(src).to reek_of(:ControlParameter, lines: [2], argument: 'bravo')
29
+ expect(src).to reek_of(:ControlParameter, lines: [3], argument: 'charlie')
30
+ end
9
31
 
10
32
  context 'parameter not used to determine code path' do
11
33
  it 'does not report a ternary check on an ivar' do
12
- src = 'def simple(arga) @ivar ? arga : 3 end'
34
+ src = 'def alfa(bravo) @charlie ? bravo : false end'
13
35
  expect(src).not_to reek_of(:ControlParameter)
14
36
  end
15
37
 
16
38
  it 'does not report a ternary check on a lvar' do
17
- src = 'def simple(arga) lvar = 27; lvar ? arga : @ivar end'
39
+ src = 'def alfa(bravo) charlie = 27; charlie ? bravo : @delta end'
18
40
  expect(src).not_to reek_of(:ControlParameter)
19
41
  end
20
42
 
21
43
  it 'does not report when parameter is unused' do
22
- src = 'def simple(arg) test = 1 end'
44
+ src = 'def alfa(bravo) charlie = 1 end'
23
45
  expect(src).not_to reek_of(:ControlParameter)
24
46
  end
25
47
 
26
48
  it 'does not report when parameter is used inside conditional' do
27
- src = 'def simple(arg) if true then puts arg end end'
49
+ src = 'def alfa(bravo) if true then puts bravo end end'
28
50
  expect(src).not_to reek_of(:ControlParameter)
29
51
  end
30
52
  end
31
53
 
32
54
  context 'parameter only used to determine code path' do
33
55
  it 'reports a ternary check on a parameter' do
34
- src = 'def simple(arga) arga ? @ivar : 3 end'
35
- expect(src).to reek_of(:ControlParameter, argument: 'arga')
56
+ src = 'def alfa(bravo); bravo ? true : false; end'
57
+ expect(src).to reek_of(:ControlParameter)
36
58
  end
37
59
 
38
60
  it 'reports a couple inside a block' do
39
- src = 'def blocks(arg) @text.map { |blk| arg ? blk : "#{blk}" } end'
40
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
61
+ src = 'def alfa(bravo); charlie.map { |delta| bravo ? delta : "#{delta}" }; end'
62
+ expect(src).to reek_of(:ControlParameter)
41
63
  end
42
64
 
43
65
  it 'reports on an if statement modifier' do
44
- src = 'def simple(arg) args = {}; args.merge(\'a\' => \'A\') if arg end'
45
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
66
+ src = 'def alfa(bravo); charlie if bravo; end'
67
+ expect(src).to reek_of(:ControlParameter)
46
68
  end
47
69
 
48
70
  it 'reports on an unless statement modifier' do
49
- src = 'def simple(arg) args = {}; args.merge(\'a\' => \'A\') unless arg end'
50
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
71
+ src = 'def alfa(bravo); charlie unless bravo; end'
72
+ expect(src).to reek_of(:ControlParameter)
51
73
  end
52
74
 
53
75
  it 'reports on if control expression' do
54
- src = 'def simple(arg) args = {}; if arg then args.merge(\'a\' => \'A\') end end'
55
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
76
+ src = 'def alfa(bravo); if bravo then charlie end; end'
77
+ expect(src).to reek_of(:ControlParameter)
56
78
  end
57
79
 
58
80
  it 'reports on if control expression with &&' do
59
- src = 'def simple(arg) if arg && true then puts "arg" end end'
60
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
81
+ src = 'def alfa(bravo); if bravo && true then charlie end; end'
82
+ expect(src).to reek_of(:ControlParameter)
61
83
  end
62
84
 
63
- it 'reports on if control expression with preceding &&' do
64
- src = 'def simple(arg) if true && arg then puts "arg" end end'
65
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
85
+ it 'reports on if control expression with `and`' do
86
+ src = 'def alfa(bravo); if bravo and true then charlie end; end'
87
+ expect(src).to reek_of(:ControlParameter)
66
88
  end
67
89
 
68
- it 'reports on if control expression with two && conditions' do
69
- src = 'def simple(a) ag = {}; if a && true && true then puts "2" end end'
70
- expect(src).to reek_of(:ControlParameter, argument: 'a')
90
+ it 'reports on if control expression with preceding &&' do
91
+ src = 'def alfa(bravo); if true && bravo then charlie end; end'
92
+ expect(src).to reek_of(:ControlParameter)
71
93
  end
72
94
 
73
95
  it 'reports on if control expression with ||' do
74
- src = 'def simple(arg) args = {}; if arg || true then puts "arg" end end'
75
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
96
+ src = 'def alfa(bravo); if bravo || true then charlie end; end'
97
+ expect(src).to reek_of(:ControlParameter)
76
98
  end
77
99
 
78
100
  it 'reports on if control expression with or' do
79
- src = 'def simple(arg) args = {}; if arg or true then puts "arg" end end'
80
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
101
+ src = 'def alfa(bravo); if bravo or true then charlie end; end'
102
+ expect(src).to reek_of(:ControlParameter)
81
103
  end
82
104
 
83
105
  it 'reports on if control expression with if' do
84
- src = 'def simple(arg) args = {}; if (arg if true) then puts "arg" end end'
85
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
106
+ src = 'def alfa(bravo); if (bravo if true) then charlie end; end'
107
+ expect(src).to reek_of(:ControlParameter)
86
108
  end
87
109
 
88
110
  it 'reports on && notation' do
89
- src = 'def simple(arg) args = {}; arg && args.merge(\'a\' => \'A\') end'
90
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
111
+ src = 'def alfa(bravo); bravo && charlie; end'
112
+ expect(src).to reek_of(:ControlParameter)
91
113
  end
92
114
 
93
115
  it 'reports on || notation' do
94
- src = 'def simple(arg) args = {}; arg || args.merge(\'a\' => \'A\') end'
95
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
116
+ src = 'def alfa(bravo); bravo || charlie; end'
117
+ expect(src).to reek_of(:ControlParameter)
96
118
  end
97
119
 
98
120
  it 'reports on case statement' do
99
- src = 'def simple(arg) case arg when nil; nil when false; nil else nil end end'
100
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
121
+ src = <<-EOS
122
+ def alfa(bravo)
123
+ case bravo
124
+ when nil then nil
125
+ else false
126
+ end
127
+ end
128
+ EOS
129
+
130
+ expect(src).to reek_of(:ControlParameter)
101
131
  end
102
132
 
103
- it 'reports on nested if statements that are both control parameters' do
133
+ it 'reports on nested if statements that are both using control parameters' do
104
134
  src = <<-EOS
105
- def nested(arg)
106
- if arg
107
- puts 'a'
108
- puts 'b' if arg
135
+ def nested(bravo)
136
+ if bravo
137
+ charlie
138
+ charlie if bravo
109
139
  end
110
140
  end
111
141
  EOS
112
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
142
+
143
+ expect(src).to reek_of(:ControlParameter)
113
144
  end
114
145
 
115
146
  it 'reports on nested if statements where the inner if is a control parameter' do
116
147
  src = <<-EOS
117
- def nested(arg)
148
+ def nested(bravo)
118
149
  if true
119
- puts 'a'
120
- puts 'b' if arg
150
+ charlie
151
+ charlie if bravo
121
152
  end
122
153
  end
123
154
  EOS
124
- expect(src).to reek_of(:ControlParameter, argument: 'arg')
155
+
156
+ expect(src).to reek_of(:ControlParameter)
125
157
  end
126
158
 
127
159
  it 'reports on explicit comparison in the condition' do
128
- src = 'def simple(arg) if arg == :foo then :foo else :bar end end'
160
+ src = 'def alfa(bravo); if bravo == charlie then charlie end; end'
129
161
  expect(src).to reek_of(:ControlParameter)
130
162
  end
131
163
 
132
164
  it 'reports on explicit negative comparison in the condition' do
133
- src = 'def simple(arg) if arg != :foo then :bar else :foo end end'
165
+ src = 'def alfa(bravo); if bravo != charlie then charlie end; end'
134
166
  expect(src).to reek_of(:ControlParameter)
135
167
  end
136
168
 
137
169
  it 'reports when the argument is compared to a regexp' do
138
- src = 'def simple(arg) if arg =~ /foo/ then :foo else :bar end end'
170
+ src = 'def alfa(bravo); if bravo =~ charlie then charlie end; end'
139
171
  expect(src).to reek_of(:ControlParameter)
140
172
  end
141
173
 
142
174
  it 'reports when the argument is reverse-compared to a regexp' do
143
- src = 'def simple(arg) if /foo/ =~ arg then :foo else :bar end end'
175
+ src = 'def alfa(bravo); if /charlie/ =~ bravo then charlie end; end'
144
176
  expect(src).to reek_of(:ControlParameter)
145
177
  end
146
178
 
147
179
  it 'reports when the argument is used in a complex regexp' do
148
- src = 'def simple(arg) if /foo#{arg}/ =~ bar then :foo else :bar end end'
180
+ src = 'def alfa(bravo); if /charlie#{bravo}/ =~ delta then charlie end; end'
149
181
  expect(src).to reek_of(:ControlParameter)
150
182
  end
151
183
 
152
184
  it 'reports when the argument is a block parameter' do
153
- src = <<-EOS
154
- def foo &blk
155
- bar(blk || proc {})
156
- end
157
- EOS
185
+ src = 'def bravo(&charlie); delta(charlie || proc {}); end'
158
186
  expect(src).to reek_of(:ControlParameter)
159
187
  end
160
188
  end
161
189
 
162
190
  context 'parameter used besides determining code path' do
163
191
  it 'does not report on if conditional expression' do
164
- src = 'def simple(arg) if arg then use(arg) else use(@other) end end'
192
+ src = 'def alfa(bravo); if bravo then charlie(bravo); end end'
165
193
  expect(src).not_to reek_of(:ControlParameter)
166
194
  end
167
195
 
168
196
  it 'does not report on an if statement modifier' do
169
- src = 'def simple(arg) args = {}; args.merge(\'a\' => arg) if arg end'
197
+ src = 'def alfa(bravo); charlie(bravo) if bravo; end'
170
198
  expect(src).not_to reek_of(:ControlParameter)
171
199
  end
172
200
 
173
201
  it 'does not report on an unless statement modifier' do
174
- src = 'def simple(arg) args = {}; args.merge(\'a\' => arg) unless arg end'
175
- expect(src).not_to reek_of(:ControlParameter)
176
- end
177
-
178
- it 'does not report on if control expression' do
179
- src = 'def simple(arg) args = {}; if arg then args.merge(\'a\' => arg) end end'
202
+ src = 'def alfa(bravo); charlie(bravo) unless bravo; end'
180
203
  expect(src).not_to reek_of(:ControlParameter)
181
204
  end
182
205
 
183
206
  it 'does not report on if control expression with &&' do
184
- src = 'def simple(arg) if arg && true then puts arg end end'
207
+ src = 'def alfa(bravo) if bravo && true then puts bravo end end'
185
208
  expect(src).not_to reek_of(:ControlParameter)
186
209
  end
187
210
 
188
211
  it 'does not report on && notation' do
189
- src = 'def simple(arg) args = {}; arg && args.merge(\'a\' => arg) end'
212
+ src = 'def alfa(bravo); bravo && charlie(bravo); end'
190
213
  expect(src).not_to reek_of(:ControlParameter)
191
214
  end
192
215
 
193
216
  it 'does not report on || notation' do
194
- src = 'def simple(arg) args = {}; arg || args.merge(\'a\' => arg) end'
217
+ src = 'def alfa(bravo); bravo || charlie(bravo) end'
195
218
  expect(src).not_to reek_of(:ControlParameter)
196
219
  end
197
220
 
198
221
  it 'does not report when parameter is used outside conditional' do
199
- src = 'def simple(arg) puts arg; if arg then @a = 1 end end'
222
+ src = 'def alfa(bravo) puts bravo; if bravo then charlie end; end'
200
223
  expect(src).not_to reek_of(:ControlParameter)
201
224
  end
202
225
 
203
226
  it 'does not report when parameter is used as a method call argument in a condition' do
204
- src = 'def simple(arg) if foo(arg) then @a = 1 end end'
227
+ src = 'def alfa(bravo); if charlie(bravo) then delta end; end'
205
228
  expect(src).not_to reek_of(:ControlParameter)
206
229
  end
207
230
 
208
231
  it 'does not report when parameter is used as a method call receiver in a condition' do
209
- src = 'def simple(arg) if arg.foo? then @a = 1 end end'
210
- expect(src).not_to reek_of(:ControlParameter)
211
- end
212
-
213
- it 'does not report when the argument is a hash on which we access a key' do
214
- src = 'def simple(arg) if arg[\'a\'] then puts \'a\' else puts \'b\' end end'
215
- expect(src).not_to reek_of :ControlParameter
216
- end
217
-
218
- it 'does not report when used in first conditional but not second' do
219
- src = <<-EOS
220
- def things(arg)
221
- if arg
222
- puts arg
223
- end
224
- if arg
225
- puts 'a'
226
- end
227
- end
228
- EOS
229
- expect(src).not_to reek_of(:ControlParameter)
230
- end
231
-
232
- it 'does not report when used in second conditional but not first' do
233
- src = <<-EOS
234
- def things(arg)
235
- if arg
236
- puts 'a'
237
- end
238
- if arg
239
- puts arg
240
- end
241
- end
242
- EOS
232
+ src = 'def alfa(bravo); if bravo.charlie? then delta end; end'
243
233
  expect(src).not_to reek_of(:ControlParameter)
244
234
  end
245
235
 
246
236
  it 'does not report when used in body of control flow operator' do
247
237
  src = <<-EOS
248
- def foo(arg)
249
- case arg
250
- when :bar
251
- puts 'a'
238
+ def alfa(bravo)
239
+ case bravo
240
+ when :charlie
241
+ puts 'charlie'
252
242
  else
253
- puts 'b'
254
- end
255
- qux or quuz(arg)
256
- end
257
- EOS
258
- expect(src).not_to reek_of(:ControlParameter)
259
- end
260
- end
261
-
262
- context 'when a smell is reported' do
263
- let(:warning) do
264
- src = <<-EOS
265
- def things(arg)
266
- @text.map do |blk|
267
- arg ? blk : "blk"
243
+ puts 'delta'
268
244
  end
269
- puts "hello" if arg
245
+ echo or foxtrot(bravo)
270
246
  end
271
247
  EOS
272
- ctx = Reek::Context::MethodContext.new(nil, Reek::Source::SourceCode.from(src).syntax_tree)
273
- smells = detector.run_for(ctx)
274
- expect(smells.length).to eq(1)
275
- smells.first
276
- end
277
-
278
- it_should_behave_like 'common fields set correctly'
279
-
280
- it 'reports the argument' do
281
- expect(warning.parameters[:argument]).to eq('arg')
282
- end
283
-
284
- it 'reports the lines' do
285
- expect(warning.lines).to eq([3, 5])
286
- end
287
248
 
288
- it 'has the right message' do
289
- expect(warning.message).to eq('is controlled by argument arg')
249
+ expect(src).not_to reek_of(:ControlParameter)
290
250
  end
291
251
  end
292
252
  end