Skip to content

Commit

Permalink
Timecop#return will temporarily return to the actual time for the giv…
Browse files Browse the repository at this point in the history
…en block (fix #45)

Add in_realtime method

Timecop#return will temporarily return to the actual time for the given block
  • Loading branch information
Travis Jeffery committed Oct 4, 2012
1 parent 52231f1 commit fd1502d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/timecop/timecop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,16 @@ def baseline=(baseline)
instance.send(:baseline=, baseline)
end

# Reverts back to system's Time.now, Date.today and DateTime.now (if it exists).
def return
instance.send(:unmock!)
nil
# Reverts back to system's Time.now, Date.today and DateTime.now (if it exists) permamently when
# no block argument is given, or temporarily reverts back to the system's time temporarily for
# the given block.
def return(&block)
if block_given?
instance.send(:return, &block)
else
instance.send(:unmock!)
nil
end
end

def return_to_baseline
Expand Down Expand Up @@ -130,6 +136,15 @@ def travel(mock_type, *args, &block) #:nodoc:
end
end

def return(&block)
current_stack = @_stack
current_baseline = @baseline
unmock!
yield
@_stack = current_stack
@baseline = current_baseline
end

def unmock! #:nodoc:
@baseline = nil
@_stack = []
Expand Down
13 changes: 13 additions & 0 deletions test/timecop_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,19 @@ def test_travel_time_returns_nil
assert_nil Timecop.travel(t_future)
end

def test_return_temporarily_returns_to_current_time_in_given_block
time_after_travel = Time.local(1990, 7, 16)
now = Time.now

Timecop.travel(time_after_travel)

assert_times_effectively_equal(time_after_travel, Time.now)
Timecop.return do
assert_times_effectively_equal(now, Time.now)
end
assert_times_effectively_equal(time_after_travel, Time.now)
end

def test_travel_time_with_block_returns_the_value_of_the_block
t_future = Time.local(2030, 10, 10, 10, 10, 10)
expected = :foo
Expand Down

0 comments on commit fd1502d

Please sign in to comment.