Removed mocha stubbing in active_support

This commit is contained in:
Ronak Jangir 2015-06-04 23:55:45 +05:30
parent 228e3f01cb
commit 81d2ce96d0
10 changed files with 366 additions and 309 deletions

@ -37,4 +37,5 @@ def jruby_skip(message = '')
skip message if defined?(JRUBY_VERSION) skip message if defined?(JRUBY_VERSION)
end end
require 'minitest/mock'
require 'mocha/setup' # FIXME: stop using mocha require 'mocha/setup' # FIXME: stop using mocha

@ -288,8 +288,9 @@ def test_read_multi_with_expires
@cache.write('foo', 'bar', :expires_in => 10) @cache.write('foo', 'bar', :expires_in => 10)
@cache.write('fu', 'baz') @cache.write('fu', 'baz')
@cache.write('fud', 'biz') @cache.write('fud', 'biz')
Time.stubs(:now).returns(time + 11) Time.stub(:now, time + 11) do
assert_equal({"fu" => "baz"}, @cache.read_multi('foo', 'fu')) assert_equal({"fu" => "baz"}, @cache.read_multi('foo', 'fu'))
end
end end
def test_fetch_multi def test_fetch_multi
@ -387,66 +388,74 @@ def test_original_store_objects_should_not_be_immutable
def test_expires_in def test_expires_in
time = Time.local(2008, 4, 24) time = Time.local(2008, 4, 24)
Time.stubs(:now).returns(time)
@cache.write('foo', 'bar') Time.stub(:now, time) do
assert_equal 'bar', @cache.read('foo') @cache.write('foo', 'bar')
assert_equal 'bar', @cache.read('foo')
end
Time.stubs(:now).returns(time + 30) Time.stub(:now, time + 30) do
assert_equal 'bar', @cache.read('foo') assert_equal 'bar', @cache.read('foo')
end
Time.stubs(:now).returns(time + 61) Time.stub(:now, time + 61) do
assert_nil @cache.read('foo') assert_nil @cache.read('foo')
end
end end
def test_race_condition_protection_skipped_if_not_defined def test_race_condition_protection_skipped_if_not_defined
@cache.write('foo', 'bar') @cache.write('foo', 'bar')
time = @cache.send(:read_entry, 'foo', {}).expires_at time = @cache.send(:read_entry, 'foo', {}).expires_at
Time.stubs(:now).returns(Time.at(time))
result = @cache.fetch('foo') do Time.stub(:now, Time.at(time)) do
assert_equal nil, @cache.read('foo') result = @cache.fetch('foo') do
'baz' assert_equal nil, @cache.read('foo')
'baz'
end
assert_equal 'baz', result
end end
assert_equal 'baz', result
end end
def test_race_condition_protection_is_limited def test_race_condition_protection_is_limited
time = Time.now time = Time.now
@cache.write('foo', 'bar', :expires_in => 60) @cache.write('foo', 'bar', :expires_in => 60)
Time.stubs(:now).returns(time + 71) Time.stub(:now, time + 71) do
result = @cache.fetch('foo', :race_condition_ttl => 10) do result = @cache.fetch('foo', :race_condition_ttl => 10) do
assert_equal nil, @cache.read('foo') assert_equal nil, @cache.read('foo')
"baz" "baz"
end
assert_equal "baz", result
end end
assert_equal "baz", result
end end
def test_race_condition_protection_is_safe def test_race_condition_protection_is_safe
time = Time.now time = Time.now
@cache.write('foo', 'bar', :expires_in => 60) @cache.write('foo', 'bar', :expires_in => 60)
Time.stubs(:now).returns(time + 61) Time.stub(:now, time + 61) do
begin begin
@cache.fetch('foo', :race_condition_ttl => 10) do @cache.fetch('foo', :race_condition_ttl => 10) do
assert_equal 'bar', @cache.read('foo') assert_equal 'bar', @cache.read('foo')
raise ArgumentError.new raise ArgumentError.new
end
rescue ArgumentError
end end
rescue ArgumentError assert_equal "bar", @cache.read('foo')
end
Time.stub(:now, time + 91) do
assert_nil @cache.read('foo')
end end
assert_equal "bar", @cache.read('foo')
Time.stubs(:now).returns(time + 91)
assert_nil @cache.read('foo')
end end
def test_race_condition_protection def test_race_condition_protection
time = Time.now time = Time.now
@cache.write('foo', 'bar', :expires_in => 60) @cache.write('foo', 'bar', :expires_in => 60)
Time.stubs(:now).returns(time + 61) Time.stub(:now, time + 61) do
result = @cache.fetch('foo', :race_condition_ttl => 10) do result = @cache.fetch('foo', :race_condition_ttl => 10) do
assert_equal 'bar', @cache.read('foo') assert_equal 'bar', @cache.read('foo')
"baz" "baz"
end
assert_equal "baz", result
end end
assert_equal "baz", result
end end
def test_crazy_key_characters def test_crazy_key_characters
@ -775,11 +784,12 @@ def test_cleanup_removes_all_expired_entries
@cache.write('foo', 'bar', expires_in: 10) @cache.write('foo', 'bar', expires_in: 10)
@cache.write('baz', 'qux') @cache.write('baz', 'qux')
@cache.write('quux', 'corge', expires_in: 20) @cache.write('quux', 'corge', expires_in: 20)
Time.stubs(:now).returns(time + 15) Time.stub(:now, time + 15) do
@cache.cleanup @cache.cleanup
assert_not @cache.exist?('foo') assert_not @cache.exist?('foo')
assert @cache.exist?('baz') assert @cache.exist?('baz')
assert @cache.exist?('quux') assert @cache.exist?('quux')
end
end end
def test_write_with_unless_exist def test_write_with_unless_exist
@ -1056,9 +1066,9 @@ def test_expired
assert !entry.expired?, 'entry not expired' assert !entry.expired?, 'entry not expired'
entry = ActiveSupport::Cache::Entry.new("value", :expires_in => 60) entry = ActiveSupport::Cache::Entry.new("value", :expires_in => 60)
assert !entry.expired?, 'entry not expired' assert !entry.expired?, 'entry not expired'
time = Time.now + 61 Time.stub(:now, Time.now + 61) do
Time.stubs(:now).returns(time) assert entry.expired?, 'entry is expired'
assert entry.expired?, 'entry is expired' end
end end
def test_compress_values def test_compress_values

