From a2e497d5ede45bd4f4a57f494027020d7bd1733b Mon Sep 17 00:00:00 2001 From: naruse Date: Sun, 10 Jul 2011 08:01:04 +0000 Subject: * ext/json: Merge json gem 1.5.4+ (f7f78896607b6f6226cd). [Bug #4700] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32493 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/json/setup_variant.rb | 11 +++++++ test/json/test_json.rb | 60 ++++++++++++++++++++-------------- test/json/test_json_addition.rb | 15 ++++++--- test/json/test_json_encoding.rb | 6 +--- test/json/test_json_fixtures.rb | 17 +++++----- test/json/test_json_generate.rb | 7 ++-- test/json/test_json_string_matching.rb | 40 +++++++++++++++++++++++ test/json/test_json_unicode.rb | 6 +--- 8 files changed, 109 insertions(+), 53 deletions(-) create mode 100644 test/json/setup_variant.rb create mode 100644 test/json/test_json_string_matching.rb (limited to 'test/json') diff --git a/test/json/setup_variant.rb b/test/json/setup_variant.rb new file mode 100644 index 0000000000..2dab184bc4 --- /dev/null +++ b/test/json/setup_variant.rb @@ -0,0 +1,11 @@ +case ENV['JSON'] +when 'pure' + $:.unshift 'lib' + require 'json/pure' +when 'ext' + $:.unshift 'ext', 'lib' + require 'json/ext' +else + $:.unshift 'ext', 'lib' + require 'json' +end diff --git a/test/json/test_json.rb b/test/json/test_json.rb index 5f186f0c08..b367e906b9 100755 --- a/test/json/test_json.rb +++ b/test/json/test_json.rb @@ -2,11 +2,7 @@ # -*- 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 File.join(File.dirname(__FILE__), 'setup_variant') require 'stringio' unless Array.method_defined?(:permutation) @@ -154,11 +150,20 @@ class TC_JSON < Test::Unit::TestCase assert_equal(@ary, parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\ ',[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]\s , [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] })) end - class SubArray < Array; end + class SubArray < Array + def <<(v) + @shifted = true + super + end + + def shifted? + @shifted + end + end class SubArray2 < Array def to_json(*a) @@ -175,9 +180,10 @@ class TC_JSON < Test::Unit::TestCase end def test_parse_array_custom_class - res = parse('[]', :array_class => SubArray) - assert_equal([], res) + res = parse('[1,2]', :array_class => SubArray) + assert_equal([1,2], res) assert_equal(SubArray, res.class) + assert res.shifted? end def test_parse_object @@ -188,6 +194,14 @@ class TC_JSON < Test::Unit::TestCase end class SubHash < Hash + def []=(k, v) + @item_set = true + super + end + + def item_set? + @item_set + end end class SubHash2 < Hash @@ -204,9 +218,10 @@ class TC_JSON < Test::Unit::TestCase end def test_parse_object_custom_class - res = parse('{}', :object_class => SubHash2) - assert_equal({}, res) - assert_equal(SubHash2, res.class) + res = parse('{"foo":"bar"}', :object_class => SubHash) + assert_equal({"foo" => "bar"}, res) + assert_equal(SubHash, res.class) + assert res.item_set? end def test_generation_of_core_subclasses_with_new_to_json @@ -392,23 +407,18 @@ EOT assert_equal orig, JSON[json5][0] end - def test_allocate - json = JSON::Parser.new("{}") - assert_raises(TypeError, '[ruby-core:35079]') {json.__send__(:initialize, "{}")} - json = JSON::Parser.allocate - assert_raises(TypeError, '[ruby-core:35079]') {json.source} + if defined?(JSON::Ext::Parser) + def test_allocate + parser = JSON::Ext::Parser.new("{}") + assert_raise(TypeError, '[ruby-core:35079]') {parser.__send__(:initialize, "{}")} + parser = JSON::Ext::Parser.allocate + assert_raise(TypeError, '[ruby-core:35079]') {parser.source} + end end def test_argument_encoding source = "{}".force_encoding("ascii-8bit") JSON::Parser.new(source) assert_equal Encoding::ASCII_8BIT, source.encoding - end - - def test_frozen_argument - source = "{}".force_encoding("ascii-8bit") - source.freeze - parser = nil - assert_nothing_raised {parser = JSON::Parser.new(source)} - end + end if defined?(Encoding::ASCII_8BIT) end diff --git a/test/json/test_json_addition.rb b/test/json/test_json_addition.rb index 36b89e5c35..a8181e8d74 100755 --- a/test/json/test_json_addition.rb +++ b/test/json/test_json_addition.rb @@ -2,11 +2,7 @@ # -*- 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 File.join(File.dirname(__FILE__), 'setup_variant') load 'json/add/core.rb' require 'date' @@ -36,6 +32,15 @@ class TC_JSONAddition < Test::Unit::TestCase end end + class A2 < A + def to_json(*args) + { + 'json_class' => self.class.name, + 'args' => [ @a ], + }.to_json(*args) + end + end + class B def self.json_creatable? false diff --git a/test/json/test_json_encoding.rb b/test/json/test_json_encoding.rb index cdeca58092..7af5e63a73 100644 --- a/test/json/test_json_encoding.rb +++ b/test/json/test_json_encoding.rb @@ -2,11 +2,7 @@ # -*- 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 File.join(File.dirname(__FILE__), 'setup_variant') class TC_JSONEncoding < Test::Unit::TestCase include JSON diff --git a/test/json/test_json_fixtures.rb b/test/json/test_json_fixtures.rb index 95e57ebfdc..e9df8f5b1c 100755 --- a/test/json/test_json_fixtures.rb +++ b/test/json/test_json_fixtures.rb @@ -2,11 +2,7 @@ # -*- 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 File.join(File.dirname(__FILE__), 'setup_variant') class TC_JSONFixtures < Test::Unit::TestCase def setup @@ -18,15 +14,20 @@ class TC_JSONFixtures < Test::Unit::TestCase def test_passing for name, source in @passed - assert JSON.parse(source), - "Did not pass for fixture '#{name}'" + begin + assert JSON.parse(source), + "Did not pass for fixture '#{name}': #{source.inspect}" + rescue => e + warn "\nCaught #{e.class}(#{e}) for fixture '#{name}': #{source.inspect}\n#{e.backtrace * "\n"}" + raise e + end end end def test_failing for name, source in @failed assert_raises(JSON::ParserError, JSON::NestingError, - "Did not fail for fixture '#{name}'") do + "Did not fail for fixture '#{name}': #{source.inspect}") do JSON.parse(source) end end diff --git a/test/json/test_json_generate.rb b/test/json/test_json_generate.rb index 5380a0645e..9b0cff4b07 100755 --- a/test/json/test_json_generate.rb +++ b/test/json/test_json_generate.rb @@ -2,11 +2,7 @@ # -*- 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 File.join(File.dirname(__FILE__), 'setup_variant') class TC_JSONGenerate < Test::Unit::TestCase include JSON @@ -58,6 +54,7 @@ EOT def test_generate_pretty json = pretty_generate(@hash) + # hashes aren't (insertion) ordered on every ruby implementation assert_equal(@json3, json) assert_equal(JSON.parse(@json3), JSON.parse(json)) parsed_json = parse(json) assert_equal(@hash, parsed_json) diff --git a/test/json/test_json_string_matching.rb b/test/json/test_json_string_matching.rb new file mode 100644 index 0000000000..df26a68a4d --- /dev/null +++ b/test/json/test_json_string_matching.rb @@ -0,0 +1,40 @@ +#!/usr/bin/env ruby +# -*- coding: utf-8 -*- + +require 'test/unit' +require File.join(File.dirname(__FILE__), 'setup_variant') +require 'stringio' +require 'time' + +class TestJsonStringMatching < Test::Unit::TestCase + include JSON + + class TestTime < ::Time + def self.json_create(string) + Time.parse(string) + end + + def to_json(*) + %{"#{strftime('%FT%T%z')}"} + end + + def ==(other) + to_i == other.to_i + end + end + + def test_match_date + t = TestTime.new + t_json = [ t ].to_json + assert_equal [ t ], + JSON.parse(t_json, + :match_string => { /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\Z/ => TestTime }) + assert_equal [ t.strftime('%FT%T%z') ], + JSON.parse(t_json, + :match_string => { /\A\d{3}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\Z/ => TestTime }) + assert_equal [ t.strftime('%FT%T%z') ], + JSON.parse(t_json, + :match_string => { /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\Z/ => TestTime }, + :create_additions => false) + end +end diff --git a/test/json/test_json_unicode.rb b/test/json/test_json_unicode.rb index 505f5d5e40..ace56cae36 100755 --- a/test/json/test_json_unicode.rb +++ b/test/json/test_json_unicode.rb @@ -2,11 +2,7 @@ # -*- 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 File.join(File.dirname(__FILE__), 'setup_variant') class TC_JSONUnicode < Test::Unit::TestCase include JSON -- cgit v1.2.3