diff options
Diffstat (limited to 'spec/ruby/library/rexml/document')
-rw-r--r-- | spec/ruby/library/rexml/document/add_element_spec.rb | 31 | ||||
-rw-r--r-- | spec/ruby/library/rexml/document/add_spec.rb | 57 | ||||
-rw-r--r-- | spec/ruby/library/rexml/document/clone_spec.rb | 20 | ||||
-rw-r--r-- | spec/ruby/library/rexml/document/doctype_spec.rb | 15 | ||||
-rw-r--r-- | spec/ruby/library/rexml/document/encoding_spec.rb | 22 | ||||
-rw-r--r-- | spec/ruby/library/rexml/document/expanded_name_spec.rb | 16 | ||||
-rw-r--r-- | spec/ruby/library/rexml/document/new_spec.rb | 36 | ||||
-rw-r--r-- | spec/ruby/library/rexml/document/node_type_spec.rb | 8 | ||||
-rw-r--r-- | spec/ruby/library/rexml/document/root_spec.rb | 12 | ||||
-rw-r--r-- | spec/ruby/library/rexml/document/stand_alone_spec.rb | 19 | ||||
-rw-r--r-- | spec/ruby/library/rexml/document/version_spec.rb | 14 | ||||
-rw-r--r-- | spec/ruby/library/rexml/document/write_spec.rb | 35 | ||||
-rw-r--r-- | spec/ruby/library/rexml/document/xml_decl_spec.rb | 15 |
13 files changed, 300 insertions, 0 deletions
diff --git a/spec/ruby/library/rexml/document/add_element_spec.rb b/spec/ruby/library/rexml/document/add_element_spec.rb new file mode 100644 index 0000000000..03c95727e2 --- /dev/null +++ b/spec/ruby/library/rexml/document/add_element_spec.rb @@ -0,0 +1,31 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Document#add_element" do + it "adds arg1 with attributes arg2 as root node" do + d = REXML::Document.new + e = REXML::Element.new("root") + d.add_element e + d.root.should == e + end + + it "sets arg2 as arg1's attributes" do + d = REXML::Document.new + e = REXML::Element.new("root") + attr = {"foo" => "bar"} + d.add_element(e,attr) + d.root.attributes["foo"].should == attr["foo"] + end + + it "accepts a node name as arg1 and adds it as root" do + d = REXML::Document.new + d.add_element "foo" + d.root.name.should == "foo" + end + + it "sets arg1's context to the root's context" do + d = REXML::Document.new("", {"foo" => "bar"}) + d.add_element "foo" + d.root.context.should == d.context + end +end diff --git a/spec/ruby/library/rexml/document/add_spec.rb b/spec/ruby/library/rexml/document/add_spec.rb new file mode 100644 index 0000000000..491c28259b --- /dev/null +++ b/spec/ruby/library/rexml/document/add_spec.rb @@ -0,0 +1,57 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +# This spec defines Document#add and Document#<< + +describe :rexml_document_add, shared: true do + before :each do + @doc = REXML::Document.new("<root/>") + @decl = REXML::XMLDecl.new("1.0") + end + + it "sets document's XML declaration" do + @doc.send(@method, @decl) + @doc.xml_decl.should == @decl + end + + it "inserts XML declaration as first node" do + @doc.send(@method, @decl) + @doc.children[0].version.should == "1.0" + end + + it "overwrites existing XML declaration" do + @doc.send(@method, @decl) + @doc.send(@method, REXML::XMLDecl.new("2.0")) + @doc.xml_decl.version.should == "2.0" + end + + it "sets document DocType" do + @doc.send(@method, REXML::DocType.new("transitional")) + @doc.doctype.name.should == "transitional" + end + + it "overwrites existing DocType" do + @doc.send(@method, REXML::DocType.new("transitional")) + @doc.send(@method, REXML::DocType.new("strict")) + @doc.doctype.name.should == "strict" + end + + it "adds root node unless it exists" do + d = REXML::Document.new("") + elem = REXML::Element.new "root" + d.send(@method, elem) + d.root.should == elem + end + + it "refuses to add second root" do + lambda { @doc.send(@method, REXML::Element.new("foo")) }.should raise_error(RuntimeError) + end +end + +describe "REXML::Document#add" do + it_behaves_like(:rexml_document_add, :add) +end + +describe "REXML::Document#<<" do + it_behaves_like(:rexml_document_add, :<<) +end diff --git a/spec/ruby/library/rexml/document/clone_spec.rb b/spec/ruby/library/rexml/document/clone_spec.rb new file mode 100644 index 0000000000..cf333bf4df --- /dev/null +++ b/spec/ruby/library/rexml/document/clone_spec.rb @@ -0,0 +1,20 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +# According to the MRI documentation (http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/index.html), +# clone's behavior "should be obvious". Apparently "obvious" means cloning +# only the attributes and the context of the document, not its children. +describe "REXML::Document#clone" do + it "clones document attributes" do + d = REXML::Document.new("foo") + d.attributes["foo"] = "bar" + e = d.clone + e.attributes.should == d.attributes + end + + it "clones document context" do + d = REXML::Document.new("foo", {"foo" => "bar"}) + e = d.clone + e.context.should == d.context + end +end diff --git a/spec/ruby/library/rexml/document/doctype_spec.rb b/spec/ruby/library/rexml/document/doctype_spec.rb new file mode 100644 index 0000000000..5f277f6ad6 --- /dev/null +++ b/spec/ruby/library/rexml/document/doctype_spec.rb @@ -0,0 +1,15 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Document#doctype" do + it "returns the doctype" do + d = REXML::Document.new + dt = REXML::DocType.new("foo") + d.add dt + d.doctype.should == dt + end + + it "returns nil if there's no doctype" do + REXML::Document.new.doctype.should == nil + end +end diff --git a/spec/ruby/library/rexml/document/encoding_spec.rb b/spec/ruby/library/rexml/document/encoding_spec.rb new file mode 100644 index 0000000000..d20be0f7b7 --- /dev/null +++ b/spec/ruby/library/rexml/document/encoding_spec.rb @@ -0,0 +1,22 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Document#encoding" do + before :each do + @doc = REXML::Document.new + end + + it "returns encoding from XML declaration" do + @doc.add REXML::XMLDecl.new(nil, "UTF-16", nil) + @doc.encoding.should == "UTF-16" + end + + it "returns encoding from XML declaration (for UTF-16 as well)" do + @doc.add REXML::XMLDecl.new("1.0", "UTF-8", nil) + @doc.encoding.should == "UTF-8" + end + + it "uses UTF-8 as default encoding" do + @doc.encoding.should == "UTF-8" + end +end diff --git a/spec/ruby/library/rexml/document/expanded_name_spec.rb b/spec/ruby/library/rexml/document/expanded_name_spec.rb new file mode 100644 index 0000000000..e18fd95c14 --- /dev/null +++ b/spec/ruby/library/rexml/document/expanded_name_spec.rb @@ -0,0 +1,16 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe :document_expanded_name, shared: true do + it "returns an empty string for root" do # root nodes have no expanded name + REXML::Document.new.send(@method).should == "" + end +end + +describe "REXML::Document#expanded_name" do + it_behaves_like(:document_expanded_name, :expanded_name) +end + +describe "REXML::Document#name" do + it_behaves_like(:document_expanded_name, :name) +end diff --git a/spec/ruby/library/rexml/document/new_spec.rb b/spec/ruby/library/rexml/document/new_spec.rb new file mode 100644 index 0000000000..0caa3fd583 --- /dev/null +++ b/spec/ruby/library/rexml/document/new_spec.rb @@ -0,0 +1,36 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Document#new" do + + it "initializes context of {} unless specified" do + d = REXML::Document.new("<foo />") + d.context.should == {} + end + + it "has empty attributes if source is nil" do + d = REXML::Document.new(nil) + d.elements.should be_empty + end + + it "can use other document context" do + s = REXML::Document.new("") + d = REXML::Document.new(s) + d.context.should == s.context + end + + it "clones source attributes" do + s = REXML::Document.new("<root />") + s.attributes["some_attr"] = "some_val" + d = REXML::Document.new(s) + d.attributes.should == s.attributes + end + + it "raises an error if source is not a Document, String or IO" do + lambda {REXML::Document.new(3)}.should raise_error(RuntimeError) + end + + it "does not perform XML validation" do + REXML::Document.new("Invalid document").should be_kind_of(REXML::Document) + end +end diff --git a/spec/ruby/library/rexml/document/node_type_spec.rb b/spec/ruby/library/rexml/document/node_type_spec.rb new file mode 100644 index 0000000000..b18b1a0dfe --- /dev/null +++ b/spec/ruby/library/rexml/document/node_type_spec.rb @@ -0,0 +1,8 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Document#node_type" do + it "returns :document" do + REXML::Document.new.node_type.should == :document + end +end diff --git a/spec/ruby/library/rexml/document/root_spec.rb b/spec/ruby/library/rexml/document/root_spec.rb new file mode 100644 index 0000000000..55be68da6f --- /dev/null +++ b/spec/ruby/library/rexml/document/root_spec.rb @@ -0,0 +1,12 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Document#root" do + it "returns document root tag name" do + REXML::Document.new("<foo/>").root.name.should == "foo" + end + + it "returns nil if there is not root" do + REXML::Document.new.root.should == nil + end +end diff --git a/spec/ruby/library/rexml/document/stand_alone_spec.rb b/spec/ruby/library/rexml/document/stand_alone_spec.rb new file mode 100644 index 0000000000..250c604dad --- /dev/null +++ b/spec/ruby/library/rexml/document/stand_alone_spec.rb @@ -0,0 +1,19 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Document#stand_alone?" do + it "returns the XMLDecl standalone value" do + d = REXML::Document.new + decl = REXML::XMLDecl.new("1.0", "UTF-8", "yes") + d.add decl + d.stand_alone?.should == "yes" + end + + # According to the docs this should return the default XMLDecl but that + # will carry some more problems when printing the document. Currently, it + # returns nil. See http://www.ruby-forum.com/topic/146812#650061 + it "returns the default value when no XML declaration present" do + REXML::Document.new.stand_alone?.should == nil + end + +end diff --git a/spec/ruby/library/rexml/document/version_spec.rb b/spec/ruby/library/rexml/document/version_spec.rb new file mode 100644 index 0000000000..ca979dbf34 --- /dev/null +++ b/spec/ruby/library/rexml/document/version_spec.rb @@ -0,0 +1,14 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Document#version" do + it "returns XML version from declaration" do + d = REXML::Document.new + d.add REXML::XMLDecl.new("1.1") + d.version.should == "1.1" + end + + it "returns the default version when declaration is not present" do + REXML::Document.new.version.should == REXML::XMLDecl::DEFAULT_VERSION + end +end diff --git a/spec/ruby/library/rexml/document/write_spec.rb b/spec/ruby/library/rexml/document/write_spec.rb new file mode 100644 index 0000000000..f2a7e2c200 --- /dev/null +++ b/spec/ruby/library/rexml/document/write_spec.rb @@ -0,0 +1,35 @@ +require 'rexml/document' +require 'rexml/formatters/transitive' +require File.expand_path('../../../../spec_helper', __FILE__) + +# Maybe this can be cleaned +describe "REXML::Document#write" do + before :each do + @d = REXML::Document.new + city = REXML::Element.new "Springfield" + street = REXML::Element.new "EvergreenTerrace" + address = REXML::Element.new "House742" + @d << city << street << address + @str = "" + end + + it "returns document source as string" do + @d.write(@str) + @str.should == "<Springfield><EvergreenTerrace><House742/></EvergreenTerrace></Springfield>" + end + + it "returns document indented" do + @d.write(@str, 2) + @str.should =~ /\s*<Springfield>\s*<EvergreenTerrace>\s*<House742\/>\s*<\/EvergreenTerrace>\s*<\/Springfield>/ + end + + it "returns document with transitive support" do + @d.write(@str, 2, true) + @str.should =~ /\s*<Springfield\s*><EvergreenTerrace\s*><House742\s*\/><\/EvergreenTerrace\s*><\/Springfield\s*>/ + end + + it "returns document with support for IE" do + @d.write(@str, -1, false, true) + @str.should == "<Springfield><EvergreenTerrace><House742 /></EvergreenTerrace></Springfield>" + end +end diff --git a/spec/ruby/library/rexml/document/xml_decl_spec.rb b/spec/ruby/library/rexml/document/xml_decl_spec.rb new file mode 100644 index 0000000000..6a5bf07c0b --- /dev/null +++ b/spec/ruby/library/rexml/document/xml_decl_spec.rb @@ -0,0 +1,15 @@ +require 'rexml/document' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "REXML::Document#xml_decl" do + it "returns XML declaration of the document" do + d = REXML::Document.new + decl = REXML::XMLDecl.new("1.0", "UTF-16", "yes") + d.add decl + d.xml_decl.should == decl + end + + it "returns default XML declaration unless present" do + REXML::Document.new.xml_decl.should == REXML::XMLDecl.new + end +end |