summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-11-20 02:55:08 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-11-20 02:55:08 +0000
commit14cab32596d3e394cafea7a7698a81034b83c172 (patch)
tree672a0450700e77f4d0129f781ce24c1799a773fa /test
parent9d740ddebeaa46f47c9b4529e8b36d72d97f9105 (diff)
* ext/json: merge JSON 1.8.1.
https://github.com/nurse/json/compare/002ac2771ce32776b32ccd2d06e5604de6c36dcd...e09ffc0d7da25d0393873936c118c188c78dbac3 * Remove Rubinius exception since transcoding should be working now. * Fix https://github.com/flori/json/issues/162 reported by Marc-Andre Lafortune <github_rocks@marc-andre.ca>. Thanks! * Applied patches by Yui NARUSE <naruse@airemix.jp> to suppress warning with -Wchar-subscripts and better validate UTF-8 strings. * Applied patch by ginriki@github to remove unnecessary if. * Add load/dump interface to JSON::GenericObject to make serialize :some_attribute, JSON::GenericObject work in Rails active models for convenient SomeModel#some_attribute.foo.bar access to serialised JSON data. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43731 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rwxr-xr-xtest/json/test_json_generate.rb54
-rw-r--r--test/json/test_json_generic_object.rb15
2 files changed, 59 insertions, 10 deletions
diff --git a/test/json/test_json_generate.rb b/test/json/test_json_generate.rb
index 5c1042d956..618b933bac 100755
--- a/test/json/test_json_generate.rb
+++ b/test/json/test_json_generate.rb
@@ -3,7 +3,6 @@
require 'test/unit'
require File.join(File.dirname(__FILE__), 'setup_variant')
-require_relative '../ruby/envutil.rb'
class TestJSONGenerate < Test::Unit::TestCase
include JSON
@@ -216,14 +215,15 @@ EOT
end
def test_gc
- assert_separately %w[-rjson --disable-gems], <<-EOS, timeout: 5
+ require_relative '../ruby/envutil.rb'
+ assert_in_out_err(%w[-rjson --disable-gems], <<-EOS, [], [])
bignum_too_long_to_embed_as_string = 1234567890123456789012345
expect = bignum_too_long_to_embed_as_string.to_s
GC.stress = true
10.times do |i|
tmp = bignum_too_long_to_embed_as_string.to_json
- assert_equal expect, tmp
+ raise "'\#{expect}' is expected, but '\#{tmp}'" unless tmp == expect
end
EOS
end if GC.respond_to?(:stress=)
@@ -252,15 +252,43 @@ EOT
assert_equal '5', state2.array_nl
end
- def test_broken_bignum # [ruby-core:38867]
- assert_separately %w[-rjson --disable-gems], <<-EOS, timeout: 5
- Bignum.class_eval do
- def to_s
+ def test_configure_hash_conversion
+ state = JSON.state.new
+ state.configure(:indent => '1')
+ assert_equal '1', state.indent
+ state = JSON.state.new
+ foo = 'foo'
+ assert_raise(TypeError) do
+ state.configure(foo)
+ end
+ def foo.to_h
+ { :indent => '2' }
+ end
+ state.configure(foo)
+ assert_equal '2', state.indent
+ end
+
+ 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
- assert_raise(TypeError){ JSON::Ext::Generator::State.new.generate(1<<64) }
- EOS
- end if defined?(JSON::Ext::Generator)
+ _, 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
def test_hash_likeness_set_symbol
state = JSON.state.new
@@ -286,4 +314,10 @@ EOT
assert_kind_of Hash, state_hash
assert_equal :bar, state_hash[:foo]
end
+
+ def test_json_generate
+ assert_raise JSON::GeneratorError do
+ assert_equal true, JSON.generate(["\xea"])
+ end
+ end
end
diff --git a/test/json/test_json_generic_object.rb b/test/json/test_json_generic_object.rb
index 77ef22e6ae..c43c7762be 100644
--- a/test/json/test_json_generic_object.rb
+++ b/test/json/test_json_generic_object.rb
@@ -49,6 +49,21 @@ class TestJSONGenericObject < Test::Unit::TestCase
assert_equal true, GenericObject.from_hash(true)
end
+ def test_json_generic_object_load
+ empty = JSON::GenericObject.load(nil)
+ assert_kind_of JSON::GenericObject, empty
+ simple_json = '{"json_class":"JSON::GenericObject","hello":"world"}'
+ simple = JSON::GenericObject.load(simple_json)
+ assert_kind_of JSON::GenericObject, simple
+ assert_equal "world", simple.hello
+ converting = JSON::GenericObject.load('{ "hello": "world" }')
+ assert_kind_of JSON::GenericObject, converting
+ assert_equal "world", converting.hello
+
+ json = JSON::GenericObject.dump(JSON::GenericObject[:hello => 'world'])
+ assert_equal JSON(json), JSON('{"json_class":"JSON::GenericObject","hello":"world"}')
+ end
+
private
def switch_json_creatable