reek 1.2.6 → 1.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. data/.yardopts +10 -0
  2. data/History.txt +20 -0
  3. data/README.md +90 -0
  4. data/bin/reek +2 -2
  5. data/config/defaults.reek +34 -4
  6. data/features/masking_smells.feature +35 -15
  7. data/features/options.feature +2 -0
  8. data/features/rake_task.feature +11 -18
  9. data/features/reports.feature +13 -15
  10. data/features/samples.feature +90 -105
  11. data/features/stdin.feature +3 -6
  12. data/features/step_definitions/reek_steps.rb +8 -4
  13. data/features/support/env.rb +2 -3
  14. data/features/yaml.feature +124 -0
  15. data/lib/reek.rb +8 -4
  16. data/lib/reek/cli/application.rb +46 -0
  17. data/lib/reek/cli/command_line.rb +106 -0
  18. data/lib/reek/cli/help_command.rb +18 -0
  19. data/lib/reek/cli/reek_command.rb +37 -0
  20. data/lib/reek/cli/report.rb +91 -0
  21. data/lib/reek/cli/version_command.rb +19 -0
  22. data/lib/reek/cli/yaml_command.rb +32 -0
  23. data/lib/reek/core/block_context.rb +18 -0
  24. data/lib/reek/core/class_context.rb +23 -0
  25. data/lib/reek/core/code_context.rb +72 -0
  26. data/lib/reek/core/code_parser.rb +192 -0
  27. data/lib/reek/core/detector_stack.rb +29 -0
  28. data/lib/reek/core/masking_collection.rb +46 -0
  29. data/lib/reek/core/method_context.rb +132 -0
  30. data/lib/reek/core/module_context.rb +64 -0
  31. data/lib/reek/{object_refs.rb → core/object_refs.rb} +8 -6
  32. data/lib/reek/{singleton_method_context.rb → core/singleton_method_context.rb} +10 -5
  33. data/lib/reek/core/smell_configuration.rb +66 -0
  34. data/lib/reek/core/sniffer.rb +110 -0
  35. data/lib/reek/core/stop_context.rb +26 -0
  36. data/lib/reek/examiner.rb +88 -0
  37. data/lib/reek/rake/task.rb +124 -0
  38. data/lib/reek/smell_warning.rb +69 -13
  39. data/lib/reek/smells.rb +29 -0
  40. data/lib/reek/smells/attribute.rb +13 -14
  41. data/lib/reek/smells/boolean_parameter.rb +33 -0
  42. data/lib/reek/smells/class_variable.rb +8 -6
  43. data/lib/reek/smells/control_couple.rb +33 -17
  44. data/lib/reek/smells/data_clump.rb +10 -6
  45. data/lib/reek/smells/duplication.rb +24 -14
  46. data/lib/reek/smells/feature_envy.rb +11 -6
  47. data/lib/reek/smells/irresponsible_module.rb +28 -0
  48. data/lib/reek/smells/large_class.rb +9 -7
  49. data/lib/reek/smells/long_method.rb +6 -5
  50. data/lib/reek/smells/long_parameter_list.rb +11 -9
  51. data/lib/reek/smells/long_yield_list.rb +37 -7
  52. data/lib/reek/smells/nested_iterators.rb +34 -9
  53. data/lib/reek/smells/simulated_polymorphism.rb +15 -11
  54. data/lib/reek/smells/smell_detector.rb +24 -12
  55. data/lib/reek/smells/uncommunicative_method_name.rb +76 -0
  56. data/lib/reek/smells/uncommunicative_module_name.rb +76 -0
  57. data/lib/reek/smells/{uncommunicative_name.rb → uncommunicative_parameter_name.rb} +14 -26
  58. data/lib/reek/smells/uncommunicative_variable_name.rb +90 -0
  59. data/lib/reek/smells/utility_function.rb +33 -9
  60. data/lib/reek/source.rb +18 -0
  61. data/lib/reek/source/code_comment.rb +19 -0
  62. data/lib/reek/source/config_file.rb +72 -0
  63. data/lib/reek/source/core_extras.rb +46 -0
  64. data/lib/reek/source/sexp_formatter.rb +16 -0
  65. data/lib/reek/source/source_code.rb +44 -0
  66. data/lib/reek/source/source_file.rb +32 -0
  67. data/lib/reek/source/source_locator.rb +36 -0
  68. data/lib/reek/source/tree_dresser.rb +128 -0
  69. data/lib/reek/spec.rb +51 -0
  70. data/lib/reek/spec/should_reek.rb +34 -0
  71. data/lib/reek/spec/should_reek_of.rb +37 -0
  72. data/lib/reek/spec/should_reek_only_of.rb +36 -0
  73. data/reek.gemspec +5 -5
  74. data/spec/reek/{help_command_spec.rb → cli/help_command_spec.rb} +3 -4
  75. data/spec/reek/{reek_command_spec.rb → cli/reek_command_spec.rb} +8 -7
  76. data/spec/reek/cli/report_spec.rb +26 -0
  77. data/spec/reek/{version_command_spec.rb → cli/version_command_spec.rb} +3 -3
  78. data/spec/reek/cli/yaml_command_spec.rb +47 -0
  79. data/spec/reek/core/block_context_spec.rb +26 -0
  80. data/spec/reek/core/class_context_spec.rb +53 -0
  81. data/spec/reek/{code_context_spec.rb → core/code_context_spec.rb} +15 -37
  82. data/spec/reek/{code_parser_spec.rb → core/code_parser_spec.rb} +5 -5
  83. data/spec/reek/{config_spec.rb → core/config_spec.rb} +2 -6
  84. data/spec/reek/{masking_collection_spec.rb → core/masking_collection_spec.rb} +3 -4
  85. data/spec/reek/{method_context_spec.rb → core/method_context_spec.rb} +6 -7
  86. data/spec/reek/core/module_context_spec.rb +42 -0
  87. data/spec/reek/{object_refs_spec.rb → core/object_refs_spec.rb} +5 -6
  88. data/spec/reek/core/singleton_method_context_spec.rb +15 -0
  89. data/spec/reek/core/smell_configuration_spec.rb +11 -0
  90. data/spec/reek/core/stop_context_spec.rb +17 -0
  91. data/spec/reek/examiner_spec.rb +42 -0
  92. data/spec/reek/smell_warning_spec.rb +82 -33
  93. data/spec/reek/smells/attribute_spec.rb +33 -7
  94. data/spec/reek/smells/boolean_parameter_spec.rb +76 -0
  95. data/spec/reek/smells/class_variable_spec.rb +15 -6
  96. data/spec/reek/smells/control_couple_spec.rb +40 -29
  97. data/spec/reek/smells/data_clump_spec.rb +28 -7
  98. data/spec/reek/smells/duplication_spec.rb +47 -41
  99. data/spec/reek/smells/feature_envy_spec.rb +76 -18
  100. data/spec/reek/smells/irresponsible_module_spec.rb +37 -0
  101. data/spec/reek/smells/large_class_spec.rb +91 -56
  102. data/spec/reek/smells/long_method_spec.rb +32 -7
  103. data/spec/reek/smells/long_parameter_list_spec.rb +42 -13
  104. data/spec/reek/smells/long_yield_list_spec.rb +65 -0
  105. data/spec/reek/smells/nested_iterators_spec.rb +94 -3
  106. data/spec/reek/smells/simulated_polymorphism_spec.rb +48 -20
  107. data/spec/reek/smells/smell_detector_shared.rb +28 -0
  108. data/spec/reek/smells/uncommunicative_method_name_spec.rb +57 -0
  109. data/spec/reek/smells/uncommunicative_module_name_spec.rb +67 -0
  110. data/spec/reek/smells/uncommunicative_parameter_name_spec.rb +61 -0
  111. data/spec/reek/smells/uncommunicative_variable_name_spec.rb +124 -0
  112. data/spec/reek/smells/utility_function_spec.rb +45 -3
  113. data/spec/reek/source/code_comment_spec.rb +24 -0
  114. data/spec/reek/source/object_source_spec.rb +20 -0
  115. data/spec/reek/{adapters/source_spec.rb → source/source_code_spec.rb} +7 -8
  116. data/spec/reek/source/tree_dresser_spec.rb +165 -0
  117. data/spec/reek/spec/should_reek_of_spec.rb +76 -0
  118. data/spec/reek/spec/should_reek_only_of_spec.rb +89 -0
  119. data/spec/reek/{adapters → spec}/should_reek_spec.rb +8 -32
  120. data/spec/samples/all_but_one_masked/clean_one.rb +1 -0
  121. data/spec/samples/all_but_one_masked/dirty.rb +1 -0
  122. data/spec/samples/all_but_one_masked/masked.reek +5 -1
  123. data/spec/samples/clean_due_to_masking/clean_one.rb +1 -0
  124. data/spec/samples/clean_due_to_masking/clean_three.rb +1 -0
  125. data/spec/samples/clean_due_to_masking/clean_two.rb +1 -0
  126. data/spec/samples/clean_due_to_masking/dirty_one.rb +1 -1
  127. data/spec/samples/clean_due_to_masking/dirty_two.rb +1 -1
  128. data/spec/samples/clean_due_to_masking/masked.reek +5 -1
  129. data/spec/samples/corrupt_config_file/dirty.rb +1 -1
  130. data/spec/samples/empty_config_file/dirty.rb +2 -1
  131. data/spec/samples/exceptions.reek +1 -1
  132. data/spec/samples/masked/dirty.rb +2 -1
  133. data/spec/samples/masked/masked.reek +3 -1
  134. data/spec/samples/mixed_results/clean_one.rb +1 -0
  135. data/spec/samples/mixed_results/clean_three.rb +1 -0
  136. data/spec/samples/mixed_results/clean_two.rb +1 -0
  137. data/spec/samples/mixed_results/dirty_one.rb +1 -0
  138. data/spec/samples/mixed_results/dirty_two.rb +1 -0
  139. data/spec/samples/not_quite_masked/dirty.rb +2 -1
  140. data/spec/samples/not_quite_masked/masked.reek +1 -1
  141. data/spec/samples/overrides/masked/dirty.rb +2 -1
  142. data/spec/samples/overrides/masked/lower.reek +3 -1
  143. data/spec/samples/three_clean_files/clean_one.rb +1 -0
  144. data/spec/samples/three_clean_files/clean_three.rb +1 -0
  145. data/spec/samples/three_clean_files/clean_two.rb +1 -0
  146. data/spec/samples/two_smelly_files/dirty_one.rb +2 -1
  147. data/spec/samples/two_smelly_files/dirty_two.rb +2 -1
  148. data/spec/spec_helper.rb +1 -2
  149. data/tasks/reek.rake +2 -2
  150. data/tasks/test.rake +12 -3
  151. metadata +81 -62
  152. data/README.rdoc +0 -84
  153. data/lib/reek/adapters/application.rb +0 -46
  154. data/lib/reek/adapters/command_line.rb +0 -77
  155. data/lib/reek/adapters/config_file.rb +0 -31
  156. data/lib/reek/adapters/core_extras.rb +0 -64
  157. data/lib/reek/adapters/rake_task.rb +0 -121
  158. data/lib/reek/adapters/report.rb +0 -86
  159. data/lib/reek/adapters/source.rb +0 -72
  160. data/lib/reek/adapters/spec.rb +0 -133
  161. data/lib/reek/block_context.rb +0 -62
  162. data/lib/reek/class_context.rb +0 -41
  163. data/lib/reek/code_context.rb +0 -68
  164. data/lib/reek/code_parser.rb +0 -203
  165. data/lib/reek/configuration.rb +0 -57
  166. data/lib/reek/detector_stack.rb +0 -37
  167. data/lib/reek/help_command.rb +0 -14
  168. data/lib/reek/if_context.rb +0 -18
  169. data/lib/reek/masking_collection.rb +0 -33
  170. data/lib/reek/method_context.rb +0 -138
  171. data/lib/reek/module_context.rb +0 -49
  172. data/lib/reek/name.rb +0 -57
  173. data/lib/reek/reek_command.rb +0 -28
  174. data/lib/reek/sexp_formatter.rb +0 -10
  175. data/lib/reek/sniffer.rb +0 -177
  176. data/lib/reek/stop_context.rb +0 -35
  177. data/lib/reek/tree_dresser.rb +0 -82
  178. data/lib/reek/version_command.rb +0 -14
  179. data/lib/reek/yield_call_context.rb +0 -12
  180. data/spec/reek/adapters/report_spec.rb +0 -31
  181. data/spec/reek/adapters/should_reek_of_spec.rb +0 -138
  182. data/spec/reek/adapters/should_reek_only_of_spec.rb +0 -87
  183. data/spec/reek/block_context_spec.rb +0 -65
  184. data/spec/reek/class_context_spec.rb +0 -161
  185. data/spec/reek/configuration_spec.rb +0 -12
  186. data/spec/reek/if_context_spec.rb +0 -17
  187. data/spec/reek/module_context_spec.rb +0 -46
  188. data/spec/reek/name_spec.rb +0 -37
  189. data/spec/reek/object_source_spec.rb +0 -23
  190. data/spec/reek/singleton_method_context_spec.rb +0 -16
  191. data/spec/reek/smells/smell_detector_spec.rb +0 -36
  192. data/spec/reek/smells/uncommunicative_name_spec.rb +0 -146
  193. data/spec/reek/sniffer_spec.rb +0 -11
  194. data/spec/reek/stop_context_spec.rb +0 -33
  195. data/spec/reek/tree_dresser_spec.rb +0 -20