@ -191,8 +191,9 @@ def test_yesterday_constructor_when_zone_is_not_set
def test_yesterday_constructor_when_zone_is_set def test_yesterday_constructor_when_zone_is_set
with_env_tz 'UTC' do with_env_tz 'UTC' do
with_tz_default ActiveSupport::TimeZone['Eastern Time (US & Canada)'] do # UTC -5 with_tz_default ActiveSupport::TimeZone['Eastern Time (US & Canada)'] do # UTC -5
Time.stubs(:now).returns Time.local(2000, 1, 1) Time.stub(:now, Time.local(2000, 1, 1)) do
assert_equal Date.new(1999, 12, 30), Date.yesterday assert_equal Date.new(1999, 12, 30), Date.yesterday
end
end end
end end
end end
@ -212,8 +213,9 @@ def test_tomorrow_constructor_when_zone_is_not_set
def test_tomorrow_constructor_when_zone_is_set def test_tomorrow_constructor_when_zone_is_set
with_env_tz 'UTC' do with_env_tz 'UTC' do
with_tz_default ActiveSupport::TimeZone['Europe/Paris'] do # UTC +1 with_tz_default ActiveSupport::TimeZone['Europe/Paris'] do # UTC +1
Time.stubs(:now).returns Time.local(1999, 12, 31, 23) Time.stub(:now, Time.local(1999, 12, 31, 23)) do
assert_equal Date.new(2000, 1, 2), Date.tomorrow assert_equal Date.new(2000, 1, 2), Date.tomorrow
end
end end
end end
end end
@ -317,23 +319,26 @@ def test_xmlschema_when_zone_is_set
end end
def test_past def test_past
Date.stubs(:current).returns(Date.new(2000, 1, 1)) Date.stub(:current, Date.new(2000, 1, 1)) do
assert_equal true, Date.new(1999, 12, 31).past? assert_equal true, Date.new(1999, 12, 31).past?
assert_equal false, Date.new(2000,1,1).past? assert_equal false, Date.new(2000,1,1).past?
assert_equal false, Date.new(2000,1,2).past? assert_equal false, Date.new(2000,1,2).past?
end
end end
def test_future def test_future
Date.stubs(:current).returns(Date.new(2000, 1, 1)) Date.stub(:current, Date.new(2000, 1, 1)) do
assert_equal false, Date.new(1999, 12, 31).future? assert_equal false, Date.new(1999, 12, 31).future?
assert_equal false, Date.new(2000,1,1).future? assert_equal false, Date.new(2000,1,1).future?
assert_equal true, Date.new(2000,1,2).future? assert_equal true, Date.new(2000,1,2).future?
end
end end
def test_current_returns_date_today_when_zone_not_set def test_current_returns_date_today_when_zone_not_set
with_env_tz 'US/Central' do with_env_tz 'US/Central' do
Time.stubs(:now).returns Time.local(1999, 12, 31, 23) Time.stub(:now, Time.local(1999, 12, 31, 23)) do
assert_equal Date.today, Date.current assert_equal Date.today, Date.current
end
end end
end end

