Skip to content

Commit

Permalink
Extract out with_env_tz helper method.
Browse files Browse the repository at this point in the history
It’s used at so many places that extracting it out into a helper file
is worth doing.
  • Loading branch information
zuhao committed Jun 18, 2014
1 parent e2f232a commit 9c49288
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 122 deletions.
18 changes: 2 additions & 16 deletions activesupport/test/core_ext/date_ext_test.rb
@@ -1,13 +1,15 @@
require 'abstract_unit'
require 'active_support/time'
require 'core_ext/date_and_time_behavior'
require 'time_zone_test_helpers'

class DateExtCalculationsTest < ActiveSupport::TestCase
def date_time_init(year,month,day,*args)
Date.new(year,month,day)
end

include DateAndTimeBehavior
include TimeZoneTestHelpers

def test_yesterday_in_calendar_reform
assert_equal Date.new(1582,10,4), Date.new(1582,10,15).yesterday
Expand Down Expand Up @@ -349,22 +351,6 @@ def test_date_advance_should_not_change_passed_options_hash
Date.new(2005,2,28).advance(options)
assert_equal({ :years => 3, :months => 11, :days => 2 }, options)
end

protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end

def with_tz_default(tz = nil)
old_tz = Time.zone
Time.zone = tz
yield
ensure
Time.zone = old_tz
end
end

class DateExtBehaviorTest < ActiveSupport::TestCase
Expand Down
10 changes: 2 additions & 8 deletions activesupport/test/core_ext/date_time_ext_test.rb
@@ -1,13 +1,15 @@
require 'abstract_unit'
require 'active_support/time'
require 'core_ext/date_and_time_behavior'
require 'time_zone_test_helpers'

class DateTimeExtCalculationsTest < ActiveSupport::TestCase
def date_time_init(year,month,day,hour,minute,second,*args)
DateTime.civil(year,month,day,hour,minute,second)
end

include DateAndTimeBehavior
include TimeZoneTestHelpers

def test_to_s
datetime = DateTime.new(2005, 2, 21, 14, 30, 0, 0)
Expand Down Expand Up @@ -352,12 +354,4 @@ def test_nsec
assert_equal 0, DateTime.civil(2000).nsec
assert_equal 500000000, DateTime.civil(2000, 1, 1, 0, 0, Rational(1,2)).nsec
end

protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end
11 changes: 3 additions & 8 deletions activesupport/test/core_ext/duration_test.rb
Expand Up @@ -2,8 +2,11 @@
require 'active_support/inflector'
require 'active_support/time'
require 'active_support/json'
require 'time_zone_test_helpers'

class DurationTest < ActiveSupport::TestCase
include TimeZoneTestHelpers

def test_is_a
d = 1.day
assert d.is_a?(ActiveSupport::Duration)
Expand Down Expand Up @@ -167,12 +170,4 @@ def test_case_when
cased = case 1.day when 1.day then "ok" end
assert_equal cased, "ok"
end

protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end
8 changes: 0 additions & 8 deletions activesupport/test/core_ext/numeric_ext_test.rb
Expand Up @@ -71,14 +71,6 @@ def test_add_one_year_to_leap_day
assert_equal Time.utc(2005,2,28,15,15,10), Time.utc(2004,2,29,15,15,10) + 1.year
assert_equal DateTime.civil(2005,2,28,15,15,10), DateTime.civil(2004,2,29,15,15,10) + 1.year
end

protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end

class NumericExtDateTest < ActiveSupport::TestCase
Expand Down
12 changes: 4 additions & 8 deletions activesupport/test/core_ext/string_ext_test.rb
Expand Up @@ -10,10 +10,12 @@
require 'active_support/core_ext/string/strip'
require 'active_support/core_ext/string/output_safety'
require 'active_support/core_ext/string/indent'
require 'time_zone_test_helpers'

class StringInflectionsTest < ActiveSupport::TestCase
include InflectorTestCases
include ConstantizeTestCases
include TimeZoneTestHelpers

