aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/object
diff options
context:
space:
mode:
authorGodfrey Chan <godfreykfc@gmail.com>2015-07-11 05:48:16 -0700
committerGodfrey Chan <godfreykfc@gmail.com>2015-07-11 12:48:09 -0700
commit2926d73e32519d5aae332a86ac3fb3ed320a140d (patch)
tree0192b8fce4448c790b80ff05f150d04ec26351c3 /activesupport/test/core_ext/object
parent14354f195540954a1dfc5c954d06389c9f71e986 (diff)
downloadrails-2926d73e32519d5aae332a86ac3fb3ed320a140d.tar.gz
rails-2926d73e32519d5aae332a86ac3fb3ed320a140d.tar.bz2
rails-2926d73e32519d5aae332a86ac3fb3ed320a140d.zip
Add tests to ensure we don't interfere with json gem's output
Diffstat (limited to 'activesupport/test/core_ext/object')
-rw-r--r--activesupport/test/core_ext/object/json_gem_encoding_test.rb52
1 files changed, 52 insertions, 0 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
new file mode 100644
index 0000000000..4368e3e1d6
--- /dev/null
+++ b/activesupport/test/core_ext/object/json_gem_encoding_test.rb
@@ -0,0 +1,52 @@
+require 'abstract_unit'
+require 'json'
+require 'json/encoding_test_cases'
+
+# These test cases were added to test that we do not interfere with json gem's
+# output when the AS encoder is loaded, primarily for problems reported in
+# #20775. They need to be executed in isolation to reproduce the scenario
+# correctly, because other test cases might have already loaded additional
+# dependencies.
+
+# The AS::JSON encoder requires the BigDecimal core_ext, which, unfortunately,
+# changes the BigDecimal#to_s output, and consequently the JSON gem output. So
+# we need to require this unfront to ensure we don't get a false failure, but
+# ideally we should just fix the BigDecimal core_ext to not change to_s without
+# arguments.
+require 'active_support/core_ext/big_decimal'
+
+class JsonGemEncodingTest < ActiveSupport::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ 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
+ end
+ end
+ end
+
+ private
+ def require_or_skip(file)
+ require(file) || skip("'#{file}' was already loaded")
+ end
+
+ def assert_raises_with_message(exception_class, message, &block)
+ err = assert_raises(exception_class) { block.call }
+ assert_match message, err.message
+ end
+end