diff options
Diffstat (limited to 'spec/rubyspec/library/rexml/node')
6 files changed, 124 insertions, 0 deletions
diff --git a/spec/rubyspec/library/rexml/node/each_recursive_spec.rb b/spec/rubyspec/library/rexml/node/each_recursive_spec.rb new file mode 100644 index 0000000000..5f26c898ea --- /dev/null +++ b/spec/rubyspec/library/rexml/node/each_recursive_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Node#each_recursive" do + before :each do + @doc = REXML::Document.new + @doc << REXML::XMLDecl.new + @root = REXML::Element.new "root" + @child1 = REXML::Element.new "child1" + @child2 = REXML::Element.new "child2" + @root << @child1 + @root << @child2 + @doc << @root + end + + it "visits all subnodes of self" do + nodes = [] + @doc.each_recursive { |node| nodes << node} + nodes.should == [@root, @child1, @child2] + end +end diff --git a/spec/rubyspec/library/rexml/node/find_first_recursive_spec.rb b/spec/rubyspec/library/rexml/node/find_first_recursive_spec.rb new file mode 100644 index 0000000000..20e87fe9e9 --- /dev/null +++ b/spec/rubyspec/library/rexml/node/find_first_recursive_spec.rb @@ -0,0 +1,25 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Node#find_first_recursive" do + before :each do + @e = REXML::Element.new("root") + @node1 = REXML::Element.new("node") + @node2 = REXML::Element.new("another node") + @subnode = REXML::Element.new("another node") + @node1 << @subnode + @e << @node1 + @e << @node2 + end + + it "finds the first element that matches block" do + found = @e.find_first_recursive { |n| n.to_s == "<node><another node/></node>"} + found.should == @node1 + end + + it "visits the nodes in preorder" do + found = @e.find_first_recursive { |n| n.to_s == "<another node/>"} + found.should == @subnode + found.should_not == @node2 + end +end diff --git a/spec/rubyspec/library/rexml/node/index_in_parent_spec.rb b/spec/rubyspec/library/rexml/node/index_in_parent_spec.rb new file mode 100644 index 0000000000..018a4c08ac --- /dev/null +++ b/spec/rubyspec/library/rexml/node/index_in_parent_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Node#index_in_parent" do + it "returns the index (starting from 1) of self in parent" do + e = REXML::Element.new("root") + node1 = REXML::Element.new("node") + node2 = REXML::Element.new("another node") + e << node1 + e << node2 + + node1.index_in_parent.should == 1 + node2.index_in_parent.should == 2 + end +end diff --git a/spec/rubyspec/library/rexml/node/next_sibling_node_spec.rb b/spec/rubyspec/library/rexml/node/next_sibling_node_spec.rb new file mode 100644 index 0000000000..9ca91c9149 --- /dev/null +++ b/spec/rubyspec/library/rexml/node/next_sibling_node_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Node#next_sibling_node" do + before :each do + @e = REXML::Element.new("root") + @node1 = REXML::Element.new("node") + @node2 = REXML::Element.new("another node") + @e << @node1 + @e << @node2 + end + + it "returns the next child node in parent" do + @node1.next_sibling_node.should == @node2 + end + + it "returns nil if there are no more child nodes next" do + @node2.next_sibling_node.should == nil + @e.next_sibling_node.should == nil + end +end diff --git a/spec/rubyspec/library/rexml/node/parent_spec.rb b/spec/rubyspec/library/rexml/node/parent_spec.rb new file mode 100644 index 0000000000..ee3c234534 --- /dev/null +++ b/spec/rubyspec/library/rexml/node/parent_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Node#parent?" do + it "returns true for Elements" do + e = REXML::Element.new("foo") + e.parent?.should == true + end + + it "returns true for Documents" do + e = REXML::Document.new + e.parent?.should == true + end + + # This includes attributes, CDatas and declarations. + it "returns false for Texts" do + e = REXML::Text.new("foo") + e.parent?.should == false + end +end + diff --git a/spec/rubyspec/library/rexml/node/previous_sibling_node_spec.rb b/spec/rubyspec/library/rexml/node/previous_sibling_node_spec.rb new file mode 100644 index 0000000000..b8e4465979 --- /dev/null +++ b/spec/rubyspec/library/rexml/node/previous_sibling_node_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Node#previous_sibling_node" do + before :each do + @e = REXML::Element.new("root") + @node1 = REXML::Element.new("node") + @node2 = REXML::Element.new("another node") + @e << @node1 + @e << @node2 + end + + it "returns the previous child node in parent" do + @node2.previous_sibling_node.should == @node1 + end + + it "returns nil if there are no more child nodes before" do + @node1.previous_sibling_node.should == nil + @e.previous_sibling_node.should == nil + end +end |