@@ -1,16 +1,16 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
-
3
- require 'reek/code_parser'
4
- require 'reek/sniffer'
5
- require 'reek/smells/long_method'
1
+ require File.join(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))), 'spec_helper')
2
+ require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'smells', 'long_method')
3
+ require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'core', 'code_parser')
4
+ require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'core', 'sniffer')
5
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'smell_detector_shared')
6
6
 
7
7
  include Reek
8
8
  include Reek::Smells
9
9
 
10
10
  def process_method(src)
11
11
  source = src.to_reek_source
12
- sniffer = Sniffer.new(source)
13
- CodeParser.new(sniffer).process_defn(source.syntax_tree)
12
+ sniffer = Core::Sniffer.new(source)
13
+ Core::CodeParser.new(sniffer).process_defn(source.syntax_tree)
14
14
  end
15
15
 
16
16
  describe LongMethod do
@@ -194,3 +194,28 @@ EOS
194
194
  method.num_statements.should == 6
195
195
  end
196
196
  end
197
+
198
+ describe LongMethod do
199
+ before(:each) do
200
+ @detector = LongMethod.new('silver')
201
+ end
202
+
203
+ it_should_behave_like 'SmellDetector'
204
+
205
+ context 'when the method has 30 statements' do
206
+ before :each do
207
+ @num_statements = 30
208
+ @ctx = mock('method_context', :null_object => true)
209
+ @ctx.should_receive(:num_statements).and_return(@num_statements)
210
+ @detector.examine_context(@ctx)
211
+ @yaml = @detector.smells_found.to_a[0].to_yaml # SMELL: too cumbersome!
212
+ end
213
+ it 'reports the number of statements' do
214
+ @yaml.should match(/statement_count:[\s]*#{@num_statements}/)
215
+ # SMELL: many tests duplicate the names of the YAML fields
216
+ end
217
+ it 'reports the correct subclass' do
218
+ @yaml.should match(/subclass:[\s]*#{LongMethod::SUBCLASS_TOO_MANY_STATEMENTS}/)
219
+ end
220
+ end
221
+ end
@@ -1,6 +1,6 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
-
3
- require 'reek/smells/long_parameter_list'
1
+ require File.join(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))), 'spec_helper')
2
+ require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'smells', 'long_parameter_list')
3
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'smell_detector_shared')
4
4
 
