time-warp 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/time_warp.rb +14 -5
  2. data/test/time_warp_test.rb +38 -0
  3. metadata +6 -6
data/lib/time_warp.rb CHANGED
@@ -7,18 +7,27 @@ module Test # :nodoc:
7
7
  ##
8
8
  # Time warp to the specified time for the duration of the passed block.
9
9
  def pretend_now_is(*args)
10
- begin
11
- Time.testing_offset = Time.now - time_from(*args)
12
- yield
13
- ensure
14
- Time.testing_offset = 0
10
+ Time.testing_offset = Time.now - time_from(*args)
11
+ if block_given?
12
+ begin
13
+ yield
14
+ ensure
15
+ reset_to_real_time
16
+ end
15
17
  end
16
18
  end
19
+
20
+ ##
21
+ # Reset to real time.
22
+ def reset_to_real_time
23
+ Time.testing_offset = 0
24
+ end
17
25
 
18
26
  private
19
27
 
20
28
  def time_from(*args)
21
29
  return args[0] if 1 == args.size && args[0].is_a?(Time)
30
+ return args[0].to_time if 1 == args.size && args[0].respond_to?(:to_time) # For example, if it's a Date.
22
31
  Time.utc(*args)
23
32
  end
24
33
 
@@ -55,4 +55,42 @@ class TimeWarpTest < Test::Unit::TestCase
55
55
  end
56
56
  assert_equal now_with_time_argument.to_s, now_with_time_utc_arguments.to_s
57
57
  end
58
+
59
+ def test_pretend_now_without_a_block
60
+ now = Time.now
61
+ now_year = now.year
62
+ now_month = now.month
63
+ now_day = now.day
64
+ now_hour = now.hour
65
+ now_minute = now.min
66
+
67
+ pretend_now_is(Time.utc(2008,"jul",25,6,15))
68
+ assert_equal 2008, Time.now.utc.year
69
+ assert_equal 7, Time.now.utc.month
70
+ assert_equal 25, Time.now.utc.day
71
+ assert_equal 6, Time.now.utc.hour
72
+ assert_equal 15, Time.now.utc.min
73
+ reset_to_real_time
74
+
75
+ assert_equal now_year, Time.now.year
76
+ assert_equal now_month, Time.now.month
77
+ assert_equal now_day, Time.now.day
78
+ assert_equal now_hour, Time.now.hour
79
+ assert_equal now_minute, Time.now.min
80
+ end
81
+
82
+ def test_pretend_now_with_an_object_that_responds_to_to_time
83
+ # Date objects in rails have to_time methods, but without Rails, they don't so we fake it
84
+ date = Object.new
85
+ def date.to_time
86
+ Time.utc(2008,"jul",25,0,0)
87
+ end
88
+ pretend_now_is(date) do #=> Fri Jul 25 00:00:00 UTC 2008
89
+ assert_equal 2008, Time.now.utc.year
90
+ assert_equal 7, Time.now.utc.month
91
+ assert_equal 25, Time.now.utc.day
92
+ assert_equal 0, Time.now.utc.hour
93
+ assert_equal 0, Time.now.utc.min
94
+ end
95
+ end
58
96
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time-warp
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease: false
4
+ hash: 7
5
+ prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 7
10
- version: 1.0.7
9
+ - 8
10
+ version: 1.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Barry Hess
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-24 00:00:00 -05:00
18
+ date: 2011-04-15 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  requirements: []
68
68
 
69
69
  rubyforge_project:
70
- rubygems_version: 1.3.7
70
+ rubygems_version: 1.5.0
71
71
  signing_key:
72
72
  specification_version: 3
73
73
  summary: Warp time in your tests