diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2014-01-26 20:32:34 +0000 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2014-01-26 21:25:54 +0000 |
commit | ef17225173131541f0f5495ea31fc51ea46cb236 (patch) | |
tree | 39ab5ced8cd009358aeb4ae9611e47b6d8f76e13 /activesupport | |
parent | 4cfc467594da86090efa63f1852fb82df9458c2b (diff) | |
download | rails-ef17225173131541f0f5495ea31fc51ea46cb236.tar.gz rails-ef17225173131541f0f5495ea31fc51ea46cb236.tar.bz2 rails-ef17225173131541f0f5495ea31fc51ea46cb236.zip |
Consolidate JSON encoding tests in one file
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/test/core_ext/time_with_zone_test.rb | 35 | ||||
-rw-r--r-- | activesupport/test/json/encoding_test.rb | 59 |
2 files changed, 48 insertions, 46 deletions
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 4ea8d2bed6..e9bf43667a 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -1,6 +1,5 @@ require 'abstract_unit' require 'active_support/time' -require 'active_support/json' class TimeWithZoneTest < ActiveSupport::TestCase @@ -66,33 +65,6 @@ class TimeWithZoneTest < ActiveSupport::TestCase 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 @@ class TimeWithZoneTest < ActiveSupport::TestCase 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 diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index 78cf4819f9..ef76d63f29 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -3,6 +3,7 @@ require 'securerandom' 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 @@ class TestJSONEncoding < ActiveSupport::TestCase 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 @@ EXPECTED 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 @@ EXPECTED 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 |