summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-03-11 13:36:06 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-03-11 13:36:06 +0000
commit4d8d3184d18b1a9d2eadb86a6688dc62eca7eabc (patch)
tree03e3a00e4e484ffdb46d5d774ab887dc695b8ed0 /test
parent9d9ab5a384de3877f29cef8c0293c1caa432692c (diff)
* ext/json: Merge 164a75c8bd2007d32c4d7665d53140d8fc126dcd.
[ruby-core:41917] [Bug #5846] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34971 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rwxr-xr-xtest/json/test_json.rb79
-rwxr-xr-xtest/json/test_json_addition.rb25
-rwxr-xr-xtest/json/test_json_generate.rb98
3 files changed, 164 insertions, 38 deletions
diff --git a/test/json/test_json.rb b/test/json/test_json.rb
index eafd758bf7..1b39360b33 100755
--- a/test/json/test_json.rb
+++ b/test/json/test_json.rb
@@ -4,6 +4,8 @@
require 'test/unit'
require File.join(File.dirname(__FILE__), 'setup_variant')
require 'stringio'
+require 'tempfile'
+require 'ostruct'
unless Array.method_defined?(:permutation)
begin
@@ -107,6 +109,8 @@ class TC_JSON < Test::Unit::TestCase
def test_parse_json_primitive_values
assert_raise(JSON::ParserError) { JSON.parse('') }
assert_raise(JSON::ParserError) { JSON.parse('', :quirks_mode => true) }
+ assert_raise(TypeError) { JSON.parse(nil) }
+ assert_raise(TypeError) { JSON.parse(nil, :quirks_mode => true) }
assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ') }
assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ', :quirks_mode => true) }
parser = JSON::Parser.new('null')
@@ -215,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(' { } '))
@@ -253,14 +285,36 @@ 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_generation_of_core_subclasses_with_new_to_json
+ 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_generate_core_subclasses_with_new_to_json
obj = SubHash2["foo" => SubHash2["bar" => true]]
obj_json = JSON(obj)
obj_again = JSON(obj_json)
@@ -271,12 +325,12 @@ class TC_JSON < Test::Unit::TestCase
assert_equal ["foo"], JSON(JSON(SubArray2["foo"]))
end
- def test_generation_of_core_subclasses_with_default_to_json
+ def test_generate_core_subclasses_with_default_to_json
assert_equal '{"foo":"bar"}', JSON(SubHash["foo" => "bar"])
assert_equal '["foo"]', JSON(SubArray["foo"])
end
- def test_generation_of_core_subclasses
+ def test_generate_of_core_subclasses
obj = SubHash["foo" => SubHash["bar" => true]]
obj_json = JSON(obj)
obj_again = JSON(obj_json)
@@ -414,7 +468,20 @@ EOT
JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
end
- def test_load_dump
+ def test_load
+ assert_equal @hash, JSON.load(@json)
+ tempfile = Tempfile.open('json')
+ tempfile.write @json
+ tempfile.rewind
+ assert_equal @hash, JSON.load(tempfile)
+ stringio = StringIO.new(@json)
+ stringio.rewind
+ assert_equal @hash, JSON.load(stringio)
+ assert_equal nil, JSON.load(nil)
+ assert_equal nil, JSON.load('')
+ end
+
+ def test_dump
too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]'
assert_equal too_deep, JSON.dump(eval(too_deep))
assert_kind_of String, Marshal.dump(eval(too_deep))
diff --git a/test/json/test_json_addition.rb b/test/json/test_json_addition.rb
index cc3820a073..e262e25953 100755
--- a/test/json/test_json_addition.rb
+++ b/test/json/test_json_addition.rb
@@ -3,7 +3,11 @@
require 'test/unit'
require File.join(File.dirname(__FILE__), 'setup_variant')
-require 'json/add/core.rb'
+require 'json/add/core'
+require 'json/add/complex'
+require 'json/add/rational'
+require 'json/add/bigdecimal'
+require 'json/add/ostruct'
require 'date'
class TC_JSONAddition < Test::Unit::TestCase
@@ -126,7 +130,7 @@ class TC_JSONAddition < Test::Unit::TestCase
def test_core
t = Time.now
- assert_equal t.inspect, JSON(JSON(t)).inspect
+ assert_equal t, JSON(JSON(t))
d = Date.today
assert_equal d, JSON(JSON(d))
d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161)
@@ -164,4 +168,21 @@ class TC_JSONAddition < Test::Unit::TestCase
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
assert_equal d, JSON.parse(d.to_json)
end
+
+ def test_rational_complex
+ assert_equal Rational(2, 9), JSON(JSON(Rational(2, 9)))
+ assert_equal Complex(2, 9), JSON(JSON(Complex(2, 9)))
+ end
+
+ def test_bigdecimal
+ assert_equal BigDecimal('3.141', 23), JSON(JSON(BigDecimal('3.141', 23)))
+ assert_equal BigDecimal('3.141', 666), JSON(JSON(BigDecimal('3.141', 666)))
+ end
+
+ def test_ostruct
+ o = OpenStruct.new
+ # XXX this won't work; o.foo = { :bar => true }
+ o.foo = { 'bar' => true }
+ assert_equal o, JSON(JSON(o))
+ end
end
diff --git a/test/json/test_json_generate.rb b/test/json/test_json_generate.rb
index da96603d61..3a1ddc6f33 100755
--- a/test/json/test_json_generate.rb
+++ b/test/json/test_json_generate.rb
@@ -43,6 +43,8 @@ EOT
def test_generate
json = generate(@hash)
assert_equal(JSON.parse(@json2), JSON.parse(json))
+ json = JSON[@hash]
+ assert_equal(JSON.parse(@json2), JSON.parse(json))
parsed_json = parse(json)
assert_equal(@hash, parsed_json)
json = generate({1=>2})
@@ -121,48 +123,51 @@ EOT
def test_pretty_state
state = PRETTY_STATE_PROTOTYPE.dup
assert_equal({
- :allow_nan => false,
- :array_nl => "\n",
- :ascii_only => false,
- :quirks_mode => false,
- :depth => 0,
- :indent => " ",
- :max_nesting => 19,
- :object_nl => "\n",
- :space => " ",
- :space_before => "",
+ :allow_nan => false,
+ :array_nl => "\n",
+ :ascii_only => false,
+ :buffer_initial_length => 1024,
+ :quirks_mode => false,
+ :depth => 0,
+ :indent => " ",
+ :max_nesting => 19,
+ :object_nl => "\n",
+ :space => " ",
+ :space_before => "",
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
end
def test_safe_state
state = SAFE_STATE_PROTOTYPE.dup
assert_equal({
- :allow_nan => false,
- :array_nl => "",
- :ascii_only => false,
- :quirks_mode => false,
- :depth => 0,
- :indent => "",
- :max_nesting => 19,
- :object_nl => "",
- :space => "",
- :space_before => "",
+ :allow_nan => false,
+ :array_nl => "",
+ :ascii_only => false,
+ :buffer_initial_length => 1024,
+ :quirks_mode => false,
+ :depth => 0,
+ :indent => "",
+ :max_nesting => 19,
+ :object_nl => "",
+ :space => "",
+ :space_before => "",
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
end
def test_fast_state
state = FAST_STATE_PROTOTYPE.dup
assert_equal({
- :allow_nan => false,
- :array_nl => "",
- :ascii_only => false,
- :quirks_mode => false,
- :depth => 0,
- :indent => "",
- :max_nesting => 0,
- :object_nl => "",
- :space => "",
- :space_before => "",
+ :allow_nan => false,
+ :array_nl => "",
+ :ascii_only => false,
+ :buffer_initial_length => 1024,
+ :quirks_mode => false,
+ :depth => 0,
+ :indent => "",
+ :max_nesting => 0,
+ :object_nl => "",
+ :space => "",
+ :space_before => "",
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
end
@@ -198,6 +203,17 @@ EOT
assert_equal 19, s.depth
end
+ def test_buffer_initial_length
+ s = JSON.state.new
+ assert_equal 1024, s.buffer_initial_length
+ s.buffer_initial_length = 0
+ assert_equal 1024, s.buffer_initial_length
+ s.buffer_initial_length = -1
+ assert_equal 1024, s.buffer_initial_length
+ s.buffer_initial_length = 128
+ assert_equal 128, s.buffer_initial_length
+ end
+
def test_gc
bignum_too_long_to_embed_as_string = 1234567890123456789012345
expect = bignum_too_long_to_embed_as_string.to_s
@@ -210,4 +226,26 @@ EOT
ensure
GC.stress = stress
end if GC.respond_to?(:stress=)
+
+ if defined?(JSON::Ext::Generator)
+ def test_broken_bignum # [ruby-core:38867]
+ pid = fork do
+ Bignum.class_eval do
+ def to_s
+ end
+ end
+ begin
+ JSON::Ext::Generator::State.new.generate(1<<64)
+ exit 1
+ rescue TypeError
+ exit 0
+ end
+ end
+ _, status = Process.waitpid2(pid)
+ assert status.success?
+ rescue NotImplementedError
+ # forking to avoid modifying core class of a parent process and
+ # introducing race conditions of tests are run in parallel
+ end
+ end
end