summaryrefslogtreecommitdiff
path: root/tests/test_json.rb
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_json.rb')
-rwxr-xr-xtests/test_json.rb55
1 files changed, 53 insertions, 2 deletions
diff --git a/tests/test_json.rb b/tests/test_json.rb
index c308933..c06fd19 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
@@ -218,13 +219,41 @@ class TC_JSON < Test::Unit::TestCase
end
end
- def test_parse_array_custom_class
+ class SubArrayWrapper
+ def initialize
+ @data = []
+ end
+
+ attr_reader :data
+
+ def [](index)
+ @data[index]
+ end
+
+ def <<(value)
+ @data << value
+ @shifted = true
+ end
+
+ def shifted?
+ @shifted
+ end
+ end
+
+ def test_parse_array_custom_array_derived_class
res = parse('[1,2]', :array_class => SubArray)
assert_equal([1,2], res)
assert_equal(SubArray, res.class)
assert res.shifted?
end
+ def test_parse_array_custom_non_array_derived_class
+ res = parse('[1,2]', :array_class => SubArrayWrapper)
+ assert_equal([1,2], res.data)
+ assert_equal(SubArrayWrapper, res.class)
+ assert res.shifted?
+ end
+
def test_parse_object
assert_equal({}, parse('{}'))
assert_equal({}, parse(' { } '))
@@ -256,13 +285,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)