summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rwxr-xr-xtests/test_json.rb18
-rwxr-xr-xtests/test_json_addition.rb10
-rwxr-xr-xtests/test_json_generate.rb96
3 files changed, 92 insertions, 32 deletions
diff --git a/tests/test_json.rb b/tests/test_json.rb
index eafd758..c308933 100755
--- a/tests/test_json.rb
+++ b/tests/test_json.rb
@@ -4,6 +4,7 @@
require 'test/unit'
require File.join(File.dirname(__FILE__), 'setup_variant')
require 'stringio'
+require 'tempfile'
unless Array.method_defined?(:permutation)
begin
@@ -107,6 +108,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')
@@ -414,7 +417,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/tests/test_json_addition.rb b/tests/test_json_addition.rb
index b9cc6d1..e262e25 100755
--- a/tests/test_json_addition.rb
+++ b/tests/test_json_addition.rb
@@ -7,6 +7,7 @@ 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
@@ -129,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)
@@ -177,4 +178,11 @@ class TC_JSONAddition < Test::Unit::TestCase
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/tests/test_json_generate.rb b/tests/test_json_generate.rb
index da96603..b363de1 100755
--- a/tests/test_json_generate.rb
+++ b/tests/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,24 @@ EOT
ensure
GC.stress = stress
end if GC.respond_to?(:stress=)
+
+ 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 if defined?(JSON::Ext)
end