vendor/json_pure/tests/test_json.rb in scout-5.1.2 vs vendor/json_pure/tests/test_json.rb in scout-5.1.3

- old
+ new

@@ -7,10 +7,24 @@ when 'ext' then require 'json/ext' else require 'json' end require 'stringio' +unless Array.method_defined?(:permutation) + begin + require 'enumerator' + require 'permutation' + class Array + def permutation + Permutation.for(self).to_enum.map { |x| x.project } + end + end + rescue LoadError + warn "Skipping permutation tests." + end +end + class TC_JSON < Test::Unit::TestCase include JSON def setup @ary = [1, "foo", 3.14, 4711.0, 2.718, nil, [1,-2,3], false, true].map do @@ -92,34 +106,28 @@ 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' + if Array.method_defined?(: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) + 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" => {} }] - perms = Permutation.for a - perms.each do |perm| + a.permutation.each do |perm| s = "a" - orig_obj = perm.project.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h } + 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 - 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]')) @@ -220,31 +228,31 @@ end def test_backslash data = [ '\\.(?i:gif|jpe?g|png)$' ] json = '["\\\\.(?i:gif|jpe?g|png)$"]' - assert_equal json, JSON.unparse(data) + assert_equal json, JSON.generate(data) assert_equal data, JSON.parse(json) # data = [ '\\"' ] json = '["\\\\\""]' - assert_equal json, JSON.unparse(data) + assert_equal json, JSON.generate(data) assert_equal data, JSON.parse(json) # - json = '["\/"]' + json = '["/"]' data = JSON.parse(json) assert_equal ['/'], data - assert_equal json, JSON.unparse(data) + assert_equal json, JSON.generate(data) # json = '["\""]' data = JSON.parse(json) assert_equal ['"'], data - assert_equal json, JSON.unparse(data) + assert_equal json, JSON.generate(data) json = '["\\\'"]' data = JSON.parse(json) assert_equal ["'"], data - assert_equal '["\'"]', JSON.unparse(data) + assert_equal '["\'"]', JSON.generate(data) end def test_wrong_inputs assert_raises(ParserError) { JSON.parse('"foo"') } assert_raises(ParserError) { JSON.parse('123') } @@ -292,10 +300,17 @@ assert_equal too_deep, ok ok = JSON.generate too_deep_ary, :max_nesting => 0 assert_equal too_deep, ok end + def test_symbolize_names + assert_equal({ "foo" => "bar", "baz" => "quux" }, + JSON.parse('{"foo":"bar", "baz":"quux"}')) + assert_equal({ :foo => "bar", :baz => "quux" }, + JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true)) + end + def test_load_dump too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]' assert_equal too_deep, JSON.dump(eval(too_deep)) assert_kind_of String, Marshal.dump(eval(too_deep)) assert_raises(ArgumentError) { JSON.dump(eval(too_deep), 19) } @@ -306,7 +321,20 @@ JSON.dump(eval(too_deep), output) assert_equal too_deep, output.string output = StringIO.new JSON.dump(eval(too_deep), output, 20) assert_equal too_deep, output.string + end + + def test_big_integers + json1 = JSON([orig = (1 << 31) - 1]) + assert_equal orig, JSON[json1][0] + json2 = JSON([orig = 1 << 31]) + assert_equal orig, JSON[json2][0] + json3 = JSON([orig = (1 << 62) - 1]) + assert_equal orig, JSON[json3][0] + json4 = JSON([orig = 1 << 62]) + assert_equal orig, JSON[json4][0] + json5 = JSON([orig = 1 << 64]) + assert_equal orig, JSON[json5][0] end end