spec/public/to_xml_spec.rb in dm-serializer-0.9.11 vs spec/public/to_xml_spec.rb in dm-serializer-0.10.0

- old
+ new

@@ -1,7 +1,6 @@ -require 'pathname' -require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper' +require 'spec_helper' { 'REXML' => nil, 'LibXML' => 'libxml', 'Nokogiri' => 'nokogiri' @@ -25,33 +24,39 @@ end protected def deserialize(result) - doc = REXML::Document.new(result) - root = doc.elements[1] - if root.attributes["type"] == "array" - root.elements.collect do |element| - a = {} - element.elements.each do |v| - a.update(v.name => cast(v.text, v.attributes["type"])) + f = lambda do |element| + case element.attributes["type"] + when "hash" + element.elements.to_a.inject({}) do |a, e| + a.update(e.name => f[e]) end - a + when "array" + element.elements.collect do |e| + f[e] + end + else + if element.elements.empty? + cast(element.text, element.attributes["type"]) + else + element.elements.to_a.inject({}) do |a, e| + a.update(e.name => f[e]) + end + end end - else - a = {} - root.elements.each do |v| - a.update(v.name => cast(v.text, v.attributes["type"])) - end - a end + + doc = REXML::Document.new(result) + f[doc.elements[1]] end def cast(value, type) boolean_conversions = {"true" => true, "false" => false} value = boolean_conversions[value] if boolean_conversions.has_key?(value) - value = value.to_i if value && type == "integer" + value = value.to_i if value && ["integer", "datamapper::types::serial"].include?(type) value end end.new DataMapper::Serialize::XMLSerializers.instance_eval { remove_const('SERIALIZER') } DataMapper::Serialize::XMLSerializers::SERIALIZER = DataMapper::Serialize::XMLSerializers::const_get(lib) @@ -60,11 +65,11 @@ it_should_behave_like "A serialization method" it "should not include the XML prologue, so that the result can be embedded in other XML documents" do planet = Planet.new xml = planet.to_xml(:element_name => "aplanet") - xml.should_not =~ /\A<\?xml/ + xml.should_not match(/\A<?xml/) end describe ':element_name option for Resource' do it 'should be used as the root node name by #to_xml' do planet = Planet.new @@ -79,27 +84,30 @@ end end describe ':collection_element_name for Collection' do before(:each) do - query = DataMapper::Query.new(DataMapper::repository(:default), QuanTum::Cat) - @collection = DataMapper::Collection.new(query) {} + @model = QuanTum::Cat + @query = DataMapper::Query.new(DataMapper::repository(:default), @model) + @collection = DataMapper::Collection.new(@query) end it 'when not specified the class name tableized and with slashes replaced with dashes should be used as the root node name' do - xml = @collection.to_xml + xml = DataMapper::Collection.new(@query).to_xml REXML::Document.new(xml).elements[1].name.should == "quan_tum-cats" end it 'should be used as the root node name by #to_xml' do - @collection.load([1]) + resources = @model.load([ { 'id' => 1 } ], @query) + @collection = DataMapper::Collection.new(@query, resources) xml = @collection.to_xml(:collection_element_name => "somanycats") REXML::Document.new(xml).elements[1].name.should == "somanycats" end it 'should respect :element_name for collection elements' do - @collection.load([1]) + resources = @model.load([ { 'id' => 1 } ], @query) + @collection = DataMapper::Collection.new(@query, resources) xml = @collection.to_xml(:collection_element_name => "somanycats", :element_name => 'cat') REXML::Document.new(xml).elements[1].elements[1].name.should == "cat" end end