summaryrefslogtreecommitdiff
path: root/test/json
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-20 17:41:14 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-20 17:41:14 +0000
commite8eb95ec33334f40f3f3bbcbf16404aee07dfa8e (patch)
treec7fade34f792e138365b59af246ea316616c04e7 /test/json
parenta91cd48aa19dd31ec5abb6e4b982f2077bedcbcd (diff)
* ext/json: import JSON v 1.1.3.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19436 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/json')
-rw-r--r--test/json/fixtures/fail18.json2
-rwxr-xr-xtest/json/runner.rb1
-rwxr-xr-xtest/json/test_json.rb64
-rwxr-xr-xtest/json/test_json_addition.rb38
-rw-r--r--test/json/test_json_generate.rb19
-rw-r--r--test/json/test_json_rails.rb11
-rwxr-xr-xtest/json/test_json_unicode.rb5
7 files changed, 104 insertions, 36 deletions
diff --git a/test/json/fixtures/fail18.json b/test/json/fixtures/fail18.json
index d61a345..e2d130c 100644
--- a/test/json/fixtures/fail18.json
+++ b/test/json/fixtures/fail18.json
@@ -1 +1 @@
-[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]] // No, we don't limit our depth: Moved to pass...
+[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]
diff --git a/test/json/runner.rb b/test/json/runner.rb
index 91bc12a..9ad0448 100755
--- a/test/json/runner.rb
+++ b/test/json/runner.rb
@@ -23,4 +23,3 @@ class TS_AllTests
end
end
Test::Unit::UI::Console::TestRunner.run(TS_AllTests)
- # vim: set et sw=2 ts=2:
diff --git a/test/json/test_json.rb b/test/json/test_json.rb
index 257b8e0..0ee993e 100755
--- a/test/json/test_json.rb
+++ b/test/json/test_json.rb
@@ -3,6 +3,7 @@
require 'test/unit'
require 'json'
+require 'stringio'
class TC_JSON < Test::Unit::TestCase
include JSON
@@ -61,6 +62,12 @@ 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('[-3141e-3]')
+ assert_raises(ParserError) { parse('[NaN]') }
+ assert parse('[NaN]', :allow_nan => true).first.nan?
+ assert_raises(ParserError) { parse('[Infinity]') }
+ assert_equal [1.0/0], parse('[Infinity]', :allow_nan => true)
+ assert_raises(ParserError) { parse('[-Infinity]') }
+ assert_equal [-1.0/0], parse('[-Infinity]', :allow_nan => true)
assert_equal([""], parse('[""]'))
assert_equal(["foobar"], parse('["foobar"]'))
assert_equal([{}], parse('[{}]'))
@@ -238,18 +245,49 @@ EOT
end
def test_nesting
- to_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]'
- assert_raises(JSON::NestingError) { JSON.parse to_deep }
- assert_raises(JSON::NestingError) { JSON.parser.new(to_deep).parse }
- assert_raises(JSON::NestingError) { JSON.parse to_deep, :max_nesting => 19 }
- ok = JSON.parse to_deep, :max_nesting => 20
- assert_kind_of Array, ok
- ok = JSON.parse to_deep, :max_nesting => nil
- assert_kind_of Array, ok
- ok = JSON.parse to_deep, :max_nesting => false
- assert_kind_of Array, ok
- ok = JSON.parse to_deep, :max_nesting => 0
- assert_kind_of Array, ok
+ assert_raises(JSON::NestingError) { JSON.parse '[[]]', :max_nesting => 1 }
+ assert_raises(JSON::NestingError) { JSON.parser.new('[[]]', :max_nesting => 1).parse }
+ assert_equal [[]], JSON.parse('[[]]', :max_nesting => 2)
+ too_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]'
+ too_deep_ary = eval too_deep
+ assert_raises(JSON::NestingError) { JSON.parse too_deep }
+ assert_raises(JSON::NestingError) { JSON.parser.new(too_deep).parse }
+ assert_raises(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 19 }
+ ok = JSON.parse too_deep, :max_nesting => 20
+ assert_equal too_deep_ary, ok
+ ok = JSON.parse too_deep, :max_nesting => nil
+ assert_equal too_deep_ary, ok
+ ok = JSON.parse too_deep, :max_nesting => false
+ assert_equal too_deep_ary, ok
+ ok = JSON.parse too_deep, :max_nesting => 0
+ assert_equal too_deep_ary, ok
+ assert_raises(JSON::NestingError) { JSON.generate [[]], :max_nesting => 1 }
+ assert_equal '[[]]', JSON.generate([[]], :max_nesting => 2)
+ assert_raises(JSON::NestingError) { JSON.generate too_deep_ary }
+ assert_raises(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 19 }
+ ok = JSON.generate too_deep_ary, :max_nesting => 20
+ assert_equal too_deep, ok
+ ok = JSON.generate too_deep_ary, :max_nesting => nil
+ assert_equal too_deep, ok
+ ok = JSON.generate too_deep_ary, :max_nesting => false
+ assert_equal too_deep, ok
+ ok = JSON.generate too_deep_ary, :max_nesting => 0
+ assert_equal too_deep, ok
+ end
+
+ def test_load_dump
+ too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]'
+ assert_equal too_deep, JSON.dump(eval(too_deep))
+ assert_kind_of String, Marshal.dump(eval(too_deep))
+ assert_raises(ArgumentError) { JSON.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_kind_of String, Marshal.dump(eval(too_deep), 20)
+ output = StringIO.new
+ JSON.dump(eval(too_deep), output)
+ assert_equal too_deep, output.string
+ output = StringIO.new
+ JSON.dump(eval(too_deep), output, 20)
+ assert_equal too_deep, output.string
end
end
- # vim: set et sw=2 ts=2:
diff --git a/test/json/test_json_addition.rb b/test/json/test_json_addition.rb
index 04c690c..cf8a92a 100755
--- a/test/json/test_json_addition.rb
+++ b/test/json/test_json_addition.rb
@@ -31,6 +31,10 @@ class TC_JSONAddition < Test::Unit::TestCase
end
class B
+ def self.json_creatable?
+ false
+ end
+
def to_json(*args)
{
'json_class' => self.class.name,
@@ -50,9 +54,6 @@ class TC_JSONAddition < Test::Unit::TestCase
end
end
- def setup
- end
-
def test_extended_json
a = A.new(666)
assert A.json_creatable?
@@ -77,14 +78,14 @@ class TC_JSONAddition < Test::Unit::TestCase
)
end
- def test_extended_json_fail
+ def test_extended_json_fail1
b = B.new
assert !B.json_creatable?
json = generate(b)
- assert_equal({ 'json_class' => B.name }, JSON.parse(json))
+ assert_equal({ "json_class"=>"TC_JSONAddition::B" }, JSON.parse(json))
end
- def test_extended_json_fail
+ def test_extended_json_fail2
c = C.new
assert !C.json_creatable?
json = generate(c)
@@ -111,9 +112,11 @@ EOT
assert_equal raw, raw_again
end
+ MyJsonStruct = Struct.new 'MyJsonStruct', :foo, :bar
+
def test_core
- t = Time.at(1198254128, 895990)
- assert_equal t, JSON(JSON(t))
+ t = Time.now
+ assert_equal t.inspect, JSON(JSON(t)).inspect
d = Date.today
assert_equal d, JSON(JSON(d))
d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161)
@@ -122,8 +125,7 @@ EOT
assert_equal 1...10, JSON(JSON(1...10))
assert_equal "a".."c", JSON(JSON("a".."c"))
assert_equal "a"..."c", JSON(JSON("a"..."c"))
- struct = Struct.new 'MyJsonStruct', :foo, :bar
- s = struct.new 4711, 'foot'
+ s = MyJsonStruct.new 4711, 'foot'
assert_equal s, JSON(JSON(s))
struct = Struct.new :foo, :bar
s = struct.new 4711, 'foot'
@@ -137,7 +139,19 @@ EOT
assert_equal e.message, e_again.message
assert_equal e.backtrace, e_again.backtrace
end
- assert_equal /foo/, JSON(JSON(/foo/))
- assert_equal /foo/i, JSON(JSON(/foo/i))
+ assert_equal(/foo/, JSON(JSON(/foo/)))
+ assert_equal(/foo/i, JSON(JSON(/foo/i)))
+ end
+
+ def test_utc_datetime
+ now = Time.now
+ d = DateTime.parse(now.to_s) # usual case
+ assert d, JSON.parse(d.to_json)
+ d = DateTime.parse(now.utc.to_s) # of = 0
+ assert d, JSON.parse(d.to_json)
+ d = DateTime.civil(2008, 6, 17, 11, 48, 32, 1) # of = 1 / 12 => 1/12
+ assert d, JSON.parse(d.to_json)
+ d = DateTime.civil(2008, 6, 17, 11, 48, 32, 12) # of = 12 / 12 => 12
+ assert d, JSON.parse(d.to_json)
end
end
diff --git a/test/json/test_json_generate.rb b/test/json/test_json_generate.rb
index e720b2d..b7e0bc2 100644
--- a/test/json/test_json_generate.rb
+++ b/test/json/test_json_generate.rb
@@ -70,6 +70,7 @@ EOT
#assert s.check_circular
h = { 1=>2 }
h[3] = h
+ assert_raises(JSON::CircularDatastructure) { generate(h) }
assert_raises(JSON::CircularDatastructure) { generate(h, s) }
s = JSON.state.new(:check_circular => true)
#assert s.check_circular
@@ -77,4 +78,22 @@ EOT
a << a
assert_raises(JSON::CircularDatastructure) { generate(a, s) }
end
+
+ def test_allow_nan
+ assert_raises(GeneratorError) { generate([JSON::NaN]) }
+ assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)
+ assert_equal '[NaN]', fast_generate([JSON::NaN])
+ assert_raises(GeneratorError) { pretty_generate([JSON::NaN]) }
+ assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true)
+ assert_raises(GeneratorError) { generate([JSON::Infinity]) }
+ assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true)
+ assert_equal '[Infinity]', fast_generate([JSON::Infinity])
+ assert_raises(GeneratorError) { pretty_generate([JSON::Infinity]) }
+ assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true)
+ assert_raises(GeneratorError) { generate([JSON::MinusInfinity]) }
+ assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true)
+ assert_equal '[-Infinity]', fast_generate([JSON::MinusInfinity])
+ assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) }
+ assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true)
+ end
end
diff --git a/test/json/test_json_rails.rb b/test/json/test_json_rails.rb
index e44ea60..c0447dd 100644
--- a/test/json/test_json_rails.rb
+++ b/test/json/test_json_rails.rb
@@ -31,6 +31,10 @@ class TC_JSONRails < Test::Unit::TestCase
end
class B
+ def self.json_creatable?
+ false
+ end
+
def to_json(*args)
{
'json_class' => self.class.name,
@@ -46,9 +50,6 @@ class TC_JSONRails < Test::Unit::TestCase
end
end
- def setup
- end
-
def test_extended_json
a = A.new(666)
assert A.json_creatable?
@@ -73,14 +74,14 @@ class TC_JSONRails < Test::Unit::TestCase
)
end
- def test_extended_json_fail
+ def test_extended_json_fail1
b = B.new
assert !B.json_creatable?
json = generate(b)
assert_equal({ 'json_class' => B.name }, JSON.parse(json))
end
- def test_extended_json_fail
+ def test_extended_json_fail2
c = C.new # with rails addition all objects are theoretically creatable
assert C.json_creatable?
json = generate(c)
diff --git a/test/json/test_json_unicode.rb b/test/json/test_json_unicode.rb
index cad9384..862c6ea 100755
--- a/test/json/test_json_unicode.rb
+++ b/test/json/test_json_unicode.rb
@@ -7,9 +7,6 @@ require 'json'
class TC_JSONUnicode < Test::Unit::TestCase
include JSON
- def setup
- end
-
def test_unicode
assert_equal '""', ''.to_json
assert_equal '"\\b"', "\b".to_json
@@ -54,7 +51,7 @@ class TC_JSONUnicode < Test::Unit::TestCase
end
end
assert_raises(JSON::GeneratorError) do
- JSON.generate(["" << 0x80])
+ JSON.generate(["\x80"])
end
assert_equal "\302\200", JSON.parse('["\u0080"]').first
end