summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2016-06-03 12:06:53 +0200
committerFlorian Frank <flori@ping.de>2016-06-03 12:06:53 +0200
commit79074a0de768113364749cb584000b7e97be04e5 (patch)
tree194c6b2bb12052ccee8469f59ac2c46497a47c1a
parent10d54d93ec145578063947951acd9f1777ea9245 (diff)
downloadjson-79074a0de768113364749cb584000b7e97be04e5.tar.gz
Test common interface
-rw-r--r--lib/json/common.rb18
-rw-r--r--tests/json_common_interface_test.rb24
2 files changed, 27 insertions, 15 deletions
diff --git a/lib/json/common.rb b/lib/json/common.rb
index f01e7a1..55908f8 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -24,7 +24,7 @@ module JSON
# Set the JSON parser class _parser_ to be used by JSON.
def parser=(parser) # :nodoc:
@parser = parser
- remove_const :Parser if JSON.const_defined_in?(self, :Parser)
+ remove_const :Parser if const_defined?(:Parser, true)
const_set :Parser, parser
end
@@ -35,8 +35,8 @@ module JSON
def deep_const_get(path) # :nodoc:
path.to_s.split(/::/).inject(Object) do |p, c|
case
- when c.empty? then p
- when JSON.const_defined_in?(p, c) then p.const_get(c)
+ when c.empty? then p
+ when p.const_defined?(c, true) then p.const_get(c)
else
begin
p.const_missing(c)
@@ -174,7 +174,7 @@ module JSON
opts = {
:max_nesting => false,
:allow_nan => true
- }.update(opts)
+ }.merge(opts)
Parser.new(source, opts).parse
end
@@ -405,16 +405,6 @@ module JSON
def self.iconv(to, from, string)
string.encode(to, from)
end
-
- if ::Object.method(:const_defined?).arity == 1
- def self.const_defined_in?(modul, constant)
- modul.const_defined?(constant)
- end
- else
- def self.const_defined_in?(modul, constant)
- modul.const_defined?(constant, false)
- end
- end
end
module ::Kernel
diff --git a/tests/json_common_interface_test.rb b/tests/json_common_interface_test.rb
index a2de037..38136fa 100644
--- a/tests/json_common_interface_test.rb
+++ b/tests/json_common_interface_test.rb
@@ -17,37 +17,57 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
'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}'
+ '"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
+ assert_match /::Parser\z/, JSON.parser.name
end
def test_generator
+ assert_match /::Generator\z/, JSON.generator.name
end
def test_state
+ assert_match /::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_deep_const_get
+ assert_raises(ArgumentError) { JSON.deep_const_get('Nix::Da') }
+ 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
+ assert_equal [ 1, NaN, 3, ], JSON.parse!('[ 1, NaN, 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 ])
end
def test_load
@@ -99,5 +119,7 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
end
def test_JSON
+ assert_equal @json, JSON(@hash)
+ assert_equal @hash, JSON(@json)
end
end