diff options
Diffstat (limited to 'spec/rubyspec/library/rexml/attributes')
18 files changed, 322 insertions, 0 deletions
diff --git a/spec/rubyspec/library/rexml/attributes/add_spec.rb b/spec/rubyspec/library/rexml/attributes/add_spec.rb new file mode 100644 index 0000000000..72e3c4c823 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/add_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/add', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#add" do + it_behaves_like :rexml_attribute_add, :add +end diff --git a/spec/rubyspec/library/rexml/attributes/append_spec.rb b/spec/rubyspec/library/rexml/attributes/append_spec.rb new file mode 100644 index 0000000000..89f3fc3e81 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/append_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/add', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#<<" do + it_behaves_like :rexml_attribute_add, :<< +end diff --git a/spec/rubyspec/library/rexml/attributes/delete_all_spec.rb b/spec/rubyspec/library/rexml/attributes/delete_all_spec.rb new file mode 100644 index 0000000000..f11f0d66a3 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/delete_all_spec.rb @@ -0,0 +1,31 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#delete_all" do + before :each do + @e = REXML::Element.new("root") + end + + it "deletes all attributes that match name" do + uri = REXML::Attribute.new("uri", "http://something") + @e.attributes << uri + @e.attributes.delete_all("uri") + @e.attributes.should be_empty + @e.attributes["uri"].should == nil + end + + it "deletes all attributes that match name with a namespace" do + ns_uri = REXML::Attribute.new("xmlns:uri", "http://something_here_too") + @e.attributes << ns_uri + @e.attributes.delete_all("xmlns:uri") + @e.attributes.should be_empty + @e.attributes["xmlns:uri"].should == nil + end + + it "returns the removed attribute" do + uri = REXML::Attribute.new("uri", "http://something_here_too") + @e.attributes << uri + attrs = @e.attributes.delete_all("uri") + attrs.first.should == uri + end +end diff --git a/spec/rubyspec/library/rexml/attributes/delete_spec.rb b/spec/rubyspec/library/rexml/attributes/delete_spec.rb new file mode 100644 index 0000000000..1c02e5c03b --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/delete_spec.rb @@ -0,0 +1,27 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#delete" do + before :each do + @e = REXML::Element.new("root") + @name = REXML::Attribute.new("name", "Pepe") + end + + it "takes an attribute name and deletes the attribute" do + @e.attributes.delete("name") + @e.attributes["name"].should be_nil + @e.attributes.should be_empty + end + + it "takes an Attribute and deletes it" do + @e.attributes.delete(@name) + @e.attributes["name"].should be_nil + @e.attributes.should be_empty + end + + it "returns the element with the attribute removed" do + ret_val = @e.attributes.delete(@name) + ret_val.should == @e + ret_val.attributes.should be_empty + end +end diff --git a/spec/rubyspec/library/rexml/attributes/each_attribute_spec.rb b/spec/rubyspec/library/rexml/attributes/each_attribute_spec.rb new file mode 100644 index 0000000000..cd1649be21 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/each_attribute_spec.rb @@ -0,0 +1,25 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#each_attribute" do + it "iterates over the attributes yielding actual Attribute objects" do + e = REXML::Element.new("root") + name = REXML::Attribute.new("name", "Joe") + ns_uri = REXML::Attribute.new("xmlns:ns", "http://some_uri") + e.add_attribute name + e.add_attribute ns_uri + + attributes = [] + + e.attributes.each_attribute do |attr| + attributes << attr + end + + attributes = attributes.sort_by {|a| a.name } + attributes.first.should == name + attributes.last.should == ns_uri + end +end + + + diff --git a/spec/rubyspec/library/rexml/attributes/each_spec.rb b/spec/rubyspec/library/rexml/attributes/each_spec.rb new file mode 100644 index 0000000000..f49bc75c0d --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/each_spec.rb @@ -0,0 +1,25 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#each" do + before :each do + @e = REXML::Element.new("root") + @name = REXML::Attribute.new("name", "Joe") + @ns_uri = REXML::Attribute.new("xmlns:ns", "http://some_uri") + @e.add_attribute @name + @e.add_attribute @ns_uri + end + + it "iterates over the attributes yielding expanded-name/value" do + attributes = [] + @e.attributes.each do |attr| + attr.should be_kind_of(Array) + attributes << attr + end + attributes = attributes.sort_by {|a| a.first } + attributes.first.should == ["name", "Joe"] + attributes.last.should == ["xmlns:ns", "http://some_uri"] + end +end + + diff --git a/spec/rubyspec/library/rexml/attributes/element_reference_spec.rb b/spec/rubyspec/library/rexml/attributes/element_reference_spec.rb new file mode 100644 index 0000000000..0e85ecafe8 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/element_reference_spec.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#[]" do + before :each do + @e = REXML::Element.new("root") + @lang = REXML::Attribute.new("language", "english") + @e.attributes << @lang + end + + it "returns the value of an attribute" do + @e.attributes["language"].should == "english" + end + + it "returns nil if the attribute does not exist" do + @e.attributes["chunky bacon"].should == nil + end +end + diff --git a/spec/rubyspec/library/rexml/attributes/element_set_spec.rb b/spec/rubyspec/library/rexml/attributes/element_set_spec.rb new file mode 100644 index 0000000000..659d259df6 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/element_set_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#[]=" do + before :each do + @e = REXML::Element.new("song") + @name = REXML::Attribute.new("name", "Holy Smoke!") + @e.attributes << @name + end + + it "sets an attribute" do + @e.attributes["author"] = "_why's foxes" + @e.attributes["author"].should == "_why's foxes" + end + + it "overwrites an existing attribute" do + @e.attributes["name"] = "Chunky Bacon" + @e.attributes["name"].should == "Chunky Bacon" + end + + it "deletes an attribute is value is nil" do + @e.attributes["name"] = nil + @e.attributes.length.should == 0 + end +end + diff --git a/spec/rubyspec/library/rexml/attributes/get_attribute_ns_spec.rb b/spec/rubyspec/library/rexml/attributes/get_attribute_ns_spec.rb new file mode 100644 index 0000000000..f4aeb76378 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/get_attribute_ns_spec.rb @@ -0,0 +1,14 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#get_attribute_ns" do + it "returns an attribute by name and namespace" do + e = REXML::Element.new("root") + attr = REXML::Attribute.new("xmlns:ns", "http://some_url") + e.attributes << attr + attr.prefix.should == "xmlns" + # This might be a bug in Attribute, commenting until those specs + # are ready + # e.attributes.get_attribute_ns(attr.prefix, "name").should == "http://some_url" + end +end diff --git a/spec/rubyspec/library/rexml/attributes/get_attribute_spec.rb b/spec/rubyspec/library/rexml/attributes/get_attribute_spec.rb new file mode 100644 index 0000000000..b7d83f5944 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/get_attribute_spec.rb @@ -0,0 +1,29 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#get_attribute" do + before :each do + @e = REXML::Element.new("root") + @name = REXML::Attribute.new("name", "Dave") + @e.attributes << @name + end + + it "fetches an attributes" do + @e.attributes.get_attribute("name").should == @name + end + + it "fetches an namespaced attribute" do + ns_name = REXML::Attribute.new("im:name", "Murray") + @e.attributes << ns_name + @e.attributes.get_attribute("name").should == @name + @e.attributes.get_attribute("im:name").should == ns_name + end + + it "returns an Attribute" do + @e.attributes.get_attribute("name").should be_kind_of(REXML::Attribute) + end + + it "returns nil if it attribute does not exist" do + @e.attributes.get_attribute("fake").should be_nil + end +end diff --git a/spec/rubyspec/library/rexml/attributes/initialize_spec.rb b/spec/rubyspec/library/rexml/attributes/initialize_spec.rb new file mode 100644 index 0000000000..2bf59b1f76 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/initialize_spec.rb @@ -0,0 +1,18 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#initialize" do + it "is auto initialized by Element" do + e = REXML::Element.new "root" + e.attributes.should be_kind_of(REXML::Attributes) + + e.attributes << REXML::Attribute.new("name", "Paul") + e.attributes["name"].should == "Paul" + end + + it "receives a parent node" do + e = REXML::Element.new "root" + e.attributes << REXML::Attribute.new("name", "Vic") + e.attributes["name"].should == "Vic" + end +end diff --git a/spec/rubyspec/library/rexml/attributes/length_spec.rb b/spec/rubyspec/library/rexml/attributes/length_spec.rb new file mode 100644 index 0000000000..cd68461e34 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/length_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/length', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#length" do + it_behaves_like :rexml_attribute_length, :length +end diff --git a/spec/rubyspec/library/rexml/attributes/namespaces_spec.rb b/spec/rubyspec/library/rexml/attributes/namespaces_spec.rb new file mode 100644 index 0000000000..41486d0316 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/namespaces_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#namespaces" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/rubyspec/library/rexml/attributes/prefixes_spec.rb b/spec/rubyspec/library/rexml/attributes/prefixes_spec.rb new file mode 100644 index 0000000000..9eca67b5ff --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/prefixes_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#prefixes" do + before :each do + @e = REXML::Element.new("root") + a1 = REXML::Attribute.new("xmlns:a", "bar") + a2 = REXML::Attribute.new("xmlns:b", "bla") + a3 = REXML::Attribute.new("xmlns:c", "baz") + @e.attributes << a1 + @e.attributes << a2 + @e.attributes << a3 + + @e.attributes << REXML::Attribute.new("xmlns", "foo") + end + + it "returns an array with the prefixes of each attribute" do + @e.attributes.prefixes.sort.should == ["a", "b", "c"] + end + + it "does not include the default namespace" do + @e.attributes.prefixes.include?("xmlns").should == false + end +end diff --git a/spec/rubyspec/library/rexml/attributes/shared/add.rb b/spec/rubyspec/library/rexml/attributes/shared/add.rb new file mode 100644 index 0000000000..872f149f45 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/shared/add.rb @@ -0,0 +1,17 @@ +describe :rexml_attribute_add, shared: true do + before :each do + @e = REXML::Element.new("root") + @attr = REXML::Attributes.new(@e) + @name = REXML::Attribute.new("name", "Joe") + end + + it "adds an attribute" do + @attr.send(@method, @name) + @attr["name"].should == "Joe" + end + + it "replaces an existing attribute" do + @attr.send(@method, REXML::Attribute.new("name", "Bruce")) + @attr["name"].should == "Bruce" + end +end diff --git a/spec/rubyspec/library/rexml/attributes/shared/length.rb b/spec/rubyspec/library/rexml/attributes/shared/length.rb new file mode 100644 index 0000000000..94681882a6 --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/shared/length.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe :rexml_attribute_length, shared: true do + it "returns the number of attributes" do + e = REXML::Element.new("root") + e.attributes.send(@method).should == 0 + + e.attributes << REXML::Attribute.new("name", "John") + e.attributes << REXML::Attribute.new("another_name", "Leo") + e.attributes.send(@method).should == 2 + end +end diff --git a/spec/rubyspec/library/rexml/attributes/size_spec.rb b/spec/rubyspec/library/rexml/attributes/size_spec.rb new file mode 100644 index 0000000000..761fcc1d5b --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/size_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/length', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#size" do + it_behaves_like :rexml_attribute_length, :size +end diff --git a/spec/rubyspec/library/rexml/attributes/to_a_spec.rb b/spec/rubyspec/library/rexml/attributes/to_a_spec.rb new file mode 100644 index 0000000000..a3de48cf1c --- /dev/null +++ b/spec/rubyspec/library/rexml/attributes/to_a_spec.rb @@ -0,0 +1,20 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'rexml/document' + +describe "REXML::Attributes#to_a" do + it "returns an array with the attributes" do + e = REXML::Element.new("root") + name = REXML::Attribute.new("name", "Dave") + last = REXML::Attribute.new("last_name", "Murray") + + e.attributes << name + e.attributes << last + + e.attributes.to_a.sort{|a,b|a.to_s<=>b.to_s}.should == [name, last] + end + + it "returns an empty array if it has no attributes" do + REXML::Element.new("root").attributes.to_a.should == [] + end +end + |