diff options
author | Godfrey Chan <godfreykfc@gmail.com> | 2015-07-11 12:33:19 -0700 |
---|---|---|
committer | Godfrey Chan <godfreykfc@gmail.com> | 2015-07-11 12:48:21 -0700 |
commit | a6b2ace016202fd38e121f5e7075c842e3e61aa3 (patch) | |
tree | 0c04a38eea9937661f1bfe57d1dff2cfe7299687 /activesupport/test/core_ext | |
parent | 696a19fb9b82d961217d8cdba85773337bf15b17 (diff) | |
download | rails-a6b2ace016202fd38e121f5e7075c842e3e61aa3.tar.gz rails-a6b2ace016202fd38e121f5e7075c842e3e61aa3.tar.bz2 rails-a6b2ace016202fd38e121f5e7075c842e3e61aa3.zip |
Expand coverage of JSON gem tests
Diffstat (limited to 'activesupport/test/core_ext')
-rw-r--r-- | activesupport/test/core_ext/object/json_gem_encoding_test.rb | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/activesupport/test/core_ext/object/json_gem_encoding_test.rb b/activesupport/test/core_ext/object/json_gem_encoding_test.rb index 4368e3e1d6..02ab17fb64 100644 --- a/activesupport/test/core_ext/object/json_gem_encoding_test.rb +++ b/activesupport/test/core_ext/object/json_gem_encoding_test.rb @@ -20,31 +20,45 @@ class JsonGemEncodingTest < ActiveSupport::TestCase JSONTest::EncodingTestCases.constants.each_with_index do |name| JSONTest::EncodingTestCases.const_get(name).each_with_index do |(subject, _), i| - test("test #{name[0..-6].underscore} #{i}") do - begin - expected = JSON.generate(subject, quirks_mode: true) - rescue JSON::GeneratorError => e - exception = e - end - - require_or_skip 'active_support/core_ext/object/json' - - if exception - assert_raises_with_message JSON::GeneratorError, e.message do - JSON.generate(subject, quirks_mode: true) - end - else - assert_equal expected, JSON.generate(subject, quirks_mode: true) - end + test("#{name[0..-6].underscore} #{i}") do + assert_same_with_or_without_active_support(subject) end end end + class CustomToJson + def to_json(*) + '"custom"' + end + end + + test "custom to_json" do + assert_same_with_or_without_active_support(CustomToJson.new) + end + private def require_or_skip(file) require(file) || skip("'#{file}' was already loaded") end + def assert_same_with_or_without_active_support(subject) + begin + expected = JSON.generate(subject, quirks_mode: true) + rescue JSON::GeneratorError => e + exception = e + end + + require_or_skip 'active_support/core_ext/object/json' + + if exception + assert_raises_with_message JSON::GeneratorError, e.message do + JSON.generate(subject, quirks_mode: true) + end + else + assert_equal expected, JSON.generate(subject, quirks_mode: true) + end + end + def assert_raises_with_message(exception_class, message, &block) err = assert_raises(exception_class) { block.call } assert_match message, err.message |