mirror of
https://github.com/ruby/ruby.git
synced 2026-08-15 11:50:22 +08:00
Ref: https://github.com/ruby/json/pull/718 The existing `Parser` interface is pretty bad, as it forces to instantiate a new instance for each document. Instead it's preferable to only take the config and do all the initialization needed, and then keep the parsing state on the stack on in ephemeral memory. This refactor makes the `JSON::Coder` pull request much easier to implement in a performant way. https://github.com/ruby/json/commit/c8d5236a92 Co-Authored-By: Étienne Barrié <etienne.barrie@gmail.com>
53 lines
1.3 KiB
Ruby
53 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
require_relative 'test_helper'
|
|
|
|
class JSONExtParserTest < Test::Unit::TestCase
|
|
include JSON
|
|
|
|
def test_allocate
|
|
parser = JSON::Ext::Parser.new("{}")
|
|
parser.__send__(:initialize, "{}")
|
|
assert_equal "{}", parser.source
|
|
|
|
parser = JSON::Ext::Parser.allocate
|
|
assert_nil 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
|
|
|
|
ex = assert_raise(ParserError) { parse('NaN') }
|
|
assert_equal "unexpected token at 'NaN'", 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)
|
|
|
|
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
|
|
end
|
|
end
|