@ -204,61 +204,69 @@ def test_xmlschema
end end
def test_today_with_offset def test_today_with_offset
Date.stubs(:current).returns(Date.new(2000, 1, 1)) Date.stub(:current, Date.new(2000, 1, 1)) do
assert_equal false, DateTime.civil(1999,12,31,23,59,59, Rational(-18000, 86400)).today? assert_equal false, DateTime.civil(1999,12,31,23,59,59, Rational(-18000, 86400)).today?
assert_equal true, DateTime.civil(2000,1,1,0,0,0, Rational(-18000, 86400)).today? assert_equal true, DateTime.civil(2000,1,1,0,0,0, Rational(-18000, 86400)).today?
assert_equal true, DateTime.civil(2000,1,1,23,59,59, Rational(-18000, 86400)).today? assert_equal true, DateTime.civil(2000,1,1,23,59,59, Rational(-18000, 86400)).today?
assert_equal false, DateTime.civil(2000,1,2,0,0,0, Rational(-18000, 86400)).today? assert_equal false, DateTime.civil(2000,1,2,0,0,0, Rational(-18000, 86400)).today?
end
end end
def test_today_without_offset def test_today_without_offset
Date.stubs(:current).returns(Date.new(2000, 1, 1)) Date.stub(:current, Date.new(2000, 1, 1)) do
assert_equal false, DateTime.civil(1999,12,31,23,59,59).today? assert_equal false, DateTime.civil(1999,12,31,23,59,59).today?
assert_equal true, DateTime.civil(2000,1,1,0).today? assert_equal true, DateTime.civil(2000,1,1,0).today?
assert_equal true, DateTime.civil(2000,1,1,23,59,59).today? assert_equal true, DateTime.civil(2000,1,1,23,59,59).today?
assert_equal false, DateTime.civil(2000,1,2,0).today? assert_equal false, DateTime.civil(2000,1,2,0).today?
end
end end
def test_past_with_offset def test_past_with_offset
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400))) DateTime.stub(:current, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400))) do
assert_equal true, DateTime.civil(2005,2,10,15,30,44, Rational(-18000, 86400)).past? assert_equal true, DateTime.civil(2005,2,10,15,30,44, Rational(-18000, 86400)).past?
assert_equal false, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).past? assert_equal false, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).past?
assert_equal false, DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).past? assert_equal false, DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).past?
end
end end
def test_past_without_offset def test_past_without_offset
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400))) DateTime.stub(:current, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400))) do
assert_equal true, DateTime.civil(2005,2,10,20,30,44).past? assert_equal true, DateTime.civil(2005,2,10,20,30,44).past?
assert_equal false, DateTime.civil(2005,2,10,20,30,45).past? assert_equal false, DateTime.civil(2005,2,10,20,30,45).past?
assert_equal false, DateTime.civil(2005,2,10,20,30,46).past? assert_equal false, DateTime.civil(2005,2,10,20,30,46).past?
end
end end
def test_future_with_offset def test_future_with_offset
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400))) DateTime.stub(:current, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400))) do
assert_equal false, DateTime.civil(2005,2,10,15,30,44, Rational(-18000, 86400)).future? assert_equal false, DateTime.civil(2005,2,10,15,30,44, Rational(-18000, 86400)).future?
assert_equal false, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).future? assert_equal false, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).future?
assert_equal true, DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).future? assert_equal true, DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).future?
end
end end
def test_future_without_offset def test_future_without_offset
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400))) DateTime.stub(:current, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400))) do
assert_equal false, DateTime.civil(2005,2,10,20,30,44).future? assert_equal false, DateTime.civil(2005,2,10,20,30,44).future?
assert_equal false, DateTime.civil(2005,2,10,20,30,45).future? assert_equal false, DateTime.civil(2005,2,10,20,30,45).future?
assert_equal true, DateTime.civil(2005,2,10,20,30,46).future? assert_equal true, DateTime.civil(2005,2,10,20,30,46).future?
end
end end
def test_current_returns_date_today_when_zone_is_not_set def test_current_returns_date_today_when_zone_is_not_set
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
Time.stubs(:now).returns Time.local(1999, 12, 31, 23, 59, 59) Time.stub(:now, Time.local(1999, 12, 31, 23, 59, 59)) do
assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current
end
end end
end end
def test_current_returns_time_zone_today_when_zone_is_set def test_current_returns_time_zone_today_when_zone_is_set
Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
Time.stubs(:now).returns Time.local(1999, 12, 31, 23, 59, 59) Time.stub(:now, Time.local(1999, 12, 31, 23, 59, 59)) do
assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current
end
end end
ensure ensure
Time.zone = nil Time.zone = nil

@ -140,28 +140,30 @@ def test_since_and_ago_with_fractional_weeks
def test_since_and_ago_anchored_to_time_now_when_time_zone_is_not_set def test_since_and_ago_anchored_to_time_now_when_time_zone_is_not_set
Time.zone = nil Time.zone = nil
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
Time.stubs(:now).returns Time.local(2000) Time.stub(:now, Time.local(2000)) do
# since # since
assert_not_instance_of ActiveSupport::TimeWithZone, 5.seconds.since assert_not_instance_of ActiveSupport::TimeWithZone, 5.seconds.since
assert_equal Time.local(2000,1,1,0,0,5), 5.seconds.since assert_equal Time.local(2000,1,1,0,0,5), 5.seconds.since
# ago # ago
assert_not_instance_of ActiveSupport::TimeWithZone, 5.seconds.ago assert_not_instance_of ActiveSupport::TimeWithZone, 5.seconds.ago
assert_equal Time.local(1999,12,31,23,59,55), 5.seconds.ago assert_equal Time.local(1999,12,31,23,59,55), 5.seconds.ago
end
end end
end end
def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_is_set def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_is_set
Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
Time.stubs(:now).returns Time.local(2000) Time.stub(:now, Time.local(2000)) do
# since # since
assert_instance_of ActiveSupport::TimeWithZone, 5.seconds.since assert_instance_of ActiveSupport::TimeWithZone, 5.seconds.since
assert_equal Time.utc(2000,1,1,0,0,5), 5.seconds.since.time assert_equal Time.utc(2000,1,1,0,0,5), 5.seconds.since.time
assert_equal 'Eastern Time (US & Canada)', 5.seconds.since.time_zone.name assert_equal 'Eastern Time (US & Canada)', 5.seconds.since.time_zone.name
# ago # ago
assert_instance_of ActiveSupport::TimeWithZone, 5.seconds.ago assert_instance_of ActiveSupport::TimeWithZone, 5.seconds.ago
assert_equal Time.utc(1999,12,31,23,59,55), 5.seconds.ago.time assert_equal Time.utc(1999,12,31,23,59,55), 5.seconds.ago.time
assert_equal 'Eastern Time (US & Canada)', 5.seconds.ago.time_zone.name assert_equal 'Eastern Time (US & Canada)', 5.seconds.ago.time_zone.name
end
end end
ensure ensure
Time.zone = nil Time.zone = nil

