require "cases/helper" require 'models/contact' require 'models/post' require 'models/author' require 'models/tagging' require 'models/comment' require 'models/company_in_module' class XmlSerializationTest < ActiveRecord::TestCase def test_should_serialize_default_root @xml = Contact.new.to_xml assert_match %r{^}, @xml assert_match %r{$}, @xml end def test_should_serialize_default_root_with_namespace @xml = Contact.new.to_xml :namespace=>"http://xml.rubyonrails.org/contact" assert_match %r{^}, @xml assert_match %r{$}, @xml end def test_should_serialize_custom_root @xml = Contact.new.to_xml :root => 'xml_contact' assert_match %r{^}, @xml assert_match %r{$}, @xml end def test_should_allow_undasherized_tags @xml = Contact.new.to_xml :root => 'xml_contact', :dasherize => false assert_match %r{^}, @xml assert_match %r{$}, @xml assert_match %r{ 'xml_contact', :camelize => true assert_match %r{^}, @xml assert_match %r{$}, @xml assert_match %r{ 25).to_xml :skip_types => true assert %r{25}.match(@xml) end def test_should_include_yielded_additions @xml = Contact.new.to_xml do |xml| xml.creator "David" end assert_match %r{David}, @xml end end class DefaultXmlSerializationTest < ActiveRecord::TestCase def setup @xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => 'ruby' }).to_xml end def test_should_serialize_string assert_match %r{aaron stack}, @xml end def test_should_serialize_integer assert_match %r{25}, @xml end def test_should_serialize_binary assert_match %r{YmluYXJ5ZGF0YQ==\n}, @xml assert_match %r{2006-08-01T00:00:00Z}, @xml end def test_should_serialize_boolean assert_match %r{false}, @xml end def test_should_serialize_yaml assert_match %r{---\s?\n:gem: ruby\n}, @xml end end class NilXmlSerializationTest < ActiveRecord::TestCase def setup @xml = Contact.new.to_xml(:root => 'xml_contact') end def test_should_serialize_string assert_match %r{}, @xml end def test_should_serialize_integer assert %r{}.match(@xml) attributes = $1 assert_match %r{nil="true"}, attributes assert_match %r{type="integer"}, attributes end def test_should_serialize_binary assert %r{}.match(@xml) attributes = $1 assert_match %r{type="binary"}, attributes assert_match %r{encoding="base64"}, attributes assert_match %r{nil="true"}, attributes end def test_should_serialize_datetime assert %r{}.match(@xml) attributes = $1 assert_match %r{nil="true"}, attributes assert_match %r{type="datetime"}, attributes end def test_should_serialize_boolean assert %r{}.match(@xml) attributes = $1 assert_match %r{type="boolean"}, attributes assert_match %r{nil="true"}, attributes end def test_should_serialize_yaml assert %r{}.match(@xml) attributes = $1 assert_match %r{type="yaml"}, attributes assert_match %r{nil="true"}, attributes end end class DatabaseConnectedXmlSerializationTest < ActiveRecord::TestCase fixtures :authors, :posts, :projects # to_xml used to mess with the hash the user provided which # caused the builder to be reused. This meant the document kept # getting appended to. def test_modules projects = MyApplication::Business::Project.all xml = projects.to_xml root = projects.first.class.to_s.underscore.pluralize.tr('/','_').dasherize assert_match "<#{root} type=\"array\">", xml assert_match "", xml end def test_passing_hash_shouldnt_reuse_builder options = {:include=>:posts} david = authors(:david) first_xml_size = david.to_xml(options).size second_xml_size = david.to_xml(options).size assert_equal first_xml_size, second_xml_size end def test_include_uses_association_name xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0 assert_match %r{}, xml assert_match %r{}, xml assert_match %r{}, xml end def test_included_associations_should_skip_types xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0, :skip_types => true assert_match %r{}, xml assert_match %r{}, xml assert_match %r{}, xml end def test_methods_are_called_on_object xml = authors(:david).to_xml :methods => :label, :indent => 0 assert_match %r{}, xml end def test_should_not_call_methods_on_associations_that_dont_respond xml = authors(:david).to_xml :include=>:hello_posts, :methods => :label, :indent => 2 assert !authors(:david).hello_posts.first.respond_to?(:label) assert_match %r{^ }, xml assert_no_match %r{^