Permalink
Browse files

Separate file of model specs. Modified node_analysis so I would have …

…every filename which contained a given duplicated method definition. Had to change Towelie controller to keep the original Towelie spec passing on that one.
  • Loading branch information...
1 parent 263186a commit 4c2affd11049ce07d0c6eb0843b65c981b64eeb3 @gilesbowkett committed Oct 3, 2008
Showing with 160 additions and 2 deletions.
  1. +1 −1 lib/node_analysis.rb
  2. +4 −1 lib/towelie.rb
  3. +155 −0 spec/model_spec.rb
@@ -9,7 +9,7 @@ def duplicated
def duplicates
(@method_definitions.collect do |node|
node if @method_definitions.duplicates? node
- end).compact.uniq
+ end).compact
end
def unique
@method_definitions - duplicates
View
@@ -25,7 +25,10 @@ def initialize(view_format)
@view = View.new(template)
end
delegate_thru_model :parse, :duplication?, :method_definitions
- delegate_thru_view :duplicated, :unique, :homonyms, :diff
+ delegate_thru_view :unique, :homonyms, :diff
+ def duplicated(*args)
+ @view.render(@model.duplicated(*args).uniq)
+ end
end
# most methods need a dir loaded. therefore we should have an object which takes a dir (and probably
View
@@ -0,0 +1,155 @@
+require 'lib/towelie'
+
+describe Towelie do
+ before(:each) do
+ @model = Model.new
+ end
+ before(:all) do
+ @def_nodes = [
+ # second_file.rb
+ [:defn, :foo,
+ [:scope,
+ [:block, [:args], [:str, "still unique"]]]],
+ [:defn, :bar,
+ [:scope,
+ [:block, [:args], [:str, "something non-unique"]]]],
+ [:defn, :baz,
+ [:scope,
+ [:block, [:args], [:str, "also unique"]]]],
+
+ # first_file.rb
+ [:defn, :foo,
+ [:scope,
+ [:block, [:args], [:str, "something unique"]]]],
+ [:defn, :bar,
+ [:scope,
+ [:block, [:args], [:str, "something non-unique"]]]]
+ ]
+ @duplicated_nodes = [
+ [:defn, :bar,
+ [:scope,
+ [:block, [:args], [:str, "something non-unique"]]]],
+ [:defn, :bar,
+ [:scope,
+ [:block, [:args], [:str, "something non-unique"]]]]
+ ]
+ @unique_nodes = [
+ [:defn, :foo,
+ [:scope,
+ [:block, [:args], [:str, "still unique"]]]],
+ [:defn, :baz,
+ [:scope,
+ [:block, [:args], [:str, "also unique"]]]],
+ [:defn, :foo,
+ [:scope,
+ [:block, [:args], [:str, "something unique"]]]]
+ ]
+ @homonym_nodes = [
+ [:defn, :foo,
+ [:scope,
+ [:block, [:args], [:str, "still unique"]]]],
+ [:defn, :foo,
+ [:scope,
+ [:block, [:args], [:str, "something unique"]]]]
+ ]
+ @one_node_diff_nodes = [
+ [:defn, :bar,
+ [:scope,
+ [:block, [:args], [:str, "bar"]]]],
+ [:defn, :foo,
+ [:scope,
+ [:block, [:args], [:str, "foo"]]]]
+ ]
+ @bigger_one_node_diff_nodes = [
+ [:defn, :bar,
+ [:scope,
+ [:block,
+ [:args],
+ [:fcall, :puts,
+ [:array, [:str, "muppetfuckers"]]],
+ [:iasgn, :@variable,
+ [:str, "bar"]]]]],
+ [:defn, :foo,
+ [:scope,
+ [:block,
+ [:args],
+ [:fcall, :puts,
+ [:array, [:str, "muppetfuckers"]]],
+ [:iasgn, :@variable,
+ [:str, "foo"]]]]]
+ ]
+ @two_node_diff_nodes = [
+ [:defn, :bar,
+ [:scope,
+ [:block,
+ [:args],
+ [:fcall, :puts,
+ [:array, [:str, "muppetfuckers"]]],
+ [:iasgn, :@variable,
+ [:str, "bar"]]]]],
+ [:defn, :foo,
+ [:scope,
+ [:block,
+ [:args],
+ [:fcall, :puts,
+ [:array, [:str, "muppetphuckers"]]],
+ [:iasgn, :@variable,
+ [:str, "foo"]]]]]
+ ]
+ end
+ it "identifies duplication" do
+ @model.parse("spec/test_data")
+ @model.duplication?.should be_true
+
+ @model.parse("spec/classes_modules")
+ @model.duplication?.should be_true
+ end
+ it "returns no false positives when identifying duplication" do
+ @model.parse("spec/non_duplicating_data")
+ @model.duplication?.should be_false
+ end
+ it "extracts :defn nodes" do
+ @model.parse("spec/test_data")
+ @model.method_definitions.should == @def_nodes
+
+ @model.parse("spec/classes_modules")
+ @model.method_definitions.should == @def_nodes
+ end
+ it "isolates duplicated blocks" do
+ @model.parse("spec/test_data")
+ @model.duplicated.should == @duplicated_nodes
+
+ @model.parse("spec/classes_modules")
+ @model.duplicated.should == @duplicated_nodes
+ end
+ it "reports unique code" do
+ @model.parse("spec/test_data")
+ @model.unique.should == @unique_nodes
+
+ @model.parse("spec/classes_modules")
+ @model.unique.should == @unique_nodes
+ end
+ it "reports distinct methods with the same name" do
+ @model.parse("spec/test_data")
+ @model.homonyms.should == @homonym_nodes
+
+ @model.parse("spec/classes_modules")
+ @model.homonyms.should == @homonym_nodes
+ end
+ it "reports methods which differ only by one node" do
+ @model.parse("spec/one_node_diff")
+ @model.diff(1).should == @one_node_diff_nodes
+
+ @model.parse("spec/larger_one_node_diff")
+ @model.diff(1).should == @bigger_one_node_diff_nodes
+ end
+ it "reports methods which differ by arbitrary numbers of nodes" do
+ @model.parse("spec/two_node_diff")
+ @model.method_definitions.should_not be_empty
+ @model.diff(2).should == @two_node_diff_nodes
+ end
+ it "attaches filenames to individual nodes" do
+ @model.parse("spec/two_node_diff")
+ @model.method_definitions[0].filename.should == "spec/two_node_diff/second_file.rb"
+ end
+end

0 comments on commit 4c2affd

Please sign in to comment.