[ruby/json] Resync

This commit is contained in:
Jean Boussier 2024-11-05 16:35:58 +01:00
parent e8522f06b5
commit ca8f21ace8
Notes: git 2024-11-05 17:00:58 +00:00
18 changed files with 2167 additions and 866 deletions

View file

@ -1 +0,0 @@
["extra comma",]

View file

@ -1 +0,0 @@
{"Extra comma": true,}

View file

@ -52,11 +52,11 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
end
def test_generator
assert_match(/::Generator\z/, JSON.generator.name)
assert_match(/::(TruffleRuby)?Generator\z/, JSON.generator.name)
end
def test_state
assert_match(/::Generator::State\z/, JSON.state.name)
assert_match(/::(TruffleRuby)?Generator::State\z/, JSON.state.name)
end
def test_create_id

View file

@ -2,53 +2,51 @@
require_relative 'test_helper'
class JSONExtParserTest < Test::Unit::TestCase
if defined?(JSON::Ext::Parser)
include JSON
include JSON
def test_allocate
parser = JSON::Ext::Parser.new("{}")
assert_raise(TypeError, '[ruby-core:35079]') do
parser.__send__(:initialize, "{}")
end
parser = JSON::Ext::Parser.allocate
assert_raise(TypeError, '[ruby-core:35079]') { parser.source }
def test_allocate
parser = JSON::Ext::Parser.new("{}")
assert_raise(TypeError, '[ruby-core:35079]') do
parser.__send__(:initialize, "{}")
end
parser = JSON::Ext::Parser.allocate
assert_raise(TypeError, '[ruby-core:35079]') { parser.source }
end
def test_error_messages
ex = assert_raise(ParserError) { parse('Infinity') }
assert_equal "unexpected token at 'Infinity'", ex.message
unless RUBY_PLATFORM =~ /java/
ex = assert_raise(ParserError) { parse('-Infinity') }
assert_equal "unexpected token at '-Infinity'", ex.message
end
def test_error_messages
ex = assert_raise(ParserError) { parse('Infinity') }
assert_equal "unexpected token at 'Infinity'", ex.message
ex = assert_raise(ParserError) { parse('NaN') }
assert_equal "unexpected token at 'NaN'", ex.message
end
unless RUBY_PLATFORM =~ /java/
ex = assert_raise(ParserError) { parse('-Infinity') }
assert_equal "unexpected token at '-Infinity'", ex.message
end
if GC.respond_to?(:stress=)
def test_gc_stress_parser_new
payload = JSON.dump([{ foo: 1, bar: 2, baz: 3, egg: { spam: 4 } }] * 10)
ex = assert_raise(ParserError) { parse('NaN') }
assert_equal "unexpected token at 'NaN'", ex.message
previous_stress = GC.stress
JSON::Parser.new(payload).parse
ensure
GC.stress = previous_stress
end
if GC.respond_to?(:stress=)
def test_gc_stress_parser_new
payload = JSON.dump([{ foo: 1, bar: 2, baz: 3, egg: { spam: 4 } }] * 10)
def test_gc_stress
payload = JSON.dump([{ foo: 1, bar: 2, baz: 3, egg: { spam: 4 } }] * 10)
previous_stress = GC.stress
JSON::Parser.new(payload).parse
ensure
GC.stress = previous_stress
end
def test_gc_stress
payload = JSON.dump([{ foo: 1, bar: 2, baz: 3, egg: { spam: 4 } }] * 10)
previous_stress = GC.stress
JSON.parse(payload)
ensure
GC.stress = previous_stress
end
end
def parse(json)
JSON::Ext::Parser.new(json).parse
previous_stress = GC.stress
JSON.parse(payload)
ensure
GC.stress = previous_stress
end
end
def parse(json)
JSON::Ext::Parser.new(json).parse
end
end

View file