@ -435,117 +435,123 @@ def test_partial_string_to_time
def test_standard_time_string_to_time_when_current_time_is_standard_time def test_standard_time_string_to_time_when_current_time_is_standard_time
with_env_tz "US/Eastern" do with_env_tz "US/Eastern" do
Time.stubs(:now).returns(Time.local(2012, 1, 1)) Time.stub(:now, Time.local(2012, 1, 1)) do
assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time
assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 -0800".to_time assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 -0800".to_time
assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 -0800".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 -0800".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 -0500".to_time assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 -0500".to_time
assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 -0500".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 -0500".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 5, 0), "2012-01-01 10:00 UTC".to_time assert_equal Time.local(2012, 1, 1, 5, 0), "2012-01-01 10:00 UTC".to_time
assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00 UTC".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00 UTC".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 PST".to_time assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 PST".to_time
assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 PST".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 PST".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 EST".to_time assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 EST".to_time
assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 EST".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 EST".to_time(:utc)
end
end end
end end
def test_standard_time_string_to_time_when_current_time_is_daylight_savings def test_standard_time_string_to_time_when_current_time_is_daylight_savings
with_env_tz "US/Eastern" do with_env_tz "US/Eastern" do
Time.stubs(:now).returns(Time.local(2012, 7, 1)) Time.stub(:now, Time.local(2012, 7, 1)) do
assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time
assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 -0800".to_time assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 -0800".to_time
assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 -0800".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 -0800".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 -0500".to_time assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 -0500".to_time
assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 -0500".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 -0500".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 5, 0), "2012-01-01 10:00 UTC".to_time assert_equal Time.local(2012, 1, 1, 5, 0), "2012-01-01 10:00 UTC".to_time
assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00 UTC".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00 UTC".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 PST".to_time assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 PST".to_time
assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 PST".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 PST".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 EST".to_time assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 EST".to_time
assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 EST".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 EST".to_time(:utc)
end
end end
end end
def test_daylight_savings_string_to_time_when_current_time_is_standard_time def test_daylight_savings_string_to_time_when_current_time_is_standard_time
with_env_tz "US/Eastern" do with_env_tz "US/Eastern" do
Time.stubs(:now).returns(Time.local(2012, 1, 1)) Time.stub(:now, Time.local(2012, 1, 1)) do
assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time
assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 -0700".to_time assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 -0700".to_time
assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 -0700".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 -0700".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 -0400".to_time assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 -0400".to_time
assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 -0400".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 -0400".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 6, 0), "2012-07-01 10:00 UTC".to_time assert_equal Time.local(2012, 7, 1, 6, 0), "2012-07-01 10:00 UTC".to_time
assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00 UTC".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00 UTC".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 PDT".to_time assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 PDT".to_time
assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 PDT".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 PDT".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 EDT".to_time assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 EDT".to_time
assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 EDT".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 EDT".to_time(:utc)
end
end end
end end
def test_daylight_savings_string_to_time_when_current_time_is_daylight_savings def test_daylight_savings_string_to_time_when_current_time_is_daylight_savings
with_env_tz "US/Eastern" do with_env_tz "US/Eastern" do
Time.stubs(:now).returns(Time.local(2012, 7, 1)) Time.stub(:now, Time.local(2012, 7, 1)) do
assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time
assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 -0700".to_time assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 -0700".to_time
assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 -0700".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 -0700".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 -0400".to_time assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 -0400".to_time
assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 -0400".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 -0400".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 6, 0), "2012-07-01 10:00 UTC".to_time assert_equal Time.local(2012, 7, 1, 6, 0), "2012-07-01 10:00 UTC".to_time
assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00 UTC".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00 UTC".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 PDT".to_time assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 PDT".to_time
assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 PDT".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 PDT".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 EDT".to_time assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 EDT".to_time
assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 EDT".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 EDT".to_time(:utc)
end
end end
end end
def test_partial_string_to_time_when_current_time_is_standard_time def test_partial_string_to_time_when_current_time_is_standard_time
with_env_tz "US/Eastern" do with_env_tz "US/Eastern" do
Time.stubs(:now).returns(Time.local(2012, 1, 1)) Time.stub(:now, Time.local(2012, 1, 1)) do
assert_equal Time.local(2012, 1, 1, 10, 0), "10:00".to_time assert_equal Time.local(2012, 1, 1, 10, 0), "10:00".to_time
assert_equal Time.utc(2012, 1, 1, 10, 0), "10:00".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 10, 0), "10:00".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 6, 0), "10:00 -0100".to_time assert_equal Time.local(2012, 1, 1, 6, 0), "10:00 -0100".to_time
assert_equal Time.utc(2012, 1, 1, 11, 0), "10:00 -0100".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 11, 0), "10:00 -0100".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 10, 0), "10:00 -0500".to_time assert_equal Time.local(2012, 1, 1, 10, 0), "10:00 -0500".to_time
assert_equal Time.utc(2012, 1, 1, 15, 0), "10:00 -0500".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 15, 0), "10:00 -0500".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 5, 0), "10:00 UTC".to_time assert_equal Time.local(2012, 1, 1, 5, 0), "10:00 UTC".to_time
assert_equal Time.utc(2012, 1, 1, 10, 0), "10:00 UTC".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 10, 0), "10:00 UTC".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 13, 0), "10:00 PST".to_time assert_equal Time.local(2012, 1, 1, 13, 0), "10:00 PST".to_time
assert_equal Time.utc(2012, 1, 1, 18, 0), "10:00 PST".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 18, 0), "10:00 PST".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 12, 0), "10:00 PDT".to_time assert_equal Time.local(2012, 1, 1, 12, 0), "10:00 PDT".to_time
assert_equal Time.utc(2012, 1, 1, 17, 0), "10:00 PDT".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 17, 0), "10:00 PDT".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 10, 0), "10:00 EST".to_time assert_equal Time.local(2012, 1, 1, 10, 0), "10:00 EST".to_time
assert_equal Time.utc(2012, 1, 1, 15, 0), "10:00 EST".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 15, 0), "10:00 EST".to_time(:utc)
assert_equal Time.local(2012, 1, 1, 9, 0), "10:00 EDT".to_time assert_equal Time.local(2012, 1, 1, 9, 0), "10:00 EDT".to_time
assert_equal Time.utc(2012, 1, 1, 14, 0), "10:00 EDT".to_time(:utc) assert_equal Time.utc(2012, 1, 1, 14, 0), "10:00 EDT".to_time(:utc)
end
end end
end end
def test_partial_string_to_time_when_current_time_is_daylight_savings def test_partial_string_to_time_when_current_time_is_daylight_savings
with_env_tz "US/Eastern" do with_env_tz "US/Eastern" do
Time.stubs(:now).returns(Time.local(2012, 7, 1)) Time.stub(:now, Time.local(2012, 7, 1)) do
assert_equal Time.local(2012, 7, 1, 10, 0), "10:00".to_time assert_equal Time.local(2012, 7, 1, 10, 0), "10:00".to_time
assert_equal Time.utc(2012, 7, 1, 10, 0), "10:00".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 10, 0), "10:00".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 7, 0), "10:00 -0100".to_time assert_equal Time.local(2012, 7, 1, 7, 0), "10:00 -0100".to_time
assert_equal Time.utc(2012, 7, 1, 11, 0), "10:00 -0100".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 11, 0), "10:00 -0100".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 11, 0), "10:00 -0500".to_time assert_equal Time.local(2012, 7, 1, 11, 0), "10:00 -0500".to_time
assert_equal Time.utc(2012, 7, 1, 15, 0), "10:00 -0500".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 15, 0), "10:00 -0500".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 6, 0), "10:00 UTC".to_time assert_equal Time.local(2012, 7, 1, 6, 0), "10:00 UTC".to_time
assert_equal Time.utc(2012, 7, 1, 10, 0), "10:00 UTC".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 10, 0), "10:00 UTC".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 14, 0), "10:00 PST".to_time assert_equal Time.local(2012, 7, 1, 14, 0), "10:00 PST".to_time
assert_equal Time.utc(2012, 7, 1, 18, 0), "10:00 PST".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 18, 0), "10:00 PST".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 13, 0), "10:00 PDT".to_time assert_equal Time.local(2012, 7, 1, 13, 0), "10:00 PDT".to_time
assert_equal Time.utc(2012, 7, 1, 17, 0), "10:00 PDT".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 17, 0), "10:00 PDT".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 11, 0), "10:00 EST".to_time assert_equal Time.local(2012, 7, 1, 11, 0), "10:00 EST".to_time
assert_equal Time.utc(2012, 7, 1, 15, 0), "10:00 EST".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 15, 0), "10:00 EST".to_time(:utc)
assert_equal Time.local(2012, 7, 1, 10, 0), "10:00 EDT".to_time assert_equal Time.local(2012, 7, 1, 10, 0), "10:00 EDT".to_time
assert_equal Time.utc(2012, 7, 1, 14, 0), "10:00 EDT".to_time(:utc) assert_equal Time.utc(2012, 7, 1, 14, 0), "10:00 EDT".to_time(:utc)
end
end end
end end

