Permalink
Browse files

adds test coverage for Date.current vs Date.today in Date.(yesterday|…

…tomorrow) implementation
  • Loading branch information...
1 parent 8672a97 commit 0e00f428a816cd24ca645794385fd7b71dbfed73 @fxn fxn committed May 2, 2010
Showing with 45 additions and 1 deletion.
  1. +45 −1 activesupport/test/core_ext/date_ext_test.rb
@@ -178,11 +178,47 @@ def test_last_month_on_31st
def test_yesterday_constructor
assert_equal Date.current - 1, Date.yesterday
end
+
+ def test_yesterday_constructor_when_zone_default_is_not_set
+ with_env_tz 'UTC' do
+ with_tz_default do
+ Time.stubs(:now).returns Time.local(2000, 1, 1)
+ assert_equal Date.new(1999, 12, 31), Date.yesterday
+ end
+ end
+ end
+
+ def test_yesterday_constructor_when_zone_default_is_set
+ with_env_tz 'UTC' do
+ with_tz_default ActiveSupport::TimeZone['Eastern Time (US & Canada)'] do # UTC -5
+ Time.stubs(:now).returns Time.local(2000, 1, 1)
+ assert_equal Date.new(1999, 12, 30), Date.yesterday
+ end
+ end
+ end
def test_tomorrow_constructor
assert_equal Date.current + 1, Date.tomorrow
end
-
+
+ def test_tomorrow_constructor_when_zone_default_is_not_set
+ with_env_tz 'UTC' do
+ with_tz_default do
+ Time.stubs(:now).returns Time.local(1999, 12, 31)
+ assert_equal Date.new(2000, 1, 1), Date.tomorrow
+ end
+ end
+ end
+
+ def test_tomorrow_constructor_when_zone_default_is_set
+ with_env_tz 'UTC' do
+ with_tz_default ActiveSupport::TimeZone['Europe/Paris'] do # UTC +1
+ Time.stubs(:now).returns Time.local(1999, 12, 31, 23)
+ assert_equal Date.new(2000, 1, 2), Date.tomorrow
+ end
+ end
+ end
+
def test_since
assert_equal Time.local(2005,2,21,0,0,45), Date.new(2005,2,21).since(45)
end
@@ -264,6 +300,14 @@ def with_env_tz(new_tz = 'US/Eastern')
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
+
+ def with_tz_default(tz = nil)
+ old_tz = Time.zone_default
+ Time.zone_default = tz
+ yield
+ ensure
+ Time.zone_default = old_tz
+ end
end
class DateExtBehaviorTest < Test::Unit::TestCase

0 comments on commit 0e00f42

Please sign in to comment.