5
5
  include Reek
6
6
  include Reek::Smells
@@ -54,19 +54,48 @@ describe LongParameterList do
54
54
  end
55
55
  end
56
56
  end
57
-
58
- describe 'yield' do
59
- it 'should not report yield with no parameters' do
60
- 'def simple(arga, argb, &blk) f(3);yield; end'.should_not reek
57
+ end
58
+
59
+ describe LongParameterList do
60
+ before(:each) do
61
+ @source_name = 'smokin'
62
+ @detector = LongParameterList.new(@source_name, {})
63
+ # SMELL: can't use the default config, because that contains an override,
64
+ # which causes the mocked matches?() method to be called twice!!
65
+ end
66
+
67
+ it_should_behave_like 'SmellDetector'
68
+
69
+ context 'looking at the YAML' do
70
+ before :each do
71
+ src = <<EOS
72
+ def badguy(arga, argb, argc, argd)
73
+ f(3)
74
+ true
75
+ end
76
+ EOS
77
+ source = src.to_reek_source
78
+ sniffer = Sniffer.new(source)
79
+ @mctx = CodeParser.new(sniffer).process_defn(source.syntax_tree)
80
+ @detector.examine_context(@mctx)
81
+ warning = @detector.smells_found.to_a[0] # SMELL: too cumbersome!
82
+ @yaml = warning.to_yaml
83
+ end
84
+ it 'reports the source' do
85
+ @yaml.should match(/source:\s*#{@source_name}/)
86
+ end
87
+ it 'reports the class' do
88
+ @yaml.should match(/\sclass:\s*LongParameterList/)
61
89
  end
62
- it 'should not report yield with few parameters' do
63
- 'def simple(arga, argb, &blk) f(3);yield a,b; end'.should_not reek
90
+ it 'reports the subclass' do
91
+ @yaml.should match(/subclass:\s*LongParameterList/)
64
92
  end
65
- it 'should report yield with many parameters' do
66
- 'def simple(arga, argb, &blk) f(3);yield arga,argb,arga,argb; end'.should reek_only_of(:LongYieldList, /simple/, /yields/, /4/)
93
+ it 'reports the number of parameters' do
94
+ @yaml.should match(/parameter_count:[\s]*#{@num_parameters}/)
95
+ # SMELL: many tests duplicate the names of the YAML fields
67
96
  end
68
- it 'should not report yield of a long expression' do
69
- 'def simple(arga, argb, &blk) f(3);yield(if @dec then argb else 5+3 end); end'.should_not reek
97
+ it 'reports the line number of the method' do
98
+ @yaml.should match(/lines:\s*- 1/)
70
99
  end
71
100
  end
72
101
  end
@@ -0,0 +1,65 @@
1
+ require File.join(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))), 'spec_helper')
2
+ require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'smells', 'long_yield_list')
3
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'smell_detector_shared')
4
+
5
+ include Reek
6
+ include Reek::Smells
7
+
8
+ describe LongYieldList do
9
+ before(:each) do
10
+ @source_name = 'oo la la'
11
+ @detector = LongYieldList.new(@source_name)
12
+ # SMELL: can't use the default config, because that contains an override,
13
+ # which causes the mocked matches?() method to be called twice!!
14
+ end
15
+
16
+ it_should_behave_like 'SmellDetector'
17
+
18
+ context 'yield' do
19
+ it 'should not report yield with no parameters' do
20
+ 'def simple(arga, argb, &blk) f(3);yield; end'.should_not reek
21
+ end
22
+ it 'should not report yield with few parameters' do
23
+ 'def simple(arga, argb, &blk) f(3);yield a,b; end'.should_not reek
24
+ end
25
+ it 'should report yield with many parameters' do
26
+ 'def simple(arga, argb, &blk) f(3);yield arga,argb,arga,argb; end'.should reek_only_of(:LongYieldList, /simple/, /yields/, /4/)
27
+ end
28
+ it 'should not report yield of a long expression' do
29
+ 'def simple(arga, argb, &blk) f(3);yield(if @dec then argb else 5+3 end); end'.should_not reek
30
+ end
31
+ end
32
+
33
+ context 'looking at the YAML' do
34
+ before :each do
35
+ src = <<EOS
36
+ def simple(arga, argb, &blk)
37
+ f(3)
38
+ yield(arga,argb,arga,argb)
39
+ end
40
+ EOS
41
+ source = src.to_reek_source
42
+ sniffer = Sniffer.new(source)
43
+ @mctx = CodeParser.new(sniffer).process_defn(source.syntax_tree)
44
+ @detector.examine_context(@mctx)
45
+ warning = @detector.smells_found.to_a[0] # SMELL: too cumbersome!
46
+ @yaml = warning.to_yaml
47
+ end
48
+ it 'reports the source' do
49
+ @yaml.should match(/source:\s*#{@source_name}/)
50
+ end
51
+ it 'reports the class' do
52
+ @yaml.should match(/class:\s*LongParameterList/)
53
+ end
54
+ it 'reports the subclass' do
55
+ @yaml.should match(/subclass:\s*LongYieldList/)
56
+ end
57
+ it 'reports the number of parameters' do
58
+ @yaml.should match(/parameter_count:[\s]*#{@num_parameters}/)
59
+ # SMELL: many tests duplicate the names of the YAML fields
60
+ end
61
+ it 'reports the line number of the method' do
62
+ @yaml.should match(/lines:\s*- 3/)
63
+ end
64
+ end
65
+ end
@@ -1,6 +1,6 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
-
3
- require 'reek/smells/nested_iterators'
1
+ require File.join(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))), 'spec_helper')
2
+ require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'smells', 'nested_iterators')
3
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'smell_detector_shared')
4
4
 