@ -605,13 +605,15 @@ def test_days_in_month_with_year
end end
def test_days_in_month_feb_in_common_year_without_year_arg def test_days_in_month_feb_in_common_year_without_year_arg
Time.stubs(:now).returns(Time.utc(2007)) Time.stub(:now, Time.utc(2007)) do
assert_equal 28, Time.days_in_month(2) assert_equal 28, Time.days_in_month(2)
end
end end
def test_days_in_month_feb_in_leap_year_without_year_arg def test_days_in_month_feb_in_leap_year_without_year_arg
Time.stubs(:now).returns(Time.utc(2008)) Time.stub(:now, Time.utc(2008)) do
assert_equal 29, Time.days_in_month(2) assert_equal 29, Time.days_in_month(2)
end
end end
def test_last_month_on_31st def test_last_month_on_31st
@ -623,68 +625,74 @@ def test_xmlschema_is_available
end end
def test_today_with_time_local def test_today_with_time_local
Date.stubs(:current).returns(Date.new(2000, 1, 1)) Date.stub(:current, Date.new(2000, 1, 1)) do
assert_equal false, Time.local(1999,12,31,23,59,59).today? assert_equal false, Time.local(1999,12,31,23,59,59).today?
assert_equal true, Time.local(2000,1,1,0).today? assert_equal true, Time.local(2000,1,1,0).today?
assert_equal true, Time.local(2000,1,1,23,59,59).today? assert_equal true, Time.local(2000,1,1,23,59,59).today?
assert_equal false, Time.local(2000,1,2,0).today? assert_equal false, Time.local(2000,1,2,0).today?
end
end end
def test_today_with_time_utc def test_today_with_time_utc
Date.stubs(:current).returns(Date.new(2000, 1, 1)) Date.stub(:current, Date.new(2000, 1, 1)) do
assert_equal false, Time.utc(1999,12,31,23,59,59).today? assert_equal false, Time.utc(1999,12,31,23,59,59).today?
assert_equal true, Time.utc(2000,1,1,0).today? assert_equal true, Time.utc(2000,1,1,0).today?
assert_equal true, Time.utc(2000,1,1,23,59,59).today? assert_equal true, Time.utc(2000,1,1,23,59,59).today?
assert_equal false, Time.utc(2000,1,2,0).today? assert_equal false, Time.utc(2000,1,2,0).today?
end
end end
def test_past_with_time_current_as_time_local def test_past_with_time_current_as_time_local
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45)) Time.stub(:current, Time.local(2005,2,10,15,30,45)) do
assert_equal true, Time.local(2005,2,10,15,30,44).past? assert_equal true, Time.local(2005,2,10,15,30,44).past?
assert_equal false, Time.local(2005,2,10,15,30,45).past? assert_equal false, Time.local(2005,2,10,15,30,45).past?
assert_equal false, Time.local(2005,2,10,15,30,46).past? assert_equal false, Time.local(2005,2,10,15,30,46).past?
assert_equal true, Time.utc(2005,2,10,20,30,44).past? assert_equal true, Time.utc(2005,2,10,20,30,44).past?
assert_equal false, Time.utc(2005,2,10,20,30,45).past? assert_equal false, Time.utc(2005,2,10,20,30,45).past?
assert_equal false, Time.utc(2005,2,10,20,30,46).past? assert_equal false, Time.utc(2005,2,10,20,30,46).past?
end
end end
end end
def test_past_with_time_current_as_time_with_zone def test_past_with_time_current_as_time_with_zone
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
twz = Time.utc(2005,2,10,15,30,45).in_time_zone('Central Time (US & Canada)') twz = Time.utc(2005,2,10,15,30,45).in_time_zone('Central Time (US & Canada)')
Time.stubs(:current).returns(twz) Time.stub(:current, twz) do
assert_equal true, Time.local(2005,2,10,10,30,44).past? assert_equal true, Time.local(2005,2,10,10,30,44).past?
assert_equal false, Time.local(2005,2,10,10,30,45).past? assert_equal false, Time.local(2005,2,10,10,30,45).past?
assert_equal false, Time.local(2005,2,10,10,30,46).past? assert_equal false, Time.local(2005,2,10,10,30,46).past?
assert_equal true, Time.utc(2005,2,10,15,30,44).past? assert_equal true, Time.utc(2005,2,10,15,30,44).past?
assert_equal false, Time.utc(2005,2,10,15,30,45).past? assert_equal false, Time.utc(2005,2,10,15,30,45).past?
assert_equal false, Time.utc(2005,2,10,15,30,46).past? assert_equal false, Time.utc(2005,2,10,15,30,46).past?
end
end end
end end
def test_future_with_time_current_as_time_local def test_future_with_time_current_as_time_local
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45)) Time.stub(:current, Time.local(2005,2,10,15,30,45)) do
assert_equal false, Time.local(2005,2,10,15,30,44).future? assert_equal false, Time.local(2005,2,10,15,30,44).future?
assert_equal false, Time.local(2005,2,10,15,30,45).future? assert_equal false, Time.local(2005,2,10,15,30,45).future?
assert_equal true, Time.local(2005,2,10,15,30,46).future? assert_equal true, Time.local(2005,2,10,15,30,46).future?
assert_equal false, Time.utc(2005,2,10,20,30,44).future? assert_equal false, Time.utc(2005,2,10,20,30,44).future?
assert_equal false, Time.utc(2005,2,10,20,30,45).future? assert_equal false, Time.utc(2005,2,10,20,30,45).future?
assert_equal true, Time.utc(2005,2,10,20,30,46).future? assert_equal true, Time.utc(2005,2,10,20,30,46).future?
end
end end
end end
def test_future_with_time_current_as_time_with_zone def test_future_with_time_current_as_time_with_zone
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
twz = Time.utc(2005,2,10,15,30,45).in_time_zone('Central Time (US & Canada)') twz = Time.utc(2005,2,10,15,30,45).in_time_zone('Central Time (US & Canada)')
Time.stubs(:current).returns(twz) Time.stub(:current, twz) do
assert_equal false, Time.local(2005,2,10,10,30,44).future? assert_equal false, Time.local(2005,2,10,10,30,44).future?
assert_equal false, Time.local(2005,2,10,10,30,45).future? assert_equal false, Time.local(2005,2,10,10,30,45).future?
assert_equal true, Time.local(2005,2,10,10,30,46).future? assert_equal true, Time.local(2005,2,10,10,30,46).future?
assert_equal false, Time.utc(2005,2,10,15,30,44).future? assert_equal false, Time.utc(2005,2,10,15,30,44).future?
assert_equal false, Time.utc(2005,2,10,15,30,45).future? assert_equal false, Time.utc(2005,2,10,15,30,45).future?
assert_equal true, Time.utc(2005,2,10,15,30,46).future? assert_equal true, Time.utc(2005,2,10,15,30,46).future?
end
end end
end end