def test_strip_heredoc_on_an_empty_string
assert_equal '', ''.strip_heredoc
Expand Down Expand Up @@ -354,6 +356,8 @@ class StringAccessTest < ActiveSupport::TestCase
end

class StringConversionsTest < ActiveSupport::TestCase
include TimeZoneTestHelpers

def test_string_to_time
with_env_tz "Europe/Moscow" do
assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:utc)
Expand Down Expand Up @@ -523,14 +527,6 @@ def test_string_to_date
assert_nil "".to_date
assert_equal Date.new(Date.today.year, 2, 3), "Feb 3rd".to_date
end

protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end

class StringBehaviourTest < ActiveSupport::TestCase
Expand Down
11 changes: 2 additions & 9 deletions activesupport/test/core_ext/time_ext_test.rb
@@ -1,13 +1,15 @@
require 'abstract_unit'
require 'active_support/time'
require 'core_ext/date_and_time_behavior'
require 'time_zone_test_helpers'

class TimeExtCalculationsTest < ActiveSupport::TestCase
def date_time_init(year,month,day,hour,minute,second,usec=0)
Time.local(year,month,day,hour,minute,second,usec)
end

include DateAndTimeBehavior
include TimeZoneTestHelpers

def test_seconds_since_midnight
assert_equal 1,Time.local(2005,1,1,0,0,1).seconds_since_midnight
Expand Down Expand Up @@ -847,15 +849,6 @@ def test_all_quarter
def test_all_year
assert_equal Time.local(2011,1,1,0,0,0)..Time.local(2011,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2011,6,7,10,10,10).all_year
end

protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end

end

class TimeExtMarshalingTest < ActiveSupport::TestCase
Expand Down
62 changes: 12 additions & 50 deletions activesupport/test/core_ext/time_with_zone_test.rb
@@ -1,7 +1,9 @@
require 'abstract_unit'
require 'active_support/time'
require 'time_zone_test_helpers'

class TimeWithZoneTest < ActiveSupport::TestCase
include TimeZoneTestHelpers

def setup
@utc = Time.utc(2000, 1, 1, 0)
Expand Down Expand Up @@ -810,23 +812,17 @@ def test_no_method_error_has_proper_context
assert_equal "undefined method `this_method_does_not_exist' for Fri, 31 Dec 1999 19:00:00 EST -05:00:Time", e.message
assert_no_match "rescue", e.backtrace.first
end

protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end

class TimeWithZoneMethodsForTimeAndDateTimeTest < ActiveSupport::TestCase
include TimeZoneTestHelpers

def setup
@t, @dt = Time.utc(2000), DateTime.civil(2000)
@t, @dt, @zone = Time.utc(2000), DateTime.civil(2000), Time.zone
end

def teardown
Time.zone = nil
Time.zone = @zone
end

def test_in_time_zone
Expand Down Expand Up @@ -882,8 +878,6 @@ def test_in_time_zone_with_time_local_instance
def test_localtime
Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
assert_equal @dt.in_time_zone.localtime, @dt.in_time_zone.utc.to_time.getlocal
ensure
Time.zone = nil
end

def test_use_zone
Expand Down Expand Up @@ -922,15 +916,15 @@ def test_time_zone_getter_and_setter
end

def test_time_zone_getter_and_setter_with_zone_default_set
old_zone_default = Time.zone_default
Time.zone_default = ActiveSupport::TimeZone['Alaska']
assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
Time.zone = ActiveSupport::TimeZone['Hawaii']
assert_equal ActiveSupport::TimeZone['Hawaii'], Time.zone
Time.zone = nil
assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
ensure
Time.zone = nil
Time.zone_default = nil
Time.zone_default = old_zone_default
end

