spec/tree_spec.rb in jumoku-0.1.3 vs spec/tree_spec.rb in jumoku-0.2.0

- old
+ new

@@ -1,40 +1,28 @@ -require File.join(File.dirname(__FILE__), 'spec_helper') +require 'spec_helper' -# The Tree builder extends the core functionalities provided -# by RawTreeBuilder. -# -# Note: these tests may make use of the "root"/"children" terminology. -# Be aware this has got *no* structural meaning as a tree is, by -# definition, undirected. Those terms are used only to simplify -# nodes creation within the tests, so I recall who branched who. -# For tests about rooted tree, see arborescence_spec.rb -describe "TreeBuilder" do +describe Tree do before :each do - class MyTree - include TreeBuilder - end - - @tree = MyTree.new + @tree = Tree.new end describe "#new" do it "should create a valid tree graph" do @tree.should be_valid @tree.nodes.should be_empty end end - ## Core API is tested in raw_tree_spec.rb. - ## The following tests focus on TreeBuilder additional methods. + ## The core TreeAPI is tested in raw_tree_spec.rb. + ## The following tests focus on methods added by TreeBuilder. describe "#add_node" do describe "an empty tree" do it "should create a new, valid tree with a single node when its first node is added" do @tree.add_node 1 - @tree.should be_empty - + @tree.should be_empty # the original tree is not modified + # a brand new tree is created instead, extending the original one new_tree = @tree.add_node 1 new_tree.nodes.should == [1] new_tree.should be_valid end end @@ -68,11 +56,11 @@ new_tree.nodes.should == [:one, :two] new_tree.should be_valid end end - describe "a populate" do + describe "a populated tree" do before :each do @tree.add_branch! 1, 2 @tree.add_branch! 2, 3 @tree.add_branch! 3, 4 end @@ -95,11 +83,11 @@ it "should raise an error when trying to remove a node" do lambda { @tree.remove_node :null }.should raise_error, RawTreeError end end - describe "a tree that's one node only" do + describe "a tree containing one node" do before :each do @tree.add_node! 1 end it "should create a new, empty, valid tree" do @@ -152,15 +140,15 @@ it "should allow for adding its first nodes, by pairs and branches" do @tree.add_nodes! 1,2, 2,3, 3,4 @tree.nodes.should == [1, 2, 3, 4] @tree.should be_valid - @tree.add_nodes! Branch.new(5, 2) + @tree.add_nodes! UndirectedBranch.new(5, 2) @tree.nodes.should == [1, 2, 3, 4, 5] @tree.should be_valid - @tree.add_nodes! 3,4, 1,10, Branch.new(10, 11), -1,1 + @tree.add_nodes! 3,4, 1,10, UndirectedBranch.new(10, 11), -1,1 @tree.nodes.size.should == 8 @tree.should be_valid lambda { @tree.add_nodes! 10, 11 }.should_not raise_error lambda { @tree.add_nodes! 1, 11 }.should raise_error, RawTreeError # cycle @@ -179,16 +167,16 @@ new_tree = @tree.add_nodes 1,2, 2,3, 3,4 @tree.should be_empty new_tree.nodes.should == [1, 2, 3, 4] new_tree.should be_valid - new_tree = @tree.add_nodes Branch.new(4, 5) + new_tree = @tree.add_nodes UndirectedBranch.new(4, 5) @tree.should be_empty new_tree.nodes.should == [4, 5] new_tree.should be_valid - new_tree = @tree.add_nodes 3,4, 4,10, Branch.new(10, 11), -1,3 + new_tree = @tree.add_nodes 3,4, 4,10, UndirectedBranch.new(10, 11), -1,3 @tree.should be_empty new_tree.nodes.size.should == 5 new_tree.should be_valid lambda { @tree.add_nodes 10, 11 }.should_not raise_error @@ -202,15 +190,15 @@ end describe "#add_branches!" do describe "an empty tree" do it "should allow for adding its first branches" do - @tree.add_branches! Branch.new(1, 2), 1,3, 3,4 + @tree.add_branches! UndirectedBranch.new(1, 2), 1,3, 3,4 @tree.nodes.should == [1, 2, 3, 4] @tree.should be_valid - branch = Branch.new(1, 4) + branch = UndirectedBranch.new(1, 4) lambda { @tree.add_branches! branch }.should raise_error, RawTreeError end end describe "a populated tree" do @@ -221,23 +209,23 @@ it "should allow for adding new branches" do @tree.add_branches! 2, 3 @tree.nodes.should == [1, 2, 3] @tree.should be_valid - @tree.add_branches! Branch.new(0,1), 3,4, Branch.new(4,5) + @tree.add_branches! UndirectedBranch.new(0,1), 3,4, UndirectedBranch.new(4,5) [0..5].all? { |n| @tree.nodes.include? n } and @tree.nodes.size.should == 5 @tree.should be_valid - lambda { @tree.add_branches! Branch.new(2, 5) }.should raise_error, RawTreeError + lambda { @tree.add_branches! UndirectedBranch.new(2, 5) }.should raise_error, RawTreeError end end end describe "add_branches" do describe "an empty tree" do it "should create a new, valid tree populated with its first branches" do - b1, b2, b3 = Branch.new(1, 2), Branch.new(2, 3), Branch.new(3, 4) + b1, b2, b3 = UndirectedBranch.new(1, 2), UndirectedBranch.new(2, 3), UndirectedBranch.new(3, 4) new_tree = @tree.add_branches b1, b2, b3 @tree.should be_empty new_tree.nodes.should == [1, 2, 3, 4] new_tree.should be_valid @@ -249,17 +237,17 @@ before :each do @tree.add_nodes! 1,2, 2,3, 3,4 end it "should create a new, valid tree extended with its new branches" do - new_tree = @tree.add_branches 4, :five, Branch.new(:five, "six") + new_tree = @tree.add_branches 4, :five, UndirectedBranch.new(:five, "six") @tree.nodes.should == (1..4).to_a new_tree.nodes.should == [1, 2, 3, 4, :five, "six"] new_tree.should be_valid lambda { @tree.add_branches 10, 11 }.should raise_error, RawTreeError - lambda { @tree.add_branches Branch.new 1,4 }.should raise_error, RawTreeError + lambda { @tree.add_branches UndirectedBranch.new 1,4 }.should raise_error, RawTreeError end end end describe "#remove_nodes!" do @@ -322,11 +310,11 @@ end describe "#remove_branches!" do describe "an empty tree" do it "should not allow for removing branches" do - lambda { @tree.remove_branches Branch.new(:foo, :bar), 1,2 }.should raise_error, RawTreeError + lambda { @tree.remove_branches UndirectedBranch.new(:foo, :bar), 1,2 }.should raise_error, RawTreeError end end describe "a tree that's only one node" do before :each do @@ -342,23 +330,23 @@ before :each do @tree.add_nodes! 1,2, 2,3, 3,4 end it "should allow for removing branches until it has no more branches or a sole node" do - @tree.remove_branches! 1,2, Branch.new(2,3), 3,4 + @tree.remove_branches! 1,2, UndirectedBranch.new(2,3), 3,4 @tree.should be_empty @tree.should be_valid - lambda { @tree.remove_branches! Branch.new(:foo, :bar), Branch.new(1, 2) }.should raise_error, RawTreeError + lambda { @tree.remove_branches! UndirectedBranch.new(:foo, :bar), UndirectedBranch.new(1, 2) }.should raise_error, RawTreeError end end end describe "#remove_branches" do describe "an empty tree" do it "should not allow for removing branches" do - lambda { @tree.remove_branches :foo, :bar, Branch.new(1, 2) }.should raise_error, RawTreeError + lambda { @tree.remove_branches :foo, :bar, UndirectedBranch.new(1, 2) }.should raise_error, RawTreeError end end describe "a tree that's one node" do before :each do @@ -374,16 +362,16 @@ before :each do @tree.add_nodes! 1,2, 2,3, 3,4 end it "should allow for removing branches until it has no more branches or a sole node, creating a new, valid tree" do - new_tree = @tree.remove_branches 1,2, Branch.new(2,3), 3,4 + new_tree = @tree.remove_branches 1,2, UndirectedBranch.new(2,3), 3,4 @tree.nodes.should == [1, 2, 3, 4] new_tree.should be_empty new_tree.should be_valid - lambda { @tree.remove_branches Branch.new(:foo, :bar), Branch.new(1, 2) }.should raise_error, RawTreeError + lambda { @tree.remove_branches UndirectedBranch.new(:foo, :bar), UndirectedBranch.new(1, 2) }.should raise_error, RawTreeError end end end describe "#node?" do @@ -525,27 +513,27 @@ end describe "#add_consecutive_nodes!" do describe "a tree" do it "should grow as a valid, populated tree if all specified nodes define a valid tree structure" do - @tree.add_consecutive_nodes!(1, 2, 3, :four, Branch.new(:foo, "bar")) - @tree.has_branches?(1,2, 2,3, Branch.new(3, :four), :four,:foo, :foo,"bar").should be_true # each specified branch exist - @tree.has_branches?(1,2, 2,3, Branch.new(3, :four), :four,:foo).should be_true # each specified branch exist - @tree.has_branches_among?(1,2, 2,3, Branch.new(3, :four), :four,:foo).should be_false # do not list every existing branch - @tree.has_branches_among?(1,2, 2,3, Branch.new(3, :four), :four,:foo, :foo,"bar").should be_true # list all existing branches - @tree.has_branches_among?(1,2, 2,3, Branch.new(3, :four), :four,:foo, :foo,"bar", Array.new, "NIL").should be_true # list all existing branches + @tree.add_consecutive_nodes!(1, 2, 3, :four, UndirectedBranch.new(:foo, "bar")) + @tree.has_branches?(1,2, 2,3, UndirectedBranch.new(3, :four), :four,:foo, :foo,"bar").should be_true # each specified branch exist + @tree.has_branches?(1,2, 2,3, UndirectedBranch.new(3, :four), :four,:foo).should be_true # each specified branch exist + @tree.has_branches_among?(1,2, 2,3, UndirectedBranch.new(3, :four), :four,:foo).should be_false # do not list every existing branch + @tree.has_branches_among?(1,2, 2,3, UndirectedBranch.new(3, :four), :four,:foo, :foo,"bar").should be_true # list all existing branches + @tree.has_branches_among?(1,2, 2,3, UndirectedBranch.new(3, :four), :four,:foo, :foo,"bar", Array.new, "NIL").should be_true # list all existing branches @tree.should be_valid end end end describe "#add_consecutive_nodes" do describe "a tree" do it "should create a new, valid, populated tree if all specified nodes define a valid tree structure" do - new_tree = @tree.add_consecutive_nodes(1, 2, 3, :four, Branch.new(:foo, "bar")) + new_tree = @tree.add_consecutive_nodes(1, 2, 3, :four, UndirectedBranch.new(:foo, "bar")) @tree.should be_empty - new_tree.has_branches?(1,2, 2,3, Branch.new(3, :four), :four,:foo, :foo,"bar").should be_true - new_tree.has_branches?(1,2, 2,3, Branch.new(3, :four), :four,:foo).should be_true + new_tree.has_branches?(1,2, 2,3, UndirectedBranch.new(3, :four), :four,:foo, :foo,"bar").should be_true + new_tree.has_branches?(1,2, 2,3, UndirectedBranch.new(3, :four), :four,:foo).should be_true new_tree.should be_valid end end end end