@ -205,45 +205,50 @@ def test_between?
end end
def test_today def test_today
Date.stubs(:current).returns(Date.new(2000, 1, 1)) Date.stub(:current, Date.new(2000, 1, 1)) do
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(1999,12,31,23,59,59) ).today? assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(1999,12,31,23,59,59) ).today?
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,1,0) ).today? assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,1,0) ).today?
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,1,23,59,59) ).today? assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,1,23,59,59) ).today?
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,2,0) ).today? assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,2,0) ).today?
end
end end
def test_past_with_time_current_as_time_local def test_past_with_time_current_as_time_local
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45)) Time.stub(:current, Time.local(2005,2,10,15,30,45)) do
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).past? assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).past?
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).past? assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).past?
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).past? assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).past?
end
end end
end end
def test_past_with_time_current_as_time_with_zone def test_past_with_time_current_as_time_with_zone
twz = ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45) ) twz = ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45) )
Time.stubs(:current).returns(twz) Time.stub(:current, twz) do
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).past? assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).past?
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).past? assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).past?
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).past? assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).past?
end
end end
def test_future_with_time_current_as_time_local def test_future_with_time_current_as_time_local
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45)) Time.stub(:current, Time.local(2005,2,10,15,30,45)) do
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).future? assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).future?
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).future? assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).future?
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).future? assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).future?
end
end end
end end
def test_future_with_time_current_as_time_with_zone def test_future_with_time_current_as_time_with_zone
twz = ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45) ) twz = ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45) )
Time.stubs(:current).returns(twz) Time.stub(:current, twz) do
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).future? assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).future?
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).future? assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).future?
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).future? assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).future?
end
end end
def test_eql? def test_eql?
@ -1033,19 +1038,21 @@ def test_time_zone_setter_with_find_zone_without_bang
def test_current_returns_time_now_when_zone_not_set def test_current_returns_time_now_when_zone_not_set
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
Time.stubs(:now).returns Time.local(2000) Time.stub(:now, Time.local(2000)) do
assert_equal false, Time.current.is_a?(ActiveSupport::TimeWithZone) assert_equal false, Time.current.is_a?(ActiveSupport::TimeWithZone)
assert_equal Time.local(2000), Time.current assert_equal Time.local(2000), Time.current
end
end end
end end
def test_current_returns_time_zone_now_when_zone_set def test_current_returns_time_zone_now_when_zone_set
Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
Time.stubs(:now).returns Time.local(2000) Time.stub(:now, Time.local(2000)) do
assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone) assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone)
assert_equal 'Eastern Time (US & Canada)', Time.current.time_zone.name assert_equal 'Eastern Time (US & Canada)', Time.current.time_zone.name
assert_equal Time.utc(2000), Time.current.time assert_equal Time.utc(2000), Time.current.time
end
end end
end end