@ -343,27 +343,25 @@ class JSONGeneratorTest < Test::Unit::TestCase
assert_equal '2', state.indent
end
if defined?(JSON::Ext::Generator)
def test_broken_bignum # [ruby-core:38867]
pid = fork do
x = 1 << 64
x.class.class_eval do
def to_s
end
end
begin
JSON::Ext::Generator::State.new.generate(x)
exit 1
rescue TypeError
exit 0
def test_broken_bignum # [ruby-core:38867]
pid = fork do
x = 1 << 64
x.class.class_eval do
def to_s
end
end
_, 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
begin
JSON::Ext::Generator::State.new.generate(x)
exit 1
rescue TypeError
exit 0
end
end
_, 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
def test_hash_likeness_set_symbol
@ -477,12 +475,20 @@ class JSONGeneratorTest < Test::Unit::TestCase
end
assert_includes error.message, "source sequence is illegal/malformed utf-8"
assert_raise(Encoding::UndefinedConversionError) do
assert_raise(JSON::GeneratorError) do
JSON.dump("\x82\xAC\xEF".b)
end
assert_raise(JSON::GeneratorError) do
"\x82\xAC\xEF".b.to_json
end
assert_raise(Encoding::UndefinedConversionError) do
JSON.dump("\x82\xAC\xEF".b)
assert_raise(JSON::GeneratorError) do
["\x82\xAC\xEF".b].to_json
end
assert_raise(JSON::GeneratorError) do
{ foo: "\x82\xAC\xEF".b }.to_json
end
end

View file

@ -40,7 +40,7 @@ class JSONParserTest < Test::Unit::TestCase
}
assert_equal(Encoding::UTF_8, e.message.encoding, bug10705)
assert_include(e.message, json, bug10705)
end if defined?(JSON::Ext::Parser)
end
def test_parsing
parser = JSON::Parser.new('"test"')
@ -180,7 +180,93 @@ class JSONParserTest < Test::Unit::TestCase
assert parse('NaN', :allow_nan => true).nan?
assert parse('Infinity', :allow_nan => true).infinite?
assert parse('-Infinity', :allow_nan => true).infinite?
assert_raise(JSON::ParserError) { parse('[ 1, ]') }
end
def test_parse_arrays_with_allow_trailing_comma
assert_equal([], parse('[]', allow_trailing_comma: true))
assert_equal([], parse('[]', allow_trailing_comma: false))
assert_raise(JSON::ParserError) { parse('[,]', allow_trailing_comma: true) }
assert_raise(JSON::ParserError) { parse('[,]', allow_trailing_comma: false) }
assert_equal([1], parse('[1]', allow_trailing_comma: true))
assert_equal([1], parse('[1]', allow_trailing_comma: false))
assert_equal([1], parse('[1,]', allow_trailing_comma: true))
assert_raise(JSON::ParserError) { parse('[1,]', allow_trailing_comma: false) }
assert_equal([1, 2, 3], parse('[1,2,3]', allow_trailing_comma: true))
assert_equal([1, 2, 3], parse('[1,2,3]', allow_trailing_comma: false))
assert_equal([1, 2, 3], parse('[1,2,3,]', allow_trailing_comma: true))
assert_raise(JSON::ParserError) { parse('[1,2,3,]', allow_trailing_comma: false) }
assert_equal([1, 2, 3], parse('[ 1 , 2 , 3 ]', allow_trailing_comma: true))
assert_equal([1, 2, 3], parse('[ 1 , 2 , 3 ]', allow_trailing_comma: false))
assert_equal([1, 2, 3], parse('[ 1 , 2 , 3 , ]', allow_trailing_comma: true))
assert_raise(JSON::ParserError) { parse('[ 1 , 2 , 3 , ]', allow_trailing_comma: false) }
assert_equal({'foo' => [1, 2, 3]}, parse('{ "foo": [1,2,3] }', allow_trailing_comma: true))
assert_equal({'foo' => [1, 2, 3]}, parse('{ "foo": [1,2,3] }', allow_trailing_comma: false))
assert_equal({'foo' => [1, 2, 3]}, parse('{ "foo": [1,2,3,] }', allow_trailing_comma: true))
assert_raise(JSON::ParserError) { parse('{ "foo": [1,2,3,] }', allow_trailing_comma: false) }
end
def test_parse_object_with_allow_trailing_comma
assert_equal({}, parse('{}', allow_trailing_comma: true))
assert_equal({}, parse('{}', allow_trailing_comma: false))
assert_raise(JSON::ParserError) { parse('{,}', allow_trailing_comma: true) }
assert_raise(JSON::ParserError) { parse('{,}', allow_trailing_comma: false) }
assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}', allow_trailing_comma: true))
assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}', allow_trailing_comma: false))
assert_equal({'foo'=>'bar'}, parse('{"foo":"bar",}', allow_trailing_comma: true))
assert_raise(JSON::ParserError) { parse('{"foo":"bar",}', allow_trailing_comma: false) }
assert_equal(
{'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'},
parse('{"foo":"bar","baz":"qux","quux":"garply"}', allow_trailing_comma: true)
)
assert_equal(
{'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'},
parse('{"foo":"bar","baz":"qux","quux":"garply"}', allow_trailing_comma: false)
)
assert_equal(
{'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'},
parse('{"foo":"bar","baz":"qux","quux":"garply",}', allow_trailing_comma: true)
)
assert_raise(JSON::ParserError) {
parse('{"foo":"bar","baz":"qux","quux":"garply",}', allow_trailing_comma: false)
}
assert_equal(
{'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'},
parse('{ "foo":"bar" , "baz":"qux" , "quux":"garply" }', allow_trailing_comma: true)
)
assert_equal(
{'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'},
parse('{ "foo":"bar" , "baz":"qux" , "quux":"garply" }', allow_trailing_comma: false)
)
assert_equal(
{'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'},
parse('{ "foo":"bar" , "baz":"qux" , "quux":"garply" , }', allow_trailing_comma: true)
)
assert_raise(JSON::ParserError) {
parse('{ "foo":"bar" , "baz":"qux" , "quux":"garply" , }', allow_trailing_comma: false)
}
assert_equal(
[{'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'}],
parse('[{"foo":"bar","baz":"qux","quux":"garply"}]', allow_trailing_comma: true)
)
assert_equal(
[{'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'}],
parse('[{"foo":"bar","baz":"qux","quux":"garply"}]', allow_trailing_comma: false)
)
assert_equal(
[{'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'}],
parse('[{"foo":"bar","baz":"qux","quux":"garply",}]', allow_trailing_comma: true)
)
assert_raise(JSON::ParserError) {
parse('[{"foo":"bar","baz":"qux","quux":"garply",}]', allow_trailing_comma: false)
}
end
def test_parse_some_strings
@ -533,7 +619,7 @@ class JSONParserTest < Test::Unit::TestCase
error = assert_raise(JSON::ParserError) do
JSON.parse('{"input":{"firstName":"Bob","lastName":"Mob","email":"bob@example.com"}')
end
if RUBY_ENGINE == "ruby" && defined?(JSON::Ext)
if RUBY_ENGINE == "ruby"
assert_equal %(unexpected token at '{"input":{"firstName":"Bob","las'), error.message
end
end

