summaryrefslogtreecommitdiff
path: root/test/json/test_json.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/json/test_json.rb')
-rwxr-xr-xtest/json/test_json.rb60
1 files changed, 35 insertions, 25 deletions
diff --git a/test/json/test_json.rb b/test/json/test_json.rb
index 5f186f0c08..b367e906b9 100755
--- a/test/json/test_json.rb
+++ b/test/json/test_json.rb
@@ -2,11 +2,7 @@
# -*- coding: utf-8 -*-
require 'test/unit'
-case ENV['JSON']
-when 'pure' then require 'json/pure'
-when 'ext' then require 'json/ext'
-else require 'json'
-end
+require File.join(File.dirname(__FILE__), 'setup_variant')
require 'stringio'
unless Array.method_defined?(:permutation)
@@ -154,11 +150,20 @@ class TC_JSON < Test::Unit::TestCase
assert_equal(@ary,
parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\
',[false],[true]]'))
- assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]
+ assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]\s
, [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
end
- class SubArray < Array; end
+ class SubArray < Array
+ def <<(v)
+ @shifted = true
+ super
+ end
+
+ def shifted?
+ @shifted
+ end
+ end
class SubArray2 < Array
def to_json(*a)
@@ -175,9 +180,10 @@ class TC_JSON < Test::Unit::TestCase
end
def test_parse_array_custom_class
- res = parse('[]', :array_class => SubArray)
- assert_equal([], res)
+ res = parse('[1,2]', :array_class => SubArray)
+ assert_equal([1,2], res)
assert_equal(SubArray, res.class)
+ assert res.shifted?
end
def test_parse_object
@@ -188,6 +194,14 @@ class TC_JSON < Test::Unit::TestCase
end
class SubHash < Hash
+ def []=(k, v)
+ @item_set = true
+ super
+ end
+
+ def item_set?
+ @item_set
+ end
end
class SubHash2 < Hash
@@ -204,9 +218,10 @@ class TC_JSON < Test::Unit::TestCase
end
def test_parse_object_custom_class
- res = parse('{}', :object_class => SubHash2)
- assert_equal({}, res)
- assert_equal(SubHash2, res.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_generation_of_core_subclasses_with_new_to_json
@@ -392,23 +407,18 @@ EOT
assert_equal orig, JSON[json5][0]
end
- def test_allocate
- json = JSON::Parser.new("{}")
- assert_raises(TypeError, '[ruby-core:35079]') {json.__send__(:initialize, "{}")}
- json = JSON::Parser.allocate
- assert_raises(TypeError, '[ruby-core:35079]') {json.source}
+ if defined?(JSON::Ext::Parser)
+ def test_allocate
+ parser = JSON::Ext::Parser.new("{}")
+ assert_raise(TypeError, '[ruby-core:35079]') {parser.__send__(:initialize, "{}")}
+ parser = JSON::Ext::Parser.allocate
+ assert_raise(TypeError, '[ruby-core:35079]') {parser.source}
+ end
end
def test_argument_encoding
source = "{}".force_encoding("ascii-8bit")
JSON::Parser.new(source)
assert_equal Encoding::ASCII_8BIT, source.encoding
- end
-
- def test_frozen_argument
- source = "{}".force_encoding("ascii-8bit")
- source.freeze
- parser = nil
- assert_nothing_raised {parser = JSON::Parser.new(source)}
- end
+ end if defined?(Encoding::ASCII_8BIT)
end