@ -3,80 +3,88 @@
require 'active_support/core_ext/numeric/time' require 'active_support/core_ext/numeric/time'
class TimeTravelTest < ActiveSupport::TestCase class TimeTravelTest < ActiveSupport::TestCase
setup do
Time.stubs now: Time.now
end
teardown do teardown do
travel_back travel_back
end end
def test_time_helper_travel def test_time_helper_travel
expected_time = Time.now + 1.day Time.stub(:now, Time.now) do
travel 1.day expected_time = Time.now + 1.day
travel 1.day
assert_equal expected_time.to_s(:db), Time.now.to_s(:db)
assert_equal expected_time.to_date, Date.today
assert_equal expected_time.to_datetime.to_s(:db), DateTime.now.to_s(:db)
end
def test_time_helper_travel_with_block
expected_time = Time.now + 1.day
travel 1.day do
assert_equal expected_time.to_s(:db), Time.now.to_s(:db) assert_equal expected_time.to_s(:db), Time.now.to_s(:db)
assert_equal expected_time.to_date, Date.today assert_equal expected_time.to_date, Date.today
assert_equal expected_time.to_datetime.to_s(:db), DateTime.now.to_s(:db) assert_equal expected_time.to_datetime.to_s(:db), DateTime.now.to_s(:db)
end end
end
assert_not_equal expected_time.to_s(:db), Time.now.to_s(:db) def test_time_helper_travel_with_block
assert_not_equal expected_time.to_date, Date.today Time.stub(:now, Time.now) do
assert_not_equal expected_time.to_datetime.to_s(:db), DateTime.now.to_s(:db) expected_time = Time.now + 1.day
travel 1.day do
assert_equal expected_time.to_s(:db), Time.now.to_s(:db)
assert_equal expected_time.to_date, Date.today
assert_equal expected_time.to_datetime.to_s(:db), DateTime.now.to_s(:db)
end
assert_not_equal expected_time.to_s(:db), Time.now.to_s(:db)
assert_not_equal expected_time.to_date, Date.today
assert_not_equal expected_time.to_datetime.to_s(:db), DateTime.now.to_s(:db)
end
end end
def test_time_helper_travel_to def test_time_helper_travel_to
expected_time = Time.new(2004, 11, 24, 01, 04, 44) Time.stub(:now, Time.now) do
travel_to expected_time expected_time = Time.new(2004, 11, 24, 01, 04, 44)
travel_to expected_time
assert_equal expected_time, Time.now
assert_equal Date.new(2004, 11, 24), Date.today
assert_equal expected_time.to_datetime, DateTime.now
end
def test_time_helper_travel_to_with_block
expected_time = Time.new(2004, 11, 24, 01, 04, 44)
travel_to expected_time do
assert_equal expected_time, Time.now assert_equal expected_time, Time.now
assert_equal Date.new(2004, 11, 24), Date.today assert_equal Date.new(2004, 11, 24), Date.today
assert_equal expected_time.to_datetime, DateTime.now assert_equal expected_time.to_datetime, DateTime.now
end end
end
assert_not_equal expected_time, Time.now def test_time_helper_travel_to_with_block
assert_not_equal Date.new(2004, 11, 24), Date.today Time.stub(:now, Time.now) do
assert_not_equal expected_time.to_datetime, DateTime.now expected_time = Time.new(2004, 11, 24, 01, 04, 44)
travel_to expected_time do
assert_equal expected_time, Time.now
assert_equal Date.new(2004, 11, 24), Date.today
assert_equal expected_time.to_datetime, DateTime.now
end
assert_not_equal expected_time, Time.now
assert_not_equal Date.new(2004, 11, 24), Date.today
assert_not_equal expected_time.to_datetime, DateTime.now
end
end end
def test_time_helper_travel_back def test_time_helper_travel_back
expected_time = Time.new(2004, 11, 24, 01, 04, 44) Time.stub(:now, Time.now) do
expected_time = Time.new(2004, 11, 24, 01, 04, 44)
travel_to expected_time travel_to expected_time
assert_equal expected_time, Time.now assert_equal expected_time, Time.now
assert_equal Date.new(2004, 11, 24), Date.today assert_equal Date.new(2004, 11, 24), Date.today
assert_equal expected_time.to_datetime, DateTime.now assert_equal expected_time.to_datetime, DateTime.now
travel_back travel_back
assert_not_equal expected_time, Time.now assert_not_equal expected_time, Time.now
assert_not_equal Date.new(2004, 11, 24), Date.today assert_not_equal Date.new(2004, 11, 24), Date.today
assert_not_equal expected_time.to_datetime, DateTime.now assert_not_equal expected_time.to_datetime, DateTime.now
end
end end
def test_travel_to_will_reset_the_usec_to_avoid_mysql_rouding def test_travel_to_will_reset_the_usec_to_avoid_mysql_rouding
travel_to Time.utc(2014, 10, 10, 10, 10, 50, 999999) do Time.stub(:now, Time.now) do
assert_equal 50, Time.now.sec travel_to Time.utc(2014, 10, 10, 10, 10, 50, 999999) do
assert_equal 0, Time.now.usec assert_equal 50, Time.now.sec
assert_equal 50, DateTime.now.sec assert_equal 0, Time.now.usec
assert_equal 0, DateTime.now.usec assert_equal 50, DateTime.now.sec
assert_equal 0, DateTime.now.usec
end
end end
end end
end end

@ -252,9 +252,10 @@ def test_parse_returns_nil_when_string_without_date_information_is_passed_in
def test_parse_with_incomplete_date def test_parse_with_incomplete_date
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
zone.stubs(:now).returns zone.local(1999,12,31) zone.stub(:now, zone.local(1999,12,31)) do
twz = zone.parse('19:00:00') twz = zone.parse('19:00:00')
assert_equal Time.utc(1999,12,31,19), twz.time assert_equal Time.utc(1999,12,31,19), twz.time
end
end end
def test_parse_with_day_omitted def test_parse_with_day_omitted
@ -284,9 +285,10 @@ def test_parse_should_black_out_app_timezone_dst_jump
def test_parse_with_missing_time_components def test_parse_with_missing_time_components
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
zone.stubs(:now).returns zone.local(1999, 12, 31, 12, 59, 59) zone.stub(:now, zone.local(1999, 12, 31, 12, 59, 59)) do
twz = zone.parse('2012-12-01') twz = zone.parse('2012-12-01')
assert_equal Time.utc(2012, 12, 1), twz.time assert_equal Time.utc(2012, 12, 1), twz.time
end
end end
def test_parse_with_javascript_date def test_parse_with_javascript_date