From d2c17dbd11417012c47b9ac48c62651a1415de5d Mon Sep 17 00:00:00 2001 From: Alexey Nayden Date: Thu, 13 Jan 2011 03:16:16 +0300 Subject: [PATCH] Complex struct encoding test Signed-off-by: Santiago Pastorino --- activesupport/test/json/encoding_test.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index 7469ae70fd..f7ca39f0f6 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -215,6 +215,29 @@ class TestJSONEncoding < Test::Unit::TestCase assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json) end + def test_struct_encoding + Struct.new('UserNameAndEmail', :name, :email) + Struct.new('UserNameAndDate', :name, :date) + Struct.new('Custom', :name, :sub) + user_email = Struct::UserNameAndEmail.new 'David', 'sample@example.com' + user_birthday = Struct::UserNameAndDate.new 'David', Date.new(2010, 01, 01) + custom = Struct::Custom.new 'David', user_birthday + + + json_strings = "" + json_string_and_date = "" + json_custom = "" + + assert_nothing_raised do + json_strings = user_email.to_json + json_string_and_date = user_birthday.to_json + json_custom = custom.to_json + end + + assert_equal %({"name":"David","email":"sample@example.com"}), json_strings + assert_equal %({"name":"David","date":"2010/01/01"}), json_string_and_date + assert_equal %({"sub":{"name":"David","date":"2010/01/01"},"name":"David"}), json_custom + end protected