1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #15856 from zuhao/refactor_activesupport_decoding_test

Use with_parse_json_times helper in tests.
This commit is contained in:
Yves Senn 2014-06-22 13:47:25 +02:00
commit e32dade866

View file

@ -73,22 +73,20 @@ class TestJSONDecoding < ActiveSupport::TestCase
TESTS.each_with_index do |(json, expected), index| TESTS.each_with_index do |(json, expected), index|
test "json decodes #{index}" do test "json decodes #{index}" do
prev = ActiveSupport.parse_json_times with_parse_json_times(true) do
ActiveSupport.parse_json_times = true silence_warnings do
silence_warnings do assert_equal expected, ActiveSupport::JSON.decode(json), "JSON decoding \
assert_equal expected, ActiveSupport::JSON.decode(json), "JSON decoding \ failed for #{json}"
failed for #{json}" end
end end
ActiveSupport.parse_json_times = prev
end end
end end
test "json decodes time json with time parsing disabled" do test "json decodes time json with time parsing disabled" do
prev = ActiveSupport.parse_json_times with_parse_json_times(false) do
ActiveSupport.parse_json_times = false expected = {"a" => "2007-01-01 01:12:34 Z"}
expected = {"a" => "2007-01-01 01:12:34 Z"} assert_equal expected, ActiveSupport::JSON.decode(%({"a": "2007-01-01 01:12:34 Z"}))
assert_equal expected, ActiveSupport::JSON.decode(%({"a": "2007-01-01 01:12:34 Z"})) end
ActiveSupport.parse_json_times = prev
end end
def test_failed_json_decoding def test_failed_json_decoding
@ -101,5 +99,15 @@ class TestJSONDecoding < ActiveSupport::TestCase
def test_cannot_pass_unsupported_options def test_cannot_pass_unsupported_options
assert_raise(ArgumentError) { ActiveSupport::JSON.decode("", create_additions: true) } assert_raise(ArgumentError) { ActiveSupport::JSON.decode("", create_additions: true) }
end end
private
def with_parse_json_times(value)
old_value = ActiveSupport.parse_json_times
ActiveSupport.parse_json_times = value
yield
ensure
ActiveSupport.parse_json_times = old_value
end
end end