diff options
author | Florian Frank <flori@ping.de> | 2015-06-15 10:06:27 +0200 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2015-06-15 10:06:27 +0200 |
commit | 6f907a9142e83141a2e01fdddfd18b2013244cd6 (patch) | |
tree | 9fa8f8571594f300a2f6038948c4e96842359ff6 /tests/json_generic_object_test.rb | |
parent | 0ece7a0404cebb8fb2ce4e6110e70fd05fb48d6b (diff) | |
download | json-6f907a9142e83141a2e01fdddfd18b2013244cd6.tar.gz |
Refactor tests and make pure parser green
Diffstat (limited to 'tests/json_generic_object_test.rb')
-rw-r--r-- | tests/json_generic_object_test.rb | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/json_generic_object_test.rb b/tests/json_generic_object_test.rb new file mode 100644 index 0000000..171fdb8 --- /dev/null +++ b/tests/json_generic_object_test.rb @@ -0,0 +1,81 @@ +require 'test_helper' + +class JSONGenericObjectTest < Test::Unit::TestCase + include JSON + + def setup + @go = GenericObject[ :a => 1, :b => 2 ] + end + + def test_attributes + assert_equal 1, @go.a + assert_equal 1, @go[:a] + assert_equal 2, @go.b + assert_equal 2, @go[:b] + assert_nil @go.c + assert_nil @go[:c] + end + + def test_generate_json + switch_json_creatable do + assert_equal @go, JSON(JSON(@go), :create_additions => true) + end + end + + def test_parse_json + assert_kind_of Hash, + JSON( + '{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }', + :create_additions => true + ) + switch_json_creatable do + assert_equal @go, l = + JSON( + '{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }', + :create_additions => true + ) + assert_equal 1, l.a + assert_equal @go, + l = JSON('{ "a": 1, "b": 2 }', :object_class => GenericObject) + assert_equal 1, l.a + assert_equal GenericObject[:a => GenericObject[:b => 2]], + l = JSON('{ "a": { "b": 2 } }', :object_class => GenericObject) + assert_equal 2, l.a.b + end + end + + def test_from_hash + result = GenericObject.from_hash( + :foo => { :bar => { :baz => true }, :quux => [ { :foobar => true } ] }) + assert_kind_of GenericObject, result.foo + assert_kind_of GenericObject, result.foo.bar + assert_equal true, result.foo.bar.baz + assert_kind_of GenericObject, result.foo.quux.first + assert_equal true, result.foo.quux.first.foobar + assert_equal true, GenericObject.from_hash(true) + end + + def test_json_generic_object_load + empty = JSON::GenericObject.load(nil) + assert_kind_of JSON::GenericObject, empty + simple_json = '{"json_class":"JSON::GenericObject","hello":"world"}' + simple = JSON::GenericObject.load(simple_json) + assert_kind_of JSON::GenericObject, simple + assert_equal "world", simple.hello + converting = JSON::GenericObject.load('{ "hello": "world" }') + assert_kind_of JSON::GenericObject, converting + assert_equal "world", converting.hello + + json = JSON::GenericObject.dump(JSON::GenericObject[:hello => 'world']) + assert_equal JSON(json), JSON('{"json_class":"JSON::GenericObject","hello":"world"}') + end + + private + + def switch_json_creatable + JSON::GenericObject.json_creatable = true + yield + ensure + JSON::GenericObject.json_creatable = false + end +end |