Skip to content

Commit

Permalink
Use with_parse_json_times helper in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
zuhao committed Jun 22, 2014
1 parent 0e9a705 commit 17ad556
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions activesupport/test/json/decoding_test.rb
Expand Up @@ -73,22 +73,20 @@ def self.json_create(object)

TESTS.each_with_index do |(json, expected), index|
test "json decodes #{index}" do
prev = ActiveSupport.parse_json_times
ActiveSupport.parse_json_times = true
silence_warnings do
assert_equal expected, ActiveSupport::JSON.decode(json), "JSON decoding \
failed for #{json}"
with_parse_json_times(true) do
silence_warnings do
assert_equal expected, ActiveSupport::JSON.decode(json), "JSON decoding \
failed for #{json}"
end
end
ActiveSupport.parse_json_times = prev
end
end

test "json decodes time json with time parsing disabled" do
prev = ActiveSupport.parse_json_times
ActiveSupport.parse_json_times = false
expected = {"a" => "2007-01-01 01:12:34 Z"}
assert_equal expected, ActiveSupport::JSON.decode(%({"a": "2007-01-01 01:12:34 Z"}))
ActiveSupport.parse_json_times = prev
with_parse_json_times(false) do
expected = {"a" => "2007-01-01 01:12:34 Z"}
assert_equal expected, ActiveSupport::JSON.decode(%({"a": "2007-01-01 01:12:34 Z"}))
end
end

def test_failed_json_decoding
Expand All @@ -101,5 +99,15 @@ def test_failed_json_decoding
def test_cannot_pass_unsupported_options
assert_raise(ArgumentError) { ActiveSupport::JSON.decode("", create_additions: true) }
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

0 comments on commit 17ad556

Please sign in to comment.