5
5
  include Reek::Smells
6
6
 
@@ -40,3 +40,94 @@ EOS
40
40
  end
41
41
  end
42
42
 
43
+ describe NestedIterators do
44
+ before(:each) do
45
+ @detector = NestedIterators.new('cuckoo')
46
+ end
47
+
48
+ it_should_behave_like 'SmellDetector'
49
+
50
+ context 'find_deepest_iterators' do
51
+ context 'with no iterators' do
52
+ it 'returns an empty list' do
53
+ src = 'def fred() nothing = true; end'
54
+ source = src.to_reek_source
55
+ sniffer = Sniffer.new(source)
56
+ @mctx = CodeParser.new(sniffer).process_defn(source.syntax_tree)
57
+ @detector.find_deepest_iterators(@mctx).should == []
58
+ end
59
+ end
60
+
61
+ context 'with one iterator' do
62
+ before :each do
63
+ src = 'def fred() nothing.each {|item| item}; end'
64
+ source = src.to_reek_source
65
+ sniffer = Sniffer.new(source)
66
+ mctx = CodeParser.new(sniffer).process_defn(source.syntax_tree)
67
+ @result = @detector.find_deepest_iterators(mctx)
68
+ end
69
+ it 'returns a depth of 1' do
70
+ @result.should == []
71
+ end
72
+ end
73
+
74
+ context 'with two non-nested iterators' do
75
+ before :each do
76
+ src = <<EOS
77
+ def fred()
78
+ nothing.each do |item|
79
+ item
80
+ end
81
+ again.each {|thing| }
82
+ end
83
+ EOS
84
+ source = src.to_reek_source
85
+ sniffer = Sniffer.new(source)
86
+ mctx = CodeParser.new(sniffer).process_defn(source.syntax_tree)
87
+ @result = @detector.find_deepest_iterators(mctx)
88
+ end
89
+ it 'returns both iterators' do
90
+ @result.length.should == 0
91
+ end
92
+ end
93
+
94
+ context 'with one nested iterator' do
95
+ before :each do
96
+ src = <<EOS
97
+ def fred()
98
+ nothing.each do |item|
99
+ again.each {|thing| item }
100
+ end
101
+ end
102
+ EOS
103
+ source = src.to_reek_source
104
+ sniffer = Sniffer.new(source)
105
+ @mctx = CodeParser.new(sniffer).process_defn(source.syntax_tree)
106
+ @result = @detector.find_deepest_iterators(@mctx)
107
+ end
108
+ it 'returns only the deepest iterator' do
109
+ @result.length.should == 1
110
+ end
111
+ it 'has depth of 2' do
112
+ @result[0][1].should == 2
113
+ end
114
+ it 'refers to the innermost exp' do
115
+ @result[0][0].line.should == 3
116
+ end
117
+
118
+ context 'when reporting yaml' do
119
+ before :each do
120
+ @detector.examine_context(@mctx)
121
+ warning = @detector.smells_found.to_a[0] # SMELL: too cumbersome!
122
+ @yaml = warning.to_yaml
123
+ end
124
+ it 'reports the depth' do
125
+ @yaml.should match(/depth:\s*2/)
126
+ end
127
+ it 'reports the deepest line number' do
128
+ @yaml.should match(/lines:[\s-]*3/)
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -1,15 +1,19 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper.rb'
1
+ require File.join(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))), 'spec_helper')
2
+ require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'smells', 'simulated_polymorphism')
3
+ require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'core', 'code_context')
4
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'smell_detector_shared')
2
5
 
