提交 ef172251 编写于 作者: A Andrew White

Consolidate JSON encoding tests in one file

上级 4cfc4675
require 'abstract_unit'
require 'active_support/time'
require 'active_support/json'
class TimeWithZoneTest < ActiveSupport::TestCase
......@@ -66,33 +65,6 @@ def test_zone
assert_equal 'EDT', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).zone #dst
end
def test_to_json_with_use_standard_json_time_format_config_set_to_false
with_standard_json_time_format(false) do
assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(@twz)
end
end
def test_to_json_with_use_standard_json_time_format_config_set_to_true
with_standard_json_time_format(true) do
assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(@twz)
end
end
def test_to_json_with_custom_subsecond_resolution
with_standard_json_time_format(true) do
ActiveSupport::JSON::Encoding.subsecond_fraction_digits = 0
assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(@twz)
end
ensure
ActiveSupport::JSON::Encoding.subsecond_fraction_digits = nil
end
def test_to_json_when_wrapping_a_date_time
twz = ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone)
assert_equal '"1999-12-31T19:00:00.000-05:00"', ActiveSupport::JSON.encode(twz)
end
def test_nsec
local = Time.local(2011,6,7,23,59,59,Rational(999999999, 1000))
with_zone = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Hawaii"], local)
......@@ -822,13 +794,6 @@ def with_env_tz(new_tz = 'US/Eastern')
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
def with_standard_json_time_format(boolean = true)
old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, boolean
yield
ensure
ActiveSupport.use_standard_json_time_format = old
end
end
class TimeWithZoneMethodsForTimeAndDateTimeTest < ActiveSupport::TestCase
......
......@@ -3,6 +3,7 @@
require 'abstract_unit'
require 'active_support/core_ext/string/inflections'
require 'active_support/json'
require 'active_support/time'
class TestJSONEncoding < ActiveSupport::TestCase
class Foo
......@@ -226,21 +227,17 @@ def test_hash_should_allow_key_filtering_with_except
end
def test_time_to_json_includes_local_offset
prev = ActiveSupport.use_standard_json_time_format
ActiveSupport.use_standard_json_time_format = true
with_env_tz 'US/Eastern' do
assert_equal %("2005-02-01T15:15:10.000-05:00"), ActiveSupport::JSON.encode(Time.local(2005,2,1,15,15,10))
with_standard_json_time_format(true) do
with_env_tz 'US/Eastern' do
assert_equal %("2005-02-01T15:15:10.000-05:00"), ActiveSupport::JSON.encode(Time.local(2005,2,1,15,15,10))
end
end
ensure
ActiveSupport.use_standard_json_time_format = prev
end
def test_hash_with_time_to_json
prev = ActiveSupport.use_standard_json_time_format
ActiveSupport.use_standard_json_time_format = false
assert_equal '{"time":"2009/01/01 00:00:00 +0000"}', { :time => Time.utc(2009) }.to_json
ensure
ActiveSupport.use_standard_json_time_format = prev
with_standard_json_time_format(false) do
assert_equal '{"time":"2009/01/01 00:00:00 +0000"}', { :time => Time.utc(2009) }.to_json
end
end
def test_nested_hash_with_float
......@@ -453,6 +450,39 @@ def test_json_gem_pretty_generate_by_passing_active_support_encoder
assert_nil h.as_json_called
end
def test_twz_to_json_with_use_standard_json_time_format_config_set_to_false
with_standard_json_time_format(false) do
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(time)
end
end
def test_twz_to_json_with_use_standard_json_time_format_config_set_to_true
with_standard_json_time_format(true) do
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(time)
end
end
def test_twz_to_json_with_custom_subsecond_resolution
with_standard_json_time_format(true) do
ActiveSupport::JSON::Encoding.subsecond_fraction_digits = 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)
end
ensure
ActiveSupport::JSON::Encoding.subsecond_fraction_digits = nil
end
def test_twz_to_json_when_wrapping_a_date_time
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
time = ActiveSupport::TimeWithZone.new(DateTime.new(2000), zone)
assert_equal '"1999-12-31T19:00:00.000-05:00"', ActiveSupport::JSON.encode(time)
end
protected
def object_keys(json_object)
......@@ -465,4 +495,11 @@ def with_env_tz(new_tz = 'US/Eastern')
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
def with_standard_json_time_format(boolean = true)
old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, boolean
yield
ensure
ActiveSupport.use_standard_json_time_format = old
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册