2016-07-13 09:27:07 -04:00
|
|
|
#frozen_string_literal: false
|
2016-07-05 07:49:39 -04:00
|
|
|
require 'test_helper'
|
|
|
|
require 'stringio'
|
|
|
|
require 'tempfile'
|
|
|
|
|
2016-07-13 09:27:07 -04:00
|
|
|
class JSONCommonInterfaceTest < Test::Unit::TestCase
|
2016-07-05 07:49:39 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
@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 @hash, JSON[@json]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parser
|
2019-06-01 07:44:24 -04:00
|
|
|
assert_match(/::Parser\z/, JSON.parser.name)
|
2016-07-05 07:49:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_generator
|
2019-06-01 07:44:24 -04:00
|
|
|
assert_match(/::Generator\z/, JSON.generator.name)
|
2016-07-05 07:49:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_state
|
2019-06-01 07:44:24 -04:00
|
|
|
assert_match(/::Generator::State\z/, JSON.state.name)
|
2016-07-05 07:49:39 -04:00
|
|
|
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_deep_const_get
|
2016-07-13 09:27:08 -04:00
|
|
|
assert_raise(ArgumentError) { JSON.deep_const_get('Nix::Da') }
|
2016-07-05 07:49:39 -04:00
|
|
|
assert_equal File::SEPARATOR, JSON.deep_const_get('File::SEPARATOR')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse
|
|
|
|
assert_equal [ 1, 2, 3, ], JSON.parse('[ 1, 2, 3 ]')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_bang
|
2017-12-21 03:57:16 -05:00
|
|
|
assert_equal [ 1, Infinity, 3, ], JSON.parse!('[ 1, Infinity, 3 ]')
|
2016-07-05 07:49:39 -04:00
|
|
|
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 ])
|
|
|
|
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_options
|
|
|
|
json = '{ "foo": NaN }'
|
|
|
|
assert JSON.load(json, nil, :allow_nan => true)['foo'].nan?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_null
|
|
|
|
assert_equal nil, JSON.load(nil, nil, :allow_blank => true)
|
2016-07-13 09:27:08 -04:00
|
|
|
assert_raise(TypeError) { JSON.load(nil, nil, :allow_blank => false) }
|
|
|
|
assert_raise(JSON::ParserError) { JSON.load('', nil, :allow_blank => false) }
|
2016-07-05 07:49:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dump
|
|
|
|
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
|
|
|
|
assert_equal too_deep, dump(eval(too_deep))
|
|
|
|
assert_kind_of String, Marshal.dump(eval(too_deep))
|
|
|
|
assert_raise(ArgumentError) { dump(eval(too_deep), 100) }
|
|
|
|
assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 100) }
|
|
|
|
assert_equal too_deep, dump(eval(too_deep), 101)
|
|
|
|
assert_kind_of String, Marshal.dump(eval(too_deep), 101)
|
|
|
|
output = StringIO.new
|
|
|
|
dump(eval(too_deep), output)
|
|
|
|
assert_equal too_deep, output.string
|
|
|
|
output = StringIO.new
|
|
|
|
dump(eval(too_deep), output, 101)
|
|
|
|
assert_equal too_deep, output.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 @hash, JSON(@json)
|
|
|
|
end
|
2020-06-30 14:07:23 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
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)
|
2020-09-25 07:28:31 -04:00
|
|
|
assert_include(key_classes, Symbol)
|
|
|
|
assert_not_include(key_classes, String)
|
2020-06-30 14:07:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def temp_file_containing(text, file_prefix = '')
|
|
|
|
raise "This method must be called with a code block." unless block_given?
|
|
|
|
|
2020-09-30 02:55:07 -04:00
|
|
|
Tempfile.create(file_prefix) do |file|
|
|
|
|
file << text
|
|
|
|
file.close
|
|
|
|
yield file.path
|
2020-06-30 14:07:23 -04:00
|
|
|
end
|
2020-09-25 10:37:12 -04:00
|
|
|
end
|
2016-07-05 07:49:39 -04:00
|
|
|
end
|