diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-03-08 02:56:25 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-03-08 02:56:25 +0000 |
commit | cd989472a5248aae4f827c35347bdb871de1822a (patch) | |
tree | 13cad5fa4cd877788747930fe6076fcd01085a0f /activesupport/test/core_ext | |
parent | 24fca9d92ea9bef676f74259996c4039e014dfe2 (diff) | |
download | rails-cd989472a5248aae4f827c35347bdb871de1822a.tar.gz rails-cd989472a5248aae4f827c35347bdb871de1822a.tar.bz2 rails-cd989472a5248aae4f827c35347bdb871de1822a.zip |
Added Hash#to_xml and Array#to_xml that makes it much easier to produce XML from basic structures [DHH] Moved Jim Weirich's wonderful Builder from Action Pack to Active Support (it's simply too useful to be stuck in AP) [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3812 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/test/core_ext')
-rw-r--r-- | activesupport/test/core_ext/array_ext_test.rb | 11 | ||||
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 30 |
2 files changed, 39 insertions, 2 deletions
diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 266b5eefab..e13c57d262 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -67,5 +67,14 @@ class ArrayExtGroupingTests < Test::Unit::TestCase assert_equal [%w(a b c), %w(d e f), ['g', false, false]], groups end - end + +class ArraToXmlTests < Test::Unit::TestCase + def test_to_xml + a = [ { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" } ] + assert_equal( + "<hashes><hash><street-address type=\"string\">Paulina</street-address><name type=\"string\">David</name></hash><hash><street-address type=\"string\">Evergreen</street-address><name type=\"string\">Jason</name></hash></hashes>", + a.to_xml + ) + end +end
\ No newline at end of file diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index d74cea0991..b7d024f754 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -1,5 +1,5 @@ require 'test/unit' -require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/hash' +require File.dirname(__FILE__) + '/../../lib/active_support' class HashExtTest < Test::Unit::TestCase def setup @@ -165,3 +165,31 @@ class HashExtTest < Test::Unit::TestCase assert_equal({ :a => 1, :b => 2, :c => 10 }, { :a => 1, :b => 2 }.reverse_merge({:a => "x", :b => "y", :c => 10}) ) end end + +class HashToXmlTest < Test::Unit::TestCase + def test_one_level + h = { :name => "David", :street => "Paulina" } + assert_equal %(<person><street type="string">Paulina</street><name type="string">David</name></person>), h.to_xml(:root => :person) + end + + def test_one_level_with_types + h = { :name => "David", :street => "Paulina", :age => 26, :moved_on => Date.new(2005, 11, 15) } + assert_equal( + "<person><street type=\"string\">Paulina</street><name type=\"string\">David</name><age type=\"integer\">26</age><moved-on type=\"date\">2005-11-15</moved-on></person>", + h.to_xml(:root => :person) + ) + end + + def test_one_level_with_nils + h = { :name => "David", :street => "Paulina", :age => nil } + assert_equal( + "<person><street type=\"string\">Paulina</street><name type=\"string\">David</name><age></age></person>", + h.to_xml(:root => :person) + ) + end + + def test_two_levels + h = { :name => "David", :address => { :street => "Paulina" } } + assert_equal %(<person><address><street type="string">Paulina</street></address><name type="string">David</name></person>), h.to_xml(:root => :person) + end +end
\ No newline at end of file |