def test_time_zone_setter_is_thread_safe
Expand Down Expand Up @@ -1002,8 +996,6 @@ def test_current_returns_time_zone_now_when_zone_set
assert_equal 'Eastern Time (US & Canada)', Time.current.time_zone.name
assert_equal Time.utc(2000), Time.current.time
end
ensure
Time.zone = nil
end

def test_time_in_time_zone_doesnt_affect_receiver
Expand All @@ -1014,25 +1006,15 @@ def test_time_in_time_zone_doesnt_affect_receiver
assert_not time.utc?, 'time expected to be local, but is UTC'
end
end

protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end

class TimeWithZoneMethodsForDate < ActiveSupport::TestCase
include TimeZoneTestHelpers

def setup
@d = Date.civil(2000)
end

def teardown
Time.zone = nil
end

def test_in_time_zone
with_tz_default 'Alaska' do
assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @d.in_time_zone.inspect
Expand Down Expand Up @@ -1065,28 +1047,17 @@ def test_in_time_zone_with_invalid_argument
assert_raise(ArgumentError) { @d.in_time_zone(-15.hours) }
assert_raise(ArgumentError) { @d.in_time_zone(Object.new) }
end

protected
def with_tz_default(tz = nil)
old_tz = Time.zone
Time.zone = tz
yield
ensure
Time.zone = old_tz
end
end

class TimeWithZoneMethodsForString < ActiveSupport::TestCase
include TimeZoneTestHelpers

def setup
@s = "Sat, 01 Jan 2000 00:00:00"
@u = "Sat, 01 Jan 2000 00:00:00 UTC +00:00"
@z = "Fri, 31 Dec 1999 19:00:00 EST -05:00"
end

def teardown
Time.zone = nil
end

def test_in_time_zone
with_tz_default 'Alaska' do
assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @s.in_time_zone.inspect
Expand Down Expand Up @@ -1141,13 +1112,4 @@ def test_in_time_zone_with_invalid_argument
assert_raise(ArgumentError) { @u.in_time_zone(Object.new) }
assert_raise(ArgumentError) { @z.in_time_zone(Object.new) }
end

protected
def with_tz_default(tz = nil)
old_tz = Time.zone
Time.zone = tz
yield
ensure
Time.zone = old_tz
end
end
10 changes: 3 additions & 7 deletions activesupport/test/json/encoding_test.rb
Expand Up @@ -4,8 +4,11 @@
require 'active_support/core_ext/string/inflections'
require 'active_support/json'
require 'active_support/time'
require 'time_zone_test_helpers'

class TestJSONEncoding < ActiveSupport::TestCase
include TimeZoneTestHelpers

class Foo
def initialize(a, b)
@a, @b = a, b
Expand Down Expand Up @@ -530,13 +533,6 @@ def object_keys(json_object)
json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
end

def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
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
Expand Down
11 changes: 3 additions & 8 deletions activesupport/test/time_zone_test.rb
@@ -1,7 +1,10 @@
require 'abstract_unit'
require 'active_support/time'
require 'time_zone_test_helpers'

class TimeZoneTest < ActiveSupport::TestCase
include TimeZoneTestHelpers

def test_utc_to_local
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
assert_equal Time.utc(1999, 12, 31, 19), zone.utc_to_local(Time.utc(2000, 1)) # standard offset -0500
Expand Down Expand Up @@ -416,12 +419,4 @@ def test_us_zones
assert ActiveSupport::TimeZone.us_zones.include?(ActiveSupport::TimeZone["Hawaii"])
assert !ActiveSupport::TimeZone.us_zones.include?(ActiveSupport::TimeZone["Kuala Lumpur"])
end

protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end
16 changes: 16 additions & 0 deletions activesupport/test/time_zone_test_helpers.rb
@@ -0,0 +1,16 @@
module TimeZoneTestHelpers
def with_tz_default(tz = nil)
old_tz = Time.zone
Time.zone = tz
yield
ensure
Time.zone = old_tz
end

def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end

0 comments on commit 9c49288

Please sign in to comment.