summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2011-12-20 20:14:10 +0100
committerFlorian Frank <flori@ping.de>2011-12-20 20:45:15 +0100
commiteacb6564c05ff7428bcbb3c34f72655b1b5d6ca3 (patch)
tree3edffa71fa198ddafa8c91771131ca537fae0ed3 /tests
parent3c7c29215d18d25fff3d3af2d599f90894c52c72 (diff)
downloadjson-eacb6564c05ff7428bcbb3c34f72655b1b5d6ca3.tar.gz
Support object class with duck type hash
Diffstat (limited to 'tests')
-rwxr-xr-xtests/test_json.rb25
1 files changed, 24 insertions, 1 deletions
diff --git a/tests/test_json.rb b/tests/test_json.rb
index c308933..fe21475 100755
--- a/tests/test_json.rb
+++ b/tests/test_json.rb
@@ -5,6 +5,7 @@ require 'test/unit'
require File.join(File.dirname(__FILE__), 'setup_variant')
require 'stringio'
require 'tempfile'
+require 'ostruct'
unless Array.method_defined?(:permutation)
begin
@@ -256,13 +257,35 @@ class TC_JSON < Test::Unit::TestCase
end
end
- def test_parse_object_custom_class
+ class SubOpenStruct < OpenStruct
+ def [](k)
+ __send__(k)
+ end
+
+ def []=(k, v)
+ @item_set = true
+ __send__("#{k}=", v)
+ end
+
+ def item_set?
+ @item_set
+ end
+ end
+
+ def test_parse_object_custom_hash_derived_class
res = parse('{"foo":"bar"}', :object_class => SubHash)
assert_equal({"foo" => "bar"}, res)
assert_equal(SubHash, res.class)
assert res.item_set?
end
+ def test_parse_object_custom_non_hash_derived_class
+ res = parse('{"foo":"bar"}', :object_class => SubOpenStruct)
+ assert_equal "bar", res.foo
+ assert_equal(SubOpenStruct, res.class)
+ assert res.item_set?
+ end
+
def test_generation_of_core_subclasses_with_new_to_json
obj = SubHash2["foo" => SubHash2["bar" => true]]
obj_json = JSON(obj)