diff options
Diffstat (limited to 'test/json')
33 files changed, 3484 insertions, 550 deletions
diff --git a/test/json/fixtures/fail1.json b/test/json/fixtures/fail1.json deleted file mode 100644 index 6216b865f1..0000000000 --- a/test/json/fixtures/fail1.json +++ /dev/null @@ -1 +0,0 @@ -"A JSON payload should be an object or array, not a string."
\ No newline at end of file diff --git a/test/json/fixtures/pass15.json b/test/json/fixtures/fail15.json index fc8376b605..fc8376b605 100644 --- a/test/json/fixtures/pass15.json +++ b/test/json/fixtures/fail15.json diff --git a/test/json/fixtures/pass16.json b/test/json/fixtures/fail16.json index c43ae3c286..c43ae3c286 100644 --- a/test/json/fixtures/pass16.json +++ b/test/json/fixtures/fail16.json diff --git a/test/json/fixtures/pass17.json b/test/json/fixtures/fail17.json index 62b9214aed..62b9214aed 100644 --- a/test/json/fixtures/pass17.json +++ b/test/json/fixtures/fail17.json diff --git a/test/json/fixtures/fail18.json b/test/json/fixtures/fail18.json index d61a345465..ebc11eb4c2 100644 --- a/test/json/fixtures/fail18.json +++ b/test/json/fixtures/fail18.json @@ -1 +1 @@ -[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]] // No, we don't limit our depth: Moved to pass... +[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] diff --git a/test/json/fixtures/pass26.json b/test/json/fixtures/fail26.json index 845d26a6a5..845d26a6a5 100644 --- a/test/json/fixtures/pass26.json +++ b/test/json/fixtures/fail26.json diff --git a/test/json/fixtures/fail29.json b/test/json/fixtures/fail29.json new file mode 100644 index 0000000000..98232c64fc --- /dev/null +++ b/test/json/fixtures/fail29.json @@ -0,0 +1 @@ +{ diff --git a/test/json/fixtures/fail30.json b/test/json/fixtures/fail30.json new file mode 100644 index 0000000000..558ed37d93 --- /dev/null +++ b/test/json/fixtures/fail30.json @@ -0,0 +1 @@ +[ diff --git a/test/json/fixtures/fail31.json b/test/json/fixtures/fail31.json new file mode 100644 index 0000000000..70773e47f7 --- /dev/null +++ b/test/json/fixtures/fail31.json @@ -0,0 +1 @@ +[1, 2, 3, diff --git a/test/json/fixtures/fail32.json b/test/json/fixtures/fail32.json new file mode 100644 index 0000000000..b18d550ca5 --- /dev/null +++ b/test/json/fixtures/fail32.json @@ -0,0 +1 @@ +{"foo": "bar" diff --git a/test/json/fixtures/fail4.json b/test/json/fixtures/fail4.json deleted file mode 100644 index 9de168bf34..0000000000 --- a/test/json/fixtures/fail4.json +++ /dev/null @@ -1 +0,0 @@ -["extra comma",]
\ No newline at end of file diff --git a/test/json/fixtures/fail9.json b/test/json/fixtures/fail9.json deleted file mode 100644 index 5815574f36..0000000000 --- a/test/json/fixtures/fail9.json +++ /dev/null @@ -1 +0,0 @@ -{"Extra comma": true,}
\ No newline at end of file diff --git a/test/json/fixtures/obsolete_fail1.json b/test/json/fixtures/obsolete_fail1.json new file mode 100644 index 0000000000..98b77de6a8 --- /dev/null +++ b/test/json/fixtures/obsolete_fail1.json @@ -0,0 +1 @@ +"A JSON payload should be an object or array, not a string." diff --git a/test/json/fixtures/pass1.json b/test/json/fixtures/pass1.json index 7828fcc137..fa9058b136 100644 --- a/test/json/fixtures/pass1.json +++ b/test/json/fixtures/pass1.json @@ -12,7 +12,7 @@ "real": -9876.543210, "e": 0.123456789e-12, "E": 1.234567890E+34, - "": 23456789012E666, + "": 23456789012E66, "zero": 0, "one": 1, "space": " ", diff --git a/test/json/json_addition_test.rb b/test/json/json_addition_test.rb new file mode 100644 index 0000000000..4d8d186873 --- /dev/null +++ b/test/json/json_addition_test.rb @@ -0,0 +1,192 @@ +# frozen_string_literal: true +require_relative 'test_helper' +require 'json/add/core' +require 'json/add/complex' +require 'json/add/rational' +require 'json/add/bigdecimal' +require 'json/add/ostruct' +require 'json/add/set' +require 'date' + +class JSONAdditionTest < Test::Unit::TestCase + include JSON + + class A + def initialize(a) + @a = a + end + + attr_reader :a + + def ==(other) + a == other.a + end + + def self.json_create(object) + new(*object['args']) + end + + def to_json(*args) + { + 'json_class' => self.class.name, + 'args' => [ @a ], + }.to_json(*args) + end + end + + class A2 < A + def to_json(*args) + { + 'json_class' => self.class.name, + 'args' => [ @a ], + }.to_json(*args) + end + end + + class B + def to_json(*args) + { + 'json_class' => self.class.name, + }.to_json(*args) + end + end + + class C + def to_json(*args) + { + 'json_class' => 'JSONAdditionTest::Nix', + }.to_json(*args) + end + end + + def test_extended_json + a = A.new(666) + json = generate(a) + a_again = parse(json, :create_additions => true) + assert_kind_of a.class, a_again + assert_equal a, a_again + end + + def test_extended_json_default + a = A.new(666) + assert A.respond_to?(:json_create) + json = generate(a) + a_hash = parse(json) + assert_kind_of Hash, a_hash + end + + def test_extended_json_disabled + a = A.new(666) + json = generate(a) + a_again = parse(json, :create_additions => true) + assert_kind_of a.class, a_again + assert_equal a, a_again + a_hash = parse(json, :create_additions => false) + assert_kind_of Hash, a_hash + assert_equal( + {"args"=>[666], "json_class"=>"JSONAdditionTest::A"}.sort_by { |k,| k }, + a_hash.sort_by { |k,| k } + ) + end + + def test_extended_json_fail1 + b = B.new + json = generate(b) + assert_equal({ "json_class"=>"JSONAdditionTest::B" }, parse(json)) + end + + def test_extended_json_fail2 + c = C.new + json = generate(c) + assert_raise(ArgumentError, NameError) { parse(json, :create_additions => true) } + end + + def test_raw_strings + raw = ''.b + raw_array = [] + for i in 0..255 + raw << i + raw_array << i + end + json = raw.to_json_raw + json_raw_object = raw.to_json_raw_object + hash = { 'json_class' => 'String', 'raw'=> raw_array } + assert_equal hash, json_raw_object + 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 = parse(json, :create_additions => true) + assert_equal raw, raw_again + end + + MyJsonStruct = Struct.new 'MyJsonStruct', :foo, :bar + + def test_core + t = Time.now + assert_equal t, JSON(JSON(t), :create_additions => true) + d = Date.today + assert_equal d, JSON(JSON(d), :create_additions => true) + d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161) + assert_equal d, JSON(JSON(d), :create_additions => true) + assert_equal 1..10, JSON(JSON(1..10), :create_additions => true) + assert_equal 1...10, JSON(JSON(1...10), :create_additions => true) + assert_equal "a".."c", JSON(JSON("a".."c"), :create_additions => true) + assert_equal "a"..."c", JSON(JSON("a"..."c"), :create_additions => true) + s = MyJsonStruct.new 4711, 'foot' + assert_equal s, JSON(JSON(s), :create_additions => true) + struct = Struct.new :foo, :bar + s = struct.new 4711, 'foot' + assert_raise(JSONError) { JSON(s) } + begin + raise TypeError, "test me" + rescue TypeError => e + e_json = JSON.generate e + e_again = JSON e_json, :create_additions => true + assert_kind_of TypeError, e_again + assert_equal e.message, e_again.message + assert_equal e.backtrace, e_again.backtrace + end + assert_equal(/foo/, JSON(JSON(/foo/), :create_additions => true)) + assert_equal(/foo/i, JSON(JSON(/foo/i), :create_additions => true)) + end + + def test_deprecated_load_create_additions + assert_deprecated_warning(/use JSON\.unsafe_load/) do + JSON.load(JSON.dump(Time.now)) + end + end + + def test_utc_datetime + now = Time.now + d = DateTime.parse(now.to_s) # usual case + assert_equal d, parse(d.to_json, :create_additions => true) + d = DateTime.parse(now.utc.to_s) # of = 0 + assert_equal d, parse(d.to_json, :create_additions => true) + d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1,24)) + assert_equal d, parse(d.to_json, :create_additions => true) + d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24)) + assert_equal d, parse(d.to_json, :create_additions => true) + end + + def test_rational_complex + assert_equal Rational(2, 9), parse(JSON(Rational(2, 9)), :create_additions => true) + assert_equal Complex(2, 9), parse(JSON(Complex(2, 9)), :create_additions => true) + end + + def test_bigdecimal + assert_equal BigDecimal('3.141', 23), JSON(JSON(BigDecimal('3.141', 23)), :create_additions => true) + assert_equal BigDecimal('3.141', 666), JSON(JSON(BigDecimal('3.141', 666)), :create_additions => true) + end if defined?(::BigDecimal) + + def test_ostruct + o = OpenStruct.new + # XXX this won't work; o.foo = { :bar => true } + o.foo = { 'bar' => true } + assert_equal o, parse(JSON(o), :create_additions => true) + end if defined?(::OpenStruct) + + def test_set + s = Set.new([:a, :b, :c, :a]) + assert_equal s, JSON.parse(JSON(s), :create_additions => true) + end +end diff --git a/test/json/json_coder_test.rb b/test/json/json_coder_test.rb new file mode 100755 index 0000000000..47e12ff919 --- /dev/null +++ b/test/json/json_coder_test.rb @@ -0,0 +1,149 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require_relative 'test_helper' + +class JSONCoderTest < Test::Unit::TestCase + def test_json_coder_with_proc + coder = JSON::Coder.new do |object| + "[Object object]" + end + assert_equal %(["[Object object]"]), coder.dump([Object.new]) + end + + def test_json_coder_with_proc_with_unsupported_value + coder = JSON::Coder.new do |object, is_key| + assert_equal false, is_key + Object.new + end + assert_raise(JSON::GeneratorError) { coder.dump([Object.new]) } + end + + def test_json_coder_hash_key + obj = Object.new + coder = JSON::Coder.new do |obj, is_key| + assert_equal true, is_key + obj.to_s + end + assert_equal %({#{obj.to_s.inspect}:1}), coder.dump({ obj => 1 }) + + coder = JSON::Coder.new { 42 } + error = assert_raise JSON::GeneratorError do + coder.dump({ obj => 1 }) + end + assert_equal "Integer not allowed as object key in JSON", error.message + end + + def test_json_coder_options + coder = JSON::Coder.new(array_nl: "\n") do |object| + 42 + end + + assert_equal "[\n42\n]", coder.dump([Object.new]) + end + + def test_json_coder_load + coder = JSON::Coder.new + assert_equal [1,2,3], coder.load("[1,2,3]") + end + + def test_json_coder_load_options + coder = JSON::Coder.new(symbolize_names: true) + assert_equal({a: 1}, coder.load('{"a":1}')) + end + + def test_json_coder_dump_NaN_or_Infinity + coder = JSON::Coder.new { |o| o.inspect } + assert_equal "NaN", coder.load(coder.dump(Float::NAN)) + assert_equal "Infinity", coder.load(coder.dump(Float::INFINITY)) + assert_equal "-Infinity", coder.load(coder.dump(-Float::INFINITY)) + end + + def test_json_coder_dump_NaN_or_Infinity_loop + coder = JSON::Coder.new { |o| o.itself } + error = assert_raise JSON::GeneratorError do + coder.dump(Float::NAN) + end + assert_include error.message, "NaN not allowed in JSON" + end + + def test_json_coder_string_invalid_encoding + calls = 0 + coder = JSON::Coder.new do |object, is_key| + calls += 1 + object + end + + error = assert_raise JSON::GeneratorError do + coder.dump("\xFF") + end + assert_equal "source sequence is illegal/malformed utf-8", error.message + assert_equal 1, calls + + error = assert_raise JSON::GeneratorError do + coder.dump({ "\xFF" => 1 }) + end + assert_equal "source sequence is illegal/malformed utf-8", error.message + assert_equal 2, calls + + calls = 0 + coder = JSON::Coder.new do |object, is_key| + calls += 1 + object.dup + end + + error = assert_raise JSON::GeneratorError do + coder.dump("\xFF") + end + assert_equal "source sequence is illegal/malformed utf-8", error.message + assert_equal 1, calls + + error = assert_raise JSON::GeneratorError do + coder.dump({ "\xFF" => 1 }) + end + assert_equal "source sequence is illegal/malformed utf-8", error.message + assert_equal 2, calls + + calls = 0 + coder = JSON::Coder.new do |object, is_key| + calls += 1 + object.bytes + end + + assert_equal "[255]", coder.dump("\xFF") + assert_equal 1, calls + + error = assert_raise JSON::GeneratorError do + coder.dump({ "\xFF" => 1 }) + end + assert_equal "Array not allowed as object key in JSON", error.message + assert_equal 2, calls + + calls = 0 + coder = JSON::Coder.new do |object, is_key| + calls += 1 + [object].pack("m") + end + + assert_equal '"/w==\\n"', coder.dump("\xFF") + assert_equal 1, calls + + assert_equal '{"/w==\\n":1}', coder.dump({ "\xFF" => 1 }) + assert_equal 2, calls + end + + def test_depth + coder = JSON::Coder.new(object_nl: "\n", array_nl: "\n", space: " ", indent: " ", depth: 1) + assert_equal %({\n "foo": 42\n }), coder.dump(foo: 42) + end + + def test_nesting_recovery + coder = JSON::Coder.new + ary = [] + ary << ary + assert_raise JSON::NestingError do + coder.dump(ary) + end + assert_equal '{"a":1}', coder.dump({ a: 1 }) + end +end diff --git a/test/json/json_common_interface_test.rb b/test/json/json_common_interface_test.rb new file mode 100644 index 0000000000..3dfd0623cd --- /dev/null +++ b/test/json/json_common_interface_test.rb @@ -0,0 +1,359 @@ +# frozen_string_literal: true + +require_relative 'test_helper' +require 'stringio' +require 'tempfile' + +class JSONCommonInterfaceTest < Test::Unit::TestCase + include JSON + + module MethodMissing + def method_missing(name, *args); end + def respond_to_missing?(name, include_private) + true + end + end + + def setup + @hash = { + 'a' => 2, + 'b' => 3.141, + 'c' => 'c', + 'd' => [ 1, "b", 3.14 ], + 'e' => { 'foo' => 'bar' }, + 'g' => "\"\0\037", + 'h' => 1000.0, + 'i' => 0.001 + } + + @hash_with_method_missing = { + 'a' => 2, + 'b' => 3.141, + 'c' => 'c', + 'd' => [ 1, "b", 3.14 ], + 'e' => { 'foo' => 'bar' }, + 'g' => "\"\0\037", + 'h' => 1000.0, + 'i' => 0.001 + } + @hash_with_method_missing.extend MethodMissing + + @json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\ + '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}' + end + + def test_index + assert_equal @json, JSON[@hash] + assert_equal @json, JSON[@hash_with_method_missing] + assert_equal @hash, JSON[@json] + end + + def test_parser + assert_match(/::Parser\z/, JSON.parser.name) + end + + def test_generator + assert_match(/::(TruffleRuby)?Generator\z/, JSON.generator.name) + end + + def test_state + assert_match(/::(TruffleRuby)?Generator::State\z/, JSON.state.name) + end + + def test_create_id + assert_equal 'json_class', JSON.create_id + JSON.create_id = 'foo_bar' + assert_equal 'foo_bar', JSON.create_id + ensure + JSON.create_id = 'json_class' + end + + def test_parse + assert_equal [ 1, 2, 3, ], JSON.parse('[ 1, 2, 3 ]') + end + + def test_parse_bang + assert_equal [ 1, Infinity, 3, ], JSON.parse!('[ 1, Infinity, 3 ]') + end + + def test_generate + assert_equal '[1,2,3]', JSON.generate([ 1, 2, 3 ]) + end + + def test_fast_generate + assert_equal '[1,2,3]', JSON.generate([ 1, 2, 3 ]) + end + + def test_pretty_generate + assert_equal "[\n 1,\n 2,\n 3\n]", JSON.pretty_generate([ 1, 2, 3 ]) + assert_equal <<~JSON.strip, JSON.pretty_generate({ a: { b: "f"}, c: "d"}) + { + "a": { + "b": "f" + }, + "c": "d" + } + JSON + + # Cause the state to be spilled on the heap. + o = Object.new + def o.to_s + "Object" + end + actual = JSON.pretty_generate({ a: { b: o}, c: "d", e: "f"}) + assert_equal <<~JSON.strip, actual + { + "a": { + "b": "Object" + }, + "c": "d", + "e": "f" + } + JSON + end + + def test_load + assert_equal @hash, JSON.load(@json) + tempfile = Tempfile.open('@json') + tempfile.write @json + tempfile.rewind + assert_equal @hash, JSON.load(tempfile) + stringio = StringIO.new(@json) + stringio.rewind + assert_equal @hash, JSON.load(stringio) + assert_equal nil, JSON.load(nil) + assert_equal nil, JSON.load('') + ensure + tempfile.close! + end + + def test_load_with_proc + visited = [] + JSON.load('{"foo": [1, 2, 3], "bar": {"baz": "plop"}}', proc { |o| visited << JSON.dump(o); o }) + + expected = [ + '"foo"', + '1', + '2', + '3', + '[1,2,3]', + '"bar"', + '"baz"', + '"plop"', + '{"baz":"plop"}', + '{"foo":[1,2,3],"bar":{"baz":"plop"}}', + ] + assert_equal expected, visited + end + + def test_load_with_options + json = '{ "foo": NaN }' + assert JSON.load(json, nil, :allow_nan => true)['foo'].nan? + assert JSON.load(json, :allow_nan => true)['foo'].nan? + end + + def test_load_null + assert_equal nil, JSON.load(nil, nil, :allow_blank => true) + assert_raise(TypeError) { JSON.load(nil, nil, :allow_blank => false) } + assert_raise(JSON::ParserError) { JSON.load('', nil, :allow_blank => false) } + end + + def test_unsafe_load + string_able_klass = Class.new do + def initialize(str) + @str = str + end + + def to_str + @str + end + end + + io_able_klass = Class.new do + def initialize(str) + @str = str + end + + def to_io + StringIO.new(@str) + end + end + + assert_equal @hash, JSON.unsafe_load(@json) + tempfile = Tempfile.open('@json') + tempfile.write @json + tempfile.rewind + assert_equal @hash, JSON.unsafe_load(tempfile) + stringio = StringIO.new(@json) + stringio.rewind + assert_equal @hash, JSON.unsafe_load(stringio) + string_able = string_able_klass.new(@json) + assert_equal @hash, JSON.unsafe_load(string_able) + io_able = io_able_klass.new(@json) + assert_equal @hash, JSON.unsafe_load(io_able) + assert_equal nil, JSON.unsafe_load(nil) + assert_equal nil, JSON.unsafe_load('') + ensure + tempfile.close! + end + + def test_unsafe_load_with_proc + visited = [] + JSON.unsafe_load('{"foo": [1, 2, 3], "bar": {"baz": "plop"}}', proc { |o| visited << JSON.dump(o); o }) + + expected = [ + '"foo"', + '1', + '2', + '3', + '[1,2,3]', + '"bar"', + '"baz"', + '"plop"', + '{"baz":"plop"}', + '{"foo":[1,2,3],"bar":{"baz":"plop"}}', + ] + assert_equal expected, visited + end + + def test_unsafe_load_default_options + too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' + assert JSON.unsafe_load(too_deep, nil).is_a?(Array) + nan_json = '{ "foo": NaN }' + assert JSON.unsafe_load(nan_json, nil)['foo'].nan? + assert_equal nil, JSON.unsafe_load(nil, nil) + t = Time.new(2025, 9, 3, 14, 50, 0) + assert_equal t.to_s, JSON.unsafe_load(JSON(t)).to_s + end + + def test_unsafe_load_with_options + nan_json = '{ "foo": NaN }' + assert_raise(JSON::ParserError) { JSON.unsafe_load(nan_json, nil, :allow_nan => false)['foo'].nan? } + # make sure it still uses the defaults when something is provided + assert JSON.unsafe_load(nan_json, nil, :allow_blank => true)['foo'].nan? + assert JSON.unsafe_load(nan_json, :allow_nan => true)['foo'].nan? + end + + def test_unsafe_load_null + assert_equal nil, JSON.unsafe_load(nil, nil, :allow_blank => true) + assert_raise(TypeError) { JSON.unsafe_load(nil, nil, :allow_blank => false) } + assert_raise(JSON::ParserError) { JSON.unsafe_load('', nil, :allow_blank => false) } + end + + def test_dump + too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' + obj = eval(too_deep) + assert_equal too_deep, dump(obj) + assert_kind_of String, Marshal.dump(obj) + assert_raise(ArgumentError) { dump(obj, 100) } + assert_raise(ArgumentError) { Marshal.dump(obj, 100) } + assert_equal too_deep, dump(obj, 101) + assert_kind_of String, Marshal.dump(obj, 101) + + assert_equal too_deep, JSON.dump(obj, StringIO.new, 101, strict: false).string + assert_equal too_deep, dump(obj, StringIO.new, 101, strict: false).string + assert_raise(JSON::GeneratorError) { JSON.dump(Object.new, StringIO.new, 101, strict: true).string } + assert_raise(JSON::GeneratorError) { dump(Object.new, StringIO.new, 101, strict: true).string } + + assert_equal too_deep, dump(obj, nil, nil, strict: false) + assert_equal too_deep, dump(obj, nil, 101, strict: false) + assert_equal too_deep, dump(obj, StringIO.new, nil, strict: false).string + assert_equal too_deep, dump(obj, nil, strict: false) + assert_equal too_deep, dump(obj, 101, strict: false) + assert_equal too_deep, dump(obj, StringIO.new, strict: false).string + assert_equal too_deep, dump(obj, strict: false) + end + + def test_dump_in_io + io = StringIO.new + assert_same io, JSON.dump([1], io) + assert_equal "[1]", io.string + + big_object = ["a" * 10, "b" * 40, { foo: 1.23 }] * 5000 + io.rewind + assert_same io, JSON.dump(big_object, io) + assert_equal JSON.dump(big_object), io.string + end + + def test_dump_should_modify_defaults + max_nesting = JSON._dump_default_options[:max_nesting] + dump([], StringIO.new, 10) + assert_equal max_nesting, JSON._dump_default_options[:max_nesting] + end + + def test_JSON + assert_equal @json, JSON(@hash) + assert_equal @json, JSON(@hash_with_method_missing) + assert_equal @hash, JSON(@json) + end + + def test_load_file + test_load_shared(:load_file) + end + + def test_load_file! + test_load_shared(:load_file!) + end + + def test_load_file_with_option + test_load_file_with_option_shared(:load_file) + end + + def test_load_file_with_option! + test_load_file_with_option_shared(:load_file!) + end + + def test_load_file_with_bad_default_external_encoding + data = { "key" => "€" } + temp_file_containing(JSON.dump(data)) do |path| + loaded_data = with_external_encoding(Encoding::US_ASCII) do + JSON.load_file(path) + end + assert_equal data, loaded_data + end + end + + def test_deprecated_dump_default_options + assert_deprecated_warning(/dump_default_options/) do + JSON.dump_default_options + end + end + + private + + def with_external_encoding(encoding) + verbose = $VERBOSE + $VERBOSE = nil + previous_encoding = Encoding.default_external + Encoding.default_external = encoding + yield + ensure + Encoding.default_external = previous_encoding + $VERBOSE = verbose + end + + def test_load_shared(method_name) + temp_file_containing(@json) do |filespec| + assert_equal JSON.public_send(method_name, filespec), @hash + end + end + + def test_load_file_with_option_shared(method_name) + temp_file_containing(@json) do |filespec| + parsed_object = JSON.public_send(method_name, filespec, symbolize_names: true) + key_classes = parsed_object.keys.map(&:class) + assert_include(key_classes, Symbol) + assert_not_include(key_classes, String) + end + end + + def temp_file_containing(text, file_prefix = '') + raise "This method must be called with a code block." unless block_given? + + Tempfile.create(file_prefix) do |file| + file << text + file.close + yield file.path + end + end +end diff --git a/test/json/json_encoding_test.rb b/test/json/json_encoding_test.rb new file mode 100644 index 0000000000..7ac06b2a7b --- /dev/null +++ b/test/json/json_encoding_test.rb @@ -0,0 +1,275 @@ +# frozen_string_literal: true + +require_relative 'test_helper' + +class JSONEncodingTest < Test::Unit::TestCase + include JSON + + def setup + @utf_8 = '"© ≠ €!"' + @ascii_8bit = @utf_8.b + @parsed = "© ≠ €!" + @generated = '"\u00a9 \u2260 \u20ac!"' + @utf_16_data = @parsed.encode(Encoding::UTF_16BE, Encoding::UTF_8) + @utf_16be = @utf_8.encode(Encoding::UTF_16BE, Encoding::UTF_8) + @utf_16le = @utf_8.encode(Encoding::UTF_16LE, Encoding::UTF_8) + @utf_32be = @utf_8.encode(Encoding::UTF_32BE, Encoding::UTF_8) + @utf_32le = @utf_8.encode(Encoding::UTF_32LE, Encoding::UTF_8) + end + + def test_parse + assert_equal @parsed, JSON.parse(@ascii_8bit) + 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_generate + assert_equal @generated, JSON.generate(@parsed, ascii_only: true) + assert_equal @generated, JSON.generate(@utf_16_data, ascii_only: true) + end + + def test_generate_shared_string + # Ref: https://github.com/ruby/json/issues/859 + s = "01234567890" + assert_equal '"234567890"', JSON.dump(s[2..-1]) + s = '01234567890123456789"a"b"c"d"e"f"g"h' + assert_equal '"\"a\"b\"c\"d\"e\"f\"g\""', JSON.dump(s[20, 15]) + s = "0123456789001234567890012345678900123456789001234567890" + assert_equal '"23456789001234567890012345678900123456789001234567890"', JSON.dump(s[2..-1]) + s = "0123456789001234567890012345678900123456789001234567890" + assert_equal '"567890012345678900123456789001234567890012345678"', JSON.dump(s[5..-3]) + end + + def test_unicode + assert_equal '""', ''.to_json + assert_equal '"\\b"', "\b".to_json + assert_equal '"\u0001"', 0x1.chr.to_json + assert_equal '"\u001f"', 0x1f.chr.to_json + 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(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(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(ascii_only: true) + assert_equal utf8, parse(json) + assert_equal '["Ã"]', generate(["Ã"], ascii_only: false) + assert_equal '["\\u00c3"]', generate(["Ã"], ascii_only: true) + assert_equal ["€"], parse('["\u20ac"]') + utf8 = ["\xf0\xa0\x80\x81"] + json = "[\"\xf0\xa0\x80\x81\"]" + assert_equal json, generate(utf8, ascii_only: false) + assert_equal utf8, parse(json) + json = '["\ud840\udc01"]' + assert_equal json, generate(utf8, ascii_only: true) + assert_equal utf8, parse(json) + assert_raise(JSON::ParserError) { parse('"\u"') } + assert_raise(JSON::ParserError) { parse('"\ud800"') } + end + + def test_chars + (0..0x7f).each do |i| + json = '"\u%04x"' % i + i = i.chr + assert_equal i, parse(json)[0] + if i == "\b" + generated = generate(i) + assert ['"\b"', '"\10"'].include?(generated) + elsif ["\n", "\r", "\t", "\f"].include?(i) + assert_equal i.dump, generate(i) + elsif i.chr < 0x20.chr + assert_equal json, generate(i) + end + end + assert_raise(JSON::GeneratorError) do + generate(["\x80"], ascii_only: true) + end + assert_equal "\302\200", parse('"\u0080"') + end + + def test_deeply_nested_structures + # Test for deeply nested arrays + nesting_level = 100 + deeply_nested = [] + current = deeply_nested + + (nesting_level - 1).times do + current << [] + current = current[0] + end + + json = generate(deeply_nested) + assert_equal deeply_nested, parse(json) + + # Test for deeply nested objects/hashes + deeply_nested_hash = {} + current_hash = deeply_nested_hash + + (nesting_level - 1).times do |i| + current_hash["key#{i}"] = {} + current_hash = current_hash["key#{i}"] + end + + json = generate(deeply_nested_hash) + assert_equal deeply_nested_hash, parse(json) + end + + def test_very_large_json_strings + # Create a large array with repeated elements + large_array = Array.new(10_000) { |i| "item#{i}" } + + json = generate(large_array) + parsed = parse(json) + + assert_equal large_array.size, parsed.size + assert_equal large_array.first, parsed.first + assert_equal large_array.last, parsed.last + + # Create a large hash + large_hash = {} + 10_000.times { |i| large_hash["key#{i}"] = "value#{i}" } + + json = generate(large_hash) + parsed = parse(json) + + assert_equal large_hash.size, parsed.size + assert_equal large_hash["key0"], parsed["key0"] + assert_equal large_hash["key9999"], parsed["key9999"] + end + + def test_invalid_utf8_sequences + invalid_utf8 = "\xFF\xFF" + error = assert_raise(JSON::GeneratorError) do + generate(invalid_utf8) + end + assert_match(%r{source sequence is illegal/malformed utf-8}, error.message) + end + + def test_surrogate_pair_handling + # Test valid surrogate pairs + assert_equal "\u{10000}", parse('"\ud800\udc00"') + assert_equal "\u{10FFFF}", parse('"\udbff\udfff"') + + # The existing test already checks for orphaned high surrogate + assert_raise(JSON::ParserError) { parse('"\ud800"') } + + # Test generating surrogate pairs + utf8_string = "\u{10437}" + generated = generate(utf8_string, ascii_only: true) + assert_match(/\\ud801\\udc37/, generated) + end + + def test_json_escaping_edge_cases + # Test escaping forward slashes + assert_equal "/", parse('"\/"') + + # Test escaping backslashes + assert_equal "\\", parse('"\\\\"') + + # Test escaping quotes + assert_equal '"', parse('"\\""') + + # Multiple escapes in sequence - different JSON parsers might handle escaped forward slashes differently + # Some parsers preserve the escaping, others don't + escaped_result = parse('"\\\\\\"\\/"') + assert_match(/\\"/, escaped_result) + assert_match(%r{/}, escaped_result) + + # Generate string with all special characters + special_chars = "\b\f\n\r\t\"\\" + escaped_json = generate(special_chars) + assert_equal special_chars, parse(escaped_json) + end + + def test_empty_objects_and_arrays + # Test empty objects with different encodings + assert_equal({}, parse('{}')) + assert_equal({}, parse('{}'.encode(Encoding::UTF_16BE))) + assert_equal({}, parse('{}'.encode(Encoding::UTF_16LE))) + assert_equal({}, parse('{}'.encode(Encoding::UTF_32BE))) + assert_equal({}, parse('{}'.encode(Encoding::UTF_32LE))) + + # Test empty arrays with different encodings + assert_equal([], parse('[]')) + assert_equal([], parse('[]'.encode(Encoding::UTF_16BE))) + assert_equal([], parse('[]'.encode(Encoding::UTF_16LE))) + assert_equal([], parse('[]'.encode(Encoding::UTF_32BE))) + assert_equal([], parse('[]'.encode(Encoding::UTF_32LE))) + + # Test generating empty objects and arrays + assert_equal '{}', generate({}) + assert_equal '[]', generate([]) + end + + def test_null_character_handling + # Test parsing null character + assert_equal "\u0000", parse('"\u0000"') + + # Test generating null character + string_with_null = "\u0000" + generated = generate(string_with_null) + assert_equal '"\u0000"', generated + + # Test null characters in middle of string + mixed_string = "before\u0000after" + generated = generate(mixed_string) + assert_equal mixed_string, parse(generated) + end + + def test_whitespace_handling + # Test parsing with various whitespace patterns + assert_equal({}, parse(' { } ')) + assert_equal({}, parse("{\r\n}")) + assert_equal([], parse(" [ \n ] ")) + assert_equal(["a", "b"], parse(" [ \n\"a\",\r\n \"b\"\n ] ")) + assert_equal({ "a" => "b" }, parse(" { \n\"a\" \r\n: \t\"b\"\n } ")) + + # Test with excessive whitespace + excessive_whitespace = " \n\r\t" * 10 + "{}" + " \n\r\t" * 10 + assert_equal({}, parse(excessive_whitespace)) + + # Mixed whitespace in keys and values + mixed_json = '{"a \n b":"c \r\n d"}' + assert_equal({ "a \n b" => "c \r\n d" }, parse(mixed_json)) + end + + def test_control_character_handling + # Test all control characters (U+0000 to U+001F) + (0..0x1F).each do |i| + # Skip already tested ones + next if [0x08, 0x0A, 0x0D, 0x0C, 0x09].include?(i) + + control_char = i.chr('UTF-8') + escaped_json = '"' + "\\u%04x" % i + '"' + assert_equal control_char, parse(escaped_json) + + # Check that the character is properly escaped when generating + assert_match(/\\u00[0-1][0-9a-f]/, generate(control_char)) + end + + # Test string with multiple control characters + control_str = "\u0001\u0002\u0003\u0004" + generated = generate(control_str) + assert_equal control_str, parse(generated) + assert_match(/\\u0001\\u0002\\u0003\\u0004/, generated) + end +end diff --git a/test/json/json_ext_parser_test.rb b/test/json/json_ext_parser_test.rb new file mode 100644 index 0000000000..e610f642f1 --- /dev/null +++ b/test/json/json_ext_parser_test.rb @@ -0,0 +1,71 @@ +# 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 something') } + unless RUBY_PLATFORM =~ /java/ + assert_equal "unexpected token 'Infinity' at line 1 column 1", ex.message + end + + ex = assert_raise(ParserError) { parse('foo bar') } + unless RUBY_PLATFORM =~ /java/ + assert_equal "unexpected token 'foo' at line 1 column 1", ex.message + end + + ex = assert_raise(ParserError) { parse('-Infinity something') } + unless RUBY_PLATFORM =~ /java/ + assert_equal "unexpected token '-Infinity' at line 1 column 1", ex.message + end + + ex = assert_raise(ParserError) { parse('NaN something') } + unless RUBY_PLATFORM =~ /java/ + assert_equal "unexpected token 'NaN' at line 1 column 1", ex.message + end + + ex = assert_raise(ParserError) { parse(' ') } + unless RUBY_PLATFORM =~ /java/ + assert_equal "unexpected end of input at line 1 column 4", ex.message + end + + ex = assert_raise(ParserError) { parse('{ ') } + unless RUBY_PLATFORM =~ /java/ + assert_equal "expected object key, got EOF at line 1 column 5", ex.message + end + 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 diff --git a/test/json/json_fixtures_test.rb b/test/json/json_fixtures_test.rb new file mode 100644 index 0000000000..c0d1037939 --- /dev/null +++ b/test/json/json_fixtures_test.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true +require_relative 'test_helper' + +class JSONFixturesTest < Test::Unit::TestCase + fixtures = File.join(File.dirname(__FILE__), 'fixtures/{fail,pass}*.json') + passed, failed = Dir[fixtures].partition { |f| f['pass'] } + + passed.each do |f| + name = File.basename(f).gsub(".", "_") + source = File.read(f) + define_method("test_#{name}") do + assert JSON.parse(source), "Did not pass for fixture '#{File.basename(f)}': #{source.inspect}" + rescue JSON::ParserError + raise "#{File.basename(f)} parsing failure" + end + end + + failed.each do |f| + name = File.basename(f).gsub(".", "_") + source = File.read(f) + define_method("test_#{name}") do + assert_raise(JSON::ParserError, JSON::NestingError, + "Did not fail for fixture '#{name}': #{source.inspect}") do + JSON.parse(source) + end + end + end +end diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb new file mode 100755 index 0000000000..d7c4173e8e --- /dev/null +++ b/test/json/json_generator_test.rb @@ -0,0 +1,1079 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require_relative 'test_helper' + +class JSONGeneratorTest < Test::Unit::TestCase + include JSON + + def setup + @hash = { + 'a' => 2, + 'b' => 3.141, + 'c' => 'c', + 'd' => [ 1, "b", 3.14 ], + 'e' => { 'foo' => 'bar' }, + 'g' => "\"\0\037", + 'h' => 1000.0, + 'i' => 0.001 + } + @json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' + + '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}' + @json3 = <<~'JSON'.chomp + { + "a": 2, + "b": 3.141, + "c": "c", + "d": [ + 1, + "b", + 3.14 + ], + "e": { + "foo": "bar" + }, + "g": "\"\u0000\u001f", + "h": 1000.0, + "i": 0.001 + } + JSON + end + + def silence + v = $VERBOSE + $VERBOSE = nil + yield + ensure + $VERBOSE = v + end + + def test_generate + json = generate(@hash) + assert_equal(parse(@json2), parse(json)) + json = JSON[@hash] + assert_equal(parse(@json2), parse(json)) + parsed_json = parse(json) + assert_equal(@hash, parsed_json) + json = generate({1=>2}) + assert_equal('{"1":2}', json) + parsed_json = parse(json) + assert_equal({"1"=>2}, parsed_json) + assert_equal '666', generate(666) + end + + def test_dump_unenclosed_hash + assert_equal '{"a":1,"b":2}', dump(a: 1, b: 2) + end + + def test_dump_strict + assert_equal '{}', dump({}, strict: true) + + assert_equal '{"array":[42,4.2,"forty-two",true,false,null]}', dump({ + "array" => [42, 4.2, "forty-two", true, false, nil] + }, strict: true) + + assert_equal '{"int":42,"float":4.2,"string":"forty-two","true":true,"false":false,"nil":null,"hash":{}}', dump({ + "int" => 42, + "float" => 4.2, + "string" => "forty-two", + "true" => true, + "false" => false, + "nil" => nil, + "hash" => {}, + }, strict: true) + + assert_equal '[]', dump([], strict: true) + + assert_equal '42', dump(42, strict: true) + assert_equal 'true', dump(true, strict: true) + + assert_equal '"hello"', dump(:hello, strict: true) + assert_equal '"hello"', :hello.to_json(strict: true) + assert_equal '"World"', "World".to_json(strict: true) + end + + def test_state_depth_to_json + depth = Object.new + def depth.to_json(state) + JSON::State.from_state(state).depth.to_s + end + + assert_equal "0", JSON.generate(depth) + assert_equal "[1]", JSON.generate([depth]) + assert_equal %({"depth":1}), JSON.generate(depth: depth) + assert_equal "[[2]]", JSON.generate([[depth]]) + assert_equal %([{"depth":2}]), JSON.generate([{depth: depth}]) + + state = JSON::State.new + assert_equal "0", state.generate(depth) + assert_equal "[1]", state.generate([depth]) + assert_equal %({"depth":1}), state.generate(depth: depth) + assert_equal "[[2]]", state.generate([[depth]]) + assert_equal %([{"depth":2}]), state.generate([{depth: depth}]) + end + + def test_state_depth_to_json_recursive + recur = Object.new + def recur.to_json(state = nil, *) + state = JSON::State.from_state(state) + if state.depth < 3 + state.generate([state.depth, self]) + else + state.generate([state.depth]) + end + end + + assert_raise(NestingError) { JSON.generate(recur, max_nesting: 3) } + assert_equal "[0,[1,[2,[3]]]]", JSON.generate(recur, max_nesting: 4) + + state = JSON::State.new(max_nesting: 3) + assert_raise(NestingError) { state.generate(recur) } + state.max_nesting = 4 + assert_equal "[0,[1,[2,[3]]]]", JSON.generate(recur, max_nesting: 4) + end + + def test_generate_pretty + json = pretty_generate({}) + assert_equal('{}', json) + + json = pretty_generate({1=>{}, 2=>[], 3=>4}) + assert_equal(<<~'JSON'.chomp, json) + { + "1": {}, + "2": [], + "3": 4 + } + JSON + + json = pretty_generate(@hash) + # hashes aren't (insertion) ordered on every ruby implementation + # assert_equal(@json3, json) + assert_equal(parse(@json3), parse(json)) + parsed_json = parse(json) + assert_equal(@hash, parsed_json) + json = pretty_generate({1=>2}) + assert_equal(<<~'JSON'.chomp, json) + { + "1": 2 + } + JSON + parsed_json = parse(json) + assert_equal({"1"=>2}, parsed_json) + assert_equal '666', pretty_generate(666) + end + + def test_generate_pretty_custom + state = State.new(:space_before => "<psb>", :space => "<ps>", :indent => "<pi>", :object_nl => "\n<po_nl>\n", :array_nl => "<pa_nl>") + json = pretty_generate({1=>{}, 2=>['a','b'], 3=>4}, state) + assert_equal(<<~'JSON'.chomp, json) + { + <po_nl> + <pi>"1"<psb>:<ps>{}, + <po_nl> + <pi>"2"<psb>:<ps>[<pa_nl><pi><pi>"a",<pa_nl><pi><pi>"b"<pa_nl><pi>], + <po_nl> + <pi>"3"<psb>:<ps>4 + <po_nl> + } + JSON + end + + def test_generate_custom + state = State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>") + json = generate({1=>{2=>3,4=>[5,6]}}, state) + assert_equal(<<~'JSON'.chomp, json) + { + <i>"1" : { + <i><i>"2" : 3, + <i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>] + <i>} + } + JSON + end + + def test_fast_generate + assert_deprecated_warning(/fast_generate/) do + json = fast_generate(@hash) + assert_equal(parse(@json2), 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_equal '666', fast_generate(666) + end + end + + def test_own_state + state = State.new + json = generate(@hash, state) + assert_equal(parse(@json2), parse(json)) + parsed_json = parse(json) + assert_equal(@hash, parsed_json) + json = generate({1=>2}, state) + assert_equal('{"1":2}', json) + parsed_json = parse(json) + assert_equal({"1"=>2}, parsed_json) + assert_equal '666', generate(666, state) + end + + def test_states + json = generate({1=>2}, nil) + assert_equal('{"1":2}', json) + s = JSON.state.new + assert s.check_circular? + assert_deprecated_warning(/JSON::State/) do + assert s[:check_circular?] + end + h = { 1=>2 } + h[3] = h + assert_raise(JSON::NestingError) { generate(h) } + assert_raise(JSON::NestingError) { generate(h, s) } + s = JSON.state.new + a = [ 1, 2 ] + a << a + assert_raise(JSON::NestingError) { generate(a, s) } + assert s.check_circular? + assert_deprecated_warning(/JSON::State/) do + assert s[:check_circular?] + end + end + + def test_falsy_state + object = { foo: [1, 2], bar: { egg: :spam }} + expected_json = JSON.generate( + object, + array_nl: "", + indent: "", + object_nl: "", + space: "", + space_before: "", + ) + + assert_equal expected_json, JSON.generate( + object, + array_nl: nil, + indent: nil, + object_nl: nil, + space: nil, + space_before: nil, + ) + end + + def test_state_defaults + state = JSON::State.new + assert_equal({ + :allow_nan => false, + :array_nl => "", + :as_json => false, + :ascii_only => false, + :buffer_initial_length => 1024, + :depth => 0, + :script_safe => false, + :strict => false, + :indent => "", + :max_nesting => 100, + :object_nl => "", + :space => "", + :space_before => "", + }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s }) + + state = JSON::State.new(allow_duplicate_key: true) + assert_equal({ + :allow_duplicate_key => true, + :allow_nan => false, + :array_nl => "", + :as_json => false, + :ascii_only => false, + :buffer_initial_length => 1024, + :depth => 0, + :script_safe => false, + :strict => false, + :indent => "", + :max_nesting => 100, + :object_nl => "", + :space => "", + :space_before => "", + }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s }) + end + + def test_allow_nan + assert_deprecated_warning(/fast_generate/) do + error = assert_raise(GeneratorError) { generate([JSON::NaN]) } + assert_same JSON::NaN, error.invalid_object + assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true) + assert_raise(GeneratorError) { fast_generate([JSON::NaN]) } + assert_raise(GeneratorError) { pretty_generate([JSON::NaN]) } + assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true) + error = assert_raise(GeneratorError) { generate([JSON::Infinity]) } + assert_same JSON::Infinity, error.invalid_object + assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true) + assert_raise(GeneratorError) { fast_generate([JSON::Infinity]) } + assert_raise(GeneratorError) { pretty_generate([JSON::Infinity]) } + assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true) + error = assert_raise(GeneratorError) { generate([JSON::MinusInfinity]) } + assert_same JSON::MinusInfinity, error.invalid_object + assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true) + assert_raise(GeneratorError) { fast_generate([JSON::MinusInfinity]) } + assert_raise(GeneratorError) { pretty_generate([JSON::MinusInfinity]) } + assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true) + end + end + + # An object that changes state.depth when it receives to_json(state) + def bad_to_json + obj = Object.new + def obj.to_json(state) + state.depth += 1 + "{#{state.object_nl}"\ + "#{state.indent * state.depth}\"foo\":#{state.space}1#{state.object_nl}"\ + "#{state.indent * (state.depth - 1)}}" + end + obj + end + + def test_depth_restored_bad_to_json + state = JSON::State.new + state.generate(bad_to_json) + assert_equal 0, state.depth + end + + def test_depth_restored_bad_to_json_in_Array + assert_equal <<~JSON.chomp, JSON.pretty_generate([bad_to_json] * 2) + [ + { + "foo": 1 + }, + { + "foo": 1 + } + ] + JSON + state = JSON::State.new + state.generate([bad_to_json]) + assert_equal 0, state.depth + end + + def test_depth_restored_bad_to_json_in_Hash + assert_equal <<~JSON.chomp, JSON.pretty_generate(a: bad_to_json, b: bad_to_json) + { + "a": { + "foo": 1 + }, + "b": { + "foo": 1 + } + } + JSON + state = JSON::State.new + state.generate(a: bad_to_json) + assert_equal 0, state.depth + end + + def test_depth + pretty = { object_nl: "\n", array_nl: "\n", space: " ", indent: " " } + state = JSON.state.new(**pretty) + assert_equal %({\n "foo": 42\n}), JSON.generate({ foo: 42 }, pretty) + assert_equal %({\n "foo": 42\n}), state.generate(foo: 42) + state.depth = 1 + assert_equal %({\n "foo": 42\n }), JSON.generate({ foo: 42 }, pretty.merge(depth: 1)) + assert_equal %({\n "foo": 42\n }), state.generate(foo: 42) + end + + def test_depth_nesting_error + ary = []; ary << ary + assert_raise(JSON::NestingError) { generate(ary) } + assert_raise(JSON::NestingError) { JSON.pretty_generate(ary) } + end + + def test_depth_nesting_error_to_json + ary = []; ary << ary + s = JSON.state.new(depth: 1) + assert_raise(JSON::NestingError) { ary.to_json(s) } + assert_equal 1, s.depth + end + + def test_depth_nesting_error_Hash_to_json + hash = {}; hash[:a] = hash + s = JSON.state.new(depth: 1) + assert_raise(JSON::NestingError) { hash.to_json(s) } + assert_equal 1, s.depth + end + + def test_depth_nesting_error_generate + ary = []; ary << ary + s = JSON.state.new(depth: 1) + assert_raise(JSON::NestingError) { s.generate(ary) } + assert_equal 1, s.depth + end + + def test_depth_exception_calling_to_json + def (obj = Object.new).to_json(*) + raise + end + s = JSON.state.new(depth: 1).freeze + assert_raise(RuntimeError) { s.generate([{ hash: obj }]) } + assert_equal 1, s.depth + end + + def test_buffer_initial_length + s = JSON.state.new + assert_equal 1024, s.buffer_initial_length + s.buffer_initial_length = 0 + assert_equal 1024, s.buffer_initial_length + s.buffer_initial_length = -1 + assert_equal 1024, s.buffer_initial_length + s.buffer_initial_length = 128 + assert_equal 128, s.buffer_initial_length + end + + def test_gc + pid = fork do + 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 + raise "#{expect}' is expected, but '#{tmp}'" unless tmp == expect + end + end + _, status = Process.waitpid2(pid) + assert_predicate status, :success? + end if GC.respond_to?(:stress=) && Process.respond_to?(:fork) + + def test_configure_using_configure_and_merge + numbered_state = { + :indent => "1", + :space => '2', + :space_before => '3', + :object_nl => '4', + :array_nl => '5' + } + state1 = JSON.state.new + state1.merge(numbered_state) + assert_equal '1', state1.indent + assert_equal '2', state1.space + assert_equal '3', state1.space_before + assert_equal '4', state1.object_nl + assert_equal '5', state1.array_nl + state2 = JSON.state.new + state2.configure(numbered_state) + assert_equal '1', state2.indent + assert_equal '2', state2.space + assert_equal '3', state2.space_before + assert_equal '4', state2.object_nl + assert_equal '5', state2.array_nl + end + + def test_configure_hash_conversion + state = JSON.state.new + state.configure(:indent => '1') + assert_equal '1', state.indent + state = JSON.state.new + foo = 'foo'.dup + assert_raise(TypeError) do + state.configure(foo) + end + def foo.to_h + { indent: '2' } + end + state.configure(foo) + assert_equal '2', state.indent + end + + 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 + 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 + assert_deprecated_warning(/JSON::State/) do + state = JSON.state.new + assert_equal nil, state[:foo] + assert_equal nil.class, state[:foo].class + assert_equal nil, state['foo'] + state[:foo] = :bar + assert_equal :bar, state[:foo] + assert_equal :bar, state['foo'] + state_hash = state.to_hash + assert_kind_of Hash, state_hash + assert_equal :bar, state_hash[:foo] + end + end + + def test_hash_likeness_set_string + assert_deprecated_warning(/JSON::State/) do + state = JSON.state.new + assert_equal nil, state[:foo] + assert_equal nil, state['foo'] + state['foo'] = :bar + assert_equal :bar, state[:foo] + assert_equal :bar, state['foo'] + state_hash = state.to_hash + assert_kind_of Hash, state_hash + assert_equal :bar, state_hash[:foo] + end + end + + def test_json_state_to_h_roundtrip + state = JSON.state.new + assert_equal state.to_h, JSON.state.new(state.to_h).to_h + end + + def test_json_generate + assert_raise JSON::GeneratorError do + generate(["\xea"]) + end + end + + def test_json_generate_error_detailed_message + error = assert_raise JSON::GeneratorError do + generate(["\xea"]) + end + + assert_not_nil(error.detailed_message) + end + + def test_json_generate_unsupported_types + assert_raise JSON::GeneratorError do + generate(Object.new, strict: true) + end + + assert_raise JSON::GeneratorError do + generate([Object.new], strict: true) + end + + assert_raise JSON::GeneratorError do + generate({ "key" => Object.new }, strict: true) + end + + assert_raise JSON::GeneratorError do + generate({ Object.new => "value" }, strict: true) + end + end + + def test_nesting + too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' + too_deep_ary = eval too_deep + assert_raise(JSON::NestingError) { generate too_deep_ary } + assert_raise(JSON::NestingError) { generate too_deep_ary, :max_nesting => 100 } + ok = generate too_deep_ary, :max_nesting => 101 + assert_equal too_deep, ok + ok = generate too_deep_ary, :max_nesting => nil + assert_equal too_deep, ok + ok = generate too_deep_ary, :max_nesting => false + assert_equal too_deep, ok + ok = generate too_deep_ary, :max_nesting => 0 + assert_equal too_deep, ok + end + + def test_backslash + data = [ '\\.(?i:gif|jpe?g|png)$' ] + json = '["\\\\.(?i:gif|jpe?g|png)$"]' + assert_equal json, generate(data) + # + data = [ '\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$' ] + json = '["\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$"]' + assert_equal json, generate(data) + # + data = [ '\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"' ] + json = '["\\\\\"\\\\\"\\\\\"\\\\\"\\\\\"\\\\\"\\\\\"\\\\\"\\\\\"\\\\\"\\\\\""]' + assert_equal json, generate(data) + # + data = [ '/' ] + json = '["/"]' + assert_equal json, generate(data) + # + data = [ '////////////////////////////////////////////////////////////////////////////////////' ] + json = '["////////////////////////////////////////////////////////////////////////////////////"]' + assert_equal json, generate(data) + # + data = [ '/' ] + json = '["\/"]' + assert_equal json, generate(data, :script_safe => true) + # + data = [ '///////////' ] + json = '["\/\/\/\/\/\/\/\/\/\/\/"]' + assert_equal json, generate(data, :script_safe => true) + # + data = [ '///////////////////////////////////////////////////////' ] + json = '["\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/"]' + assert_equal json, generate(data, :script_safe => true) + # + data = [ "\u2028\u2029" ] + json = '["\u2028\u2029"]' + assert_equal json, generate(data, :script_safe => true) + # + data = [ "ABC \u2028 DEF \u2029 GHI" ] + json = '["ABC \u2028 DEF \u2029 GHI"]' + assert_equal json, generate(data, :script_safe => true) + # + data = [ "/\u2028\u2029" ] + json = '["\/\u2028\u2029"]' + assert_equal json, generate(data, :escape_slash => true) + # + data = ['"'] + json = '["\""]' + assert_equal json, generate(data) + # + data = ['"""""""""""""""""""""""""'] + json = '["\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\""]' + assert_equal json, generate(data) + # + data = '"""""' + json = '"\"\"\"\"\""' + assert_equal json, generate(data) + # + data = "abc\n" + json = '"abc\\n"' + assert_equal json, generate(data) + # + data = "\nabc" + json = '"\\nabc"' + assert_equal json, generate(data) + # + data = "\n" + json = '"\\n"' + assert_equal json, generate(data) + # + (0..16).each do |i| + data = ('a' * i) + "\n" + json = '"' + ('a' * i) + '\\n"' + assert_equal json, generate(data) + end + # + (0..16).each do |i| + data = "\n" + ('a' * i) + json = '"' + '\\n' + ('a' * i) + '"' + assert_equal json, generate(data) + end + # + data = ["'"] + json = '["\\\'"]' + assert_equal '["\'"]', generate(data) + # + data = ["倩", "瀨"] + json = '["倩","瀨"]' + assert_equal json, generate(data, script_safe: true) + # + data = '["This is a "test" of the emergency broadcast system."]' + json = "\"[\\\"This is a \\\"test\\\" of the emergency broadcast system.\\\"]\"" + assert_equal json, generate(data) + # + data = '\tThis is a test of the emergency broadcast system.' + json = "\"\\\\tThis is a test of the emergency broadcast system.\"" + assert_equal json, generate(data) + # + data = 'This\tis a test of the emergency broadcast system.' + json = "\"This\\\\tis a test of the emergency broadcast system.\"" + assert_equal json, generate(data) + # + data = 'This is\ta test of the emergency broadcast system.' + json = "\"This is\\\\ta test of the emergency broadcast system.\"" + assert_equal json, generate(data) + # + data = 'This is a test of the emergency broadcast\tsystem.' + json = "\"This is a test of the emergency broadcast\\\\tsystem.\"" + assert_equal json, generate(data) + # + data = 'This is a test of the emergency broadcast\tsystem.\n' + json = "\"This is a test of the emergency broadcast\\\\tsystem.\\\\n\"" + assert_equal json, generate(data) + data = '"' * 15 + json = "\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\"" + assert_equal json, generate(data) + data = "\"\"\"\"\"\"\"\"\"\"\"\"\"\"a" + json = "\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"a\"" + assert_equal json, generate(data) + data = "\u0001\u0001\u0001\u0001" + json = "\"\\u0001\\u0001\\u0001\\u0001\"" + assert_equal json, generate(data) + data = "\u0001a\u0001a\u0001a\u0001a" + json = "\"\\u0001a\\u0001a\\u0001a\\u0001a\"" + assert_equal json, generate(data) + data = "\u0001aa\u0001aa" + json = "\"\\u0001aa\\u0001aa\"" + assert_equal json, generate(data) + data = "\u0001aa\u0001aa\u0001aa" + json = "\"\\u0001aa\\u0001aa\\u0001aa\"" + assert_equal json, generate(data) + data = "\u0001aa\u0001aa\u0001aa\u0001aa\u0001aa\u0001aa" + json = "\"\\u0001aa\\u0001aa\\u0001aa\\u0001aa\\u0001aa\\u0001aa\"" + assert_equal json, generate(data) + data = "\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002" + json = "\"\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\"" + assert_equal json, generate(data) + data = "ab\u0002c" + json = "\"ab\\u0002c\"" + assert_equal json, generate(data) + data = "ab\u0002cab\u0002cab\u0002cab\u0002c" + json = "\"ab\\u0002cab\\u0002cab\\u0002cab\\u0002c\"" + assert_equal json, generate(data) + data = "ab\u0002cab\u0002cab\u0002cab\u0002cab\u0002cab\u0002c" + json = "\"ab\\u0002cab\\u0002cab\\u0002cab\\u0002cab\\u0002cab\\u0002c\"" + assert_equal json, generate(data) + data = "\n\t\f\b\n\t\f\b\n\t\f\b\n\t\f" + json = "\"\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\"" + assert_equal json, generate(data) + data = "\n\t\f\b\n\t\f\b\n\t\f\b\n\t\f\b" + json = "\"\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\"" + assert_equal json, generate(data) + data = "a\n\t\f\b\n\t\f\b\n\t\f\b\n\t" + json = "\"a\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\"" + assert_equal json, generate(data) + end + + def test_string_subclass + s = Class.new(String) do + def to_s; self; end + undef to_json + end + assert_nothing_raised(SystemStackError) do + assert_equal '["foo"]', JSON.generate([s.new('foo')]) + end + end + + def test_invalid_encoding_string + error = assert_raise(JSON::GeneratorError) do + "\x82\xAC\xEF".to_json + end + assert_includes error.message, "source sequence is illegal/malformed utf-8" + + error = assert_raise(JSON::GeneratorError) do + JSON.dump("\x82\xAC\xEF") + end + assert_includes error.message, "source sequence is illegal/malformed utf-8" + + 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(JSON::GeneratorError) do + ["\x82\xAC\xEF".b].to_json + end + + badly_encoded = "\x82\xAC\xEF".b + exception = assert_raise(JSON::GeneratorError) do + { foo: badly_encoded }.to_json + end + + assert_kind_of EncodingError, exception.cause + assert_same badly_encoded, exception.invalid_object + end + + class MyCustomString < String + def to_json(_state = nil) + '"my_custom_key"' + end + + def to_s + self + end + end + + def test_string_subclass_as_keys + # Ref: https://github.com/ruby/json/issues/667 + # if key.to_s doesn't return a bare string, we call `to_json` on it. + key = MyCustomString.new("won't be used") + assert_equal '{"my_custom_key":1}', JSON.generate(key => 1) + end + + class FakeString + def to_json(_state = nil) + raise "Shouldn't be called" + end + + def to_s + self + end + end + + def test_custom_object_as_keys + key = FakeString.new + error = assert_raise(TypeError) do + JSON.generate(key => 1) + end + assert_match "FakeString", error.message + end + + def test_to_json_called_with_state_object + object = Object.new + called = false + argument = nil + object.singleton_class.define_method(:to_json) do |state| + called = true + argument = state + "<hello>" + end + + assert_equal "<hello>", JSON.dump(object) + assert called, "#to_json wasn't called" + assert_instance_of JSON::State, argument + end + + module CustomToJSON + def to_json(*) + %{"#{self.class.name}#to_json"} + end + end + + module CustomToS + def to_s + "#{self.class.name}#to_s" + end + end + + class ArrayWithToJSON < Array + include CustomToJSON + end + + def test_array_subclass_with_to_json + assert_equal '["JSONGeneratorTest::ArrayWithToJSON#to_json"]', JSON.generate([ArrayWithToJSON.new]) + assert_equal '{"[]":1}', JSON.generate(ArrayWithToJSON.new => 1) + end + + class ArrayWithToS < Array + include CustomToS + end + + def test_array_subclass_with_to_s + assert_equal '[[]]', JSON.generate([ArrayWithToS.new]) + assert_equal '{"JSONGeneratorTest::ArrayWithToS#to_s":1}', JSON.generate(ArrayWithToS.new => 1) + end + + class HashWithToJSON < Hash + include CustomToJSON + end + + def test_hash_subclass_with_to_json + assert_equal '["JSONGeneratorTest::HashWithToJSON#to_json"]', JSON.generate([HashWithToJSON.new]) + assert_equal '{"{}":1}', JSON.generate(HashWithToJSON.new => 1) + end + + class HashWithToS < Hash + include CustomToS + end + + def test_hash_subclass_with_to_s + assert_equal '[{}]', JSON.generate([HashWithToS.new]) + assert_equal '{"JSONGeneratorTest::HashWithToS#to_s":1}', JSON.generate(HashWithToS.new => 1) + end + + class StringWithToJSON < String + include CustomToJSON + end + + def test_string_subclass_with_to_json + assert_equal '["JSONGeneratorTest::StringWithToJSON#to_json"]', JSON.generate([StringWithToJSON.new]) + assert_equal '{"":1}', JSON.generate(StringWithToJSON.new => 1) + end + + class StringWithToS < String + include CustomToS + end + + def test_string_subclass_with_to_s + assert_equal '[""]', JSON.generate([StringWithToS.new]) + assert_equal '{"JSONGeneratorTest::StringWithToS#to_s":1}', JSON.generate(StringWithToS.new => 1) + end + + def test_string_subclass_with_broken_to_s + klass = Class.new(String) do + def to_s + false + end + end + s = klass.new("test") + assert_equal '["test"]', JSON.generate([s]) + + omit("Can't figure out how to match behavior in java code") if RUBY_PLATFORM == "java" + + assert_raise TypeError do + JSON.generate(s => 1) + end + end + + if defined?(JSON::Ext::Generator) and RUBY_PLATFORM != "java" + def test_valid_utf8_in_different_encoding + utf8_string = "€™" + wrong_encoding_string = utf8_string.b + # This behavior is historical. Not necessary desirable. We should deprecated it. + # The pure and java version of the gem already don't behave this way. + assert_warning(/UTF-8 string passed as BINARY, this will raise an encoding error in json 3.0/) do + assert_equal utf8_string.to_json, wrong_encoding_string.to_json + end + + assert_warning(/UTF-8 string passed as BINARY, this will raise an encoding error in json 3.0/) do + assert_equal JSON.dump(utf8_string), JSON.dump(wrong_encoding_string) + end + end + + def test_string_ext_included_calls_super + included = false + + Module.send(:alias_method, :included_orig, :included) + Module.send(:remove_method, :included) + Module.send(:define_method, :included) do |base| + included_orig(base) + included = true + end + + Class.new(String) do + include JSON::Ext::Generator::GeneratorMethods::String + end + + assert included + ensure + if Module.private_method_defined?(:included_orig) + Module.send(:remove_method, :included) if Module.method_defined?(:included) + Module.send(:alias_method, :included, :included_orig) + Module.send(:remove_method, :included_orig) + end + end + end + + def test_nonutf8_encoding + assert_equal("\"5\u{b0}\"", "5\xb0".dup.force_encoding(Encoding::ISO_8859_1).to_json) + end + + def test_utf8_multibyte + assert_equal('["foßbar"]', JSON.generate(["foßbar"])) + assert_equal('"n€ßt€ð2"', JSON.generate("n€ßt€ð2")) + assert_equal('"\"\u0000\u001f"', JSON.generate("\"\u0000\u001f")) + end + + def test_fragment + fragment = JSON::Fragment.new(" 42") + assert_equal '{"number": 42}', JSON.generate({ number: fragment }) + assert_equal '{"number": 42}', JSON.generate({ number: fragment }, strict: true) + end + + def test_json_generate_as_json_convert_to_proc + object = Object.new + assert_equal object.object_id.to_json, JSON.generate(object, strict: true, as_json: -> (o, is_key) { o.object_id }) + end + + def test_as_json_nan_does_not_call_to_json + def (obj = Object.new).to_json(*) + "null" + end + assert_raise(JSON::GeneratorError) do + JSON.generate(Float::NAN, strict: true, as_json: proc { obj }) + end + end + + def assert_float_roundtrip(expected, actual) + assert_equal(expected, JSON.generate(actual)) + assert_equal(actual, JSON.parse(JSON.generate(actual)), "JSON: #{JSON.generate(actual)}") + end + + def test_json_generate_float + assert_float_roundtrip "-1.0", -1.0 + assert_float_roundtrip "1.0", 1.0 + assert_float_roundtrip "0.0", 0.0 + assert_float_roundtrip "12.2", 12.2 + assert_float_roundtrip "2.34375", 7.5 / 3.2 + assert_float_roundtrip "12.0", 12.0 + assert_float_roundtrip "100.0", 100.0 + assert_float_roundtrip "1000.0", 1000.0 + + if RUBY_ENGINE == "jruby" + assert_float_roundtrip "1.7468619377842371E9", 1746861937.7842371 + else + assert_float_roundtrip "1746861937.7842371", 1746861937.7842371 + end + + if RUBY_ENGINE == "ruby" + assert_float_roundtrip "100000000000000.0", 100000000000000.0 + assert_float_roundtrip "1e+15", 1e+15 + assert_float_roundtrip "-100000000000000.0", -100000000000000.0 + assert_float_roundtrip "-1e+15", -1e+15 + assert_float_roundtrip "1111111111111111.1", 1111111111111111.1 + assert_float_roundtrip "1.1111111111111112e+16", 11111111111111111.1 + assert_float_roundtrip "-1111111111111111.1", -1111111111111111.1 + assert_float_roundtrip "-1.1111111111111112e+16", -11111111111111111.1 + + assert_float_roundtrip "-0.000000022471348024634545", -2.2471348024634545e-08 + assert_float_roundtrip "-0.0000000022471348024634545", -2.2471348024634545e-09 + assert_float_roundtrip "-2.2471348024634546e-10", -2.2471348024634545e-10 + end + end + + def test_numbers_of_various_sizes + numbers = [ + 0, 1, -1, 9, -9, 13, -13, 91, -91, 513, -513, 7513, -7513, + 17591, -17591, -4611686018427387904, 4611686018427387903, + 2**62, 2**63, 2**64, -(2**62), -(2**63), -(2**64) + ] + + numbers.each do |number| + assert_equal "[#{number}]", JSON.generate([number]) + end + end + + def test_generate_duplicate_keys_allowed + hash = { foo: 1, "foo" => 2 } + assert_equal %({"foo":1,"foo":2}), JSON.generate(hash, allow_duplicate_key: true) + end + + def test_generate_duplicate_keys_deprecated + hash = { foo: 1, "foo" => 2 } + assert_deprecated_warning(/allow_duplicate_key/) do + assert_equal %({"foo":1,"foo":2}), JSON.generate(hash) + end + end + + def test_generate_duplicate_keys_disallowed + hash = { foo: 1, "foo" => 2 } + error = assert_raise JSON::GeneratorError do + JSON.generate(hash, allow_duplicate_key: false) + end + assert_equal %(detected duplicate key "foo" in #{hash.inspect}), error.message + end + + def test_frozen + state = JSON::State.new.freeze + assert_raise(FrozenError) do + state.configure(max_nesting: 1) + end + setters = state.methods.grep(/\w=$/) + assert_not_empty setters + setters.each do |setter| + assert_raise(FrozenError) do + state.send(setter, 1) + end + end + end + + # The case when the State is frozen is tested in JSONCoderTest#test_nesting_recovery + def test_nesting_recovery + state = JSON::State.new + ary = [] + ary << ary + assert_raise(JSON::NestingError) { state.generate(ary) } + assert_equal 0, state.depth + assert_equal '{"a":1}', state.generate({ a: 1 }) + end +end diff --git a/test/json/json_generic_object_test.rb b/test/json/json_generic_object_test.rb new file mode 100644 index 0000000000..57e3bf3c52 --- /dev/null +++ b/test/json/json_generic_object_test.rb @@ -0,0 +1,91 @@ +# frozen_string_literal: true +require_relative 'test_helper' + +# ostruct is required to test JSON::GenericObject +begin + require "ostruct" +rescue LoadError + return +end + +class JSONGenericObjectTest < Test::Unit::TestCase + def setup + if defined?(JSON::GenericObject) + @go = JSON::GenericObject[ :a => 1, :b => 2 ] + else + omit("JSON::GenericObject is not available") + end + end + + def test_attributes + assert_equal 1, @go.a + assert_equal 1, @go[:a] + assert_equal 2, @go.b + assert_equal 2, @go[:b] + assert_nil @go.c + assert_nil @go[:c] + end + + def test_generate_json + switch_json_creatable do + assert_equal @go, JSON(JSON(@go), :create_additions => true) + end + end + + def test_parse_json + assert_kind_of Hash, + JSON( + '{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }', + :create_additions => true + ) + switch_json_creatable do + assert_equal @go, l = + JSON( + '{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }', + :create_additions => true + ) + assert_equal 1, l.a + assert_equal @go, + l = JSON('{ "a": 1, "b": 2 }', :object_class => JSON::GenericObject) + assert_equal 1, l.a + assert_equal JSON::GenericObject[:a => JSON::GenericObject[:b => 2]], + l = JSON('{ "a": { "b": 2 } }', :object_class => JSON::GenericObject) + assert_equal 2, l.a.b + end + end + + def test_from_hash + result = JSON::GenericObject.from_hash( + :foo => { :bar => { :baz => true }, :quux => [ { :foobar => true } ] }) + assert_kind_of JSON::GenericObject, result.foo + assert_kind_of JSON::GenericObject, result.foo.bar + assert_equal true, result.foo.bar.baz + assert_kind_of JSON::GenericObject, result.foo.quux.first + assert_equal true, result.foo.quux.first.foobar + assert_equal true, JSON::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 + JSON::GenericObject.json_creatable = true + yield + ensure + JSON::GenericObject.json_creatable = false + end +end diff --git a/test/json/json_parser_test.rb b/test/json/json_parser_test.rb new file mode 100644 index 0000000000..ac53ba9f0c --- /dev/null +++ b/test/json/json_parser_test.rb @@ -0,0 +1,859 @@ +# frozen_string_literal: true +require_relative 'test_helper' +require 'stringio' +require 'tempfile' +begin + require 'ostruct' +rescue LoadError +end +begin + require 'bigdecimal' +rescue LoadError +end + +class JSONParserTest < Test::Unit::TestCase + include JSON + + def test_construction + parser = JSON::Parser.new('test') + assert_equal 'test', parser.source + end + + def test_argument_encoding_unmodified + source = "{}".encode(Encoding::UTF_16) + JSON::Parser.new(source) + assert_equal Encoding::UTF_16, source.encoding + end + + def test_argument_encoding_for_binary_unmodified + source = "{}".b + JSON::Parser.new(source) + assert_equal Encoding::ASCII_8BIT, source.encoding + end + + def test_error_message_encoding + bug10705 = '[ruby-core:67386] [Bug #10705]' + json = ".\"\xE2\x88\x9A\"" + assert_equal(Encoding::UTF_8, json.encoding) + e = assert_raise(JSON::ParserError) { + JSON::Ext::Parser.new(json).parse + } + assert_equal(Encoding::UTF_8, e.message.encoding, bug10705) + assert_include(e.message, json, bug10705) + end + + def test_parsing + parser = JSON::Parser.new('"test"') + assert_equal 'test', parser.parse + end + + def test_parser_reset + parser = Parser.new('{"a":"b"}') + assert_equal({ 'a' => 'b' }, parser.parse) + assert_equal({ 'a' => 'b' }, parser.parse) + end + + def test_parse_values + assert_equal(nil, parse('null')) + assert_equal(false, parse('false')) + assert_equal(true, parse('true')) + assert_equal(-23, parse('-23')) + assert_equal(23, parse('23')) + assert_in_delta(0.23, parse('0.23'), 1e-2) + assert_in_delta(0.0, parse('0e0'), 1e-2) + assert_equal("", parse('""')) + assert_equal("foobar", parse('"foobar"')) + end + + def test_parse_simple_arrays + assert_equal([], parse('[]')) + assert_equal([], parse(' [ ] ')) + assert_equal([ nil ], parse('[null]')) + assert_equal([ false ], parse('[false]')) + assert_equal([ true ], parse('[true]')) + assert_equal([ -23 ], parse('[-23]')) + assert_equal([ 23 ], parse('[23]')) + assert_equal_float([ 0.23 ], parse('[0.23]')) + assert_equal_float([ 0.0 ], parse('[0e0]')) + assert_equal([""], parse('[""]')) + assert_equal(["foobar"], parse('["foobar"]')) + assert_equal([{}], parse('[{}]')) + end + + def test_parse_simple_objects + assert_equal({}, parse('{}')) + assert_equal({}, parse(' { } ')) + assert_equal({ "a" => nil }, parse('{ "a" : null}')) + assert_equal({ "a" => nil }, parse('{"a":null}')) + assert_equal({ "a" => false }, parse('{ "a" : false } ')) + assert_equal({ "a" => false }, parse('{"a":false}')) + assert_raise(JSON::ParserError) { parse('{false}') } + assert_equal({ "a" => true }, parse('{"a":true}')) + assert_equal({ "a" => true }, parse(' { "a" : true } ')) + assert_equal({ "a" => -23 }, parse(' { "a" : -23 } ')) + assert_equal({ "a" => -23 }, parse(' { "a" : -23 } ')) + assert_equal({ "a" => 23 }, parse('{"a":23 } ')) + assert_equal({ "a" => 23 }, parse(' { "a" : 23 } ')) + assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } ')) + assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } ')) + assert_equal({ "" => 123 }, parse('{"":123}')) + end + + def test_parse_numbers + assert_raise(JSON::ParserError) { parse('+23.2') } + assert_raise(JSON::ParserError) { parse('+23') } + assert_raise(JSON::ParserError) { parse('.23') } + assert_raise(JSON::ParserError) { parse('023') } + assert_raise(JSON::ParserError) { parse('-023') } + assert_raise(JSON::ParserError) { parse('023.12') } + assert_raise(JSON::ParserError) { parse('-023.12') } + assert_raise(JSON::ParserError) { parse('023e12') } + assert_raise(JSON::ParserError) { parse('-023e12') } + assert_raise(JSON::ParserError) { parse('-') } + assert_raise(JSON::ParserError) { parse('-.1') } + assert_raise(JSON::ParserError) { parse('-e0') } + assert_equal(23, parse('23')) + assert_equal(-23, parse('-23')) + assert_equal_float(3.141, parse('3.141')) + assert_equal_float(-3.141, parse('-3.141')) + assert_equal_float(3.141, parse('3141e-3')) + assert_equal_float(3.141, parse('3141.1e-3')) + assert_equal_float(3.141, parse('3141E-3')) + assert_equal_float(3.141, parse('3141.0E-3')) + assert_equal_float(-3.141, parse('-3141.0e-3')) + assert_equal_float(-3.141, parse('-3141e-3')) + assert_raise(ParserError) { parse('NaN') } + assert parse('NaN', :allow_nan => true).nan? + assert_raise(ParserError) { parse('Infinity') } + assert_equal(1.0/0, parse('Infinity', :allow_nan => true)) + assert_raise(ParserError) { parse('-Infinity') } + assert_equal(-1.0/0, parse('-Infinity', :allow_nan => true)) + capture_output { assert_equal(Float::INFINITY, parse("23456789012E666")) } + end + + def test_parse_bignum + bignum = Integer('1234567890' * 10) + assert_equal(bignum, JSON.parse(bignum.to_s)) + assert_equal(bignum.to_f, JSON.parse(bignum.to_s + ".0")) + end + + def test_parse_bigdecimals + assert_equal(BigDecimal, JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"].class) + assert_equal(BigDecimal("0.901234567890123456789E1"),JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"] ) + end if defined?(::BigDecimal) + + def test_parse_string_mixed_unicode + assert_equal(["éé"], JSON.parse("[\"\\u00e9é\"]")) + 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| + json = pretty_generate(perm) + assert_equal perm, parse(json) + 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 = 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 + + def test_parse_control_chars_in_string + 0.upto(31) do |ord| + assert_raise JSON::ParserError do + parse(%("#{ord.chr}")) + end + end + end + + def test_parse_allowed_control_chars_in_string + 0.upto(31) do |ord| + assert_equal ord.chr, parse(%("#{ord.chr}"), allow_control_characters: true) + end + end + + def test_parse_arrays + assert_equal([1,2,3], parse('[1,2,3]')) + assert_equal([1.2,2,3], parse('[1.2,2,3]')) + assert_equal([[],[[],[]]], parse('[[],[[],[]]]')) + assert_equal([], parse('[]')) + assert_equal([], parse(' [ ] ')) + assert_equal([1], parse('[1]')) + assert_equal([1], parse(' [ 1 ] ')) + ary = [[1], ["foo"], [3.14], [4711.0], [2.718], [nil], + [[1, -2, 3]], [false], [true]] + 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]\s + , [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] })) + end + + def test_parse_json_primitive_values + assert_raise(JSON::ParserError) { parse('') } + assert_raise(TypeError) { parse(nil) } + assert_raise(JSON::ParserError) { parse(' /* foo */ ') } + assert_equal nil, parse('null') + assert_equal false, parse('false') + assert_equal true, parse('true') + assert_equal 23, parse('23') + assert_equal 1, parse('1') + assert_equal_float 3.141, parse('3.141'), 1E-3 + assert_equal 2 ** 64, parse('18446744073709551616') + assert_equal 'foo', parse('"foo"') + assert parse('NaN', :allow_nan => true).nan? + assert parse('Infinity', :allow_nan => true).infinite? + assert parse('-Infinity', :allow_nan => true).infinite? + 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 + assert_equal([""], parse('[""]')) + assert_equal(["\\"], parse('["\\\\"]')) + assert_equal(['"'], parse('["\""]')) + assert_equal(['\\"\\'], parse('["\\\\\\"\\\\"]')) + assert_equal( + ["\"\b\n\r\t\0\037"], + parse('["\"\b\n\r\t\u0000\u001f"]') + ) + end + + if RUBY_ENGINE != "jruby" # https://github.com/ruby/json/issues/138 + def test_parse_broken_string + s = parse(%{["\x80"]})[0] + assert_equal("\x80", s) + assert_equal Encoding::UTF_8, s.encoding + assert_equal false, s.valid_encoding? + + s = parse(%{["\x80"]}.b)[0] + assert_equal("\x80", s) + assert_equal Encoding::UTF_8, s.encoding + assert_equal false, s.valid_encoding? + + input = %{["\x80"]}.dup.force_encoding(Encoding::US_ASCII) + assert_raise(Encoding::InvalidByteSequenceError) { parse(input) } + end + end + + def test_invalid_unicode_escape + assert_raise(JSON::ParserError) { parse('"\u"') } + assert_raise(JSON::ParserError) { parse('"\ua"') } + assert_raise(JSON::ParserError) { parse('"\uaa"') } + assert_raise(JSON::ParserError) { parse('"\uaaa"') } + assert_equal "\uaaaa", parse('"\uaaaa"') + + assert_raise(JSON::ParserError) { parse('"\u______"') } + assert_raise(JSON::ParserError) { parse('"\u1_____"') } + assert_raise(JSON::ParserError) { parse('"\u11____"') } + assert_raise(JSON::ParserError) { parse('"\u111___"') } + end + + def test_unicode_followed_by_newline + # Ref: https://github.com/ruby/json/issues/912 + assert_equal "🌌\n".bytes, JSON.parse('"\ud83c\udf0c\n"').bytes + assert_equal "🌌\n", JSON.parse('"\ud83c\udf0c\n"') + assert_predicate JSON.parse('"\ud83c\udf0c\n"'), :valid_encoding? + end + + def test_invalid_surogates + assert_raise(JSON::ParserError) { parse('"\\uD800"') } + assert_raise(JSON::ParserError) { parse('"\\uD800_________________"') } + assert_raise(JSON::ParserError) { parse('"\\uD800\\u0041"') } + assert_raise(JSON::ParserError) { parse('"\\uD800\\u004') } + end + + def test_parse_big_integers + json1 = JSON(orig = (1 << 31) - 1) + assert_equal orig, parse(json1) + json2 = JSON(orig = 1 << 31) + assert_equal orig, parse(json2) + json3 = JSON(orig = (1 << 62) - 1) + assert_equal orig, parse(json3) + json4 = JSON(orig = 1 << 62) + assert_equal orig, parse(json4) + json5 = JSON(orig = 1 << 64) + assert_equal orig, parse(json5) + end + + def test_parse_escaped_key + doc = { + "test\r1" => 1, + "entries" => [ + "test\t2" => 2, + "test\n3" => 3, + ] + } + + assert_equal doc, parse(JSON.generate(doc)) + end + + def test_parse_duplicate_key + expected = {"a" => 2} + expected_sym = {a: 2} + + assert_equal expected, parse('{"a": 1, "a": 2}', allow_duplicate_key: true) + assert_raise(ParserError) { parse('{"a": 1, "a": 2}', allow_duplicate_key: false) } + assert_raise(ParserError) { parse('{"a": 1, "a": 2}', allow_duplicate_key: false, symbolize_names: true) } + + assert_deprecated_warning(/duplicate key "a"/) do + assert_equal expected, parse('{"a": 1, "a": 2}') + end + assert_deprecated_warning(/duplicate key "a"/) do + assert_equal expected_sym, parse('{"a": 1, "a": 2}', symbolize_names: true) + end + + if RUBY_ENGINE == 'ruby' + assert_deprecated_warning(/#{File.basename(__FILE__)}\:#{__LINE__ + 1}/) do + assert_equal expected, parse('{"a": 1, "a": 2}') + end + end + + unless RUBY_ENGINE == 'jruby' + assert_raise(ParserError) do + fake_key = Object.new + JSON.load('{"a": 1, "a": 2}', -> (obj) { obj == "a" ? fake_key : obj }, allow_duplicate_key: false) + end + + assert_deprecated_warning(/duplicate key #<Object:0x/) do + fake_key = Object.new + JSON.load('{"a": 1, "a": 2}', -> (obj) { obj == "a" ? fake_key : obj }) + end + end + end + + def test_some_wrong_inputs + assert_raise(ParserError) { parse('[] bla') } + assert_raise(ParserError) { parse('[] 1') } + assert_raise(ParserError) { parse('[] []') } + assert_raise(ParserError) { parse('[] {}') } + assert_raise(ParserError) { parse('{} []') } + assert_raise(ParserError) { parse('{} {}') } + assert_raise(ParserError) { parse('[NULL]') } + assert_raise(ParserError) { parse('[FALSE]') } + assert_raise(ParserError) { parse('[TRUE]') } + assert_raise(ParserError) { parse('[07] ') } + assert_raise(ParserError) { parse('[0a]') } + assert_raise(ParserError) { parse('[1.]') } + assert_raise(ParserError) { parse(' ') } + end + + def test_symbolize_names + assert_equal({ "foo" => "bar", "baz" => "quux" }, + parse('{"foo":"bar", "baz":"quux"}')) + assert_equal({ :foo => "bar", :baz => "quux" }, + parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true)) + assert_raise(ArgumentError) do + parse('{}', :symbolize_names => true, :create_additions => true) + end + end + + def test_freeze + assert_predicate parse('{}', :freeze => true), :frozen? + assert_predicate parse('[]', :freeze => true), :frozen? + assert_predicate parse('"foo"', :freeze => true), :frozen? + + assert_same(-'foo', parse('"foo"', :freeze => true)) + assert_same(-'foo', parse('{"foo": 1}', :freeze => true).keys.first) + end + + def test_parse_comments + json = <<~JSON + { + "key1":"value1", // eol comment + "key2":"value2" /* multi line + * comment */, + "key3":"value3" /* multi line + // nested eol comment + * comment */ + } + JSON + assert_equal( + { "key1" => "value1", "key2" => "value2", "key3" => "value3" }, + parse(json)) + json = <<~JSON + { + "key1":"value1" /* multi line + // nested eol comment + /* illegal nested multi line comment */ + * comment */ + } + JSON + assert_raise(ParserError) { parse(json) } + json = <<~JSON + { + "key1":"value1" /* multi line + // nested eol comment + /* legal nested multi line comment start sequence */ + } + JSON + assert_equal({ "key1" => "value1" }, parse(json)) + json = <<~JSON + { + "key1":"value1" /* multi line + // nested eol comment + closed multi comment */ + and again, throw an Error */ + } + JSON + assert_raise(ParserError) { parse(json) } + json = <<~JSON + { + "key1":"value1" /*/*/ + } + JSON + assert_equal({ "key1" => "value1" }, parse(json)) + assert_equal({}, parse('{} /**/')) + assert_raise(ParserError) { parse('{} /* comment not closed') } + assert_raise(ParserError) { parse('{} /*/') } + assert_raise(ParserError) { parse('{} /x wrong comment') } + assert_raise(ParserError) { parse('{} /') } + end + + def test_nesting + too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' + too_deep_ary = eval too_deep + assert_raise(JSON::NestingError) { parse too_deep } + assert_raise(JSON::NestingError) { parse too_deep, :max_nesting => 100 } + ok = parse too_deep, :max_nesting => 101 + assert_equal too_deep_ary, ok + ok = parse too_deep, :max_nesting => nil + assert_equal too_deep_ary, ok + ok = parse too_deep, :max_nesting => false + assert_equal too_deep_ary, ok + ok = parse too_deep, :max_nesting => 0 + assert_equal too_deep_ary, ok + end + + def test_backslash + data = [ '\\.(?i:gif|jpe?g|png)$' ] + json = '["\\\\.(?i:gif|jpe?g|png)$"]' + assert_equal data, parse(json) + # + data = [ '\\"' ] + json = '["\\\\\""]' + assert_equal data, parse(json) + # + json = '["/"]' + data = [ '/' ] + assert_equal data, parse(json) + # + json = '["\""]' + data = ['"'] + assert_equal data, parse(json) + # + json = '["\\/"]' + data = ["/"] + assert_equal data, parse(json) + + json = '["\/"]' + data = [ '/' ] + assert_equal data, parse(json) + + data = ['"""""""""""""""""""""""""'] + json = '["\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\""]' + assert_equal data, parse(json) + + data = '["This is a "test" of the emergency broadcast system."]' + json = "\"[\\\"This is a \\\"test\\\" of the emergency broadcast system.\\\"]\"" + assert_equal data, parse(json) + + data = '\tThis is a test of the emergency broadcast system.' + json = "\"\\\\tThis is a test of the emergency broadcast system.\"" + assert_equal data, parse(json) + + data = 'This\tis a test of the emergency broadcast system.' + json = "\"This\\\\tis a test of the emergency broadcast system.\"" + assert_equal data, parse(json) + + data = 'This is\ta test of the emergency broadcast system.' + json = "\"This is\\\\ta test of the emergency broadcast system.\"" + assert_equal data, parse(json) + + data = 'This is a test of the emergency broadcast\tsystem.' + json = "\"This is a test of the emergency broadcast\\\\tsystem.\"" + assert_equal data, parse(json) + + data = 'This is a test of the emergency broadcast\tsystem.\n' + json = "\"This is a test of the emergency broadcast\\\\tsystem.\\\\n\"" + assert_equal data, parse(json) + + data = '"' * 15 + json = "\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\"" + assert_equal data, parse(json) + + data = "\"\"\"\"\"\"\"\"\"\"\"\"\"\"a" + json = "\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"a\"" + assert_equal data, parse(json) + + data = "\u0001\u0001\u0001\u0001" + json = "\"\\u0001\\u0001\\u0001\\u0001\"" + assert_equal data, parse(json) + + data = "\u0001a\u0001a\u0001a\u0001a" + json = "\"\\u0001a\\u0001a\\u0001a\\u0001a\"" + assert_equal data, parse(json) + + data = "\u0001aa\u0001aa" + json = "\"\\u0001aa\\u0001aa\"" + assert_equal data, parse(json) + + data = "\u0001aa\u0001aa\u0001aa" + json = "\"\\u0001aa\\u0001aa\\u0001aa\"" + assert_equal data, parse(json) + + data = "\u0001aa\u0001aa\u0001aa\u0001aa\u0001aa\u0001aa" + json = "\"\\u0001aa\\u0001aa\\u0001aa\\u0001aa\\u0001aa\\u0001aa\"" + assert_equal data, parse(json) + + data = "\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002" + json = "\"\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\"" + assert_equal data, parse(json) + + data = "ab\u0002c" + json = "\"ab\\u0002c\"" + assert_equal data, parse(json) + + data = "ab\u0002cab\u0002cab\u0002cab\u0002c" + json = "\"ab\\u0002cab\\u0002cab\\u0002cab\\u0002c\"" + assert_equal data, parse(json) + + data = "ab\u0002cab\u0002cab\u0002cab\u0002cab\u0002cab\u0002c" + json = "\"ab\\u0002cab\\u0002cab\\u0002cab\\u0002cab\\u0002cab\\u0002c\"" + assert_equal data, parse(json) + + data = "\n\t\f\b\n\t\f\b\n\t\f\b\n\t\f" + json = "\"\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\"" + assert_equal data, parse(json) + + data = "\n\t\f\b\n\t\f\b\n\t\f\b\n\t\f\b" + json = "\"\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\"" + assert_equal data, parse(json) + + data = "a\n\t\f\b\n\t\f\b\n\t\f\b\n\t" + json = "\"a\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\"" + assert_equal data, parse(json) + end + + class SubArray < Array + def <<(v) + @shifted = true + super + end + + def shifted? + @shifted + end + end + + class SubArray2 < Array + def to_json(*a) + { + JSON.create_id => self.class.name, + 'ary' => to_a, + }.to_json(*a) + end + + def self.json_create(o) + o.delete JSON.create_id + o['ary'] + end + end + + class SubArrayWrapper + def initialize + @data = [] + end + + attr_reader :data + + def [](index) + @data[index] + end + + def <<(value) + @data << value + @shifted = true + end + + def shifted? + @shifted + end + end + + def test_parse_array_custom_array_derived_class + res = parse('[1,2]', :array_class => SubArray) + assert_equal([1,2], res) + assert_equal(SubArray, res.class) + assert res.shifted? + end + + def test_parse_array_custom_non_array_derived_class + res = parse('[1,2]', :array_class => SubArrayWrapper) + assert_equal([1,2], res.data) + assert_equal(1, res[0]) + assert_equal(SubArrayWrapper, res.class) + assert res.shifted? + end + + def test_parse_object + assert_equal({}, parse('{}')) + assert_equal({}, parse(' { } ')) + assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}')) + assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } ')) + end + + class SubHash < Hash + def []=(k, v) + @item_set = true + super + end + + def item_set? + @item_set + end + end + + class SubHash2 < Hash + def to_json(*a) + { + JSON.create_id => self.class.name, + }.merge(self).to_json(*a) + end + + def self.json_create(o) + o.delete JSON.create_id + self[o] + end + end + + def test_parse_object_custom_hash_derived_class + res = parse('{"foo":"bar"}', :object_class => SubHash) + assert_equal({"foo" => "bar"}, res) + assert_equal(SubHash, res.class) + assert res.item_set? + end + + if defined?(::OpenStruct) + class SubOpenStruct < OpenStruct + def [](k) + __send__(k) + end + + def []=(k, v) + @item_set = true + __send__("#{k}=", v) + end + + def item_set? + @item_set + end + end + + def test_parse_object_custom_non_hash_derived_class + res = parse('{"foo":"bar"}', :object_class => SubOpenStruct) + assert_equal "bar", res.foo + assert_equal "bar", res[:foo] + assert_equal(SubOpenStruct, res.class) + assert res.item_set? + end + + def test_parse_generic_object + res = parse( + '{"foo":"bar", "baz":{}}', + :object_class => JSON::GenericObject + ) + assert_equal(JSON::GenericObject, res.class) + assert_equal "bar", res.foo + assert_equal "bar", res["foo"] + assert_equal "bar", res[:foo] + assert_equal "bar", res.to_hash[:foo] + assert_equal(JSON::GenericObject, res.baz.class) + end + end + + def test_generate_core_subclasses_with_new_to_json + obj = SubHash2["foo" => SubHash2["bar" => true]] + obj_json = JSON(obj) + obj_again = parse(obj_json, :create_additions => true) + assert_kind_of SubHash2, obj_again + assert_kind_of SubHash2, obj_again['foo'] + assert obj_again['foo']['bar'] + assert_equal obj, obj_again + assert_equal ["foo"], + JSON(JSON(SubArray2["foo"]), :create_additions => true) + end + + def test_generate_core_subclasses_with_default_to_json + assert_equal '{"foo":"bar"}', JSON(SubHash["foo" => "bar"]) + assert_equal '["foo"]', JSON(SubArray["foo"]) + end + + def test_generate_of_core_subclasses + obj = SubHash["foo" => SubHash["bar" => true]] + obj_json = JSON(obj) + obj_again = JSON(obj_json) + assert_kind_of Hash, obj_again + assert_kind_of Hash, obj_again['foo'] + assert obj_again['foo']['bar'] + assert_equal obj, obj_again + end + + def test_parsing_frozen_ascii8bit_string + assert_equal( + { 'foo' => 'bar' }, + JSON('{ "foo": "bar" }'.b.freeze) + ) + end + + def test_parse_error_message_length + # Error messages aren't consistent across backends, but we can at least + # enforce that if they include fragments of the source it should be of + # reasonable size. + error = assert_raise(JSON::ParserError) do + JSON.parse('{"foo": ' + ('A' * 500) + '}') + end + assert_operator 80, :>, error.message.bytesize + end + + def test_parse_error_incomplete_hash + error = assert_raise(JSON::ParserError) do + JSON.parse('{"input":{"firstName":"Bob","lastName":"Mob","email":"bob@example.com"}') + end + if RUBY_ENGINE == "ruby" + assert_equal %(expected ',' or '}' after object value, got: EOF at line 1 column 72), error.message + end + end + + def test_parse_error_snippet + omit "C ext only test" unless RUBY_ENGINE == "ruby" + + error = assert_raise(JSON::ParserError) { JSON.parse("あああああああああああああああああああああああ") } + assert_equal "unexpected character: 'ああああああああああ' at line 1 column 1", error.message + + error = assert_raise(JSON::ParserError) { JSON.parse("aあああああああああああああああああああああああ") } + assert_equal "unexpected character: 'aああああああああああ' at line 1 column 1", error.message + + error = assert_raise(JSON::ParserError) { JSON.parse("abあああああああああああああああああああああああ") } + assert_equal "unexpected character: 'abあああああああああ' at line 1 column 1", error.message + + error = assert_raise(JSON::ParserError) { JSON.parse("abcあああああああああああああああああああああああ") } + assert_equal "unexpected character: 'abcあああああああああ' at line 1 column 1", error.message + end + + def test_parse_leading_slash + # ref: https://github.com/ruby/ruby/pull/12598 + assert_raise(JSON::ParserError) do + JSON.parse("/foo/bar") + end + end + + def test_parse_whitespace_after_newline + assert_equal [], JSON.parse("[\n#{' ' * (8 + 8 + 4 + 3)}]") + end + + def test_frozen + parser_config = JSON::Parser::Config.new({}).freeze + assert_raise FrozenError do + parser_config.send(:initialize, {}) + end + end + + private + + def assert_equal_float(expected, actual, delta = 1e-2) + Array === expected and expected = expected.first + Array === actual and actual = actual.first + assert_in_delta(expected, actual, delta) + end +end diff --git a/test/json/json_ryu_fallback_test.rb b/test/json/json_ryu_fallback_test.rb new file mode 100644 index 0000000000..59ba76d392 --- /dev/null +++ b/test/json/json_ryu_fallback_test.rb @@ -0,0 +1,169 @@ +# frozen_string_literal: true +require_relative 'test_helper' +begin + require 'bigdecimal' +rescue LoadError +end + +class JSONRyuFallbackTest < Test::Unit::TestCase + include JSON + + # Test that numbers with more than 17 significant digits fall back to rb_cstr_to_dbl + def test_more_than_17_significant_digits + # These numbers have > 17 significant digits and should use fallback path + # They should still parse correctly, just not via the Ryu optimization + + test_cases = [ + # input, expected (rounded to double precision) + ["1.23456789012345678901234567890", 1.2345678901234567], + ["123456789012345678.901234567890", 1.2345678901234568e+17], + ["0.123456789012345678901234567890", 0.12345678901234568], + ["9999999999999999999999999999.9", 1.0e+28], + # Edge case: exactly 18 digits + ["123456789012345678", 123456789012345680.0], + # Many fractional digits + ["0.12345678901234567890123456789", 0.12345678901234568], + ] + + test_cases.each do |input, expected| + result = JSON.parse(input) + assert_in_delta(expected, result, 1e-10, + "Failed to parse #{input} correctly (>17 digits, fallback path)") + end + end + + # Test decimal_class option forces fallback + def test_decimal_class_option + input = "3.141" + + # Without decimal_class: uses Ryu, returns Float + result_float = JSON.parse(input) + assert_instance_of(Float, result_float) + assert_equal(3.141, result_float) + + # With decimal_class: uses fallback, returns BigDecimal + result_bigdecimal = JSON.parse(input, decimal_class: BigDecimal) + assert_instance_of(BigDecimal, result_bigdecimal) + assert_equal(BigDecimal("3.141"), result_bigdecimal) + end if defined?(::BigDecimal) + + # Test that numbers with <= 17 digits use Ryu optimization + def test_ryu_optimization_used_for_normal_numbers + test_cases = [ + ["3.141", 3.141], + ["1.23456789012345e100", 1.23456789012345e100], + ["0.00000000000001", 1.0e-14], + ["123456789012345.67", 123456789012345.67], + ["-1.7976931348623157e+308", -1.7976931348623157e+308], + ["2.2250738585072014e-308", 2.2250738585072014e-308], + # Exactly 17 significant digits + ["12345678901234567", 12345678901234567.0], + ["1.2345678901234567", 1.2345678901234567], + ] + + test_cases.each do |input, expected| + result = JSON.parse(input) + assert_in_delta(expected, result, expected.abs * 1e-15, + "Failed to parse #{input} correctly (<=17 digits, Ryu path)") + end + end + + # Test edge cases at the boundary (17 digits) + def test_seventeen_digit_boundary + # Exactly 17 significant digits should use Ryu + input_17 = "12345678901234567.0" # Force it to be a float with .0 + result = JSON.parse(input_17) + assert_in_delta(12345678901234567.0, result, 1e-10) + + # 18 significant digits should use fallback + input_18 = "123456789012345678.0" + result = JSON.parse(input_18) + # Note: This will be rounded to double precision + assert_in_delta(123456789012345680.0, result, 1e-10) + end + + # Test that leading zeros don't count toward the 17-digit limit + def test_leading_zeros_dont_count + test_cases = [ + ["0.00012345678901234567", 0.00012345678901234567], # 17 significant digits + ["0.000000000000001234567890123456789", 1.234567890123457e-15], # >17 significant + ] + + test_cases.each do |input, expected| + result = JSON.parse(input) + assert_in_delta(expected, result, expected.abs * 1e-10, + "Failed to parse #{input} correctly") + end + end + + # Test that Ryu handles special values correctly + def test_special_double_values + test_cases = [ + ["1.7976931348623157e+308", Float::MAX], # Largest finite double + ["2.2250738585072014e-308", Float::MIN], # Smallest normalized double + ] + + test_cases.each do |input, expected| + result = JSON.parse(input) + assert_in_delta(expected, result, expected.abs * 1e-10, + "Failed to parse #{input} correctly") + end + + # Test zero separately + result_pos_zero = JSON.parse("0.0") + assert_equal(0.0, result_pos_zero) + + # Note: JSON.parse doesn't preserve -0.0 vs +0.0 distinction in standard mode + result_neg_zero = JSON.parse("-0.0") + assert_equal(0.0, result_neg_zero.abs) + end + + # Test subnormal numbers that caused precision issues before fallback was added + # These are extreme edge cases discovered by fuzzing (4 in 6 billion numbers tested) + def test_subnormal_edge_cases_round_trip + # These subnormal numbers (~1e-310) had 1 ULP rounding errors in original Ryu + # They now use rb_cstr_to_dbl fallback for exact precision + test_cases = [ + "-3.2652630314355e-310", + "3.9701623107025e-310", + "-3.6607772435415e-310", + "2.9714076801985e-310", + ] + + test_cases.each do |input| + # Parse the number + result = JSON.parse(input) + + # Should be bit-identical + assert_equal(result, JSON.parse(result.to_s), + "Subnormal #{input} failed round-trip test") + + # Should be bit-identical + assert_equal(result, JSON.parse(JSON.dump(result)), + "Subnormal #{input} failed round-trip test") + + # Verify the value is in the expected subnormal range + assert(result.abs < 2.225e-308, + "#{input} should be subnormal (< 2.225e-308)") + end + end + + # Test invalid numbers are properly rejected + def test_invalid_numbers_rejected + invalid_cases = [ + "-", + ".", + "-.", + "-.e10", + "1.2.3", + "1e", + "1e+", + ] + + invalid_cases.each do |input| + assert_raise(JSON::ParserError, "Should reject invalid number: #{input}") do + JSON.parse(input) + end + end + end +end diff --git a/test/json/json_string_matching_test.rb b/test/json/json_string_matching_test.rb new file mode 100644 index 0000000000..21cd649025 --- /dev/null +++ b/test/json/json_string_matching_test.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true +require_relative 'test_helper' +require 'time' + +class JSONStringMatchingTest < 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 + time_regexp = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/ + assert_equal [ t ], + parse( + t_json, + :create_additions => true, + :match_string => { time_regexp => TestTime } + ) + assert_equal [ t.strftime('%FT%T%z') ], + parse( + t_json, + :match_string => { time_regexp => TestTime } + ) + end +end diff --git a/test/json/ractor_test.rb b/test/json/ractor_test.rb new file mode 100644 index 0000000000..e53c405a74 --- /dev/null +++ b/test/json/ractor_test.rb @@ -0,0 +1,114 @@ +# frozen_string_literal: true + +require_relative 'test_helper' + +begin + require_relative './lib/helper' +rescue LoadError +end + +class JSONInRactorTest < Test::Unit::TestCase + unless Ractor.method_defined?(:value) + module RactorBackport + refine Ractor do + alias_method :value, :take + end + end + + using RactorBackport + end + + def test_generate + pid = fork do + Warning[:experimental] = false + r = Ractor.new do + json = JSON.generate({ + 'a' => 2, + 'b' => 3.141, + 'c' => 'c', + 'd' => [ 1, "b", 3.14 ], + 'e' => { 'foo' => 'bar' }, + 'g' => "\"\0\037", + 'h' => 1000.0, + 'i' => 0.001 + }) + JSON.parse(json) + end + expected_json = JSON.parse('{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' + + '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}') + actual_json = r.value + + if expected_json == actual_json + exit 0 + else + puts "Expected:" + puts expected_json + puts "Actual:" + puts actual_json + puts + exit 1 + end + end + _, status = Process.waitpid2(pid) + assert_predicate status, :success? + end + + def test_coder + coder = JSON::Coder.new.freeze + assert Ractor.shareable?(coder) + pid = fork do + Warning[:experimental] = false + r = Ractor.new(coder) do |coder| + json = coder.dump({ + 'a' => 2, + 'b' => 3.141, + 'c' => 'c', + 'd' => [ 1, "b", 3.14 ], + 'e' => { 'foo' => 'bar' }, + 'g' => "\"\0\037", + 'h' => 1000.0, + 'i' => 0.001 + }) + coder.load(json) + end + expected_json = JSON.parse('{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' + + '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}') + actual_json = r.value + + if expected_json == actual_json + exit 0 + else + puts "Expected:" + puts expected_json + puts "Actual:" + puts actual_json + puts + exit 1 + end + end + _, status = Process.waitpid2(pid) + assert_predicate status, :success? + end + + class NonNative + def initialize(value) + @value = value + end + end + + def test_coder_proc + block = Ractor.shareable_proc { |value| value.as_json } + coder = JSON::Coder.new(&block).freeze + assert Ractor.shareable?(coder) + + pid = fork do + Warning[:experimental] = false + assert_equal [{}], Ractor.new(coder) { |coder| + coder.load('[{}]') + }.value + end + + _, status = Process.waitpid2(pid) + assert_predicate status, :success? + end if Ractor.respond_to?(:shareable_proc) +end if defined?(Ractor) && Process.respond_to?(:fork) diff --git a/test/json/runner.rb b/test/json/runner.rb deleted file mode 100755 index 930fed7579..0000000000 --- a/test/json/runner.rb +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env ruby - -require 'test/unit/ui/console/testrunner' -require 'test/unit/testsuite' -$:.unshift File.expand_path(File.dirname($0)) -$:.unshift 'tests' -require 'test_json' -require 'test_json_generate' -require 'test_json_unicode' -require 'test_json_addition' -require 'test_json_fixtures' - -class TS_AllTests - def self.suite - suite = Test::Unit::TestSuite.new name - suite << TC_JSONGenerate.suite - suite << TC_JSON.suite - suite << TC_JSONUnicode.suite - suite << TC_JSONAddition.suite - suite << TC_JSONFixtures.suite - end -end -Test::Unit::UI::Console::TestRunner.run(TS_AllTests) - # vim: set et sw=2 ts=2: diff --git a/test/json/test_helper.rb b/test/json/test_helper.rb new file mode 100644 index 0000000000..24cde4348c --- /dev/null +++ b/test/json/test_helper.rb @@ -0,0 +1,53 @@ +$LOAD_PATH.unshift(File.expand_path('../../../ext', __FILE__), File.expand_path('../../../lib', __FILE__)) + +if ENV["JSON_COVERAGE"] + # This test helper is loaded inside Ruby's own test suite, so we try to not mess it up. + require 'coverage' + + branches_supported = Coverage.respond_to?(:supported?) && Coverage.supported?(:branches) + + # Coverage module must be started before SimpleCov to work around the cyclic require order. + # Track both branches and lines, or else SimpleCov misleadingly reports 0/0 = 100% for non-branching files. + Coverage.start(lines: true, + branches: branches_supported) + + require 'simplecov' + SimpleCov.start do + # Enabling both coverage types to let SimpleCov know to output them together in reports + enable_coverage :line + enable_coverage :branch if branches_supported + + # Can't always trust SimpleCov to find files implicitly + track_files 'lib/**/*.rb' + + add_filter 'lib/json/truffle_ruby' unless RUBY_ENGINE == 'truffleruby' + end +end + +require 'json' +require 'test/unit' + +if ENV["JSON_COMPACT"] + 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(expand_heap: true, toward: :empty) + rescue NotImplementedError, ArgumentError + # Some platforms don't support compaction + end + end + + if GC.respond_to?(:auto_compact=) + begin + GC.auto_compact = true + rescue NotImplementedError + # Some platforms don't support compaction + end + end +end + +unless defined?(Test::Unit::CoreAssertions) + require "core_assertions" + Test::Unit::TestCase.include Test::Unit::CoreAssertions +end diff --git a/test/json/test_json.rb b/test/json/test_json.rb deleted file mode 100755 index 2be7b4a9a3..0000000000 --- a/test/json/test_json.rb +++ /dev/null @@ -1,255 +0,0 @@ -#!/usr/bin/env ruby - -require 'test/unit' -require 'json' - -class TC_JSON < Test::Unit::TestCase - include JSON - - def setup - $KCODE = 'UTF8' - @ary = [1, "foo", 3.14, 4711.0, 2.718, nil, [1,-2,3], false, true].map do - |x| [x] - end - @ary_to_parse = ["1", '"foo"', "3.14", "4711.0", "2.718", "null", - "[1,-2,3]", "false", "true"].map do - |x| "[#{x}]" - end - @hash = { - 'a' => 2, - 'b' => 3.141, - 'c' => 'c', - 'd' => [ 1, "b", 3.14 ], - 'e' => { 'foo' => 'bar' }, - 'g' => "\"\0\037", - 'h' => 1000.0, - 'i' => 0.001 - } - @json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' + - '"g":"\\"\\u0000\\u001f","h":1.0E3,"i":1.0E-3}' - end - suite << TC_JSON.suite - - def test_construction - parser = JSON::Parser.new('test') - assert_equal 'test', parser.source - end - - def assert_equal_float(expected, is) - assert_in_delta(expected.first, is.first, 1e-2) - end - - def test_parse_simple_arrays - assert_equal([], parse('[]')) - assert_equal([], parse(' [ ] ')) - assert_equal([nil], parse('[null]')) - assert_equal([false], parse('[false]')) - assert_equal([true], parse('[true]')) - assert_equal([-23], parse('[-23]')) - assert_equal([23], parse('[23]')) - assert_equal([0.23], parse('[0.23]')) - assert_equal([0.0], parse('[0e0]')) - assert_raises(JSON::ParserError) { parse('[+23.2]') } - assert_raises(JSON::ParserError) { parse('[+23]') } - assert_raises(JSON::ParserError) { parse('[.23]') } - assert_raises(JSON::ParserError) { parse('[023]') } - assert_equal_float [3.141], parse('[3.141]') - assert_equal_float [-3.141], parse('[-3.141]') - assert_equal_float [3.141], parse('[3141e-3]') - assert_equal_float [3.141], parse('[3141.1e-3]') - assert_equal_float [3.141], parse('[3141E-3]') - assert_equal_float [3.141], parse('[3141.0E-3]') - assert_equal_float [-3.141], parse('[-3141.0e-3]') - assert_equal_float [-3.141], parse('[-3141e-3]') - assert_equal([""], parse('[""]')) - assert_equal(["foobar"], parse('["foobar"]')) - assert_equal([{}], parse('[{}]')) - end - - def test_parse_simple_objects - assert_equal({}, parse('{}')) - assert_equal({}, parse(' { } ')) - assert_equal({ "a" => nil }, parse('{ "a" : null}')) - assert_equal({ "a" => nil }, parse('{"a":null}')) - assert_equal({ "a" => false }, parse('{ "a" : false } ')) - assert_equal({ "a" => false }, parse('{"a":false}')) - assert_raises(JSON::ParserError) { parse('{false}') } - assert_equal({ "a" => true }, parse('{"a":true}')) - assert_equal({ "a" => true }, parse(' { "a" : true } ')) - assert_equal({ "a" => -23 }, parse(' { "a" : -23 } ')) - assert_equal({ "a" => -23 }, parse(' { "a" : -23 } ')) - assert_equal({ "a" => 23 }, parse('{"a":23 } ')) - assert_equal({ "a" => 23 }, parse(' { "a" : 23 } ')) - assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } ')) - assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } ')) - end - - begin - require 'permutation' - def test_parse_more_complex_arrays - a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }] - perms = Permutation.for a - perms.each do |perm| - orig_ary = perm.project - json = pretty_generate(orig_ary) - assert_equal orig_ary, parse(json) - end - end - - def test_parse_complex_objects - a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }] - perms = Permutation.for a - perms.each do |perm| - s = "a" - orig_obj = perm.project.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h } - json = pretty_generate(orig_obj) - assert_equal orig_obj, parse(json) - end - end - rescue LoadError - warn "Skipping permutation tests." - end - - def test_parse_arrays - assert_equal([1,2,3], parse('[1,2,3]')) - assert_equal([1.2,2,3], parse('[1.2,2,3]')) - assert_equal([[],[[],[]]], parse('[[],[[],[]]]')) - end - - def test_parse_values - assert_equal([""], parse('[""]')) - assert_equal(["\\"], parse('["\\\\"]')) - assert_equal(['"'], parse('["\""]')) - assert_equal(['\\"\\'], parse('["\\\\\\"\\\\"]')) - assert_equal(["\"\b\n\r\t\0\037"], - parse('["\"\b\n\r\t\u0000\u001f"]')) - for i in 0 ... @ary.size - assert_equal(@ary[i], parse(@ary_to_parse[i])) - end - end - - def test_parse_array - assert_equal([], parse('[]')) - assert_equal([], parse(' [ ] ')) - assert_equal([1], parse('[1]')) - assert_equal([1], parse(' [ 1 ] ')) - 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] - , [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] })) - end - - def test_parse_object - assert_equal({}, parse('{}')) - assert_equal({}, parse(' { } ')) - assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}')) - assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } ')) - end - - def test_parser_reset - parser = Parser.new(@json) - assert_equal(@hash, parser.parse) - assert_equal(@hash, parser.parse) - end - - def test_comments - json = <<EOT -{ - "key1":"value1", // eol comment - "key2":"value2" /* multi line - * comment */, - "key3":"value3" /* multi line - // nested eol comment - * comment */ -} -EOT - assert_equal( - { "key1" => "value1", "key2" => "value2", "key3" => "value3" }, - parse(json)) - json = <<EOT -{ - "key1":"value1" /* multi line - // nested eol comment - /* illegal nested multi line comment */ - * comment */ -} -EOT - assert_raises(ParserError) { parse(json) } - json = <<EOT -{ - "key1":"value1" /* multi line - // nested eol comment - closed multi comment */ - and again, throw an Error */ -} -EOT - assert_raises(ParserError) { parse(json) } - json = <<EOT -{ - "key1":"value1" /*/*/ -} -EOT - assert_equal({ "key1" => "value1" }, parse(json)) - end - - def test_backslash - data = [ '\\.(?i:gif|jpe?g|png)$' ] - json = '["\\\\.(?i:gif|jpe?g|png)$"]' - assert_equal json, JSON.unparse(data) - assert_equal data, JSON.parse(json) - # - data = [ '\\"' ] - json = '["\\\\\""]' - assert_equal json, JSON.unparse(data) - assert_equal data, JSON.parse(json) - # - json = '["\/"]' - data = JSON.parse(json) - assert_equal ['/'], data - assert_equal json, JSON.unparse(data) - # - json = '["\""]' - data = JSON.parse(json) - assert_equal ['"'], data - assert_equal json, JSON.unparse(data) - json = '["\\\'"]' - data = JSON.parse(json) - assert_equal ["'"], data - assert_equal '["\'"]', JSON.unparse(data) - end - - def test_wrong_inputs - assert_raises(ParserError) { JSON.parse('"foo"') } - assert_raises(ParserError) { JSON.parse('123') } - assert_raises(ParserError) { JSON.parse('[] bla') } - assert_raises(ParserError) { JSON.parse('[] 1') } - assert_raises(ParserError) { JSON.parse('[] []') } - assert_raises(ParserError) { JSON.parse('[] {}') } - assert_raises(ParserError) { JSON.parse('{} []') } - assert_raises(ParserError) { JSON.parse('{} {}') } - assert_raises(ParserError) { JSON.parse('[NULL]') } - assert_raises(ParserError) { JSON.parse('[FALSE]') } - assert_raises(ParserError) { JSON.parse('[TRUE]') } - assert_raises(ParserError) { JSON.parse('[07] ') } - assert_raises(ParserError) { JSON.parse('[0a]') } - assert_raises(ParserError) { JSON.parse('[1.]') } - assert_raises(ParserError) { JSON.parse(' ') } - end - - def test_nesting - to_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]' - assert_raises(JSON::NestingError) { JSON.parse to_deep } - assert_raises(JSON::NestingError) { JSON.parser.new(to_deep).parse } - assert_raises(JSON::NestingError) { JSON.parse to_deep, :max_nesting => 19 } - ok = JSON.parse to_deep, :max_nesting => 20 - assert_kind_of Array, ok - ok = JSON.parse to_deep, :max_nesting => nil - assert_kind_of Array, ok - ok = JSON.parse to_deep, :max_nesting => false - assert_kind_of Array, ok - ok = JSON.parse to_deep, :max_nesting => 0 - assert_kind_of Array, ok - end -end - # vim: set et sw=2 ts=2: diff --git a/test/json/test_json_addition.rb b/test/json/test_json_addition.rb deleted file mode 100755 index e527f70a11..0000000000 --- a/test/json/test_json_addition.rb +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/env ruby - -require 'test/unit' -require 'json' - -class TC_JSONAddition < Test::Unit::TestCase - include JSON - - class A - def initialize(a) - @a = a - end - - attr_reader :a - - def ==(other) - a == other.a - end - - def self.json_create(object) - new(*object['args']) - end - - def to_json(*args) - { - 'json_class' => self.class, - 'args' => [ @a ], - }.to_json(*args) - end - end - - class B - def to_json(*args) - { - 'json_class' => self.class, - }.to_json(*args) - end - end - - class C - def to_json(*args) - { - 'json_class' => 'TC_JSONAddition::Nix', - }.to_json(*args) - end - end - - def setup - $KCODE = 'UTF8' - end - - def test_extended_json - a = A.new(666) - assert A.json_creatable? - json = generate(a) - a_again = JSON.parse(json) - assert_kind_of a.class, a_again - assert_equal a, a_again - end - - def test_extended_json_fail - b = B.new - assert !B.json_creatable? - json = generate(b) - assert_equal({ 'json_class' => B.name }, JSON.parse(json)) - end - - def test_extended_json_fail - c = C.new - assert !C.json_creatable? - json = generate(c) - assert_raises(ArgumentError) { JSON.parse(json) } - end - - def test_raw_strings - raw = '' - raw_array = [] - for i in 0..255 - raw << i - raw_array << i - end - json = raw.to_json_raw - 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 - raw_again = JSON.parse(json) - assert_equal raw, raw_again - end -end diff --git a/test/json/test_json_fixtures.rb b/test/json/test_json_fixtures.rb deleted file mode 100755 index 665dcbd5ea..0000000000 --- a/test/json/test_json_fixtures.rb +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env ruby - -require 'test/unit' -require 'json' - -class TC_JSONFixtures < Test::Unit::TestCase - def setup - $KCODE = 'UTF8' - fixtures = File.join(File.dirname(__FILE__), 'fixtures/*.json') - passed, failed = Dir[fixtures].partition { |f| f['pass'] } - @passed = passed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort - @failed = failed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort - end - - def test_passing - for name, source in @passed - assert JSON.parse(source), - "Did not pass for fixture '#{name}'" - end - end - - def test_failing - for name, source in @failed - assert_raises(JSON::ParserError, JSON::NestingError, - "Did not fail for fixture '#{name}'") do - JSON.parse(source) - end - end - end -end diff --git a/test/json/test_json_generate.rb b/test/json/test_json_generate.rb deleted file mode 100644 index 82d8c3d286..0000000000 --- a/test/json/test_json_generate.rb +++ /dev/null @@ -1,81 +0,0 @@ -require 'test/unit' -require 'json' - -class TC_JSONGenerate < Test::Unit::TestCase - include JSON - - def setup - $KCODE = 'UTF8' - @hash = { - 'a' => 2, - 'b' => 3.141, - 'c' => 'c', - 'd' => [ 1, "b", 3.14 ], - 'e' => { 'foo' => 'bar' }, - 'g' => "\"\0\037", - 'h' => 1000.0, - 'i' => 0.001 - } - @json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' + - '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}' - @json3 = <<'EOT'.chomp -{ - "a": 2, - "b": 3.141, - "c": "c", - "d": [ - 1, - "b", - 3.14 - ], - "e": { - "foo": "bar" - }, - "g": "\"\u0000\u001f", - "h": 1000.0, - "i": 0.001 -} -EOT - end - - def test_unparse - json = unparse(@hash) - assert_equal(JSON.parse(@json2), JSON.parse(json)) - parsed_json = parse(json) - assert_equal(@hash, parsed_json) - json = generate({1=>2}) - assert_equal('{"1":2}', json) - parsed_json = parse(json) - assert_equal({"1"=>2}, parsed_json) - end - - def test_unparse_pretty - json = pretty_unparse(@hash) - assert_equal(JSON.parse(@json3), JSON.parse(json)) - parsed_json = parse(json) - assert_equal(@hash, parsed_json) - json = pretty_generate({1=>2}) - assert_equal(<<'EOT'.chomp, json) -{ - "1": 2 -} -EOT - parsed_json = parse(json) - assert_equal({"1"=>2}, parsed_json) - 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 - h = { 1=>2 } - h[3] = h - assert_raises(JSON::CircularDatastructure) { generate(h, s) } - s = JSON.state.new(:check_circular => true) - #assert s.check_circular - a = [ 1, 2 ] - a << a - assert_raises(JSON::CircularDatastructure) { generate(a, s) } - end -end diff --git a/test/json/test_json_unicode.rb b/test/json/test_json_unicode.rb deleted file mode 100755 index a91f4b576c..0000000000 --- a/test/json/test_json_unicode.rb +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env ruby - -require 'test/unit' -require 'json' - -class TC_JSONUnicode < Test::Unit::TestCase - include JSON - - def setup - $KCODE = 'UTF8' - end - - def test_unicode - assert_equal '""', ''.to_json - assert_equal '"\\b"', "\b".to_json - assert_equal '"\u0001"', 0x1.chr.to_json - assert_equal '"\u001f"', 0x1f.chr.to_json - assert_equal '" "', ' '.to_json - assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json - utf8 = [ "© ≠ €! \01" ] - json = '["\u00a9 \u2260 \u20ac! \u0001"]' - assert_equal json, utf8.to_json - assert_equal utf8, parse(json) - utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"] - json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]" - assert_equal json, utf8.to_json - assert_equal utf8, parse(json) - utf8 = ['საქართველო'] - json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]" - assert_equal json, utf8.to_json - assert_equal utf8, parse(json) - assert_equal '["\\u00c3"]', JSON.generate(["Ã"]) - assert_equal ["€"], JSON.parse('["\u20ac"]') - utf8 = ["\xf0\xa0\x80\x81"] - json = '["\ud840\udc01"]' - assert_equal json, JSON.generate(utf8) - assert_equal utf8, JSON.parse(json) - end - - def test_chars - (0..0x7f).each do |i| - json = '["\u%04x"]' % i - if RUBY_VERSION >= "1.9." - i = i.chr - end - assert_equal i, JSON.parse(json).first[0] - if i == ?\b - generated = JSON.generate(["" << i]) - assert '["\b"]' == generated || '["\10"]' == generated - elsif [?\n, ?\r, ?\t, ?\f].include?(i) - assert_equal '[' << ('' << i).dump << ']', JSON.generate(["" << i]) - elsif i.chr < 0x20.chr - assert_equal json, JSON.generate(["" << i]) - end - end - assert_raises(JSON::GeneratorError) do - JSON.generate(["" << 0x80]) - end - assert_equal "\302\200", JSON.parse('["\u0080"]').first - end -end |
