3
3
require 'abstract_unit'
4
4
require 'active_support/core_ext/string/inflections'
5
5
require 'active_support/json'
6
+ require 'active_support/time'
6
7
7
8
class TestJSONEncoding < ActiveSupport ::TestCase
8
9
class Foo
@@ -226,21 +227,17 @@ def test_hash_should_allow_key_filtering_with_except
226
227
end
227
228
228
229
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
233
234
end
234
- ensure
235
- ActiveSupport . use_standard_json_time_format = prev
236
235
end
237
236
238
237
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
244
241
end
245
242
246
243
def test_nested_hash_with_float
@@ -453,6 +450,39 @@ def test_json_gem_pretty_generate_by_passing_active_support_encoder
453
450
assert_nil h . as_json_called
454
451
end
455
452
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
+
456
486
protected
457
487
458
488
def object_keys ( json_object )
@@ -465,4 +495,11 @@ def with_env_tz(new_tz = 'US/Eastern')
465
495
ensure
466
496
old_tz ? ENV [ 'TZ' ] = old_tz : ENV . delete ( 'TZ' )
467
497
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
468
505
end
0 commit comments