3
- require 'reek/smells/simulated_polymorphism'
4
- require 'reek/code_context'
5
-
6
- include Reek
6
+ include Reek::Core
7
7
  include Reek::Smells
8
8
 
9
9
  describe SimulatedPolymorphism do
10
10
  before :each do
11
- @detector = SimulatedPolymorphism.new
11
+ @source_name = 'howdy-doody'
12
+ @detector = SimulatedPolymorphism.new(@source_name)
12
13
  end
14
+
15
+ it_should_behave_like 'SmellDetector'
16
+
13
17
  context 'with no conditionals' do
14
18
  it 'gathers an empty hash' do
15
19
  ast = 'module Stable; end'.to_reek_source.syntax_tree
@@ -28,36 +32,60 @@ describe SimulatedPolymorphism do
28
32
 
29
33
  context 'with three identical conditionals' do
30
34
  before :each do
31
- cond = '@field == :sym'
32
- @cond_expr = cond.to_reek_source.syntax_tree
35
+ @cond = '@field == :sym'
36
+ @cond_expr = @cond.to_reek_source.syntax_tree
33
37
  src = <<EOS
34
38
  class Scrunch
35
39
  def first
36
- return #{cond} ? 0 : 3;
40
+ puts "hello" if @debug
41
+ return #{@cond} ? 0 : 3;
37
42
  end
