reek 1.3.8 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +11 -0
- data/README.md +22 -14
- data/Rakefile +2 -15
- data/assets/html_output.html.erb +103 -0
- data/features/command_line_interface/options.feature +3 -0
- data/features/command_line_interface/smell_selection.feature +19 -0
- data/features/rake_task/rake_task.feature +1 -1
- data/features/reports/reports.feature +16 -0
- data/features/reports/yaml.feature +26 -23
- data/features/samples.feature +2 -1
- data/features/step_definitions/reek_steps.rb +15 -15
- data/features/support/env.rb +7 -9
- data/lib/reek/cli/application.rb +2 -4
- data/lib/reek/cli/command.rb +12 -0
- data/lib/reek/cli/help_command.rb +3 -6
- data/lib/reek/cli/options.rb +147 -0
- data/lib/reek/cli/reek_command.rb +18 -14
- data/lib/reek/cli/report/formatter.rb +56 -0
- data/lib/reek/cli/report/report.rb +106 -0
- data/lib/reek/cli/report/strategy.rb +63 -0
- data/lib/reek/cli/version_command.rb +3 -6
- data/lib/reek/config_file_exception.rb +0 -1
- data/lib/reek/core/code_context.rb +1 -3
- data/lib/reek/core/code_parser.rb +13 -12
- data/lib/reek/core/method_context.rb +13 -2
- data/lib/reek/core/module_context.rb +0 -4
- data/lib/reek/core/object_refs.rb +2 -3
- data/lib/reek/core/singleton_method_context.rb +0 -2
- data/lib/reek/core/smell_configuration.rb +3 -5
- data/lib/reek/core/smell_repository.rb +7 -8
- data/lib/reek/core/sniffer.rb +4 -10
- data/lib/reek/core/stop_context.rb +2 -4
- data/lib/reek/core/warning_collector.rb +0 -1
- data/lib/reek/examiner.rb +19 -17
- data/lib/reek/rake/task.rb +7 -10
- data/lib/reek/smell_warning.rb +4 -8
- data/lib/reek/smells.rb +0 -1
- data/lib/reek/smells/attribute.rb +8 -11
- data/lib/reek/smells/boolean_parameter.rb +5 -7
- data/lib/reek/smells/class_variable.rb +6 -7
- data/lib/reek/smells/control_parameter.rb +78 -45
- data/lib/reek/smells/data_clump.rb +13 -16
- data/lib/reek/smells/duplicate_method_call.rb +13 -11
- data/lib/reek/smells/feature_envy.rb +6 -7
- data/lib/reek/smells/irresponsible_module.rb +4 -6
- data/lib/reek/smells/long_parameter_list.rb +5 -7
- data/lib/reek/smells/long_yield_list.rb +2 -4
- data/lib/reek/smells/nested_iterators.rb +12 -22
- data/lib/reek/smells/nil_check.rb +35 -46
- data/lib/reek/smells/prima_donna_method.rb +24 -16
- data/lib/reek/smells/repeated_conditional.rb +8 -10
- data/lib/reek/smells/smell_detector.rb +9 -7
- data/lib/reek/smells/too_many_instance_variables.rb +7 -9
- data/lib/reek/smells/too_many_methods.rb +6 -8
- data/lib/reek/smells/too_many_statements.rb +4 -6
- data/lib/reek/smells/uncommunicative_method_name.rb +5 -7
- data/lib/reek/smells/uncommunicative_module_name.rb +5 -7
- data/lib/reek/smells/uncommunicative_parameter_name.rb +7 -9
- data/lib/reek/smells/uncommunicative_variable_name.rb +15 -18
- data/lib/reek/smells/unused_parameters.rb +5 -45
- data/lib/reek/smells/utility_function.rb +9 -10
- data/lib/reek/source.rb +0 -1
- data/lib/reek/source/code_comment.rb +7 -8
- data/lib/reek/source/config_file.rb +2 -4
- data/lib/reek/source/core_extras.rb +1 -1
- data/lib/reek/source/reference_collector.rb +1 -2
- data/lib/reek/source/sexp_extensions.rb +93 -10
- data/lib/reek/source/sexp_formatter.rb +2 -3
- data/lib/reek/source/sexp_node.rb +19 -15
- data/lib/reek/source/source_code.rb +4 -14
- data/lib/reek/source/source_file.rb +3 -5
- data/lib/reek/source/source_locator.rb +5 -6
- data/lib/reek/source/source_repository.rb +3 -3
- data/lib/reek/source/tree_dresser.rb +2 -2
- data/lib/reek/spec.rb +1 -2
- data/lib/reek/spec/should_reek.rb +8 -5
- data/lib/reek/spec/should_reek_of.rb +6 -4
- data/lib/reek/spec/should_reek_only_of.rb +10 -6
- data/lib/reek/version.rb +1 -1
- data/reek.gemspec +34 -30
- data/spec/gem/updates_spec.rb +3 -4
- data/spec/gem/yard_spec.rb +1 -2
- data/spec/matchers/smell_of_matcher.rb +12 -14
- data/spec/quality/reek_source_spec.rb +42 -0
- data/spec/reek/cli/help_command_spec.rb +7 -5
- data/spec/reek/cli/report_spec.rb +89 -22
- data/spec/reek/cli/version_command_spec.rb +8 -6
- data/spec/reek/core/code_context_spec.rb +25 -26
- data/spec/reek/core/code_parser_spec.rb +6 -6
- data/spec/reek/core/method_context_spec.rb +18 -18
- data/spec/reek/core/module_context_spec.rb +5 -5
- data/spec/reek/core/object_refs_spec.rb +21 -22
- data/spec/reek/core/smell_configuration_spec.rb +22 -21
- data/spec/reek/core/stop_context_spec.rb +2 -2
- data/spec/reek/core/warning_collector_spec.rb +3 -3
- data/spec/reek/examiner_spec.rb +9 -9
- data/spec/reek/smell_warning_spec.rb +29 -29
- data/spec/reek/smells/attribute_spec.rb +6 -6
- data/spec/reek/smells/behaves_like_variable_detector.rb +6 -6
- data/spec/reek/smells/boolean_parameter_spec.rb +17 -17
- data/spec/reek/smells/class_variable_spec.rb +9 -9
- data/spec/reek/smells/control_parameter_spec.rb +161 -137
- data/spec/reek/smells/data_clump_spec.rb +22 -19
- data/spec/reek/smells/duplicate_method_call_spec.rb +71 -27
- data/spec/reek/smells/feature_envy_spec.rb +32 -32
- data/spec/reek/smells/irresponsible_module_spec.rb +21 -21
- data/spec/reek/smells/long_parameter_list_spec.rb +14 -14
- data/spec/reek/smells/long_yield_list_spec.rb +6 -6
- data/spec/reek/smells/nested_iterators_spec.rb +21 -21
- data/spec/reek/smells/nil_check_spec.rb +23 -15
- data/spec/reek/smells/prima_donna_method_spec.rb +5 -5
- data/spec/reek/smells/repeated_conditional_spec.rb +14 -14
- data/spec/reek/smells/smell_detector_shared.rb +9 -9
- data/spec/reek/smells/too_many_instance_variables_spec.rb +12 -12
- data/spec/reek/smells/too_many_methods_spec.rb +10 -10
- data/spec/reek/smells/too_many_statements_spec.rb +41 -41
- data/spec/reek/smells/uncommunicative_method_name_spec.rb +4 -4
- data/spec/reek/smells/uncommunicative_module_name_spec.rb +12 -12
- data/spec/reek/smells/uncommunicative_parameter_name_spec.rb +21 -21
- data/spec/reek/smells/uncommunicative_variable_name_spec.rb +49 -49
- data/spec/reek/smells/unused_parameters_spec.rb +26 -16
- data/spec/reek/smells/utility_function_spec.rb +20 -20
- data/spec/reek/source/code_comment_spec.rb +37 -37
- data/spec/reek/source/object_source_spec.rb +5 -5
- data/spec/reek/source/reference_collector_spec.rb +9 -9
- data/spec/reek/source/sexp_extensions_spec.rb +73 -52
- data/spec/reek/source/sexp_formatter_spec.rb +3 -4
- data/spec/reek/source/sexp_node_spec.rb +3 -3
- data/spec/reek/source/source_code_spec.rb +16 -15
- data/spec/reek/source/tree_dresser_spec.rb +2 -2
- data/spec/reek/spec/should_reek_of_spec.rb +11 -11
- data/spec/reek/spec/should_reek_only_of_spec.rb +11 -11
- data/spec/reek/spec/should_reek_spec.rb +11 -11
- data/spec/samples/one_smelly_file/dirty.rb +3 -0
- data/spec/spec_helper.rb +0 -6
- data/tasks/develop.rake +8 -16
- data/tasks/reek.rake +5 -13
- data/tasks/test.rake +5 -22
- metadata +56 -34
- data/lib/reek/cli/command_line.rb +0 -126
- data/lib/reek/cli/report.rb +0 -138
@@ -2,7 +2,7 @@ shared_examples_for 'a variable detector' do
|
|
2
2
|
context 'with no variables' do
|
3
3
|
it "doesn't record a smell" do
|
4
4
|
@detector.examine_context(@ctx)
|
5
|
-
@detector.smells_found.length.
|
5
|
+
expect(@detector.smells_found.length).to eq(0)
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
@@ -14,10 +14,10 @@ shared_examples_for 'a variable detector' do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'records only one smell' do
|
17
|
-
@detector.smells_found.length.
|
17
|
+
expect(@detector.smells_found.length).to eq(1)
|
18
18
|
end
|
19
19
|
it 'mentions the variable name in the report' do
|
20
|
-
@detector.
|
20
|
+
expect(@detector).to have_smell([/something/])
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -29,11 +29,11 @@ shared_examples_for 'a variable detector' do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'records both smells' do
|
32
|
-
@detector.num_smells.
|
32
|
+
expect(@detector.num_smells).to eq(2)
|
33
33
|
end
|
34
34
|
it 'mentions both variable names in the report' do
|
35
|
-
@detector.
|
36
|
-
@detector.
|
35
|
+
expect(@detector).to have_smell([/something/])
|
36
|
+
expect(@detector).to have_smell([/something_else/])
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
@@ -9,34 +9,34 @@ describe BooleanParameter do
|
|
9
9
|
context 'in a method' do
|
10
10
|
it 'reports a parameter defaulted to true' do
|
11
11
|
src = 'def cc(arga = true) end'
|
12
|
-
src.
|
12
|
+
expect(src).to smell_of(BooleanParameter, BooleanParameter::PARAMETER_KEY => 'arga')
|
13
13
|
end
|
14
14
|
it 'reports a parameter defaulted to false' do
|
15
15
|
src = 'def cc(arga = false) end'
|
16
|
-
src.
|
16
|
+
expect(src).to smell_of(BooleanParameter, BooleanParameter::PARAMETER_KEY => 'arga')
|
17
17
|
end
|
18
18
|
it 'reports two parameters defaulted to booleans' do
|
19
19
|
src = 'def cc(nowt, arga = true, argb = false, &blk) end'
|
20
|
-
src.
|
21
|
-
|
22
|
-
|
20
|
+
expect(src).to smell_of(BooleanParameter,
|
21
|
+
{ BooleanParameter::PARAMETER_KEY => 'arga' },
|
22
|
+
BooleanParameter::PARAMETER_KEY => 'argb')
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
context 'in a singleton method' do
|
27
27
|
it 'reports a parameter defaulted to true' do
|
28
28
|
src = 'def self.cc(arga = true) end'
|
29
|
-
src.
|
29
|
+
expect(src).to smell_of(BooleanParameter, BooleanParameter::PARAMETER_KEY => 'arga')
|
30
30
|
end
|
31
31
|
it 'reports a parameter defaulted to false' do
|
32
32
|
src = 'def fred.cc(arga = false) end'
|
33
|
-
src.
|
33
|
+
expect(src).to smell_of(BooleanParameter, BooleanParameter::PARAMETER_KEY => 'arga')
|
34
34
|
end
|
35
35
|
it 'reports two parameters defaulted to booleans' do
|
36
36
|
src = 'def Module.cc(nowt, arga = true, argb = false, &blk) end'
|
37
|
-
src.
|
38
|
-
|
39
|
-
|
37
|
+
expect(src).to smell_of(BooleanParameter,
|
38
|
+
{ BooleanParameter::PARAMETER_KEY => 'arga' },
|
39
|
+
BooleanParameter::PARAMETER_KEY => 'argb')
|
40
40
|
end
|
41
41
|
end
|
42
42
|
end
|
@@ -54,13 +54,13 @@ describe BooleanParameter do
|
|
54
54
|
ctx = MethodContext.new(nil, src.to_reek_source.syntax_tree)
|
55
55
|
@detector.examine(ctx)
|
56
56
|
smells = @detector.smells_found.to_a
|
57
|
-
smells.length.
|
58
|
-
smells[0].smell_class.
|
59
|
-
smells[0].smell[BooleanParameter::PARAMETER_KEY].
|
60
|
-
smells[0].source.
|
61
|
-
smells[0].smell_class.
|
62
|
-
smells[0].subclass.
|
63
|
-
smells[0].lines.
|
57
|
+
expect(smells.length).to eq(1)
|
58
|
+
expect(smells[0].smell_class).to eq(BooleanParameter::SMELL_CLASS)
|
59
|
+
expect(smells[0].smell[BooleanParameter::PARAMETER_KEY]).to eq('arga')
|
60
|
+
expect(smells[0].source).to eq(@source_name)
|
61
|
+
expect(smells[0].smell_class).to eq(BooleanParameter::SMELL_CLASS)
|
62
|
+
expect(smells[0].subclass).to eq(BooleanParameter::SMELL_SUBCLASS)
|
63
|
+
expect(smells[0].lines).to eq([1])
|
64
64
|
end
|
65
65
|
end
|
66
66
|
end
|
@@ -18,11 +18,11 @@ describe ClassVariable do
|
|
18
18
|
context 'with no class variables' do
|
19
19
|
it 'records nothing in the class' do
|
20
20
|
exp = ast(:class, :Fred)
|
21
|
-
@detector.examine_context(CodeContext.new(nil, exp)).
|
21
|
+
expect(@detector.examine_context(CodeContext.new(nil, exp))).to be_empty
|
22
22
|
end
|
23
23
|
it 'records nothing in the module' do
|
24
24
|
exp = ast(:module, :Fred)
|
25
|
-
@detector.examine_context(CodeContext.new(nil, exp)).
|
25
|
+
expect(@detector.examine_context(CodeContext.new(nil, exp))).to be_empty
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -33,10 +33,10 @@ describe ClassVariable do
|
|
33
33
|
@smells = @detector.examine_context(CodeContext.new(nil, ast))
|
34
34
|
end
|
35
35
|
it 'records only that class variable' do
|
36
|
-
@smells.length.
|
36
|
+
expect(@smells.length).to eq(1)
|
37
37
|
end
|
38
38
|
it 'records the variable name' do
|
39
|
-
@smells[0].smell[ClassVariable::VARIABLE_KEY].
|
39
|
+
expect(@smells[0].smell[ClassVariable::VARIABLE_KEY]).to eq(@class_variable)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -91,10 +91,10 @@ end
|
|
91
91
|
EOS
|
92
92
|
ctx = CodeContext.new(nil, src.to_reek_source.syntax_tree)
|
93
93
|
@warning = @detector.examine_context(ctx)[0]
|
94
|
-
@warning.source.
|
95
|
-
@warning.smell_class.
|
96
|
-
@warning.subclass.
|
97
|
-
@warning.smell[ClassVariable::VARIABLE_KEY].
|
98
|
-
@warning.lines.
|
94
|
+
expect(@warning.source).to eq(@source_name)
|
95
|
+
expect(@warning.smell_class).to eq(ClassVariable::SMELL_CLASS)
|
96
|
+
expect(@warning.subclass).to eq(ClassVariable::SMELL_SUBCLASS)
|
97
|
+
expect(@warning.smell[ClassVariable::VARIABLE_KEY]).to eq(@class_variable)
|
98
|
+
expect(@warning.lines).to eq([2])
|
99
99
|
end
|
100
100
|
end
|
@@ -12,233 +12,257 @@ describe ControlParameter do
|
|
12
12
|
|
13
13
|
it_should_behave_like 'SmellDetector'
|
14
14
|
|
15
|
-
it 'should not report a ternary check on an ivar' do
|
16
|
-
src = 'def simple(arga) @ivar ? arga : 3 end'
|
17
|
-
src.should_not smell_of(ControlParameter)
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'should not report a ternary check on a lvar' do
|
21
|
-
src = 'def simple(arga) lvar = 27; lvar ? arga : @ivar end'
|
22
|
-
src.should_not smell_of(ControlParameter)
|
23
|
-
end
|
24
15
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
16
|
+
context 'parameter not used to determine code path' do
|
17
|
+
it 'does not report a ternary check on an ivar' do
|
18
|
+
src = 'def simple(arga) @ivar ? arga : 3 end'
|
19
|
+
expect(src).not_to smell_of(ControlParameter)
|
20
|
+
end
|
29
21
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
22
|
+
it 'does not report a ternary check on a lvar' do
|
23
|
+
src = 'def simple(arga) lvar = 27; lvar ? arga : @ivar end'
|
24
|
+
expect(src).not_to smell_of(ControlParameter)
|
25
|
+
end
|
34
26
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
puts arg
|
40
|
-
end
|
41
|
-
if arg
|
42
|
-
puts 'a'
|
43
|
-
end
|
44
|
-
end
|
45
|
-
EOS
|
46
|
-
src.should_not smell_of(ControlParameter)
|
47
|
-
end
|
27
|
+
it 'does not report when parameter is unused' do
|
28
|
+
src = 'def simple(arg) test = 1 end'
|
29
|
+
expect(src).not_to smell_of(ControlParameter)
|
30
|
+
end
|
48
31
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
puts 'a'
|
54
|
-
end
|
55
|
-
if arg
|
56
|
-
puts arg
|
57
|
-
end
|
58
|
-
end
|
59
|
-
EOS
|
60
|
-
src.should_not smell_of(ControlParameter)
|
32
|
+
it 'does not report when parameter is used inside conditional' do
|
33
|
+
src = 'def simple(arg) if true then puts arg end end'
|
34
|
+
expect(src).not_to smell_of(ControlParameter)
|
35
|
+
end
|
61
36
|
end
|
62
37
|
|
63
|
-
it 'should not report on complex non smelly method' do
|
64
|
-
src = <<EOS
|
65
|
-
def self.guess(arg)
|
66
|
-
case arg
|
67
|
-
when ""
|
68
|
-
t = self
|
69
|
-
when "a"
|
70
|
-
t = Switch::OptionalArgument
|
71
|
-
when "b"
|
72
|
-
t = Switch::PlacedArgument
|
73
|
-
else
|
74
|
-
t = Switch::RequiredArgument
|
75
|
-
end
|
76
|
-
self >= t or incompatible_argument_styles(arg, t)
|
77
|
-
t
|
78
|
-
end
|
79
|
-
EOS
|
80
|
-
src.should_not smell_of(ControlParameter)
|
81
|
-
end
|
82
38
|
|
83
39
|
context 'parameter only used to determine code path' do
|
84
|
-
it '
|
40
|
+
it 'reports a ternary check on a parameter' do
|
85
41
|
src = 'def simple(arga) arga ? @ivar : 3 end'
|
86
|
-
src.
|
42
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arga')
|
87
43
|
end
|
88
44
|
|
89
|
-
it '
|
45
|
+
it 'reports a couple inside a block' do
|
90
46
|
src = 'def blocks(arg) @text.map { |blk| arg ? blk : "#{blk}" } end'
|
91
|
-
src.
|
47
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
92
48
|
end
|
93
49
|
|
94
|
-
it '
|
50
|
+
it 'reports on an if statement modifier' do
|
95
51
|
src = 'def simple(arg) args = {}; args.merge(\'a\' => \'A\') if arg end'
|
96
|
-
src.
|
52
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
97
53
|
end
|
98
54
|
|
99
|
-
it '
|
55
|
+
it 'reports on an unless statement modifier' do
|
100
56
|
src = 'def simple(arg) args = {}; args.merge(\'a\' => \'A\') unless arg end'
|
101
|
-
src.
|
57
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
102
58
|
end
|
103
59
|
|
104
|
-
it '
|
60
|
+
it 'reports on if control expression' do
|
105
61
|
src = 'def simple(arg) args = {}; if arg then args.merge(\'a\' => \'A\') end end'
|
106
|
-
src.
|
62
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
107
63
|
end
|
108
64
|
|
109
|
-
it '
|
65
|
+
it 'reports on if control expression with &&' do
|
110
66
|
src = 'def simple(arg) if arg && true then puts "arg" end end'
|
111
|
-
src.
|
67
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
112
68
|
end
|
113
69
|
|
114
|
-
it '
|
70
|
+
it 'reports on if control expression with preceding &&' do
|
115
71
|
src = 'def simple(arg) if true && arg then puts "arg" end end'
|
116
|
-
src.
|
72
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
117
73
|
end
|
118
74
|
|
119
|
-
it '
|
75
|
+
it 'reports on if control expression with two && conditions' do
|
120
76
|
src = 'def simple(a) ag = {}; if a && true && true then puts "2" end end'
|
121
|
-
src.
|
77
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'a')
|
122
78
|
end
|
123
79
|
|
124
|
-
it '
|
80
|
+
it 'reports on if control expression with ||' do
|
125
81
|
src = 'def simple(arg) args = {}; if arg || true then puts "arg" end end'
|
126
|
-
src.
|
82
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
127
83
|
end
|
128
84
|
|
129
|
-
it '
|
85
|
+
it 'reports on if control expression with or' do
|
130
86
|
src = 'def simple(arg) args = {}; if arg or true then puts "arg" end end'
|
131
|
-
src.
|
87
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
132
88
|
end
|
133
89
|
|
134
|
-
it '
|
90
|
+
it 'reports on if control expression with if' do
|
91
|
+
src = 'def simple(arg) args = {}; if (arg if true) then puts "arg" end end'
|
92
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'reports on && notation' do
|
135
96
|
src = 'def simple(arg) args = {}; arg && args.merge(\'a\' => \'A\') end'
|
136
|
-
src.
|
97
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
137
98
|
end
|
138
99
|
|
139
|
-
it '
|
100
|
+
it 'reports on || notation' do
|
140
101
|
src = 'def simple(arg) args = {}; arg || args.merge(\'a\' => \'A\') end'
|
141
|
-
src.
|
102
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
142
103
|
end
|
143
104
|
|
144
|
-
it '
|
105
|
+
it 'reports on case statement' do
|
145
106
|
src = 'def simple(arg) case arg when nil; nil when false; nil else nil end end'
|
146
|
-
src.
|
107
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
147
108
|
end
|
148
109
|
|
149
|
-
it '
|
150
|
-
src =
|
151
|
-
|
110
|
+
it 'reports on nested if statements that are both control parameters' do
|
111
|
+
src = <<-EOS
|
112
|
+
def nested(arg)
|
113
|
+
if arg
|
114
|
+
puts 'a'
|
115
|
+
puts 'b' if arg
|
116
|
+
end
|
117
|
+
end
|
118
|
+
EOS
|
119
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
152
120
|
end
|
153
121
|
|
154
|
-
it '
|
155
|
-
src =
|
156
|
-
def nested(arg)
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
end
|
162
|
-
EOS
|
163
|
-
src.
|
122
|
+
it 'reports on nested if statements where the inner if is a control parameter' do
|
123
|
+
src = <<-EOS
|
124
|
+
def nested(arg)
|
125
|
+
if true
|
126
|
+
puts 'a'
|
127
|
+
puts 'b' if arg
|
128
|
+
end
|
129
|
+
end
|
130
|
+
EOS
|
131
|
+
expect(src).to smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
164
132
|
end
|
165
133
|
|
166
|
-
it '
|
167
|
-
src =
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
EOS
|
175
|
-
src.should smell_of(ControlParameter, ControlParameter::PARAMETER_KEY => 'arg')
|
134
|
+
it 'reports on explicit comparison in the condition' do
|
135
|
+
src = 'def simple(arg) if arg == :foo then :foo else :bar end end'
|
136
|
+
expect(src).to smell_of(ControlParameter)
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'reports on explicit negative comparison in the condition' do
|
140
|
+
src = 'def simple(arg) if arg != :foo then :bar else :foo end end'
|
141
|
+
expect(src).to smell_of(ControlParameter)
|
176
142
|
end
|
177
143
|
end
|
178
144
|
|
179
145
|
context 'parameter used besides determining code path' do
|
180
|
-
it '
|
146
|
+
it 'does not report on if conditional expression' do
|
181
147
|
src = 'def simple(arg) if arg then use(arg) else use(@other) end end'
|
182
|
-
src.
|
148
|
+
expect(src).not_to smell_of(ControlParameter)
|
183
149
|
end
|
184
150
|
|
185
|
-
it '
|
151
|
+
it 'does not report on an if statement modifier' do
|
186
152
|
src = 'def simple(arg) args = {}; args.merge(\'a\' => arg) if arg end'
|
187
|
-
src.
|
153
|
+
expect(src).not_to smell_of(ControlParameter)
|
188
154
|
end
|
189
155
|
|
190
|
-
it '
|
156
|
+
it 'does not report on an unless statement modifier' do
|
191
157
|
src = 'def simple(arg) args = {}; args.merge(\'a\' => arg) unless arg end'
|
192
|
-
src.
|
158
|
+
expect(src).not_to smell_of(ControlParameter)
|
193
159
|
end
|
194
160
|
|
195
|
-
it '
|
161
|
+
it 'does not report on if control expression' do
|
196
162
|
src = 'def simple(arg) args = {}; if arg then args.merge(\'a\' => arg) end end'
|
197
|
-
src.
|
163
|
+
expect(src).not_to smell_of(ControlParameter)
|
198
164
|
end
|
199
165
|
|
200
|
-
it '
|
166
|
+
it 'does not report on if control expression with &&' do
|
201
167
|
src = 'def simple(arg) if arg && true then puts arg end end'
|
202
|
-
src.
|
168
|
+
expect(src).not_to smell_of(ControlParameter)
|
203
169
|
end
|
204
170
|
|
205
|
-
it '
|
171
|
+
it 'does not report on && notation' do
|
206
172
|
src = 'def simple(arg) args = {}; arg && args.merge(\'a\' => arg) end'
|
207
|
-
src.
|
173
|
+
expect(src).not_to smell_of(ControlParameter)
|
208
174
|
end
|
209
175
|
|
210
|
-
it '
|
176
|
+
it 'does not report on || notation' do
|
211
177
|
src = 'def simple(arg) args = {}; arg || args.merge(\'a\' => arg) end'
|
212
|
-
src.
|
178
|
+
expect(src).not_to smell_of(ControlParameter)
|
213
179
|
end
|
214
180
|
|
215
|
-
it '
|
181
|
+
it 'does not report when parameter is used outside conditional' do
|
216
182
|
src = 'def simple(arg) puts arg; if arg then @a = 1 end end'
|
217
|
-
src.
|
183
|
+
expect(src).not_to smell_of(ControlParameter)
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'does not report when parameter is used as a method call argument in a condition' do
|
187
|
+
src = 'def simple(arg) if foo(arg) then @a = 1 end end'
|
188
|
+
expect(src).not_to smell_of(ControlParameter)
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'does not report when parameter is used as a method call receiver in a condition' do
|
192
|
+
src = 'def simple(arg) if arg.foo? then @a = 1 end end'
|
193
|
+
expect(src).not_to smell_of(ControlParameter)
|
194
|
+
end
|
195
|
+
|
196
|
+
it 'does not report when the argument is a hash on which we access a key' do
|
197
|
+
src = 'def simple(arg) if arg[\'a\'] then puts \'a\' else puts \'b\' end end'
|
198
|
+
expect(src).not_to smell_of ControlParameter
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'does not report when used in first conditional but not second' do
|
202
|
+
src = <<-EOS
|
203
|
+
def things(arg)
|
204
|
+
if arg
|
205
|
+
puts arg
|
206
|
+
end
|
207
|
+
if arg
|
208
|
+
puts 'a'
|
209
|
+
end
|
210
|
+
end
|
211
|
+
EOS
|
212
|
+
expect(src).not_to smell_of(ControlParameter)
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'does not report when used in second conditional but not first' do
|
216
|
+
src = <<-EOS
|
217
|
+
def things(arg)
|
218
|
+
if arg
|
219
|
+
puts 'a'
|
220
|
+
end
|
221
|
+
if arg
|
222
|
+
puts arg
|
223
|
+
end
|
224
|
+
end
|
225
|
+
EOS
|
226
|
+
expect(src).not_to smell_of(ControlParameter)
|
227
|
+
end
|
228
|
+
|
229
|
+
it 'does not report when used in body of control flow operator' do
|
230
|
+
src = <<-EOS
|
231
|
+
def foo(arg)
|
232
|
+
case arg
|
233
|
+
when :bar
|
234
|
+
puts 'a'
|
235
|
+
else
|
236
|
+
puts 'b'
|
237
|
+
end
|
238
|
+
qux or quuz(arg)
|
239
|
+
end
|
240
|
+
EOS
|
241
|
+
expect(src).not_to smell_of(ControlParameter)
|
218
242
|
end
|
219
243
|
end
|
220
244
|
|
221
245
|
context 'when a smell is reported' do
|
222
246
|
before :each do
|
223
|
-
src =
|
224
|
-
def things(arg)
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
end
|
230
|
-
EOS
|
247
|
+
src = <<-EOS
|
248
|
+
def things(arg)
|
249
|
+
@text.map do |blk|
|
250
|
+
arg ? blk : "blk"
|
251
|
+
end
|
252
|
+
puts "hello" if arg
|
253
|
+
end
|
254
|
+
EOS
|
231
255
|
ctx = MethodContext.new(nil, src.to_reek_source.syntax_tree)
|
232
256
|
smells = @detector.examine(ctx)
|
233
|
-
smells.length.
|
257
|
+
expect(smells.length).to eq(1)
|
234
258
|
@warning = smells[0]
|
235
259
|
end
|
236
260
|
|
237
261
|
it_should_behave_like 'common fields set correctly'
|
238
262
|
|
239
263
|
it 'has the correct fields' do
|
240
|
-
@warning.smell[ControlParameter::PARAMETER_KEY].
|
241
|
-
@warning.lines.
|
264
|
+
expect(@warning.smell[ControlParameter::PARAMETER_KEY]).to eq('arg')
|
265
|
+
expect(@warning.lines).to eq([3, 5])
|
242
266
|
end
|
243
267
|
end
|
244
268
|
end
|