From bf54231757cda144f1f11483562a19104d21ae02 Mon Sep 17 00:00:00 2001 From: Federico Builes Date: Thu, 3 Apr 2008 08:29:23 -0500 Subject: [PATCH] More specs for REXML::Element --- .../1.8/library/rexml/element/add_element_spec.rb | 37 ++++++++++++++ .../library/rexml/element/delete_element_spec.rb | 50 ++++++++++++++++++++ .../element/each_element_with_attribute_spec.rb | 34 +++++++++++++ .../rexml/element/each_element_with_text_spec.rb | 30 ++++++++++++ spec/ruby/1.8/library/rexml/element/new_spec.rb | 6 +- .../1.8/library/rexml/shared/each_element_spec.rb | 38 +++++++++++++++ 6 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 spec/ruby/1.8/library/rexml/element/add_element_spec.rb create mode 100644 spec/ruby/1.8/library/rexml/element/delete_element_spec.rb create mode 100644 spec/ruby/1.8/library/rexml/element/each_element_with_attribute_spec.rb create mode 100644 spec/ruby/1.8/library/rexml/element/each_element_with_text_spec.rb create mode 100644 spec/ruby/1.8/library/rexml/shared/each_element_spec.rb diff --git a/spec/ruby/1.8/library/rexml/element/add_element_spec.rb b/spec/ruby/1.8/library/rexml/element/add_element_spec.rb new file mode 100644 index 0000000..4a1a17b --- /dev/null +++ b/spec/ruby/1.8/library/rexml/element/add_element_spec.rb @@ -0,0 +1,37 @@ +require 'rexml/document' + +describe "REXML::Element#add_element" do + before :each do + @root = REXML::Element.new("root") + end + + it "adds a child without attributes" do + name = REXML::Element.new("name") + @root.add_element name + @root.elements["name"].name.should == name.name + @root.elements["name"].attributes.should == name.attributes + @root.elements["name"].context == name.context + end + + it "adds a child with attributes" do + person = REXML::Element.new("person") + @root.add_element(person, {"name" => "Madonna"}) + @root.elements["person"].name.should == person.name + @root.elements["person"].attributes.should == person.attributes + @root.elements["person"].context == person.context + end + + it "adds a child with name" do + @root.add_element "name" + @root.elements["name"].name.should == "name" + @root.elements["name"].attributes.should == {} + @root.elements["name"].context == {} + end + + it "returns the added child" do + name = @root.add_element "name" + @root.elements["name"].name.should == name.name + @root.elements["name"].attributes.should == name.attributes + @root.elements["name"].context == name.context + end +end diff --git a/spec/ruby/1.8/library/rexml/element/delete_element_spec.rb b/spec/ruby/1.8/library/rexml/element/delete_element_spec.rb new file mode 100644 index 0000000..1410f37 --- /dev/null +++ b/spec/ruby/1.8/library/rexml/element/delete_element_spec.rb @@ -0,0 +1,50 @@ +require 'rexml/document' + +describe "REXML::Element#delete_element" do + before :each do + @root = REXML::Element.new("root") + end + + it "deletes the child element" do + node = REXML::Element.new("some_node") + @root.add_element node + @root.delete_element node + @root.elements.size.should == 0 + end + + it "deletes a child via XPath" do + @root.add_element "some_node" + @root.delete_element "some_node" + @root.elements.size.should == 0 + end + + it "deletes the child at index" do + @root.add_element "some_node" + @root.delete_element 1 + @root.elements.size.should == 0 + end + + # According to the docs this should return the deleted element + # but it won't if it's an Element. + ruby_bug do + it "deletes Element and returns it" do + node = REXML::Element.new("some_node") + @root.add_element node + del_node = @root.delete_element node + del_node.should == node + end + end + + # Note how passing the string will return the removed element + # but passing the Element as above won't. + it "deletes an element and returns it" do + node = REXML::Element.new("some_node") + @root.add_element node + del_node = @root.delete_element "some_node" + del_node.should == node + end + + it "returns nil unless element exists" do + @root.delete_element("something").should == nil + end +end diff --git a/spec/ruby/1.8/library/rexml/element/each_element_with_attribute_spec.rb b/spec/ruby/1.8/library/rexml/element/each_element_with_attribute_spec.rb new file mode 100644 index 0000000..3b6997f --- /dev/null +++ b/spec/ruby/1.8/library/rexml/element/each_element_with_attribute_spec.rb @@ -0,0 +1,34 @@ +require 'rexml/document' + +describe "REXML::Element#each_element_with_attributes" do + before :each do + @document = REXML::Element.new("people") + @father = REXML::Element.new("Person") + @father.attributes["name"] = "Joe" + @son = REXML::Element.new("Child") + @son.attributes["name"] = "Fred" + @document.root << @father + @document.root << @son + @childs = [] + end + + it "returns childs with attribute" do + @document.each_element_with_attribute("name") { |elem| @childs << elem } + @childs[0].should == @father + @childs[1].should == @son + end + + it "takes attribute value as second argument" do + @document.each_element_with_attribute("name", "Fred"){ |elem| elem.should == @son } + end + + it "takes max number of childs as third argument" do + @document.each_element_with_attribute("name", nil, 1) { |elem| @childs << elem } + @childs.size.should == 1 + @childs[0].should == @father + end + + it "takes XPath filter as fourth argument" do + @document.each_element_with_attribute("name", nil, 0, "Child"){ |elem| elem.should == @son} + end +end diff --git a/spec/ruby/1.8/library/rexml/element/each_element_with_text_spec.rb b/spec/ruby/1.8/library/rexml/element/each_element_with_text_spec.rb new file mode 100644 index 0000000..8b2a75e --- /dev/null +++ b/spec/ruby/1.8/library/rexml/element/each_element_with_text_spec.rb @@ -0,0 +1,30 @@ +require 'rexml/document' + +describe "REXML::Element#each_element_with_text" do + before :each do + @document = REXML::Element.new("people") + + @joe = REXML::Element.new("Person") + @joe.text = "Joe" + @fred = REXML::Element.new("Person") + @fred.text = "Fred" + @another = REXML::Element.new("AnotherPerson") + @another.text = "Fred" + @document.root << @joe + @document.root << @fred + @document.root << @another + @childs = [] + end + + it "returns childs with text" do + @document.each_element_with_text("Joe"){|c| c.should == @joe} + end + + it "takes max as second argument" do + @document.each_element_with_text("Fred", 1){ |c| c.should == @fred} + end + + it "takes XPath filter as third argument" do + @document.each_element_with_text("Fred", 0, "Person"){ |c| c.should == @fred} + end +end diff --git a/spec/ruby/1.8/library/rexml/element/new_spec.rb b/spec/ruby/1.8/library/rexml/element/new_spec.rb index f96ee24..4c6e9f4 100644 --- a/spec/ruby/1.8/library/rexml/element/new_spec.rb +++ b/spec/ruby/1.8/library/rexml/element/new_spec.rb @@ -3,18 +3,18 @@ require File.dirname(__FILE__) + '/../../../spec_helper' describe "REXML::Element#new" do - it "creates an element from tag name" do + it "creates element from tag name" do REXML::Element.new("foo").name.should == "foo" end - it "creates an element with the default attributes" do + it "creates element with default attributes" do e = REXML::Element.new e.name.should == REXML::Element::UNDEFINED e.context.should == nil e.parent.should == nil end - it "creates an element from another element" do + it "creates element from another element" do e = REXML::Element.new "foo" f = REXML::Element.new e e.name.should == f.name diff --git a/spec/ruby/1.8/library/rexml/shared/each_element_spec.rb b/spec/ruby/1.8/library/rexml/shared/each_element_spec.rb new file mode 100644 index 0000000..0e48211 --- /dev/null +++ b/spec/ruby/1.8/library/rexml/shared/each_element_spec.rb @@ -0,0 +1,38 @@ +require 'rexml/document' +require File.dirname(__FILE__) + '/../../../spec_helper' + +shared :each_element do |klass, cmd| + describe "REXML::#{klass}##{cmd}" do + before :each do + @e = REXML::Element.new "root" + s1 = REXML::Element.new "node1" + s2 = REXML::Element.new "node2" + s3 = REXML::Element.new "node3" + s4 = REXML::Element.new "sub_node" + @e << s1 + @e << s2 + @e << s3 + @e << s4 + end + + it "iterates through element" do + str = "" + @e.each_element { |elem| str << elem.name << " " } + str.should == "node1 node2 node3 sub_node " + end + + it "iterates through element filtering with XPath" do + str = "" + @e.each_element("/*"){ |e| str << e.name << " "} + str.should == "node1 node2 node3 sub_node " + end + end +end + +describe "REXML::Element#each_element" do + it_behaves_like(:each_element, "Element", :each_element) +end + +describe "REXML::Elements#each" do + it_behaves_like(:each_element, "Elements", :each) +end \ No newline at end of file -- 1.5.0.5