Permalink
Browse files

Ensure Time#mock_time is nulled out when mocking is finished.

  • Loading branch information...
jtrupiano committed Sep 21, 2009
1 parent 44d6682 commit c967546e22f289dcf030c6427a33064cc2edbdf8
Showing with 25 additions and 24 deletions.
  1. +1 −22 lib/timecop/timecop.rb
  2. +24 −2 test/test_timecop.rb
View
@@ -121,6 +121,7 @@ def travel(mock_type, *args, &block)
end
def unmock!
+ @_stack = []
Time.unmock!
end
@@ -183,25 +184,3 @@ def parse_travel_args(*args)
return year, month, day, hour, minute, second
end
end
-
-#def with_dates(*dates, &block)
-# dates.flatten.each do |date|
-# begin
-# DateTime.forced_now = case date
-# when String: DateTime.parse(date)
-# when Time: DateTime.parse(date.to_s)
-# else
-# date
-# end
-# Date.forced_today = Date.new(DateTime.forced_now.year,
-#DateTime.forced_now.month, DateTime.forced_now.day)
-# yield
-# rescue Exception => e
-# raise e
-# ensure
-# DateTime.forced_now = nil
-# Date.forced_today = nil
-# end
-# end
-#end
-
View
@@ -26,6 +26,30 @@ def test_freeze_changes_and_resets_time
assert_not_equal t, Time.now
end
+ def test_freeze_then_return_unsets_mock_time
+ Timecop.freeze(1)
+ Timecop.return
+ assert_nil Time.send(:mock_time)
+ end
+
+ def test_travel_then_return_unsets_mock_time
+ Timecop.travel(1)
+ Timecop.return
+ assert_nil Time.send(:mock_time)
+ end
+
+ def test_freeze_with_block_unsets_mock_time
+ assert_nil Time.send(:mock_time), "test is invalid"
+ Timecop.freeze(1) do; end
+ assert_nil Time.send(:mock_time)
+ end
+
+ def test_travel_with_block_unsets_mock_time
+ assert_nil Time.send(:mock_time), "test is invalid"
+ Timecop.travel(1) do; end
+ assert_nil Time.send(:mock_time)
+ end
+
def test_recursive_freeze
t = Time.local(2008, 10, 10, 10, 10, 10)
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
@@ -142,7 +166,6 @@ def test_recursive_rebasing_maintains_each_context
end
assert((t - Time.now).abs < 2000, "Failed to restore previously-traveled time.")
end
- assert_nil Time.send(:mock_time)
end
def test_recursive_travel_then_freeze
@@ -155,7 +178,6 @@ def test_recursive_travel_then_freeze
end
assert((t - Time.now).abs < 2000, "Failed to restore previously-traveled time.")
end
- assert_nil Time.send(:mock_time)
end
def test_recursive_freeze_then_travel

0 comments on commit c967546

Please sign in to comment.