Skip to content

Commit acf6e04

Browse files
authored
Merge pull request #852 from etiennebarrie/coverage
Improve test coverage
2 parents 8f90de7 + bae760a commit acf6e04

File tree

7 files changed

+26
-43
lines changed

7 files changed

+26
-43
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ group :development do
1010
gem "test-unit"
1111
gem "test-unit-ruby-core"
1212
gem "all_images", "~> 0" unless RUBY_PLATFORM =~ /java/
13+
gem "simplecov", require: false
1314
end
1415

1516
group :release do

lib/json/common.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def create_additions_proc(opts)
7373
if opts[:create_additions] != false
7474
if class_name = object[JSON.create_id]
7575
klass = JSON.deep_const_get(class_name)
76-
if (klass.respond_to?(:json_creatable?) && klass.json_creatable?) || klass.respond_to?(:json_create)
76+
if klass.respond_to?(:json_creatable?) ? klass.json_creatable? : klass.respond_to?(:json_create)
7777
create_additions_warning if create_additions.nil?
7878
object = klass.json_create(object)
7979
end
@@ -97,7 +97,7 @@ def create_additions_warning
9797

9898
class << self
9999
def deprecation_warning(message, uplevel = 3) # :nodoc:
100-
gem_root = File.expand_path("../../../", __FILE__) + "/"
100+
gem_root = File.expand_path("..", __dir__) + "/"
101101
caller_locations(uplevel, 10).each do |frame|
102102
if frame.path.nil? || frame.path.start_with?(gem_root) || frame.path.end_with?("/truffle/cext_ruby.rb", ".c")
103103
uplevel += 1

lib/json/generic_object.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,6 @@ def to_hash
5252
table
5353
end
5454

55-
def [](name)
56-
__send__(name)
57-
end unless method_defined?(:[])
58-
59-
def []=(name, value)
60-
__send__("#{name}=", value)
61-
end unless method_defined?(:[]=)
62-
6355
def |(other)
6456
self.class[other.to_hash.merge(to_hash)]
6557
end

test/json/json_encoding_test.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,11 @@ def test_very_large_json_strings
145145
end
146146

147147
def test_invalid_utf8_sequences
148-
# Create strings with invalid UTF-8 sequences
149148
invalid_utf8 = "\xFF\xFF"
150-
151-
# Test that generating JSON with invalid UTF-8 raises an error
152-
# Different JSON implementations may handle this differently,
153-
# so we'll check if any exception is raised
154-
begin
149+
error = assert_raise(JSON::GeneratorError) do
155150
generate(invalid_utf8)
156-
raise "Expected an exception when generating JSON with invalid UTF8"
157-
rescue StandardError => e
158-
assert true
159-
assert_match(%r{source sequence is illegal/malformed utf-8}, e.message)
160151
end
152+
assert_match(%r{source sequence is illegal/malformed utf-8}, error.message)
161153
end
162154

163155
def test_surrogate_pair_handling

test/json/json_generic_object_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class JSONGenericObjectTest < Test::Unit::TestCase
55

66
def setup
7-
if defined?(GenericObject)
7+
if defined?(JSON::GenericObject)
88
@go = JSON::GenericObject[ :a => 1, :b => 2 ]
99
else
1010
omit("JSON::GenericObject is not available")
@@ -40,23 +40,23 @@ def test_parse_json
4040
)
4141
assert_equal 1, l.a
4242
assert_equal @go,
43-
l = JSON('{ "a": 1, "b": 2 }', :object_class => GenericObject)
43+
l = JSON('{ "a": 1, "b": 2 }', :object_class => JSON::GenericObject)
4444
assert_equal 1, l.a
45-
assert_equal GenericObject[:a => GenericObject[:b => 2]],
46-
l = JSON('{ "a": { "b": 2 } }', :object_class => GenericObject)
45+
assert_equal JSON::GenericObject[:a => JSON::GenericObject[:b => 2]],
46+
l = JSON('{ "a": { "b": 2 } }', :object_class => JSON::GenericObject)
4747
assert_equal 2, l.a.b
4848
end
4949
end
5050

