summaryrefslogtreecommitdiff
path: root/test/json
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-04-26 00:06:35 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-04-26 00:06:35 +0000
commit54592ad6276a17d95f2186c8631197ddb172020b (patch)
treefd071c795bc3e589237d89dd593f93d7b0376a4f /test/json
parent9372cdb80a376f352fc55ea895f1ad688cf4a0df (diff)
* ext/json: Update to JSON 1.4.1.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27493 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/json')
-rwxr-xr-xtest/json/test_json.rb73
-rwxr-xr-xtest/json/test_json_addition.rb10
-rw-r--r--test/json/test_json_encoding.rb68
-rwxr-xr-x[-rw-r--r--]test/json/test_json_generate.rb44
-rwxr-xr-x[-rw-r--r--]test/json/test_json_rails.rb12
-rwxr-xr-xtest/json/test_json_unicode.rb26
6 files changed, 181 insertions, 52 deletions
diff --git a/test/json/test_json.rb b/test/json/test_json.rb
index ce90d6e..f5a432f 100755
--- a/test/json/test_json.rb
+++ b/test/json/test_json.rb
@@ -9,6 +9,20 @@ else require 'json'
end
require 'stringio'
+unless Array.method_defined?(:permutation)
+ begin
+ require 'enumerator'
+ require 'permutation'
+ class Array
+ def permutation
+ Permutation.for(self).to_enum.map { |x| x.project }
+ end
+ end
+ rescue LoadError
+ warn "Skipping permutation tests."
+ end
+end
+
class TC_JSON < Test::Unit::TestCase
include JSON
@@ -94,22 +108,23 @@ class TC_JSON < Test::Unit::TestCase
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
end
- def test_parse_more_complex_arrays
- a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
- a.permutation.each do |perm|
- orig_ary = a
- json = pretty_generate(orig_ary)
- assert_equal orig_ary, parse(json)
+ if Array.method_defined?(:permutation)
+ def test_parse_more_complex_arrays
+ a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
+ a.permutation.each do |perm|
+ json = pretty_generate(perm)
+ assert_equal perm, parse(json)
+ end
end
- end
- def test_parse_complex_objects
- a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
- a.permutation.each do |perm|
- s = "a"
- orig_obj = a.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
- json = pretty_generate(orig_obj)
- assert_equal orig_obj, parse(json)
+ def test_parse_complex_objects
+ a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
+ a.permutation.each do |perm|
+ s = "a"
+ orig_obj = perm.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
+ json = pretty_generate(orig_obj)
+ assert_equal orig_obj, parse(json)
+ end
end
end
@@ -215,27 +230,27 @@ EOT
def test_backslash
data = [ '\\.(?i:gif|jpe?g|png)$' ]
json = '["\\\\.(?i:gif|jpe?g|png)$"]'
- assert_equal json, JSON.unparse(data)
+ assert_equal json, JSON.generate(data)
assert_equal data, JSON.parse(json)
#
data = [ '\\"' ]
json = '["\\\\\""]'
- assert_equal json, JSON.unparse(data)
+ assert_equal json, JSON.generate(data)
assert_equal data, JSON.parse(json)
#
json = '["/"]'
data = JSON.parse(json)
assert_equal ['/'], data
- assert_equal json, JSON.unparse(data)
+ assert_equal json, JSON.generate(data)
#
json = '["\""]'
data = JSON.parse(json)
assert_equal ['"'], data
- assert_equal json, JSON.unparse(data)
+ assert_equal json, JSON.generate(data)
json = '["\\\'"]'
data = JSON.parse(json)
assert_equal ["'"], data
- assert_equal '["\'"]', JSON.unparse(data)
+ assert_equal '["\'"]', JSON.generate(data)
end
def test_wrong_inputs
@@ -287,6 +302,13 @@ EOT
assert_equal too_deep, ok
end
+ def test_symbolize_names
+ assert_equal({ "foo" => "bar", "baz" => "quux" },
+ JSON.parse('{"foo":"bar", "baz":"quux"}'))
+ assert_equal({ :foo => "bar", :baz => "quux" },
+ JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
+ end
+
def test_load_dump
too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]'
assert_equal too_deep, JSON.dump(eval(too_deep))
@@ -302,4 +324,17 @@ EOT
JSON.dump(eval(too_deep), output, 20)
assert_equal too_deep, output.string
end
+
+ def test_big_integers
+ json1 = JSON([orig = (1 << 31) - 1])
+ assert_equal orig, JSON[json1][0]
+ json2 = JSON([orig = 1 << 31])
+ assert_equal orig, JSON[json2][0]
+ json3 = JSON([orig = (1 << 62) - 1])
+ assert_equal orig, JSON[json3][0]
+ json4 = JSON([orig = 1 << 62])
+ assert_equal orig, JSON[json4][0]
+ json5 = JSON([orig = 1 << 64])
+ assert_equal orig, JSON[json5][0]
+ end
end
diff --git a/test/json/test_json_addition.rb b/test/json/test_json_addition.rb
index 51e4a67..ec89c07 100755
--- a/test/json/test_json_addition.rb
+++ b/test/json/test_json_addition.rb
@@ -95,7 +95,7 @@ class TC_JSONAddition < Test::Unit::TestCase
c = C.new
assert !C.json_creatable?
json = generate(c)
- assert_raises(ArgumentError) { JSON.parse(json) }
+ assert_raises(ArgumentError, NameError) { JSON.parse(json) }
end
def test_raw_strings
@@ -110,11 +110,9 @@ class TC_JSONAddition < Test::Unit::TestCase
json_raw_object = raw.to_json_raw_object
hash = { 'json_class' => 'String', 'raw'=> raw_array }
assert_equal hash, json_raw_object
- json_raw = <<EOT.chomp
-{\"json_class\":\"String\",\"raw\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255]}
-EOT
-# "
- assert_equal json_raw, json
+ assert_match /\A\{.*\}\Z/, json
+ assert_match /"json_class":"String"/, json
+ assert_match /"raw":\[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255\]/, json
raw_again = JSON.parse(json)
assert_equal raw, raw_again
end
diff --git a/test/json/test_json_encoding.rb b/test/json/test_json_encoding.rb
new file mode 100644
index 0000000..fdea329
--- /dev/null
+++ b/test/json/test_json_encoding.rb
@@ -0,0 +1,68 @@
+#!/usr/bin/env ruby
+# -*- 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 'iconv'
+
+class TC_JSONEncoding < Test::Unit::TestCase
+ include JSON
+
+ def setup
+ @utf_8 = '["© ≠ €!"]'
+ @parsed = [ "© ≠ €!" ]
+ @utf_16_data = Iconv.iconv('utf-16be', 'utf-8', @parsed.first)
+ @generated = '["\u00a9 \u2260 \u20ac!"]'
+ if defined?(::Encoding)
+ @utf_8_ascii_8bit = @utf_8.dup.force_encoding(Encoding::ASCII_8BIT)
+ @utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
+ @utf_16be_ascii_8bit = @utf_16be.dup.force_encoding(Encoding::ASCII_8BIT)
+ @utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
+ @utf_16le_ascii_8bit = @utf_16le.dup.force_encoding(Encoding::ASCII_8BIT)
+ @utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
+ @utf_32be_ascii_8bit = @utf_32be.dup.force_encoding(Encoding::ASCII_8BIT)
+ @utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
+ @utf_32le_ascii_8bit = @utf_32le.dup.force_encoding(Encoding::ASCII_8BIT)
+ else
+ @utf_8_ascii_8bit = @utf_8.dup
+ @utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
+ @utf_16be_ascii_8bit = @utf_16be.dup
+ @utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
+ @utf_16le_ascii_8bit = @utf_16le.dup
+ @utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
+ @utf_32be_ascii_8bit = @utf_32be.dup
+ @utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
+ @utf_32le_ascii_8bit = @utf_32le.dup
+ end
+ end
+
+ def test_parse
+ assert_equal @parsed, JSON.parse(@utf_8)
+ assert_equal @parsed, JSON.parse(@utf_16be)
+ assert_equal @parsed, JSON.parse(@utf_16le)
+ assert_equal @parsed, JSON.parse(@utf_32be)
+ assert_equal @parsed, JSON.parse(@utf_32le)
+ end
+
+ def test_parse_ascii_8bit
+ assert_equal @parsed, JSON.parse(@utf_8_ascii_8bit)
+ assert_equal @parsed, JSON.parse(@utf_16be_ascii_8bit)
+ assert_equal @parsed, JSON.parse(@utf_16le_ascii_8bit)
+ assert_equal @parsed, JSON.parse(@utf_32be_ascii_8bit)
+ assert_equal @parsed, JSON.parse(@utf_32le_ascii_8bit)
+ end
+
+ def test_generate
+ assert_equal @generated, JSON.generate(@parsed, :ascii_only => true)
+ if defined?(::Encoding)
+ assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true)
+ else
+ # XXX checking of correct utf8 data is not as strict (yet?) without :ascii_only
+ assert_raises(JSON::GeneratorError) { JSON.generate(@utf_16_data, :ascii_only => true) }
+ end
+ end
+end
diff --git a/test/json/test_json_generate.rb b/test/json/test_json_generate.rb
index bbb75ba..26ca1ad 100644..100755
--- a/test/json/test_json_generate.rb
+++ b/test/json/test_json_generate.rb
@@ -44,8 +44,8 @@ class TC_JSONGenerate < Test::Unit::TestCase
EOT
end
- def test_unparse
- json = unparse(@hash)
+ def test_generate
+ json = generate(@hash)
assert_equal(JSON.parse(@json2), JSON.parse(json))
parsed_json = parse(json)
assert_equal(@hash, parsed_json)
@@ -53,10 +53,11 @@ EOT
assert_equal('{"1":2}', json)
parsed_json = parse(json)
assert_equal({"1"=>2}, parsed_json)
+ assert_raise(GeneratorError) { generate(666) }
end
- def test_unparse_pretty
- json = pretty_unparse(@hash)
+ def test_generate_pretty
+ json = pretty_generate(@hash)
assert_equal(JSON.parse(@json3), JSON.parse(json))
parsed_json = parse(json)
assert_equal(@hash, parsed_json)
@@ -68,38 +69,53 @@ EOT
EOT
parsed_json = parse(json)
assert_equal({"1"=>2}, parsed_json)
+ assert_raise(GeneratorError) { pretty_generate(666) }
+ end
+
+ def test_fast_generate
+ json = fast_generate(@hash)
+ assert_equal(JSON.parse(@json2), JSON.parse(json))
+ parsed_json = parse(json)
+ assert_equal(@hash, parsed_json)
+ json = fast_generate({1=>2})
+ assert_equal('{"1":2}', json)
+ parsed_json = parse(json)
+ assert_equal({"1"=>2}, parsed_json)
+ assert_raise(GeneratorError) { fast_generate(666) }
end
def test_states
json = generate({1=>2}, nil)
assert_equal('{"1":2}', json)
- s = JSON.state.new(:check_circular => true)
- #assert s.check_circular
+ s = JSON.state.new
+ assert s.check_circular?
+ 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
+ assert_raises(JSON::NestingError) { generate(h) }
+ assert_raises(JSON::NestingError) { generate(h, s) }
+ s = JSON.state.new
a = [ 1, 2 ]
a << a
- assert_raises(JSON::CircularDatastructure) { generate(a, s) }
+ assert_raises(JSON::NestingError) { generate(a, s) }
+ assert s.check_circular?
+ assert s[:check_circular?]
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) { 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) { 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) { fast_generate([JSON::MinusInfinity]) }
assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) }
assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true)
end
diff --git a/test/json/test_json_rails.rb b/test/json/test_json_rails.rb
index 341d332..6fd201b 100644..100755
--- a/test/json/test_json_rails.rb
+++ b/test/json/test_json_rails.rb
@@ -116,7 +116,7 @@ class TC_JSONRails < Test::Unit::TestCase
c = C.new # with rails addition all objects are theoretically creatable
assert C.json_creatable?
json = generate(c)
- assert_raises(ArgumentError) { JSON.parse(json) }
+ assert_raises(ArgumentError, NameError) { JSON.parse(json) }
end
def test_raw_strings
@@ -131,16 +131,14 @@ class TC_JSONRails < Test::Unit::TestCase
json_raw_object = raw.to_json_raw_object
hash = { 'json_class' => 'String', 'raw'=> raw_array }
assert_equal hash, json_raw_object
- json_raw = <<EOT.chomp
-{\"json_class\":\"String\",\"raw\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255]}
-EOT
-# "
- assert_equal json_raw, json
+ assert_match /\A\{.*\}\Z/, json
+ assert_match /"json_class":"String"/, json
+ assert_match /"raw":\[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255\]/, json
raw_again = JSON.parse(json)
assert_equal raw, raw_again
end
def test_symbol
- assert_equal '"foo"', JSON(:foo) # we don't want an object here
+ assert_equal '"foo"', :foo.to_json # we don't want an object here
end
end
diff --git a/test/json/test_json_unicode.rb b/test/json/test_json_unicode.rb
index 1454fe1..505f5d5 100755
--- a/test/json/test_json_unicode.rb
+++ b/test/json/test_json_unicode.rb
@@ -19,22 +19,36 @@ class TC_JSONUnicode < Test::Unit::TestCase
assert_equal '" "', ' '.to_json
assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json
utf8 = [ "© ≠ €! \01" ]
+ json = '["© ≠ €! \u0001"]'
+ assert_equal json, utf8.to_json(:ascii_only => false)
+ assert_equal utf8, parse(json)
json = '["\u00a9 \u2260 \u20ac! \u0001"]'
- assert_equal json, utf8.to_json
+ assert_equal json, utf8.to_json(:ascii_only => true)
+ assert_equal utf8, parse(json)
+ utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
+ json = "[\"\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212\"]"
assert_equal utf8, parse(json)
+ assert_equal json, utf8.to_json(:ascii_only => false)
utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
+ assert_equal utf8, parse(json)
json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]"
- assert_equal json, utf8.to_json
+ assert_equal json, utf8.to_json(:ascii_only => true)
assert_equal utf8, parse(json)
utf8 = ['საქართველო']
+ json = '["საქართველო"]'
+ assert_equal json, utf8.to_json(:ascii_only => false)
json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]"
- assert_equal json, utf8.to_json
+ assert_equal json, utf8.to_json(:ascii_only => true)
assert_equal utf8, parse(json)
- assert_equal '["\\u00c3"]', JSON.generate(["Ã"])
+ assert_equal '["Ã"]', JSON.generate(["Ã"], :ascii_only => false)
+ assert_equal '["\\u00c3"]', JSON.generate(["Ã"], :ascii_only => true)
assert_equal ["€"], JSON.parse('["\u20ac"]')
utf8 = ["\xf0\xa0\x80\x81"]
+ json = "[\"\xf0\xa0\x80\x81\"]"
+ assert_equal json, JSON.generate(utf8, :ascii_only => false)
+ assert_equal utf8, JSON.parse(json)
json = '["\ud840\udc01"]'
- assert_equal json, JSON.generate(utf8)
+ assert_equal json, JSON.generate(utf8, :ascii_only => true)
assert_equal utf8, JSON.parse(json)
end
@@ -55,7 +69,7 @@ class TC_JSONUnicode < Test::Unit::TestCase
end
end
assert_raise(JSON::GeneratorError) do
- JSON.generate(["\x80"])
+ JSON.generate(["\x80"], :ascii_only => true)
end
assert_equal "\302\200", JSON.parse('["\u0080"]').first
end