diff --git a/lib/core_ext/time.rb b/lib/core_ext/time.rb index 90bf7f5..058707a 100644 --- a/lib/core_ext/time.rb +++ b/lib/core_ext/time.rb @@ -14,7 +14,7 @@ def now end alias_method :real_new, :new - def self.new(*args) + def new(*args) args.empty? ? now : real_new(*args) end end diff --git a/test/time_warp_test.rb b/test/time_warp_test.rb index aab1683..f05b874 100644 --- a/test/time_warp_test.rb +++ b/test/time_warp_test.rb @@ -15,6 +15,16 @@ def test_pretend_now_is_should_set_now_back_in_time end end + def test_pretend_now_is_should_set_new_back_in_time + pretend_now_is(Time.utc(2008,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 UTC 2008 + assert_equal 2008, Time.new.utc.year + assert_equal 7, Time.new.utc.month + assert_equal 25, Time.new.utc.day + assert_equal 6, Time.new.utc.hour + assert_equal 15, Time.new.utc.min + end + end + def test_pretend_now_is_should_set_now_forward_in_time future_year = Time.now.year + 1 pretend_now_is(Time.utc(future_year,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 UTC future_year @@ -26,6 +36,17 @@ def test_pretend_now_is_should_set_now_forward_in_time end end + def test_pretend_now_is_should_set_new_forward_in_time + future_year = Time.now.year + 1 + pretend_now_is(Time.utc(future_year,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 UTC future_year + assert_equal future_year, Time.new.utc.year + assert_equal 7, Time.new.utc.month + assert_equal 25, Time.new.utc.day + assert_equal 6, Time.new.utc.hour + assert_equal 15, Time.new.utc.min + end + end + def test_pretend_now_should_revert_to_real_now_after_block now = Time.now now_year = now.year @@ -45,6 +66,31 @@ def test_pretend_now_should_revert_to_real_now_after_block assert_equal now_minute, Time.now.min end + def test_pretend_now_should_revert_to_real_new_after_block + now = Time.new + now_year = now.year + now_month = now.month + now_day = now.day + now_hour = now.hour + now_minute = now.min + + pretend_now_is(Time.utc(2008,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 UTC 2008 + # block of code + end + + assert_equal now_year, Time.new.year + assert_equal now_month, Time.new.month + assert_equal now_day, Time.new.day + assert_equal now_hour, Time.new.hour + assert_equal now_minute, Time.new.min + end + + def test_time_new_with_arguments_ignores_pretend_now + pretend_now_is(Time.utc(2008,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 UTC 2008 + assert_equal 2012, Time.new(2012).year + end + end + def test_pretend_now_resolves_to_the_same_value_regardless_of_setting_by_time_argument_or_time_utc_arguments now_with_time_argument = now_with_time_utc_arguments = nil pretend_now_is(Time.utc(2008,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 UTC 2008