|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +class JsonCompatibilityTest < ActiveSupport::TestCase |
| 6 | + REFUSED_KEYWORD = ->(_text) { raise ArgumentError, "unknown keyword: :quirks_mode" } |
| 7 | + |
| 8 | + test "reports no problem when Active Support decodes JSON" do |
| 9 | + assert_nil SolidObjects::JsonCompatibility.new.problem |
| 10 | + end |
| 11 | + |
| 12 | + test "names the versions and the correction when Active Support cannot decode JSON" do |
| 13 | + problem = SolidObjects::JsonCompatibility.new(decode: REFUSED_KEYWORD).problem |
| 14 | + |
| 15 | + assert_includes problem, "unknown keyword: :quirks_mode" |
| 16 | + assert_includes problem, "json gem #{JSON::VERSION}" |
| 17 | + assert_includes problem, "Active Support #{ActiveSupport.version}" |
| 18 | + assert_includes problem, 'gem "json", "~> 2"' |
| 19 | + end |
| 20 | + |
| 21 | + test "does not hide a different decode failure" do |
| 22 | + broken = ->(_text) { raise NoMethodError, "decode is gone" } |
| 23 | + |
| 24 | + assert_raises(NoMethodError) { SolidObjects::JsonCompatibility.new(decode: broken).problem } |
| 25 | + end |
| 26 | + |
| 27 | + test "warns on the error stream when there is a problem" do |
| 28 | + _output, errors = capture_io { SolidObjects::JsonCompatibility.new(decode: REFUSED_KEYWORD).warn_about_problem } |
| 29 | + |
| 30 | + assert_includes errors, "[solid_objects]" |
| 31 | + assert_includes errors, 'gem "json", "~> 2"' |
| 32 | + end |
| 33 | + |
| 34 | + test "writes nothing when there is no problem" do |
| 35 | + output, errors = capture_io { SolidObjects::JsonCompatibility.new.warn_about_problem } |
| 36 | + |
| 37 | + assert_empty output |
| 38 | + assert_empty errors |
| 39 | + end |
| 40 | + |
| 41 | + test "the engine checks JSON compatibility during the boot" do |
| 42 | + names = SolidObjects::Engine.instance.initializers.map(&:name) |
| 43 | + |
| 44 | + assert_includes names, "solid_objects.json_compatibility" |
| 45 | + end |
| 46 | +end |
0 commit comments