Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid hardcoded magic number in test teardown. #15857

Merged
merged 1 commit into from Jun 22, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 19 additions & 14 deletions activesupport/test/json/encoding_test.rb
Expand Up @@ -494,31 +494,28 @@ def test_twz_to_json_with_use_standard_json_time_format_config_set_to_true

def test_twz_to_json_with_custom_time_precision
with_standard_json_time_format(true) do
ActiveSupport::JSON::Encoding.time_precision = 0
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(time)
with_time_precision(0) do
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(time)
end
end
ensure
ActiveSupport::JSON::Encoding.time_precision = 3
end

def test_time_to_json_with_custom_time_precision
with_standard_json_time_format(true) do
ActiveSupport::JSON::Encoding.time_precision = 0
assert_equal "\"2000-01-01T00:00:00Z\"", ActiveSupport::JSON.encode(Time.utc(2000))
with_time_precision(0) do
assert_equal "\"2000-01-01T00:00:00Z\"", ActiveSupport::JSON.encode(Time.utc(2000))
end
end
ensure
ActiveSupport::JSON::Encoding.time_precision = 3
end

def test_datetime_to_json_with_custom_time_precision
with_standard_json_time_format(true) do
ActiveSupport::JSON::Encoding.time_precision = 0
assert_equal "\"2000-01-01T00:00:00+00:00\"", ActiveSupport::JSON.encode(DateTime.new(2000))
with_time_precision(0) do
assert_equal "\"2000-01-01T00:00:00+00:00\"", ActiveSupport::JSON.encode(DateTime.new(2000))
end
end
ensure
ActiveSupport::JSON::Encoding.time_precision = 3
end

def test_twz_to_json_when_wrapping_a_date_time
Expand All @@ -539,4 +536,12 @@ def with_standard_json_time_format(boolean = true)
ensure
ActiveSupport.use_standard_json_time_format = old
end

def with_time_precision(value)
old_value = ActiveSupport::JSON::Encoding.time_precision
ActiveSupport::JSON::Encoding.time_precision = value
yield
ensure
ActiveSupport::JSON::Encoding.time_precision = old_value
end
end