mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Add tests of JSON 1.1.9.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24729 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
596b68cc6b
commit
d99ac4f829
6 changed files with 153 additions and 70 deletions
|
@ -2,7 +2,11 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
require 'json'
|
case ENV['JSON']
|
||||||
|
when 'pure' then require 'json/pure'
|
||||||
|
when 'ext' then require 'json/ext'
|
||||||
|
else require 'json'
|
||||||
|
end
|
||||||
require 'stringio'
|
require 'stringio'
|
||||||
|
|
||||||
class TC_JSON < Test::Unit::TestCase
|
class TC_JSON < Test::Unit::TestCase
|
||||||
|
@ -26,7 +30,7 @@ class TC_JSON < Test::Unit::TestCase
|
||||||
'h' => 1000.0,
|
'h' => 1000.0,
|
||||||
'i' => 0.001
|
'i' => 0.001
|
||||||
}
|
}
|
||||||
@json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
|
@json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
|
||||||
'"g":"\\"\\u0000\\u001f","h":1.0E3,"i":1.0E-3}'
|
'"g":"\\"\\u0000\\u001f","h":1.0E3,"i":1.0E-3}'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -49,10 +53,10 @@ class TC_JSON < Test::Unit::TestCase
|
||||||
assert_equal([23], parse('[23]'))
|
assert_equal([23], parse('[23]'))
|
||||||
assert_equal([0.23], parse('[0.23]'))
|
assert_equal([0.23], parse('[0.23]'))
|
||||||
assert_equal([0.0], parse('[0e0]'))
|
assert_equal([0.0], parse('[0e0]'))
|
||||||
assert_raise(JSON::ParserError) { parse('[+23.2]') }
|
assert_raises(JSON::ParserError) { parse('[+23.2]') }
|
||||||
assert_raise(JSON::ParserError) { parse('[+23]') }
|
assert_raises(JSON::ParserError) { parse('[+23]') }
|
||||||
assert_raise(JSON::ParserError) { parse('[.23]') }
|
assert_raises(JSON::ParserError) { parse('[.23]') }
|
||||||
assert_raise(JSON::ParserError) { parse('[023]') }
|
assert_raises(JSON::ParserError) { parse('[023]') }
|
||||||
assert_equal_float [3.141], parse('[3.141]')
|
assert_equal_float [3.141], parse('[3.141]')
|
||||||
assert_equal_float [-3.141], parse('[-3.141]')
|
assert_equal_float [-3.141], parse('[-3.141]')
|
||||||
assert_equal_float [3.141], parse('[3141e-3]')
|
assert_equal_float [3.141], parse('[3141e-3]')
|
||||||
|
@ -61,11 +65,11 @@ class TC_JSON < Test::Unit::TestCase
|
||||||
assert_equal_float [3.141], parse('[3141.0E-3]')
|
assert_equal_float [3.141], parse('[3141.0E-3]')
|
||||||
assert_equal_float [-3.141], parse('[-3141.0e-3]')
|
assert_equal_float [-3.141], parse('[-3141.0e-3]')
|
||||||
assert_equal_float [-3.141], parse('[-3141e-3]')
|
assert_equal_float [-3.141], parse('[-3141e-3]')
|
||||||
assert_raise(ParserError) { parse('[NaN]') }
|
assert_raises(ParserError) { parse('[NaN]') }
|
||||||
assert parse('[NaN]', :allow_nan => true).first.nan?
|
assert parse('[NaN]', :allow_nan => true).first.nan?
|
||||||
assert_raise(ParserError) { parse('[Infinity]') }
|
assert_raises(ParserError) { parse('[Infinity]') }
|
||||||
assert_equal [1.0/0], parse('[Infinity]', :allow_nan => true)
|
assert_equal [1.0/0], parse('[Infinity]', :allow_nan => true)
|
||||||
assert_raise(ParserError) { parse('[-Infinity]') }
|
assert_raises(ParserError) { parse('[-Infinity]') }
|
||||||
assert_equal [-1.0/0], parse('[-Infinity]', :allow_nan => true)
|
assert_equal [-1.0/0], parse('[-Infinity]', :allow_nan => true)
|
||||||
assert_equal([""], parse('[""]'))
|
assert_equal([""], parse('[""]'))
|
||||||
assert_equal(["foobar"], parse('["foobar"]'))
|
assert_equal(["foobar"], parse('["foobar"]'))
|
||||||
|
@ -79,7 +83,7 @@ class TC_JSON < Test::Unit::TestCase
|
||||||
assert_equal({ "a" => nil }, parse('{"a":null}'))
|
assert_equal({ "a" => nil }, parse('{"a":null}'))
|
||||||
assert_equal({ "a" => false }, parse('{ "a" : false } '))
|
assert_equal({ "a" => false }, parse('{ "a" : false } '))
|
||||||
assert_equal({ "a" => false }, parse('{"a":false}'))
|
assert_equal({ "a" => false }, parse('{"a":false}'))
|
||||||
assert_raise(JSON::ParserError) { parse('{false}') }
|
assert_raises(JSON::ParserError) { parse('{false}') }
|
||||||
assert_equal({ "a" => true }, parse('{"a":true}'))
|
assert_equal({ "a" => true }, parse('{"a":true}'))
|
||||||
assert_equal({ "a" => true }, parse(' { "a" : true } '))
|
assert_equal({ "a" => true }, parse(' { "a" : true } '))
|
||||||
assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
|
assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
|
||||||
|
@ -90,22 +94,30 @@ class TC_JSON < Test::Unit::TestCase
|
||||||
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_parse_more_complex_arrays
|
begin
|
||||||
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
require 'permutation'
|
||||||
a.permutation do |orig_ary|
|
def test_parse_more_complex_arrays
|
||||||
json = pretty_generate(orig_ary)
|
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
||||||
assert_equal orig_ary, parse(json)
|
perms = Permutation.for a
|
||||||
|
perms.each do |perm|
|
||||||
|
orig_ary = perm.project
|
||||||
|
json = pretty_generate(orig_ary)
|
||||||
|
assert_equal orig_ary, parse(json)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def test_parse_complex_objects
|
def test_parse_complex_objects
|
||||||
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
||||||
a.permutation do |orig_ary|
|
perms = Permutation.for a
|
||||||
s = "a"
|
perms.each do |perm|
|
||||||
orig_obj = orig_ary.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
|
s = "a"
|
||||||
json = pretty_generate(orig_obj)
|
orig_obj = perm.project.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
|
||||||
assert_equal orig_obj, parse(json)
|
json = pretty_generate(orig_obj)
|
||||||
|
assert_equal orig_obj, parse(json)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
rescue LoadError
|
||||||
|
warn "Skipping permutation tests."
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_parse_arrays
|
def test_parse_arrays
|
||||||
|
@ -134,10 +146,18 @@ class TC_JSON < Test::Unit::TestCase
|
||||||
assert_equal(@ary,
|
assert_equal(@ary,
|
||||||
parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\
|
parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\
|
||||||
',[false],[true]]'))
|
',[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]
|
||||||
, [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
|
, [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class SubArray < Array; end
|
||||||
|
|
||||||
|
def test_parse_array_custom_class
|
||||||
|
res = parse('[]', :array_class => SubArray)
|
||||||
|
assert_equal([], res)
|
||||||
|
assert_equal(SubArray, res.class)
|
||||||
|
end
|
||||||
|
|
||||||
def test_parse_object
|
def test_parse_object
|
||||||
assert_equal({}, parse('{}'))
|
assert_equal({}, parse('{}'))
|
||||||
assert_equal({}, parse(' { } '))
|
assert_equal({}, parse(' { } '))
|
||||||
|
@ -145,6 +165,14 @@ class TC_JSON < Test::Unit::TestCase
|
||||||
assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
|
assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class SubHash < Hash; end
|
||||||
|
|
||||||
|
def test_parse_object_custom_class
|
||||||
|
res = parse('{}', :object_class => SubHash)
|
||||||
|
assert_equal({}, res)
|
||||||
|
assert_equal(SubHash, res.class)
|
||||||
|
end
|
||||||
|
|
||||||
def test_parser_reset
|
def test_parser_reset
|
||||||
parser = Parser.new(@json)
|
parser = Parser.new(@json)
|
||||||
assert_equal(@hash, parser.parse)
|
assert_equal(@hash, parser.parse)
|
||||||
|
@ -173,7 +201,7 @@ EOT
|
||||||
* comment */
|
* comment */
|
||||||
}
|
}
|
||||||
EOT
|
EOT
|
||||||
assert_raise(ParserError) { parse(json) }
|
assert_raises(ParserError) { parse(json) }
|
||||||
json = <<EOT
|
json = <<EOT
|
||||||
{
|
{
|
||||||
"key1":"value1" /* multi line
|
"key1":"value1" /* multi line
|
||||||
|
@ -182,7 +210,7 @@ EOT
|
||||||
and again, throw an Error */
|
and again, throw an Error */
|
||||||
}
|
}
|
||||||
EOT
|
EOT
|
||||||
assert_raise(ParserError) { parse(json) }
|
assert_raises(ParserError) { parse(json) }
|
||||||
json = <<EOT
|
json = <<EOT
|
||||||
{
|
{
|
||||||
"key1":"value1" /*/*/
|
"key1":"value1" /*/*/
|
||||||
|
@ -202,7 +230,7 @@ EOT
|
||||||
assert_equal json, JSON.unparse(data)
|
assert_equal json, JSON.unparse(data)
|
||||||
assert_equal data, JSON.parse(json)
|
assert_equal data, JSON.parse(json)
|
||||||
#
|
#
|
||||||
json = '["\/"]'
|
json = '["/"]'
|
||||||
data = JSON.parse(json)
|
data = JSON.parse(json)
|
||||||
assert_equal ['/'], data
|
assert_equal ['/'], data
|
||||||
assert_equal json, JSON.unparse(data)
|
assert_equal json, JSON.unparse(data)
|
||||||
|
@ -218,32 +246,32 @@ EOT
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_wrong_inputs
|
def test_wrong_inputs
|
||||||
assert_raise(ParserError) { JSON.parse('"foo"') }
|
assert_raises(ParserError) { JSON.parse('"foo"') }
|
||||||
assert_raise(ParserError) { JSON.parse('123') }
|
assert_raises(ParserError) { JSON.parse('123') }
|
||||||
assert_raise(ParserError) { JSON.parse('[] bla') }
|
assert_raises(ParserError) { JSON.parse('[] bla') }
|
||||||
assert_raise(ParserError) { JSON.parse('[] 1') }
|
assert_raises(ParserError) { JSON.parse('[] 1') }
|
||||||
assert_raise(ParserError) { JSON.parse('[] []') }
|
assert_raises(ParserError) { JSON.parse('[] []') }
|
||||||
assert_raise(ParserError) { JSON.parse('[] {}') }
|
assert_raises(ParserError) { JSON.parse('[] {}') }
|
||||||
assert_raise(ParserError) { JSON.parse('{} []') }
|
assert_raises(ParserError) { JSON.parse('{} []') }
|
||||||
assert_raise(ParserError) { JSON.parse('{} {}') }
|
assert_raises(ParserError) { JSON.parse('{} {}') }
|
||||||
assert_raise(ParserError) { JSON.parse('[NULL]') }
|
assert_raises(ParserError) { JSON.parse('[NULL]') }
|
||||||
assert_raise(ParserError) { JSON.parse('[FALSE]') }
|
assert_raises(ParserError) { JSON.parse('[FALSE]') }
|
||||||
assert_raise(ParserError) { JSON.parse('[TRUE]') }
|
assert_raises(ParserError) { JSON.parse('[TRUE]') }
|
||||||
assert_raise(ParserError) { JSON.parse('[07] ') }
|
assert_raises(ParserError) { JSON.parse('[07] ') }
|
||||||
assert_raise(ParserError) { JSON.parse('[0a]') }
|
assert_raises(ParserError) { JSON.parse('[0a]') }
|
||||||
assert_raise(ParserError) { JSON.parse('[1.]') }
|
assert_raises(ParserError) { JSON.parse('[1.]') }
|
||||||
assert_raise(ParserError) { JSON.parse(' ') }
|
assert_raises(ParserError) { JSON.parse(' ') }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_nesting
|
def test_nesting
|
||||||
assert_raise(JSON::NestingError) { JSON.parse '[[]]', :max_nesting => 1 }
|
assert_raises(JSON::NestingError) { JSON.parse '[[]]', :max_nesting => 1 }
|
||||||
assert_raise(JSON::NestingError) { JSON.parser.new('[[]]', :max_nesting => 1).parse }
|
assert_raises(JSON::NestingError) { JSON.parser.new('[[]]', :max_nesting => 1).parse }
|
||||||
assert_equal [[]], JSON.parse('[[]]', :max_nesting => 2)
|
assert_equal [[]], JSON.parse('[[]]', :max_nesting => 2)
|
||||||
too_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]'
|
too_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]'
|
||||||
too_deep_ary = eval too_deep
|
too_deep_ary = eval too_deep
|
||||||
assert_raise(JSON::NestingError) { JSON.parse too_deep }
|
assert_raises(JSON::NestingError) { JSON.parse too_deep }
|
||||||
assert_raise(JSON::NestingError) { JSON.parser.new(too_deep).parse }
|
assert_raises(JSON::NestingError) { JSON.parser.new(too_deep).parse }
|
||||||
assert_raise(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 19 }
|
assert_raises(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 19 }
|
||||||
ok = JSON.parse too_deep, :max_nesting => 20
|
ok = JSON.parse too_deep, :max_nesting => 20
|
||||||
assert_equal too_deep_ary, ok
|
assert_equal too_deep_ary, ok
|
||||||
ok = JSON.parse too_deep, :max_nesting => nil
|
ok = JSON.parse too_deep, :max_nesting => nil
|
||||||
|
@ -252,10 +280,10 @@ EOT
|
||||||
assert_equal too_deep_ary, ok
|
assert_equal too_deep_ary, ok
|
||||||
ok = JSON.parse too_deep, :max_nesting => 0
|
ok = JSON.parse too_deep, :max_nesting => 0
|
||||||
assert_equal too_deep_ary, ok
|
assert_equal too_deep_ary, ok
|
||||||
assert_raise(JSON::NestingError) { JSON.generate [[]], :max_nesting => 1 }
|
assert_raises(JSON::NestingError) { JSON.generate [[]], :max_nesting => 1 }
|
||||||
assert_equal '[[]]', JSON.generate([[]], :max_nesting => 2)
|
assert_equal '[[]]', JSON.generate([[]], :max_nesting => 2)
|
||||||
assert_raise(JSON::NestingError) { JSON.generate too_deep_ary }
|
assert_raises(JSON::NestingError) { JSON.generate too_deep_ary }
|
||||||
assert_raise(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 19 }
|
assert_raises(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 19 }
|
||||||
ok = JSON.generate too_deep_ary, :max_nesting => 20
|
ok = JSON.generate too_deep_ary, :max_nesting => 20
|
||||||
assert_equal too_deep, ok
|
assert_equal too_deep, ok
|
||||||
ok = JSON.generate too_deep_ary, :max_nesting => nil
|
ok = JSON.generate too_deep_ary, :max_nesting => nil
|
||||||
|
@ -270,8 +298,8 @@ EOT
|
||||||
too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]'
|
too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]'
|
||||||
assert_equal too_deep, JSON.dump(eval(too_deep))
|
assert_equal too_deep, JSON.dump(eval(too_deep))
|
||||||
assert_kind_of String, Marshal.dump(eval(too_deep))
|
assert_kind_of String, Marshal.dump(eval(too_deep))
|
||||||
assert_raise(ArgumentError) { JSON.dump(eval(too_deep), 19) }
|
assert_raises(ArgumentError) { JSON.dump(eval(too_deep), 19) }
|
||||||
assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 19) }
|
assert_raises(ArgumentError) { Marshal.dump(eval(too_deep), 19) }
|
||||||
assert_equal too_deep, JSON.dump(eval(too_deep), 20)
|
assert_equal too_deep, JSON.dump(eval(too_deep), 20)
|
||||||
assert_kind_of String, Marshal.dump(eval(too_deep), 20)
|
assert_kind_of String, Marshal.dump(eval(too_deep), 20)
|
||||||
output = StringIO.new
|
output = StringIO.new
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
|
case ENV['JSON']
|
||||||
|
when 'pure' then require 'json/pure'
|
||||||
|
when 'ext' then require 'json/ext'
|
||||||
|
else require 'json'
|
||||||
|
end
|
||||||
require 'json/add/core'
|
require 'json/add/core'
|
||||||
require 'date'
|
require 'date'
|
||||||
|
|
||||||
|
@ -17,7 +23,7 @@ class TC_JSONAddition < Test::Unit::TestCase
|
||||||
def ==(other)
|
def ==(other)
|
||||||
a == other.a
|
a == other.a
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.json_create(object)
|
def self.json_create(object)
|
||||||
new(*object['args'])
|
new(*object['args'])
|
||||||
end
|
end
|
||||||
|
@ -89,11 +95,12 @@ class TC_JSONAddition < Test::Unit::TestCase
|
||||||
c = C.new
|
c = C.new
|
||||||
assert !C.json_creatable?
|
assert !C.json_creatable?
|
||||||
json = generate(c)
|
json = generate(c)
|
||||||
assert_raise(ArgumentError) { JSON.parse(json) }
|
assert_raises(ArgumentError) { JSON.parse(json) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_raw_strings
|
def test_raw_strings
|
||||||
raw = ''
|
raw = ''
|
||||||
|
raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT)
|
||||||
raw_array = []
|
raw_array = []
|
||||||
for i in 0..255
|
for i in 0..255
|
||||||
raw << i
|
raw << i
|
||||||
|
@ -129,7 +136,7 @@ EOT
|
||||||
assert_equal s, JSON(JSON(s))
|
assert_equal s, JSON(JSON(s))
|
||||||
struct = Struct.new :foo, :bar
|
struct = Struct.new :foo, :bar
|
||||||
s = struct.new 4711, 'foot'
|
s = struct.new 4711, 'foot'
|
||||||
assert_raise(JSONError) { JSON(s) }
|
assert_raises(JSONError) { JSON(s) }
|
||||||
begin
|
begin
|
||||||
raise TypeError, "test me"
|
raise TypeError, "test me"
|
||||||
rescue TypeError => e
|
rescue TypeError => e
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
require 'json'
|
case ENV['JSON']
|
||||||
|
when 'pure' then require 'json/pure'
|
||||||
|
when 'ext' then require 'json/ext'
|
||||||
|
else require 'json'
|
||||||
|
end
|
||||||
|
|
||||||
class TC_JSONFixtures < Test::Unit::TestCase
|
class TC_JSONFixtures < Test::Unit::TestCase
|
||||||
def setup
|
def setup
|
||||||
|
@ -20,7 +25,7 @@ class TC_JSONFixtures < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_failing
|
def test_failing
|
||||||
for name, source in @failed
|
for name, source in @failed
|
||||||
assert_raise(JSON::ParserError, JSON::NestingError,
|
assert_raises(JSON::ParserError, JSON::NestingError,
|
||||||
"Did not fail for fixture '#{name}'") do
|
"Did not fail for fixture '#{name}'") do
|
||||||
JSON.parse(source)
|
JSON.parse(source)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
require 'json'
|
case ENV['JSON']
|
||||||
|
when 'pure' then require 'json/pure'
|
||||||
|
when 'ext' then require 'json/ext'
|
||||||
|
else require 'json'
|
||||||
|
end
|
||||||
|
|
||||||
class TC_JSONGenerate < Test::Unit::TestCase
|
class TC_JSONGenerate < Test::Unit::TestCase
|
||||||
include JSON
|
include JSON
|
||||||
|
@ -70,30 +77,30 @@ EOT
|
||||||
#assert s.check_circular
|
#assert s.check_circular
|
||||||
h = { 1=>2 }
|
h = { 1=>2 }
|
||||||
h[3] = h
|
h[3] = h
|
||||||
assert_raise(JSON::CircularDatastructure) { generate(h) }
|
assert_raises(JSON::CircularDatastructure) { generate(h) }
|
||||||
assert_raise(JSON::CircularDatastructure) { generate(h, s) }
|
assert_raises(JSON::CircularDatastructure) { generate(h, s) }
|
||||||
s = JSON.state.new(:check_circular => true)
|
s = JSON.state.new(:check_circular => true)
|
||||||
#assert s.check_circular
|
#assert s.check_circular
|
||||||
a = [ 1, 2 ]
|
a = [ 1, 2 ]
|
||||||
a << a
|
a << a
|
||||||
assert_raise(JSON::CircularDatastructure) { generate(a, s) }
|
assert_raises(JSON::CircularDatastructure) { generate(a, s) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_allow_nan
|
def test_allow_nan
|
||||||
assert_raise(GeneratorError) { generate([JSON::NaN]) }
|
assert_raises(GeneratorError) { generate([JSON::NaN]) }
|
||||||
assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)
|
assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)
|
||||||
assert_equal '[NaN]', fast_generate([JSON::NaN])
|
assert_equal '[NaN]', fast_generate([JSON::NaN])
|
||||||
assert_raise(GeneratorError) { pretty_generate([JSON::NaN]) }
|
assert_raises(GeneratorError) { pretty_generate([JSON::NaN]) }
|
||||||
assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true)
|
assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true)
|
||||||
assert_raise(GeneratorError) { generate([JSON::Infinity]) }
|
assert_raises(GeneratorError) { generate([JSON::Infinity]) }
|
||||||
assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true)
|
assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true)
|
||||||
assert_equal '[Infinity]', fast_generate([JSON::Infinity])
|
assert_equal '[Infinity]', fast_generate([JSON::Infinity])
|
||||||
assert_raise(GeneratorError) { pretty_generate([JSON::Infinity]) }
|
assert_raises(GeneratorError) { pretty_generate([JSON::Infinity]) }
|
||||||
assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true)
|
assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true)
|
||||||
assert_raise(GeneratorError) { generate([JSON::MinusInfinity]) }
|
assert_raises(GeneratorError) { generate([JSON::MinusInfinity]) }
|
||||||
assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true)
|
assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true)
|
||||||
assert_equal '[-Infinity]', fast_generate([JSON::MinusInfinity])
|
assert_equal '[-Infinity]', fast_generate([JSON::MinusInfinity])
|
||||||
assert_raise(GeneratorError) { pretty_generate([JSON::MinusInfinity]) }
|
assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) }
|
||||||
assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true)
|
assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
|
case ENV['JSON']
|
||||||
|
when 'pure' then require 'json/pure'
|
||||||
|
when 'ext' then require 'json/ext'
|
||||||
|
else require 'json'
|
||||||
|
end
|
||||||
require 'json/add/rails'
|
require 'json/add/rails'
|
||||||
require 'date'
|
require 'date'
|
||||||
|
|
||||||
|
@ -17,7 +23,7 @@ class TC_JSONRails < Test::Unit::TestCase
|
||||||
def ==(other)
|
def ==(other)
|
||||||
a == other.a
|
a == other.a
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.json_create(object)
|
def self.json_create(object)
|
||||||
new(*object['args'])
|
new(*object['args'])
|
||||||
end
|
end
|
||||||
|
@ -50,13 +56,38 @@ class TC_JSONRails < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class D
|
||||||
|
def initialize
|
||||||
|
@foo = 666
|
||||||
|
end
|
||||||
|
|
||||||
|
attr_reader :foo
|
||||||
|
|
||||||
|
def ==(other)
|
||||||
|
foo == other.foo
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_extended_json
|
def test_extended_json
|
||||||
a = A.new(666)
|
a = A.new(666)
|
||||||
assert A.json_creatable?
|
assert A.json_creatable?
|
||||||
|
assert_equal 666, a.a
|
||||||
json = generate(a)
|
json = generate(a)
|
||||||
a_again = JSON.parse(json)
|
a_again = JSON.parse(json)
|
||||||
assert_kind_of a.class, a_again
|
assert_kind_of a.class, a_again
|
||||||
assert_equal a, a_again
|
assert_equal a, a_again
|
||||||
|
assert_equal 666, a_again.a
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_extended_json_generic_object
|
||||||
|
d = D.new
|
||||||
|
assert D.json_creatable?
|
||||||
|
assert_equal 666, d.foo
|
||||||
|
json = generate(d)
|
||||||
|
d_again = JSON.parse(json)
|
||||||
|
assert_kind_of d.class, d_again
|
||||||
|
assert_equal d, d_again
|
||||||
|
assert_equal 666, d_again.foo
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_extended_json_disabled
|
def test_extended_json_disabled
|
||||||
|
@ -85,11 +116,12 @@ class TC_JSONRails < Test::Unit::TestCase
|
||||||
c = C.new # with rails addition all objects are theoretically creatable
|
c = C.new # with rails addition all objects are theoretically creatable
|
||||||
assert C.json_creatable?
|
assert C.json_creatable?
|
||||||
json = generate(c)
|
json = generate(c)
|
||||||
assert_raise(ArgumentError) { JSON.parse(json) }
|
assert_raises(ArgumentError) { JSON.parse(json) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_raw_strings
|
def test_raw_strings
|
||||||
raw = ''
|
raw = ''
|
||||||
|
raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT)
|
||||||
raw_array = []
|
raw_array = []
|
||||||
for i in 0..255
|
for i in 0..255
|
||||||
raw << i
|
raw << i
|
||||||
|
|
|
@ -2,7 +2,11 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
require 'json'
|
case ENV['JSON']
|
||||||
|
when 'pure' then require 'json/pure'
|
||||||
|
when 'ext' then require 'json/ext'
|
||||||
|
else require 'json'
|
||||||
|
end
|
||||||
|
|
||||||
class TC_JSONUnicode < Test::Unit::TestCase
|
class TC_JSONUnicode < Test::Unit::TestCase
|
||||||
include JSON
|
include JSON
|
||||||
|
|
Loading…
Add table
Reference in a new issue