5151
def test_from_hash
5252
result = JSON::GenericObject.from_hash(
5353
:foo => { :bar => { :baz => true }, :quux => [ { :foobar => true } ] })
54-
assert_kind_of GenericObject, result.foo
55-
assert_kind_of GenericObject, result.foo.bar
54+
assert_kind_of JSON::GenericObject, result.foo
55+
assert_kind_of JSON::GenericObject, result.foo.bar
5656
assert_equal true, result.foo.bar.baz
57-
assert_kind_of GenericObject, result.foo.quux.first
57+
assert_kind_of JSON::GenericObject, result.foo.quux.first
5858
assert_equal true, result.foo.quux.first.foobar
59-
assert_equal true, GenericObject.from_hash(true)
59+
assert_equal true, JSON::GenericObject.from_hash(true)
6060
end
6161

6262
def test_json_generic_object_load

test/json/json_parser_test.rb

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def test_parse_duplicate_key
347347
assert_equal expected_sym, parse('{"a": 1, "a": 2}', symbolize_names: true)
348348
end
349349

350-
if RUBY_ENGINE == 'RUBY_ENGINE'
350+
if RUBY_ENGINE == 'ruby'
351351
assert_deprecated_warning(/#{File.basename(__FILE__)}\:#{__LINE__ + 1}/) do
352352
assert_equal expected, parse('{"a": 1, "a": 2}')
353353
end
@@ -397,10 +397,8 @@ def test_freeze
397397
assert_predicate parse('[]', :freeze => true), :frozen?
398398
assert_predicate parse('"foo"', :freeze => true), :frozen?
399399

400-
if string_deduplication_available?
401-
assert_same(-'foo', parse('"foo"', :freeze => true))
402-
assert_same(-'foo', parse('{"foo": 1}', :freeze => true).keys.first)
403-
end
400+
assert_same(-'foo', parse('"foo"', :freeze => true))
401+
assert_same(-'foo', parse('{"foo": 1}', :freeze => true).keys.first)
404402
end
405403

406404
def test_parse_comments
@@ -637,6 +635,7 @@ def test_parse_array_custom_array_derived_class
637635
def test_parse_array_custom_non_array_derived_class
638636
res = parse('[1,2]', :array_class => SubArrayWrapper)
639637
assert_equal([1,2], res.data)
638+
assert_equal(1, res[0])
640639
assert_equal(SubArrayWrapper, res.class)
641640
assert res.shifted?
642641
end
@@ -698,6 +697,7 @@ def item_set?
698697
def test_parse_object_custom_non_hash_derived_class
699698
res = parse('{"foo":"bar"}', :object_class => SubOpenStruct)
700699
assert_equal "bar", res.foo
700+
assert_equal "bar", res[:foo]
701701
assert_equal(SubOpenStruct, res.class)
702702
assert res.item_set?
703703
end
@@ -794,16 +794,6 @@ def test_parse_leading_slash
794794

795795
private
796796

797-
def string_deduplication_available?
798-
r1 = rand.to_s
799-
r2 = r1.dup
800-
begin
801-
(-r1).equal?(-r2)
802-
rescue NoMethodError
803-
false # No String#-@
804-
end
805-
end
806-
807797
def assert_equal_float(expected, actual, delta = 1e-2)
808798
Array === expected and expected = expected.first
809799
Array === actual and actual = actual.first

test/json/test_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
$LOAD_PATH.unshift(File.expand_path('../../../ext', __FILE__), File.expand_path('../../../lib', __FILE__))
22

3+
begin
4+
require 'simplecov'
5+
rescue LoadError
6+
# Don't fail Ruby's test suite
7+
else
8+
SimpleCov.start
9+
end
10+
311
require 'json'
412
require 'test/unit'
513

0 commit comments

Comments
 (0)