Skip to content

Commit

Permalink
test cases for record.to_xml [#458 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
Neeraj Singh authored and josevalim committed Apr 30, 2010
1 parent 34908e4 commit 883f27a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
Expand Up @@ -95,6 +95,14 @@ def setup
assert_match %r{<awesome type=\"boolean\">false</awesome>}, @contact.to_xml
end

test "should serialize array" do
assert_match %r{<social type=\"array\">\s*<social>twitter</social>\s*<social>github</social>\s*</social>}, @contact.to_xml(:methods => :social)
end

test "should serialize hash" do
assert_match %r{<network>\s*<git type=\"symbol\">github</git>\s*</network>}, @contact.to_xml(:methods => :network)
end

test "should serialize yaml" do
assert_match %r{<preferences type=\"yaml\">--- !ruby/object:OpenStruct \ntable:\s*:name: John\n</preferences>}, @contact.to_xml
end
Expand Down
8 changes: 8 additions & 0 deletions activemodel/test/models/contact.rb
Expand Up @@ -3,6 +3,14 @@ class Contact

attr_accessor :id, :name, :age, :created_at, :awesome, :preferences

def social
%w(twitter github)
end

def network
{:git => :github}
end

def initialize(options = {})
options.each { |name, value| send("#{name}=", value) }
end
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/xml_serialization_test.rb
Expand Up @@ -234,4 +234,12 @@ def test_should_has_many_array_elements_should_include_type_when_different_from_
assert types.include?('StiPost')
end

def test_should_produce_xml_for_methods_returning_array
xml = authors(:david).to_xml(:methods => :social)
array = Hash.from_xml(xml)['author']['social']
assert_equal 2, array.size
assert array.include? 'twitter'
assert array.include? 'github'
end

end
4 changes: 4 additions & 0 deletions activerecord/test/models/author.rb
Expand Up @@ -104,6 +104,10 @@ def label
"#{id}-#{name}"
end

def social
%w(twitter github)
end

private
def log_before_adding(object)
@post_log << "before_adding#{object.id || '<new>'}"
Expand Down

0 comments on commit 883f27a

Please sign in to comment.