bjhess-time-warp 1.0.1 → 1.0.2

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 (4) hide show
  1. data/README +19 -0
  2. data/lib/time_warp.rb +9 -2
  3. data/test/time_warp_test.rb +11 -0
  4. metadata +1 -1
data/README CHANGED
@@ -11,6 +11,9 @@ mocking around with baseline Ruby classes to be asking for trouble. Eventually
11
11
  unusual behavior will rear its head and a day will be lost debugging
12
12
  tests - the most excruciating debugging one can be subjected to.
13
13
 
14
+ Rather than mock Time.now, the time-warp gem will temporarily stub its return
15
+ value for the duration of the block defined within a pretend_now_is method
16
+ call. See examples below for practical usage.
14
17
 
15
18
  Installation
16
19
  ============
@@ -24,6 +27,10 @@ Gem:
24
27
  $ gem sources -a http://gems.github.com
25
28
  $ sudo gem install bjhess-time-warp
26
29
 
30
+ Gem config in a Rails app. environment.rb:
31
+
32
+ config.gem 'bjhess-time-warp', :lib => 'time_warp', :source => "http://gems.github.com"
33
+
27
34
  Example
28
35
  =======
29
36
 
@@ -66,6 +73,18 @@ goal is to assure a particular day of week when each test method executes:
66
73
  end
67
74
  end
68
75
 
76
+ The pretend_now_is method may also be called with the arguments for the
77
+ Time#utc call, rather than a Time argument. So:
78
+
79
+ pretend_now_is(Time.utc(2008,"jul",24,20)) do
80
+ # Shifted code
81
+ end
82
+
83
+ Becomes:
84
+
85
+ pretend_now_is(2008,"jul",24,20) do
86
+ # Shifted code
87
+ end
69
88
 
70
89
  Credits
71
90
  =======
data/lib/time_warp.rb CHANGED
@@ -6,14 +6,21 @@ module Test # :nodoc:
6
6
 
7
7
  ##
8
8
  # Time warp to the specified time for the duration of the passed block.
9
- def pretend_now_is(time)
9
+ def pretend_now_is(*args)
10
10
  begin
11
- Time.testing_offset = Time.now - time
11
+ Time.testing_offset = Time.now - time_from(*args)
12
12
  yield
13
13
  ensure
14
14
  Time.testing_offset = 0
15
15
  end
16
16
  end
17
+
18
+ private
19
+
20
+ def time_from(*args)
21
+ return args[0] if 1 == args.size && args[0].is_a?(Time)
22
+ Time.utc(*args)
23
+ end
17
24
 
18
25
  end
19
26
  end
@@ -44,4 +44,15 @@ class TimeWarpTest < Test::Unit::TestCase
44
44
  assert_equal now_hour, Time.now.hour
45
45
  assert_equal now_minute, Time.now.min
46
46
  end
47
+
48
+ def test_pretend_now_resolves_to_the_same_value_regardless_of_setting_by_time_argument_or_time_utc_arguments
49
+ now_with_time_argument = now_with_time_utc_arguments = nil
50
+ pretend_now_is(Time.utc(2008,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 UTC 2008
51
+ now_with_time_argument = Time.now.utc
52
+ end
53
+ pretend_now_is(2008,"jul",25,6,15) do #=> Fri Jul 25 06:15:00 UTC 2008
54
+ now_with_time_utc_arguments = Time.now.utc
55
+ end
56
+ assert_equal now_with_time_argument.to_s, now_with_time_utc_arguments.to_s
57
+ end
47
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bjhess-time-warp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Barry Hess