diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/library/rexml/element | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) | |
download | ruby-1d15d5f08032acf1b7bceacbb450d617ff6e0931.tar.gz |
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory.
[Misc #13792] [ruby-core:82287]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/library/rexml/element')
34 files changed, 874 insertions, 0 deletions
diff --git a/spec/ruby/library/rexml/element/add_attribute_spec.rb b/spec/ruby/library/rexml/element/add_attribute_spec.rb new file mode 100644 index 0000000000..998f5d6251 --- /dev/null +++ b/spec/ruby/library/rexml/element/add_attribute_spec.rb @@ -0,0 +1,41 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#add_attribute" do + before :each do + @person = REXML::Element.new "person" + @person.attributes["name"] = "Bill" + end + + it "adds a new attribute" do + @person.add_attribute("age", "17") + @person.attributes["age"].should == "17" + end + + it "overwrites an existing attribute" do + @person.add_attribute("name", "Bill") + @person.attributes["name"].should == "Bill" + end + + it "accepts a pair of strings" do + @person.add_attribute("male", "true") + @person.attributes["male"].should == "true" + end + + it "accepts an Attribute for key" do + attr = REXML::Attribute.new("male", "true") + @person.add_attribute attr + @person.attributes["male"].should == "true" + end + + it "ignores value if key is an Attribute" do + attr = REXML::Attribute.new("male", "true") + @person.add_attribute(attr, "false") + @person.attributes["male"].should == "true" + end + + it "returns the attribute added" do + attr = REXML::Attribute.new("name", "Tony") + @person.add_attribute(attr).should == attr + end +end diff --git a/spec/ruby/library/rexml/element/add_attributes_spec.rb b/spec/ruby/library/rexml/element/add_attributes_spec.rb new file mode 100644 index 0000000000..aa64b677ca --- /dev/null +++ b/spec/ruby/library/rexml/element/add_attributes_spec.rb @@ -0,0 +1,22 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#add_attribute" do + before :each do + @person = REXML::Element.new "person" + @person.attributes["name"] = "Bill" + end + + it "adds multiple attributes from a hash" do + @person.add_attributes({"name" => "Joe", "age" => "27"}) + @person.attributes["name"].should == "Joe" + @person.attributes["age"].should == "27" + end + + it "adds multiple attributes from an array" do + attrs = { "name" => "Joe", "age" => "27"} + @person.add_attributes attrs.to_a + @person.attributes["name"].should == "Joe" + @person.attributes["age"].should == "27" + end +end diff --git a/spec/ruby/library/rexml/element/add_element_spec.rb b/spec/ruby/library/rexml/element/add_element_spec.rb new file mode 100644 index 0000000000..b6aab3da6a --- /dev/null +++ b/spec/ruby/library/rexml/element/add_element_spec.rb @@ -0,0 +1,39 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + + +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.should == 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.should == 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.should == nil + 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.should == name.context + end +end diff --git a/spec/ruby/library/rexml/element/add_namespace_spec.rb b/spec/ruby/library/rexml/element/add_namespace_spec.rb new file mode 100644 index 0000000000..60db839f02 --- /dev/null +++ b/spec/ruby/library/rexml/element/add_namespace_spec.rb @@ -0,0 +1,24 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#add_namespace" do + before :each do + @elem = REXML::Element.new("person") + end + + it "adds a namespace to element" do + @elem.add_namespace("foo", "bar") + @elem.namespace("foo").should == "bar" + end + + it "accepts a prefix string as prefix" do + @elem.add_namespace("xmlns:foo", "bar") + @elem.namespace("foo").should == "bar" + end + + it "uses prefix as URI if uri is nil" do + @elem.add_namespace("some_uri", nil) + @elem.namespace.should == "some_uri" + end +end + diff --git a/spec/ruby/library/rexml/element/add_text_spec.rb b/spec/ruby/library/rexml/element/add_text_spec.rb new file mode 100644 index 0000000000..5d116ee6d3 --- /dev/null +++ b/spec/ruby/library/rexml/element/add_text_spec.rb @@ -0,0 +1,24 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#add_namespace" do + before :each do + @name = REXML::Element.new "Name" + end + + it "adds text to an element" do + @name.add_text "Ringo" + @name.to_s.should == "<Name>Ringo</Name>" + end + + it "accepts a Text" do + @name.add_text(REXML::Text.new("Ringo")) + @name.to_s.should == "<Name>Ringo</Name>" + end + + it "joins the new text with the old one" do + @name.add_text "Ringo" + @name.add_text " Starr" + @name.to_s.should == "<Name>Ringo Starr</Name>" + end +end diff --git a/spec/ruby/library/rexml/element/attribute_spec.rb b/spec/ruby/library/rexml/element/attribute_spec.rb new file mode 100644 index 0000000000..9c5fb7a20e --- /dev/null +++ b/spec/ruby/library/rexml/element/attribute_spec.rb @@ -0,0 +1,17 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#attribute" do + it "returns an attribute by name" do + person = REXML::Element.new "Person" + attribute = REXML::Attribute.new("drink", "coffee") + person.add_attribute(attribute) + person.attribute("drink").should == attribute + end + + it "supports attributes inside namespaces" do + e = REXML::Element.new("element") + e.add_attributes({"xmlns:ns" => "http://some_uri"}) + e.attribute("ns", "ns").to_s.should == "http://some_uri" + end +end diff --git a/spec/ruby/library/rexml/element/attributes_spec.rb b/spec/ruby/library/rexml/element/attributes_spec.rb new file mode 100644 index 0000000000..7cc5310ed1 --- /dev/null +++ b/spec/ruby/library/rexml/element/attributes_spec.rb @@ -0,0 +1,19 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#attributes" do + it "returns element's Attributes" do + p = REXML::Element.new "Person" + + name = REXML::Attribute.new("name", "John") + attrs = REXML::Attributes.new(p) + attrs.add name + + p.add_attribute name + p.attributes.should == attrs + end + + it "returns an empty hash if element has no attributes" do + REXML::Element.new("Person").attributes.should == {} + end +end diff --git a/spec/ruby/library/rexml/element/cdatas_spec.rb b/spec/ruby/library/rexml/element/cdatas_spec.rb new file mode 100644 index 0000000000..1b44abe1e7 --- /dev/null +++ b/spec/ruby/library/rexml/element/cdatas_spec.rb @@ -0,0 +1,24 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#cdatas" do + before :each do + @e = REXML::Element.new("Root") + end + + it "returns the array of children cdatas" do + c = REXML::CData.new("Primary") + d = REXML::CData.new("Secondary") + @e << c + @e << d + @e.cdatas.should == [c, d] + end + + it "freezes the returned array" do + @e.cdatas.frozen?.should == true + end + + it "returns an empty array if element has no cdata" do + @e.cdatas.should == [] + end +end diff --git a/spec/ruby/library/rexml/element/clone_spec.rb b/spec/ruby/library/rexml/element/clone_spec.rb new file mode 100644 index 0000000000..08f97e7793 --- /dev/null +++ b/spec/ruby/library/rexml/element/clone_spec.rb @@ -0,0 +1,29 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#clone" do + before :each do + @e = REXML::Element.new "a" + end + it "creates a copy of element" do + @e.clone.to_s.should == @e.to_s + end + + it "copies the attributes" do + @e.add_attribute("foo", "bar") + @e.clone.to_s.should == @e.to_s + end + + it "does not copy the text" do + @e.add_text "some text..." + @e.clone.to_s.should_not == @e + @e.clone.to_s.should == "<a/>" + end + + it "does not copy the child elements" do + b = REXML::Element.new "b" + @e << b + @e.clone.should_not == @e + @e.clone.to_s.should == "<a/>" + end +end diff --git a/spec/ruby/library/rexml/element/comments_spec.rb b/spec/ruby/library/rexml/element/comments_spec.rb new file mode 100644 index 0000000000..158b008b4f --- /dev/null +++ b/spec/ruby/library/rexml/element/comments_spec.rb @@ -0,0 +1,20 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#comments" do + before :each do + @e = REXML::Element.new "root" + @c1 = REXML::Comment.new "this is a comment" + @c2 = REXML::Comment.new "this is another comment" + @e << @c1 + @e << @c2 + end + + it "returns the array of comments" do + @e.comments.should == [@c1, @c2] + end + + it "returns a frozen object" do + @e.comments.frozen?.should == true + end +end diff --git a/spec/ruby/library/rexml/element/delete_attribute_spec.rb b/spec/ruby/library/rexml/element/delete_attribute_spec.rb new file mode 100644 index 0000000000..930db603b8 --- /dev/null +++ b/spec/ruby/library/rexml/element/delete_attribute_spec.rb @@ -0,0 +1,39 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#delete_attribute" do + before :each do + @e = REXML::Element.new("Person") + @attr = REXML::Attribute.new("name", "Sean") + @e.add_attribute(@attr) + end + + it "deletes an attribute from the element" do + @e.delete_attribute("name") + @e.attributes["name"].should be_nil + end + +# Bug was filled with a patch in Ruby's tracker #20298 + quarantine! do + it "receives an Attribute" do + @e.add_attribute(@attr) + @e.delete_attribute(@attr) + @e.attributes["name"].should be_nil + end + end + + # Docs say that it returns the removed attribute but then examples + # show it returns the element with the attribute removed. + # Also fixed in #20298 + it "returns the element with the attribute removed" do + elem = @e.delete_attribute("name") + elem.attributes.should be_empty + elem.to_s.should eql("<Person/>") + end + + it "returns nil if the attribute does not exist" do + @e.delete_attribute("name") + at = @e.delete_attribute("name") + at.should be_nil + end +end diff --git a/spec/ruby/library/rexml/element/delete_element_spec.rb b/spec/ruby/library/rexml/element/delete_element_spec.rb new file mode 100644 index 0000000000..e6e36364ba --- /dev/null +++ b/spec/ruby/library/rexml/element/delete_element_spec.rb @@ -0,0 +1,49 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +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. + 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 + + # 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/library/rexml/element/delete_namespace_spec.rb b/spec/ruby/library/rexml/element/delete_namespace_spec.rb new file mode 100644 index 0000000000..10de705076 --- /dev/null +++ b/spec/ruby/library/rexml/element/delete_namespace_spec.rb @@ -0,0 +1,25 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#delete_namespace" do + + before :each do + @doc = REXML::Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>" + end + + it "deletes a namespace from the element" do + @doc.root.delete_namespace 'foo' + @doc.root.namespace("foo").should be_nil + @doc.root.to_s.should == "<a xmlns='twiddle'/>" + end + + it "deletes default namespace when called with no args" do + @doc.root.delete_namespace + @doc.root.namespace.should be_empty + @doc.root.to_s.should == "<a xmlns:foo='bar'/>" + end + + it "returns the element" do + @doc.root.delete_namespace.should == @doc.root + end +end diff --git a/spec/ruby/library/rexml/element/document_spec.rb b/spec/ruby/library/rexml/element/document_spec.rb new file mode 100644 index 0000000000..bdf9205a85 --- /dev/null +++ b/spec/ruby/library/rexml/element/document_spec.rb @@ -0,0 +1,18 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#document" do + + it "returns the element's document" do + d = REXML::Document.new("<root><elem/></root>") + d << REXML::XMLDecl.new + d.root.document.should == d + d.root.document.to_s.should == d.to_s + end + + it "returns nil if it belongs to no document" do + REXML::Element.new("standalone").document.should be_nil + end +end + + diff --git a/spec/ruby/library/rexml/element/each_element_with_attribute_spec.rb b/spec/ruby/library/rexml/element/each_element_with_attribute_spec.rb new file mode 100644 index 0000000000..2769fd2d26 --- /dev/null +++ b/spec/ruby/library/rexml/element/each_element_with_attribute_spec.rb @@ -0,0 +1,35 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +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/library/rexml/element/each_element_with_text_spec.rb b/spec/ruby/library/rexml/element/each_element_with_text_spec.rb new file mode 100644 index 0000000000..79848c779c --- /dev/null +++ b/spec/ruby/library/rexml/element/each_element_with_text_spec.rb @@ -0,0 +1,31 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +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/library/rexml/element/get_text_spec.rb b/spec/ruby/library/rexml/element/get_text_spec.rb new file mode 100644 index 0000000000..9ae343e097 --- /dev/null +++ b/spec/ruby/library/rexml/element/get_text_spec.rb @@ -0,0 +1,18 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#get_text" do + before :each do + @doc = REXML::Document.new "<p>some text<b>this is bold!</b> more text</p>" + end + + it "returns the first text child node" do + @doc.root.get_text.value.should == "some text" + @doc.root.get_text.should be_kind_of(REXML::Text) + end + + it "returns text from an element matching path" do + @doc.root.get_text("b").value.should == "this is bold!" + @doc.root.get_text("b").should be_kind_of(REXML::Text) + end +end diff --git a/spec/ruby/library/rexml/element/has_attributes_spec.rb b/spec/ruby/library/rexml/element/has_attributes_spec.rb new file mode 100644 index 0000000000..f6c1c45e31 --- /dev/null +++ b/spec/ruby/library/rexml/element/has_attributes_spec.rb @@ -0,0 +1,17 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#has_attributes?" do + before :each do + @e = REXML::Element.new("test_elem") + end + + it "returns true when element has any attributes" do + @e.add_attribute("name", "Joe") + @e.has_attributes?.should be_true + end + + it "returns false if element has no attributes" do + @e.has_attributes?.should be_false + end +end diff --git a/spec/ruby/library/rexml/element/has_elements_spec.rb b/spec/ruby/library/rexml/element/has_elements_spec.rb new file mode 100644 index 0000000000..54898b0d19 --- /dev/null +++ b/spec/ruby/library/rexml/element/has_elements_spec.rb @@ -0,0 +1,18 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#has_elements?" do + before :each do + @e = REXML::Element.new("root") + end + + it "returns true if element has child elements" do + child = REXML::Element.new("child") + @e << child + @e.has_elements?.should be_true + end + + it "returns false if element doesn't have child elements" do + @e.has_elements?.should be_false + end +end diff --git a/spec/ruby/library/rexml/element/has_text_spec.rb b/spec/ruby/library/rexml/element/has_text_spec.rb new file mode 100644 index 0000000000..4747149ac7 --- /dev/null +++ b/spec/ruby/library/rexml/element/has_text_spec.rb @@ -0,0 +1,16 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#has_text?" do + + it "returns true if element has a Text child" do + e = REXML::Element.new("Person") + e.text = "My text" + e.has_text?.should be_true + end + + it "returns false if it has no Text childs" do + e = REXML::Element.new("Person") + e.has_text?.should be_false + end +end diff --git a/spec/ruby/library/rexml/element/inspect_spec.rb b/spec/ruby/library/rexml/element/inspect_spec.rb new file mode 100644 index 0000000000..8e93afb562 --- /dev/null +++ b/spec/ruby/library/rexml/element/inspect_spec.rb @@ -0,0 +1,27 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#inspect" do + + before :each do + @name = REXML::Element.new "name" + end + + it "returns the node as a string" do + @name.inspect.should == "<name/>" + end + + it "inserts '...' if the node has children" do + e = REXML::Element.new "last_name" + @name << e + @name.inspect.should == "<name> ... </>" + # This might make more sense but differs from MRI's default behavior + # @name.inspect.should == "<name> ... </name>" + end + + it "inserts the attributes in the string" do + @name.add_attribute "language" + @name.attributes["language"] = "english" + @name.inspect.should == "<name language='english'/>" + end +end diff --git a/spec/ruby/library/rexml/element/instructions_spec.rb b/spec/ruby/library/rexml/element/instructions_spec.rb new file mode 100644 index 0000000000..01a2374820 --- /dev/null +++ b/spec/ruby/library/rexml/element/instructions_spec.rb @@ -0,0 +1,21 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#instructions" do + before :each do + @elem = REXML::Element.new("root") + end + it "returns the Instruction children nodes" do + inst = REXML::Instruction.new("xml-stylesheet", "href='headlines.css'") + @elem << inst + @elem.instructions.first.should == inst + end + + it "returns an empty array if it has no Instruction children" do + @elem.instructions.should be_empty + end + + it "freezes the returned array" do + @elem.instructions.frozen?.should be_true + end +end diff --git a/spec/ruby/library/rexml/element/namespace_spec.rb b/spec/ruby/library/rexml/element/namespace_spec.rb new file mode 100644 index 0000000000..a0b7ba0c83 --- /dev/null +++ b/spec/ruby/library/rexml/element/namespace_spec.rb @@ -0,0 +1,27 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#namespace" do + before :each do + @doc = REXML::Document.new("<a xmlns='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>") + @elem = @doc.elements["//b"] + end + + it "returns the default namespace" do + @elem.namespace.should == "1" + end + + it "accepts a namespace prefix" do + @elem.namespace("y").should == "2" + @doc.elements["//c"].namespace("z").should == "3" + end + + it "returns an empty String if default namespace is not defined" do + e = REXML::Document.new("<a/>") + e.root.namespace.should be_empty + end + + it "returns nil if namespace is not defined" do + @elem.namespace("z").should be_nil + end +end diff --git a/spec/ruby/library/rexml/element/namespaces_spec.rb b/spec/ruby/library/rexml/element/namespaces_spec.rb new file mode 100644 index 0000000000..646503e184 --- /dev/null +++ b/spec/ruby/library/rexml/element/namespaces_spec.rb @@ -0,0 +1,32 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#namespaces" do + before :each do + doc = REXML::Document.new("<a xmlns='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>") + @elem = doc.elements["//c"] + end + + it "returns a hash of the namespaces" do + ns = {"y"=>"2", "z"=>"3", "xmlns"=>"1"} + @elem.namespaces.keys.sort.should == ns.keys.sort + @elem.namespaces.values.sort.should == ns.values.sort + end + + it "returns an empty hash if no namespaces exist" do + e = REXML::Element.new "element" + e.namespaces.kind_of?(Hash).should == true + e.namespaces.should be_empty + end + + it "uses namespace prefixes as keys" do + prefixes = ["y", "z", "xmlns"] + @elem.namespaces.keys.sort.should == prefixes.sort + end + + it "uses namespace values as the hash values" do + values = ["2", "3", "1"] + @elem.namespaces.values.sort.should == values.sort + end + +end diff --git a/spec/ruby/library/rexml/element/new_spec.rb b/spec/ruby/library/rexml/element/new_spec.rb new file mode 100644 index 0000000000..c18a33624b --- /dev/null +++ b/spec/ruby/library/rexml/element/new_spec.rb @@ -0,0 +1,35 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#new" do + + it "creates element from tag name" do + REXML::Element.new("foo").name.should == "foo" + end + + 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 element from another element" do + e = REXML::Element.new "foo" + f = REXML::Element.new e + e.name.should == f.name + e.context.should == f.context + e.parent.should == f.parent + end + + it "takes parent as second argument" do + parent = REXML::Element.new "foo" + child = REXML::Element.new "bar", parent + child.parent.should == parent + end + + it "takes context as third argument" do + context = {"some_key" => "some_value"} + REXML::Element.new("foo", nil, context).context.should == context + end +end diff --git a/spec/ruby/library/rexml/element/next_element_spec.rb b/spec/ruby/library/rexml/element/next_element_spec.rb new file mode 100644 index 0000000000..51b8438ba7 --- /dev/null +++ b/spec/ruby/library/rexml/element/next_element_spec.rb @@ -0,0 +1,19 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#next_element" do + before :each do + @a = REXML::Element.new "a" + @b = REXML::Element.new "b" + @c = REXML::Element.new "c" + @a.root << @b + @a.root << @c + end + it "returns next existing element" do + @a.elements["b"].next_element.should == @c + end + + it "returns nil on last element" do + @a.elements["c"].next_element.should == nil + end +end diff --git a/spec/ruby/library/rexml/element/node_type_spec.rb b/spec/ruby/library/rexml/element/node_type_spec.rb new file mode 100644 index 0000000000..3c9b713fde --- /dev/null +++ b/spec/ruby/library/rexml/element/node_type_spec.rb @@ -0,0 +1,8 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#node_type" do + it "returns :element" do + REXML::Element.new("MyElem").node_type.should == :element + end +end diff --git a/spec/ruby/library/rexml/element/prefixes_spec.rb b/spec/ruby/library/rexml/element/prefixes_spec.rb new file mode 100644 index 0000000000..03c46eab9e --- /dev/null +++ b/spec/ruby/library/rexml/element/prefixes_spec.rb @@ -0,0 +1,23 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#prefixes" do + before :each do + doc = REXML::Document.new("<a xmlns='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>") + @elem = doc.elements["//c"] + end + + it "returns an array of the prefixes of the namespaces" do + @elem.prefixes.should == ["y", "z"] + end + + it "does not include the default namespace" do + @elem.prefixes.include?("xmlns").should == false + end + + it "returns an empty array if no namespace was defined" do + doc = REXML::Document.new "<root><something/></root>" + root = doc.elements["//root"] + root.prefixes.should == [] + end +end diff --git a/spec/ruby/library/rexml/element/previous_element_spec.rb b/spec/ruby/library/rexml/element/previous_element_spec.rb new file mode 100644 index 0000000000..49279e8e94 --- /dev/null +++ b/spec/ruby/library/rexml/element/previous_element_spec.rb @@ -0,0 +1,20 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#previous_element" do + before :each do + @a = REXML::Element.new "a" + @b = REXML::Element.new "b" + @c = REXML::Element.new "c" + @a.root << @b + @a.root << @c + end + + it "returns previous element" do + @a.elements["c"].previous_element.should == @b + end + + it "returns nil on first element" do + @a.elements["b"].previous_element.should == nil + end +end diff --git a/spec/ruby/library/rexml/element/raw_spec.rb b/spec/ruby/library/rexml/element/raw_spec.rb new file mode 100644 index 0000000000..a872c36c8b --- /dev/null +++ b/spec/ruby/library/rexml/element/raw_spec.rb @@ -0,0 +1,24 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#raw" do + it "returns true if raw mode is set to all" do + REXML::Element.new("MyElem", nil, {raw: :all}).raw.should == true + end + + it "returns true if raw mode is set to expanded_name" do + REXML::Element.new("MyElem", nil, {raw: "MyElem"}).raw.should == true + end + + it "returns false if raw mode is not set" do + REXML::Element.new("MyElem", nil, {raw: ""}).raw.should == false + end + + it "returns false if raw is not :all or expanded_name" do + REXML::Element.new("MyElem", nil, {raw: "Something"}).raw.should == false + end + + it "returns nil if context is not set" do + REXML::Element.new("MyElem").raw.should == nil + end +end diff --git a/spec/ruby/library/rexml/element/root_spec.rb b/spec/ruby/library/rexml/element/root_spec.rb new file mode 100644 index 0000000000..24e488e701 --- /dev/null +++ b/spec/ruby/library/rexml/element/root_spec.rb @@ -0,0 +1,28 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Element#root" do + before :each do + @doc = REXML::Document.new + @root = REXML::Element.new "root" + @node = REXML::Element.new "node" + @doc << @root << @node + end + + it "returns first child on documents" do + @doc.root.should == @root + end + + it "returns self on root nodes" do + @root.root.should == @root + end + + it "returns parent's root on child nodes" do + @node.root.should == @root + end + + it "returns self on standalone nodes" do + e = REXML::Element.new "Elem" # Note that it doesn't have a parent node + e.root.should == e + end +end diff --git a/spec/ruby/library/rexml/element/text_spec.rb b/spec/ruby/library/rexml/element/text_spec.rb new file mode 100644 index 0000000000..25791d6397 --- /dev/null +++ b/spec/ruby/library/rexml/element/text_spec.rb @@ -0,0 +1,46 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#text" do + before :each do + @e = REXML::Element.new "name" + @e.text = "John" + end + + it "returns the text node of element" do + @e.text.should == "John" + end + + it "returns the text node value" do + t = REXML::Text.new "Joe" + @e.text = t + @e.text.should == "Joe" + @e.text.should_not == t + end + + it "returns nil if no text is attached" do + elem = REXML::Element.new "name" + elem.text.should == nil + end +end + +describe "REXML::Element#text=" do + before :each do + @e = REXML::Element.new "name" + @e.text = "John" + end + + it "sets the text node" do + @e.to_s.should == "<name>John</name>" + end + + it "replaces existing text" do + @e.text = "Joe" + @e.to_s.should == "<name>Joe</name>" + end + + it "receives nil as an argument" do + @e.text = nil + @e.to_s.should == "<name/>" + end +end diff --git a/spec/ruby/library/rexml/element/texts_spec.rb b/spec/ruby/library/rexml/element/texts_spec.rb new file mode 100644 index 0000000000..de3a818866 --- /dev/null +++ b/spec/ruby/library/rexml/element/texts_spec.rb @@ -0,0 +1,16 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#texts" do + + it "returns an array of the Text children" do + e = REXML::Element.new("root") + e.add_text "First" + e.add_text "Second" + e.texts.should == ["FirstSecond"] + end + + it "returns an empty array if it has no Text children" do + REXML::Element.new("root").texts.should == [] + end +end diff --git a/spec/ruby/library/rexml/element/whitespace_spec.rb b/spec/ruby/library/rexml/element/whitespace_spec.rb new file mode 100644 index 0000000000..ea9ff42c03 --- /dev/null +++ b/spec/ruby/library/rexml/element/whitespace_spec.rb @@ -0,0 +1,23 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Element#whitespace" do + it "returns true if whitespace is respected in the element" do + e = REXML::Element.new("root") + e.whitespace.should be_true + + e = REXML::Element.new("root", nil, respect_whitespace: :all) + e.whitespace.should be_true + + e = REXML::Element.new("root", nil, respect_whitespace: ["root"]) + e.whitespace.should be_true + end + + it "returns false if whitespace is ignored inside element" do + e = REXML::Element.new("root", nil, compress_whitespace: :all) + e.whitespace.should be_false + + e = REXML::Element.new("root", nil, compress_whitespace: ["root"]) + e.whitespace.should be_false + end +end |