Skip to content

Commit a6b2ace

Browse files
committed
Expand coverage of JSON gem tests
1 parent 696a19f commit a6b2ace

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

activesupport/test/core_ext/object/json_gem_encoding_test.rb

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,45 @@ class JsonGemEncodingTest < ActiveSupport::TestCase
2020

2121
JSONTest::EncodingTestCases.constants.each_with_index do |name|
2222
JSONTest::EncodingTestCases.const_get(name).each_with_index do |(subject, _), i|
23-
test("test #{name[0..-6].underscore} #{i}") do
24-
begin
25-
expected = JSON.generate(subject, quirks_mode: true)
26-
rescue JSON::GeneratorError => e
27-
exception = e
28-
end
29-
30-
require_or_skip 'active_support/core_ext/object/json'
31-
32-
if exception
33-
assert_raises_with_message JSON::GeneratorError, e.message do
34-
JSON.generate(subject, quirks_mode: true)
35-
end
36-
else
37-
assert_equal expected, JSON.generate(subject, quirks_mode: true)
38-
end
23+
test("#{name[0..-6].underscore} #{i}") do
24+
assert_same_with_or_without_active_support(subject)
3925
end
4026
end
4127
end
4228

29+
class CustomToJson
30+
def to_json(*)
31+
'"custom"'
32+
end
33+
end
34+
35+
test "custom to_json" do
36+
assert_same_with_or_without_active_support(CustomToJson.new)
37+
end
38+
4339
private
4440
def require_or_skip(file)
4541
require(file) || skip("'#{file}' was already loaded")
4642
end
4743

44+
def assert_same_with_or_without_active_support(subject)
45+
begin
46+
expected = JSON.generate(subject, quirks_mode: true)
47+
rescue JSON::GeneratorError => e
48+
exception = e
49+
end
50+
51+
require_or_skip 'active_support/core_ext/object/json'
52+
53+
if exception
54+
assert_raises_with_message JSON::GeneratorError, e.message do
55+
JSON.generate(subject, quirks_mode: true)
56+
end
57+
else
58+
assert_equal expected, JSON.generate(subject, quirks_mode: true)
59+
end
60+
end
61+
4862
def assert_raises_with_message(exception_class, message, &block)
4963
err = assert_raises(exception_class) { block.call }
5064
assert_match message, err.message

0 commit comments

Comments
 (0)