Skip to content
This repository has been archived by the owner on Jun 11, 2019. It is now read-only.

Ensured that Time.new is also warped #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/core_ext/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions test/time_warp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down