38
43
  def second
39
- if #{cond}
44
+ if #{@cond}
40
45
  @other += " quarts"
41
46
  end
42
47
  end
43
48
  def third
44
- raise 'flu!' unless #{cond}
49
+ raise 'flu!' unless #{@cond}
45
50
  end
46
51
  end
47
52
  EOS
48
53
 
49
54
  ast = src.to_reek_source.syntax_tree
50
- ctx = CodeContext.new(nil, ast)
51
- @conds = @detector.conditional_counts(ctx)
55
+ @ctx = CodeContext.new(nil, ast)
56
+ @conds = @detector.conditional_counts(@ctx)
52
57
  end
53
- it 'finds one matching conditional' do
54
- @conds.length.should == 1
55
- end
56
- it 'returns the condition expr' do
57
- @conds.keys[0].should == @cond_expr
58
+ it 'finds both conditionals' do
59
+ @conds.length.should == 2
58
60
  end
59
61
  it 'knows there are three copies' do
60
- @conds[@cond_expr].should == 3
62
+ @conds[@cond_expr].length.should == 3
63
+ end
64
+
65
+ context 'looking at the YAML' do
66
+ before :each do
67
+ @detector.examine(@ctx)
68
+ warning = @detector.smells_found.to_a[0] # SMELL: too cumbersome!
69
+ @yaml = warning.to_yaml
70
+ end
71
+ it 'reports the source' do
72
+ @yaml.should match(/source:\s*#{@source_name}/)
73
+ end
74
+ it 'reports the class' do
75
+ @yaml.should match(/class:\s*SimulatedPolymorphism/)
76
+ end
77
+ it 'reports the subclass' do
78
+ @yaml.should match(/subclass:\s*RepeatedConditional/)
79
+ end
80
+ it 'reports the expression' do
81
+ @yaml.should match(/expression:\s*\(#{@cond}\)/)
82
+ end
83
+ it 'reports the number of occurrences' do
84
+ @yaml.should match(/occurrences:\s*3/)
85
+ end
86
+ it 'reports the referring lines' do
87
+ @yaml.should match(/lines:\s*- 4\s*- 7\s*- 12/)
88
+ end
61
89
  end
62
90
  end
63
91
 
@@ -90,7 +118,7 @@ EOS
90
118
  @conds.keys[0].should == @cond_expr
91
119
  end
92
120
  it 'knows there are three copies' do
93
- @conds[@cond_expr].should == 2
121
+ @conds[@cond_expr].length.should == 2
94
122
  end
95
123
  end
96
124
 
@@ -0,0 +1,28 @@
1
+ require File.join(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))), 'spec_helper')
2
+ require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'core', 'smell_configuration')
3
+
4
+ include Reek::Core
5
+
6
+ shared_examples_for 'SmellDetector' do
7
+ context 'exception matching follows the context' do
8
+ before :each do
9
+ @ctx = mock('context')
10
+ end
11
+ it 'when false' do
12
+ @ctx.should_receive(:matches?).and_return(false)
13
+ @detector.exception?(@ctx).should == false
14
+ end
15
+
16
+ it 'when true' do
17
+ @ctx.should_receive(:matches?).and_return(true)
18
+ @detector.exception?(@ctx).should == true
19
+ end
20
+ end
21
+
22
+ context 'configuration' do
23
+ it 'becomes disabled when disabled' do
24
+ @detector.configure({@detector.smell_type => {SmellConfiguration::ENABLED_KEY => false}})
25
+ @detector.should_not be_enabled
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,57 @@
1
+ require File.join(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))), 'spec_helper')
2
+ require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'smells', 'uncommunicative_method_name')
3
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'smell_detector_shared')
4
+ require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'core', 'code_parser')
5
+ require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'core', 'sniffer')
6
+
7
+ include Reek
8
+ include Reek::Smells
9
+
10
+ describe UncommunicativeMethodName do
11
+ before :each do
12
+ @source_name = 'wallamalloo'
13
+ @detector = UncommunicativeMethodName.new(@source_name)
14
+ end
15
+
16
+ it_should_behave_like 'SmellDetector'
17
+
18
+ it 'should not report one-word method name' do
19
+ 'def help(fred) basics(17) end'.should_not reek
20
+ end
21
+ it 'should report one-letter method name' do
22
+ 'def x(fred) basics(17) end'.should reek_only_of(:UncommunicativeMethodName, /x/)
23
+ end
24
+ it 'should report name of the form "x2"' do
25
+ 'def x2(fred) basics(17) end'.should reek_only_of(:UncommunicativeMethodName, /x2/)
26
+ end
27
+ it 'should report long name ending in a number' do
28
+ 'def method2(fred) basics(17) end'.should reek_only_of(:UncommunicativeMethodName, /method2/)
29
+ end
30
+
31
+ context 'looking at the YAML' do
32
+ before :each do
33
+ src = 'def bad3() end'
34
+ source = src.to_reek_source
35
+ sniffer = Core::Sniffer.new(source)
36
+ @mctx = Core::CodeParser.new(sniffer).process_defn(source.syntax_tree)
37
+ @detector.examine(@mctx)
38
+ warning = @detector.smells_found.to_a[0] # SMELL: too cumbersome!
39
+ @yaml = warning.to_yaml
40
+ end
41
+ it 'reports the source' do
42
+ @yaml.should match(/source:\s*#{@source_name}/)
43
+ end
44
+ it 'reports the class' do
45
+ @yaml.should match(/\sclass:\s*UncommunicativeName/)
46
+ end
47
+ it 'reports the subclass' do
48
+ @yaml.should match(/subclass:\s*UncommunicativeMethodName/)
49
+ end
50
+ it 'reports the variable name' do
51
+ @yaml.should match(/method_name:\s*bad3/)
52
+ end
53
+ it 'reports the line number of the method def' do
54
+ @yaml.should match(/lines:\s*- 1/)
55
+ end
56
+ end
57
+ end