View file

@ -1,30 +1,14 @@
case ENV['JSON']
when 'pure'
$LOAD_PATH.unshift(File.expand_path('../../../lib', __FILE__))
$stderr.puts("Testing JSON::Pure")
require 'json/pure'
when 'ext'
$stderr.puts("Testing JSON::Ext")
$LOAD_PATH.unshift(File.expand_path('../../../ext', __FILE__), File.expand_path('../../../lib', __FILE__))
require 'json/ext'
else
$LOAD_PATH.unshift(File.expand_path('../../../ext', __FILE__), File.expand_path('../../../lib', __FILE__))
$stderr.puts("Testing JSON")
require 'json'
end
$LOAD_PATH.unshift(File.expand_path('../../../ext', __FILE__), File.expand_path('../../../lib', __FILE__))
require 'json'
require 'test/unit'
begin
require 'byebug'
rescue LoadError
end
if GC.respond_to?(:verify_compaction_references)
# This method was added in Ruby 3.0.0. Calling it this way asks the GC to
# move objects around, helping to find object movement bugs.
begin
GC.verify_compaction_references(double_heap: true, toward: :empty)
rescue NotImplementedError
GC.verify_compaction_references(expand_heap: true, toward: :empty)
rescue NotImplementedError, ArgumentError
# Some platforms don't support compaction
end
end