diff options
Diffstat (limited to 'activesupport/test/json')
-rw-r--r-- | activesupport/test/json/decoding_test.rb | 76 | ||||
-rw-r--r-- | activesupport/test/json/encoding_test.rb | 60 | ||||
-rw-r--r-- | activesupport/test/json/encoding_test_cases.rb | 4 |
3 files changed, 70 insertions, 70 deletions
diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb index de314921ec..dd8382754b 100644 --- a/activesupport/test/json/decoding_test.rb +++ b/activesupport/test/json/decoding_test.rb @@ -13,57 +13,57 @@ class TestJSONDecoding < ActiveSupport::TestCase end TESTS = { - %q({"returnTo":{"\/categories":"\/"}}) => {"returnTo" => {"/categories" => "/"}}, - %q({"return\\"To\\":":{"\/categories":"\/"}}) => {"return\"To\":" => {"/categories" => "/"}}, - %q({"returnTo":{"\/categories":1}}) => {"returnTo" => {"/categories" => 1}}, - %({"returnTo":[1,"a"]}) => {"returnTo" => [1, "a"]}, - %({"returnTo":[1,"\\"a\\",", "b"]}) => {"returnTo" => [1, "\"a\",", "b"]}, - %({"a": "'", "b": "5,000"}) => {"a" => "'", "b" => "5,000"}, - %({"a": "a's, b's and c's", "b": "5,000"}) => {"a" => "a's, b's and c's", "b" => "5,000"}, + %q({"returnTo":{"\/categories":"\/"}}) => { "returnTo" => { "/categories" => "/" } }, + %q({"return\\"To\\":":{"\/categories":"\/"}}) => { "return\"To\":" => { "/categories" => "/" } }, + %q({"returnTo":{"\/categories":1}}) => { "returnTo" => { "/categories" => 1 } }, + %({"returnTo":[1,"a"]}) => { "returnTo" => [1, "a"] }, + %({"returnTo":[1,"\\"a\\",", "b"]}) => { "returnTo" => [1, "\"a\",", "b"] }, + %({"a": "'", "b": "5,000"}) => { "a" => "'", "b" => "5,000" }, + %({"a": "a's, b's and c's", "b": "5,000"}) => { "a" => "a's, b's and c's", "b" => "5,000" }, # multibyte - %({"matzue": "松江", "asakusa": "浅草"}) => {"matzue" => "松江", "asakusa" => "浅草"}, - %({"a": "2007-01-01"}) => {"a" => Date.new(2007, 1, 1)}, - %({"a": "2007-01-01 01:12:34 Z"}) => {"a" => Time.utc(2007, 1, 1, 1, 12, 34)}, + %({"matzue": "松江", "asakusa": "浅草"}) => { "matzue" => "松江", "asakusa" => "浅草" }, + %({"a": "2007-01-01"}) => { "a" => Date.new(2007, 1, 1) }, + %({"a": "2007-01-01 01:12:34 Z"}) => { "a" => Time.utc(2007, 1, 1, 1, 12, 34) }, %(["2007-01-01 01:12:34 Z"]) => [Time.utc(2007, 1, 1, 1, 12, 34)], %(["2007-01-01 01:12:34 Z", "2007-01-01 01:12:35 Z"]) => [Time.utc(2007, 1, 1, 1, 12, 34), Time.utc(2007, 1, 1, 1, 12, 35)], # no time zone - %({"a": "2007-01-01 01:12:34"}) => {"a" => Time.new(2007, 1, 1, 1, 12, 34, "-05:00")}, + %({"a": "2007-01-01 01:12:34"}) => { "a" => Time.new(2007, 1, 1, 1, 12, 34, "-05:00") }, # invalid date - %({"a": "1089-10-40"}) => {"a" => "1089-10-40"}, + %({"a": "1089-10-40"}) => { "a" => "1089-10-40" }, # xmlschema date notation - %({"a": "2009-08-10T19:01:02"}) => {"a" => Time.new(2009, 8, 10, 19, 1, 2, "-04:00")}, - %({"a": "2009-08-10T19:01:02Z"}) => {"a" => Time.utc(2009, 8, 10, 19, 1, 2)}, - %({"a": "2009-08-10T19:01:02+02:00"}) => {"a" => Time.utc(2009, 8, 10, 17, 1, 2)}, - %({"a": "2009-08-10T19:01:02-05:00"}) => {"a" => Time.utc(2009, 8, 11, 00, 1, 2)}, + %({"a": "2009-08-10T19:01:02"}) => { "a" => Time.new(2009, 8, 10, 19, 1, 2, "-04:00") }, + %({"a": "2009-08-10T19:01:02Z"}) => { "a" => Time.utc(2009, 8, 10, 19, 1, 2) }, + %({"a": "2009-08-10T19:01:02+02:00"}) => { "a" => Time.utc(2009, 8, 10, 17, 1, 2) }, + %({"a": "2009-08-10T19:01:02-05:00"}) => { "a" => Time.utc(2009, 8, 11, 00, 1, 2) }, # needs to be *exact* - %({"a": " 2007-01-01 01:12:34 Z "}) => {"a" => " 2007-01-01 01:12:34 Z "}, - %({"a": "2007-01-01 : it's your birthday"}) => {"a" => "2007-01-01 : it's your birthday"}, + %({"a": " 2007-01-01 01:12:34 Z "}) => { "a" => " 2007-01-01 01:12:34 Z " }, + %({"a": "2007-01-01 : it's your birthday"}) => { "a" => "2007-01-01 : it's your birthday" }, %([]) => [], %({}) => {}, - %({"a":1}) => {"a" => 1}, - %({"a": ""}) => {"a" => ""}, - %({"a":"\\""}) => {"a" => "\""}, - %({"a": null}) => {"a" => nil}, - %({"a": true}) => {"a" => true}, - %({"a": false}) => {"a" => false}, - '{"bad":"\\\\","trailing":""}' => {"bad" => "\\", "trailing" => ""}, - %q({"a": "http:\/\/test.host\/posts\/1"}) => {"a" => "http://test.host/posts/1"}, - %q({"a": "\u003cunicode\u0020escape\u003e"}) => {"a" => "<unicode escape>"}, - '{"a": "\\\\u0020skip double backslashes"}' => {"a" => "\\u0020skip double backslashes"}, - %q({"a": "\u003cbr /\u003e"}) => {"a" => "<br />"}, - %q({"b":["\u003ci\u003e","\u003cb\u003e","\u003cu\u003e"]}) => {"b" => ["<i>","<b>","<u>"]}, + %({"a":1}) => { "a" => 1 }, + %({"a": ""}) => { "a" => "" }, + %({"a":"\\""}) => { "a" => "\"" }, + %({"a": null}) => { "a" => nil }, + %({"a": true}) => { "a" => true }, + %({"a": false}) => { "a" => false }, + '{"bad":"\\\\","trailing":""}' => { "bad" => "\\", "trailing" => "" }, + %q({"a": "http:\/\/test.host\/posts\/1"}) => { "a" => "http://test.host/posts/1" }, + %q({"a": "\u003cunicode\u0020escape\u003e"}) => { "a" => "<unicode escape>" }, + '{"a": "\\\\u0020skip double backslashes"}' => { "a" => "\\u0020skip double backslashes" }, + %q({"a": "\u003cbr /\u003e"}) => { "a" => "<br />" }, + %q({"b":["\u003ci\u003e","\u003cb\u003e","\u003cu\u003e"]}) => { "b" => ["<i>","<b>","<u>"] }, # test combination of dates and escaped or unicode encoded data in arrays %q([{"d":"1970-01-01", "s":"\u0020escape"},{"d":"1970-01-01", "s":"\u0020escape"}]) => - [{"d" => Date.new(1970, 1, 1), "s" => " escape"},{"d" => Date.new(1970, 1, 1), "s" => " escape"}], + [{ "d" => Date.new(1970, 1, 1), "s" => " escape" },{ "d" => Date.new(1970, 1, 1), "s" => " escape" }], %q([{"d":"1970-01-01","s":"http:\/\/example.com"},{"d":"1970-01-01","s":"http:\/\/example.com"}]) => - [{"d" => Date.new(1970, 1, 1), "s" => "http://example.com"}, - {"d" => Date.new(1970, 1, 1), "s" => "http://example.com"}], + [{ "d" => Date.new(1970, 1, 1), "s" => "http://example.com" }, + { "d" => Date.new(1970, 1, 1), "s" => "http://example.com" }], # tests escaping of "\n" char with Yaml backend - %q({"a":"\n"}) => {"a"=>"\n"}, - %q({"a":"\u000a"}) => {"a"=>"\n"}, - %q({"a":"Line1\u000aLine2"}) => {"a"=>"Line1\nLine2"}, + %q({"a":"\n"}) => { "a"=>"\n" }, + %q({"a":"\u000a"}) => { "a"=>"\n" }, + %q({"a":"Line1\u000aLine2"}) => { "a"=>"Line1\nLine2" }, # prevent json unmarshalling - '{"json_class":"TestJSONDecoding::Foo"}' => {"json_class"=>"TestJSONDecoding::Foo"}, + '{"json_class":"TestJSONDecoding::Foo"}' => { "json_class"=>"TestJSONDecoding::Foo" }, # json "fragments" - these are invalid JSON, but ActionPack relies on this '"a string"' => "a string", "1.1" => 1.1, @@ -89,7 +89,7 @@ class TestJSONDecoding < ActiveSupport::TestCase test "json decodes time json with time parsing disabled" do with_parse_json_times(false) do - expected = {"a" => "2007-01-01 01:12:34 Z"} + expected = { "a" => "2007-01-01 01:12:34 Z" } assert_equal expected, ActiveSupport::JSON.decode(%({"a": "2007-01-01 01:12:34 Z"})) end end diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index 9c95bf35b8..d125cc939f 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -93,16 +93,16 @@ class TestJSONEncoding < ActiveSupport::TestCase end def test_hash_key_identifiers_are_always_quoted - values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"} + values = { 0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B" } assert_equal %w( "$" "A" "A0" "A0B" "_" "a" "0" "1" ).sort, object_keys(ActiveSupport::JSON.encode(values)) end def test_hash_should_allow_key_filtering_with_only - assert_equal %({"a":1}), ActiveSupport::JSON.encode({"a" => 1, :b => 2, :c => 3}, only: "a") + assert_equal %({"a":1}), ActiveSupport::JSON.encode({ "a" => 1, :b => 2, :c => 3 }, only: "a") end def test_hash_should_allow_key_filtering_with_except - assert_equal %({"b":2}), ActiveSupport::JSON.encode({"foo" => "bar", :b => 2, :c => 3}, except: ["foo", :c]) + assert_equal %({"b":2}), ActiveSupport::JSON.encode({ "foo" => "bar", :b => 2, :c => 3 }, except: ["foo", :c]) end def test_time_to_json_includes_local_offset @@ -135,7 +135,7 @@ class TestJSONEncoding < ActiveSupport::TestCase h = JSONTest::Hashlike.new json = h.to_json only: [:foo] - assert_equal({"foo"=>"hello"}, JSON.parse(json)) + assert_equal({ "foo"=>"hello" }, JSON.parse(json)) end def test_object_to_json_with_options @@ -144,7 +144,7 @@ class TestJSONEncoding < ActiveSupport::TestCase obj.instance_variable_set :@bar, "world" json = obj.to_json only: ["foo"] - assert_equal({"foo"=>"hello"}, JSON.parse(json)) + assert_equal({ "foo"=>"hello" }, JSON.parse(json)) end def test_struct_to_json_with_options @@ -153,7 +153,7 @@ class TestJSONEncoding < ActiveSupport::TestCase struct.bar = "world" json = struct.to_json only: [:foo] - assert_equal({"foo"=>"hello"}, JSON.parse(json)) + assert_equal({ "foo"=>"hello" }, JSON.parse(json)) end def test_hash_should_pass_encoding_options_to_children_in_as_json @@ -166,7 +166,7 @@ class TestJSONEncoding < ActiveSupport::TestCase } json = person.as_json only: [:address, :city] - assert_equal({ "address" => { "city" => "London" }}, json) + assert_equal({ "address" => { "city" => "London" } }, json) end def test_hash_should_pass_encoding_options_to_children_in_to_json @@ -184,13 +184,13 @@ class TestJSONEncoding < ActiveSupport::TestCase def test_array_should_pass_encoding_options_to_children_in_as_json people = [ - { name: "John", address: { city: "London", country: "UK" }}, - { name: "Jean", address: { city: "Paris" , country: "France" }} + { name: "John", address: { city: "London", country: "UK" } }, + { name: "Jean", address: { city: "Paris" , country: "France" } } ] json = people.as_json only: [:address, :city] expected = [ - { "address" => { "city" => "London" }}, - { "address" => { "city" => "Paris" }} + { "address" => { "city" => "London" } }, + { "address" => { "city" => "Paris" } } ] assert_equal(expected, json) @@ -198,8 +198,8 @@ class TestJSONEncoding < ActiveSupport::TestCase def test_array_should_pass_encoding_options_to_children_in_to_json people = [ - { name: "John", address: { city: "London", country: "UK" }}, - { name: "Jean", address: { city: "Paris" , country: "France" }} + { name: "John", address: { city: "London", country: "UK" } }, + { name: "Jean", address: { city: "Paris" , country: "France" } } ] json = people.to_json only: [:address, :city] @@ -210,8 +210,8 @@ class TestJSONEncoding < ActiveSupport::TestCase include Enumerable def initialize() @people = [ - { name: "John", address: { city: "London", country: "UK" }}, - { name: "Jean", address: { city: "Paris" , country: "France" }} + { name: "John", address: { city: "London", country: "UK" } }, + { name: "Jean", address: { city: "Paris" , country: "France" } } ] end def each(*, &blk) @@ -225,8 +225,8 @@ class TestJSONEncoding < ActiveSupport::TestCase def test_enumerable_should_generate_json_with_as_json json = People.new.as_json only: [:address, :city] expected = [ - { "address" => { "city" => "London" }}, - { "address" => { "city" => "Paris" }} + { "address" => { "city" => "London" } }, + { "address" => { "city" => "Paris" } } ] assert_equal(expected, json) @@ -240,8 +240,8 @@ class TestJSONEncoding < ActiveSupport::TestCase def test_enumerable_should_pass_encoding_options_to_children_in_as_json json = People.new.each.as_json only: [:address, :city] expected = [ - { "address" => { "city" => "London" }}, - { "address" => { "city" => "Paris" }} + { "address" => { "city" => "London" } }, + { "address" => { "city" => "Paris" } } ] assert_equal(expected, json) @@ -267,9 +267,9 @@ class TestJSONEncoding < ActiveSupport::TestCase f.foo = "hello" f.bar = "world" - hash = {"foo" => f, "other_hash" => {"foo" => "other_foo", "test" => "other_test"}} - assert_equal({"foo"=>{"foo"=>"hello","bar"=>"world"}, - "other_hash" => {"foo"=>"other_foo","test"=>"other_test"}}, ActiveSupport::JSON.decode(hash.to_json)) + hash = { "foo" => f, "other_hash" => { "foo" => "other_foo", "test" => "other_test" } } + assert_equal({ "foo"=>{ "foo"=>"hello","bar"=>"world" }, + "other_hash" => { "foo"=>"other_foo","test"=>"other_test" } }, ActiveSupport::JSON.decode(hash.to_json)) end def test_array_to_json_should_not_keep_options_around @@ -277,9 +277,9 @@ class TestJSONEncoding < ActiveSupport::TestCase f.foo = "hello" f.bar = "world" - array = [f, {"foo" => "other_foo", "test" => "other_test"}] - assert_equal([{"foo"=>"hello","bar"=>"world"}, - {"foo"=>"other_foo","test"=>"other_test"}], ActiveSupport::JSON.decode(array.to_json)) + array = [f, { "foo" => "other_foo", "test" => "other_test" }] + assert_equal([{ "foo"=>"hello","bar"=>"world" }, + { "foo"=>"other_foo","test"=>"other_test" }], ActiveSupport::JSON.decode(array.to_json)) end class OptionsTest @@ -290,7 +290,7 @@ class TestJSONEncoding < ActiveSupport::TestCase def test_hash_as_json_without_options json = { foo: OptionsTest.new }.as_json - assert_equal({"foo" => :default}, json) + assert_equal({ "foo" => :default }, json) end def test_array_as_json_without_options @@ -316,15 +316,15 @@ class TestJSONEncoding < ActiveSupport::TestCase json_custom = custom.to_json end - assert_equal({"name" => "David", + assert_equal({ "name" => "David", "sub" => { "name" => "David", - "date" => "2010-01-01" }}, ActiveSupport::JSON.decode(json_custom)) + "date" => "2010-01-01" } }, ActiveSupport::JSON.decode(json_custom)) - assert_equal({"name" => "David", "email" => "sample@example.com"}, + assert_equal({ "name" => "David", "email" => "sample@example.com" }, ActiveSupport::JSON.decode(json_strings)) - assert_equal({"name" => "David", "date" => "2010-01-01"}, + assert_equal({ "name" => "David", "date" => "2010-01-01" }, ActiveSupport::JSON.decode(json_string_and_date)) end diff --git a/activesupport/test/json/encoding_test_cases.rb b/activesupport/test/json/encoding_test_cases.rb index f8c44f314f..ff2ed3a788 100644 --- a/activesupport/test/json/encoding_test_cases.rb +++ b/activesupport/test/json/encoding_test_cases.rb @@ -51,8 +51,8 @@ module JSONTest ArrayTests = [[ ["a", "b", "c"], %([\"a\",\"b\",\"c\"]) ], [ [1, "a", :b, nil, false], %([1,\"a\",\"b\",null,false]) ]] - HashTests = [[ {foo: "bar"}, %({\"foo\":\"bar\"}) ], - [ {1 => 1, 2 => "a", 3 => :b, 4 => nil, 5 => false}, %({\"1\":1,\"2\":\"a\",\"3\":\"b\",\"4\":null,\"5\":false}) ]] + HashTests = [[ { foo: "bar" }, %({\"foo\":\"bar\"}) ], + [ { 1 => 1, 2 => "a", 3 => :b, 4 => nil, 5 => false }, %({\"1\":1,\"2\":\"a\",\"3\":\"b\",\"4\":null,\"5\":false}) ]] RangeTests = [[ 1..2, %("1..2")], [ 1...2, %("1...2")], |