Skip to content

Commit ef17225

Browse files
committed
Consolidate JSON encoding tests in one file
1 parent 4cfc467 commit ef17225

File tree

2 files changed

+48
-46
lines changed

2 files changed

+48
-46
lines changed

activesupport/test/core_ext/time_with_zone_test.rb

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'abstract_unit'
22
require 'active_support/time'
3-
require 'active_support/json'
43

54
class TimeWithZoneTest < ActiveSupport::TestCase
65

@@ -66,33 +65,6 @@ def test_zone
6665
assert_equal 'EDT', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).zone #dst
6766
end
6867

69-
def test_to_json_with_use_standard_json_time_format_config_set_to_false
70-
with_standard_json_time_format(false) do
71-
assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(@twz)
72-
end
73-
end
74-
75-
def test_to_json_with_use_standard_json_time_format_config_set_to_true
76-
with_standard_json_time_format(true) do
77-
assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(@twz)
78-
end
79-
end
80-
81-
def test_to_json_with_custom_subsecond_resolution
82-
with_standard_json_time_format(true) do
83-
ActiveSupport::JSON::Encoding.subsecond_fraction_digits = 0
84-
85-
assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(@twz)
86-
end
87-
ensure
88-
ActiveSupport::JSON::Encoding.subsecond_fraction_digits = nil
89-
end
90-
91-
def test_to_json_when_wrapping_a_date_time
92-
twz = ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone)
93-
assert_equal '"1999-12-31T19:00:00.000-05:00"', ActiveSupport::JSON.encode(twz)
94-
end
95-
9668
def test_nsec
9769
local = Time.local(2011,6,7,23,59,59,Rational(999999999, 1000))
9870
with_zone = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Hawaii"], local)
@@ -822,13 +794,6 @@ def with_env_tz(new_tz = 'US/Eastern')
822794
ensure
823795
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
824796
end
825-
826-
def with_standard_json_time_format(boolean = true)
827-
old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, boolean
828-
yield
829-
ensure
830-
ActiveSupport.use_standard_json_time_format = old
831-
end
832797
end
833798

834799
class TimeWithZoneMethodsForTimeAndDateTimeTest < ActiveSupport::TestCase

activesupport/test/json/encoding_test.rb

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'abstract_unit'
44
require 'active_support/core_ext/string/inflections'
55
require 'active_support/json'
6+
require 'active_support/time'
67

78
class TestJSONEncoding < ActiveSupport::TestCase
89
class Foo
@@ -226,21 +227,17 @@ def test_hash_should_allow_key_filtering_with_except
226227
end
227228

228229
def test_time_to_json_includes_local_offset
229-
prev = ActiveSupport.use_standard_json_time_format
230-
ActiveSupport.use_standard_json_time_format = true
231-
with_env_tz 'US/Eastern' do
232-
assert_equal %("2005-02-01T15:15:10.000-05:00"), ActiveSupport::JSON.encode(Time.local(2005,2,1,15,15,10))
230+
with_standard_json_time_format(true) do
231+
with_env_tz 'US/Eastern' do
232+
assert_equal %("2005-02-01T15:15:10.000-05:00"), ActiveSupport::JSON.encode(Time.local(2005,2,1,15,15,10))
233+
end
233234
end
234-
ensure
235-
ActiveSupport.use_standard_json_time_format = prev
236235
end
237236

238237
def test_hash_with_time_to_json
239-
prev = ActiveSupport.use_standard_json_time_format
240-
ActiveSupport.use_standard_json_time_format = false
241-
assert_equal '{"time":"2009/01/01 00:00:00 +0000"}', { :time => Time.utc(2009) }.to_json
242-
ensure
243-
ActiveSupport.use_standard_json_time_format = prev
238+
with_standard_json_time_format(false) do
239+
assert_equal '{"time":"2009/01/01 00:00:00 +0000"}', { :time => Time.utc(2009) }.to_json
240+
end
244241
end
245242

246243
def test_nested_hash_with_float
@@ -453,6 +450,39 @@ def test_json_gem_pretty_generate_by_passing_active_support_encoder
453450
assert_nil h.as_json_called
454451
end
455452

453+
def test_twz_to_json_with_use_standard_json_time_format_config_set_to_false
454+
with_standard_json_time_format(false) do
455+
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
456+
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
457+
assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(time)
458+
end
459+
end
460+
461+
def test_twz_to_json_with_use_standard_json_time_format_config_set_to_true
462+
with_standard_json_time_format(true) do
463+
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
464+
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
465+
assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(time)
466+
end
467+
end
468+
469+
def test_twz_to_json_with_custom_subsecond_resolution
470+
with_standard_json_time_format(true) do
471+
ActiveSupport::JSON::Encoding.subsecond_fraction_digits = 0
472+
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
473+
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
474+
assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(time)
475+
end
476+
ensure
477+
ActiveSupport::JSON::Encoding.subsecond_fraction_digits = nil
478+
end
479+
480+
def test_twz_to_json_when_wrapping_a_date_time
481+
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
482+
time = ActiveSupport::TimeWithZone.new(DateTime.new(2000), zone)
483+
assert_equal '"1999-12-31T19:00:00.000-05:00"', ActiveSupport::JSON.encode(time)
484+
end
485+
456486
protected
457487

458488
def object_keys(json_object)
@@ -465,4 +495,11 @@ def with_env_tz(new_tz = 'US/Eastern')
465495
ensure
466496
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
467497
end
498+
499+
def with_standard_json_time_format(boolean = true)
500+
old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, boolean
501+
yield
502+
ensure
503+
ActiveSupport.use_standard_json_time_format = old
504+
end
468505
end

0 commit comments

Comments
 (0)