diff options
author | José Valim <jose.valim@gmail.com> | 2009-06-19 16:10:24 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-06-19 16:10:24 +0200 |
commit | 8037fee9ff1dd13c421e6689229457c1ef084074 (patch) | |
tree | c60b2bf3e297be3e1bccb64ffd034acb137a7aa8 /activemodel/test/cases/json_serialization_test.rb | |
parent | 908b5305d4446433433313e315deb4083c58233e (diff) | |
parent | 9f7eaea201b2f408d9effbf82f2731957e284adf (diff) | |
download | rails-8037fee9ff1dd13c421e6689229457c1ef084074.tar.gz rails-8037fee9ff1dd13c421e6689229457c1ef084074.tar.bz2 rails-8037fee9ff1dd13c421e6689229457c1ef084074.zip |
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'activemodel/test/cases/json_serialization_test.rb')
-rw-r--r-- | activemodel/test/cases/json_serialization_test.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/activemodel/test/cases/json_serialization_test.rb b/activemodel/test/cases/json_serialization_test.rb new file mode 100644 index 0000000000..abcec67a85 --- /dev/null +++ b/activemodel/test/cases/json_serialization_test.rb @@ -0,0 +1,64 @@ +require 'cases/helper' + +class JsonSerializationTest < ActiveModel::TestCase + class Contact + extend ActiveModel::Naming + include ActiveModel::Serializers::JSON + attr_accessor :name, :age, :created_at, :awesome, :preferences + end + + def setup + @contact = Contact.new + @contact.name = 'Konata Izumi' + @contact.age = 16 + @contact.created_at = Time.utc(2006, 8, 1) + @contact.awesome = true + @contact.preferences = { 'shows' => 'anime' } + end + + test "should include root in json" do + begin + Contact.include_root_in_json = true + json = @contact.to_json + + assert_match %r{^\{"contact":\{}, json + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"awesome":true}, json + assert_match %r{"preferences":\{"shows":"anime"\}}, json + ensure + Contact.include_root_in_json = false + end + end + + test "should encode all encodable attributes" do + json = @contact.to_json + + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"awesome":true}, json + assert_match %r{"preferences":\{"shows":"anime"\}}, json + end + + test "should allow attribute filtering with only" do + json = @contact.to_json(:only => [:name, :age]) + + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert_no_match %r{"awesome":true}, json + assert !json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_no_match %r{"preferences":\{"shows":"anime"\}}, json + end + + test "should allow attribute filtering with except" do + json = @contact.to_json(:except => [:name, :age]) + + assert_no_match %r{"name":"Konata Izumi"}, json + assert_no_match %r{"age":16}, json + assert_match %r{"awesome":true}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"preferences":\{"shows":"anime"\}}, json + end +end |