timecop 0.4.3 → 0.4.4
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.
- data/History.rdoc +11 -1
- data/VERSION.yml +1 -1
- data/lib/timecop/time_stack_item.rb +3 -3
- data/lib/timecop/timecop.rb +70 -66
- data/test/time_stack_item_test.rb +17 -13
- metadata +2 -3
data/History.rdoc
CHANGED
@@ -1,4 +1,14 @@
|
|
1
|
-
=== 0.4.
|
1
|
+
=== 0.4.4 / 2012-07-29
|
2
|
+
|
3
|
+
* Maintenance
|
4
|
+
* Can now configure Timecop to not use ActiveSupport by setting Timecop.active_support = false
|
5
|
+
|
6
|
+
=== 0.4.3 / 2012-07-27
|
7
|
+
|
8
|
+
* Maintenance
|
9
|
+
* Fix alias of Time#new/Time#now when Time#new given with args
|
10
|
+
|
11
|
+
=== 0.4.2 / 2012-07-27
|
2
12
|
|
3
13
|
* Maintenance
|
4
14
|
* Mock Time#new the same as Time#now since they're synonyms
|
data/VERSION.yml
CHANGED
@@ -82,8 +82,8 @@ class Timecop
|
|
82
82
|
time_klass = Time
|
83
83
|
time_klass = Time.zone if Time.respond_to? :zone
|
84
84
|
arg = args.shift
|
85
|
-
if arg.is_a?(Time)
|
86
|
-
if arg.respond_to?(:in_time_zone)
|
85
|
+
if arg.is_a?(Time)
|
86
|
+
if Timecop.active_support != false && arg.respond_to?(:in_time_zone)
|
87
87
|
arg.in_time_zone
|
88
88
|
else
|
89
89
|
arg.getlocal
|
@@ -99,7 +99,7 @@ class Timecop
|
|
99
99
|
elsif arg.nil?
|
100
100
|
Time.now
|
101
101
|
else
|
102
|
-
if
|
102
|
+
if arg.is_a?(String) && Timecop.active_support != false && Time.respond_to?(:parse)
|
103
103
|
Time.parse(arg)
|
104
104
|
else
|
105
105
|
# we'll just assume it's a list of y/m/d/h/m/s
|
data/lib/timecop/timecop.rb
CHANGED
@@ -13,78 +13,82 @@ require 'timecop/time_stack_item'
|
|
13
13
|
class Timecop
|
14
14
|
include Singleton
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
16
|
+
class << self
|
17
|
+
attr_accessor :active_support
|
18
|
+
|
19
|
+
# Allows you to run a block of code and "fake" a time throughout the execution of that block.
|
20
|
+
# This is particularly useful for writing test methods where the passage of time is critical to the business
|
21
|
+
# logic being tested. For example:
|
22
|
+
#
|
23
|
+
# joe = User.find(1)
|
24
|
+
# joe.purchase_home()
|
25
|
+
# assert !joe.mortgage_due?
|
26
|
+
# Timecop.freeze(2008, 10, 5) do
|
27
|
+
# assert joe.mortgage_due?
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# freeze and travel will respond to several different arguments:
|
31
|
+
# 1. Timecop.freeze(time_inst)
|
32
|
+
# 2. Timecop.freeze(datetime_inst)
|
33
|
+
# 3. Timecop.freeze(date_inst)
|
34
|
+
# 4. Timecop.freeze(offset_in_seconds)
|
35
|
+
# 5. Timecop.freeze(year, month, day, hour=0, minute=0, second=0)
|
36
|
+
#
|
37
|
+
# When a block is also passed, Time.now, DateTime.now and Date.today are all reset to their
|
38
|
+
# previous values after the block has finished executing. This allows us to nest multiple
|
39
|
+
# calls to Timecop.travel and have each block maintain it's concept of "now."
|
40
|
+
#
|
41
|
+
# * Note: Timecop.freeze will actually freeze time. This can cause unanticipated problems if
|
42
|
+
# benchmark or other timing calls are executed, which implicitly expect Time to actually move
|
43
|
+
# forward.
|
44
|
+
#
|
45
|
+
# * Rails Users: Be especially careful when setting this in your development environment in a
|
46
|
+
# rails project. Generators will load your environment, including the migration generator,
|
47
|
+
# which will lead to files being generated with the timestamp set by the Timecop.freeze call
|
48
|
+
# in your dev environment
|
49
|
+
#
|
50
|
+
# Returns the frozen time.
|
51
|
+
def freeze(*args, &block)
|
52
|
+
instance().send(:travel, :freeze, *args, &block)
|
53
|
+
Time.now
|
54
|
+
end
|
52
55
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
56
|
+
# Allows you to run a block of code and "fake" a time throughout the execution of that block.
|
57
|
+
# See Timecop#freeze for a sample of how to use (same exact usage syntax)
|
58
|
+
#
|
59
|
+
# * Note: Timecop.travel will not freeze time (as opposed to Timecop.freeze). This is a particularly
|
60
|
+
# good candidate for use in environment files in rails projects.
|
61
|
+
#
|
62
|
+
# Returns the 'new' current Time.
|
63
|
+
def travel(*args, &block)
|
64
|
+
instance().send(:travel, :travel, *args, &block)
|
65
|
+
Time.now
|
66
|
+
end
|
64
67
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
+
def baseline
|
69
|
+
instance().send(:baseline)
|
70
|
+
end
|
68
71
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
+
def baseline=(baseline)
|
73
|
+
instance().send(:baseline=, baseline)
|
74
|
+
end
|
72
75
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
76
|
+
# Reverts back to system's Time.now, Date.today and DateTime.now (if it exists).
|
77
|
+
#
|
78
|
+
# Returns Time.now, which is now the real current time.
|
79
|
+
def return
|
80
|
+
instance().send(:unmock!)
|
81
|
+
Time.now
|
82
|
+
end
|
80
83
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
84
|
+
def return_to_baseline
|
85
|
+
instance().send(:return_to_baseline)
|
86
|
+
Time.now
|
87
|
+
end
|
85
88
|
|
86
|
-
|
87
|
-
|
89
|
+
def top_stack_item #:nodoc:
|
90
|
+
instance().instance_variable_get(:@_stack).last
|
91
|
+
end
|
88
92
|
end
|
89
93
|
|
90
94
|
protected
|
@@ -5,6 +5,7 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
|
5
5
|
class TestTimeStackItem < Test::Unit::TestCase
|
6
6
|
|
7
7
|
def teardown
|
8
|
+
Timecop.active_support = nil
|
8
9
|
Timecop.return
|
9
10
|
end
|
10
11
|
|
@@ -182,19 +183,22 @@ class TestTimeStackItem < Test::Unit::TestCase
|
|
182
183
|
Timecop.freeze(2011, 01, 02, hour=0, minute=0, second=0)
|
183
184
|
end
|
184
185
|
|
185
|
-
def
|
186
|
+
def test_parse_with_active_support_off
|
187
|
+
date = '2012-01-02'
|
188
|
+
Timecop.active_support = false
|
189
|
+
Time.expects(:parse).never
|
190
|
+
Timecop.freeze(date)
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_uses_active_supports_in_time_zone
|
186
194
|
time = Time.now
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
assert_equal d, stack_item.day
|
196
|
-
assert_equal h, stack_item.hour
|
197
|
-
assert_equal min, stack_item.min
|
198
|
-
assert_equal s, stack_item.sec
|
195
|
+
Time.any_instance.expects(:in_time_zone).returns(time)
|
196
|
+
Timecop::TimeStackItem.new(:freeze, time)
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_configured_off_active_support_in_time_zone_xxx
|
200
|
+
Timecop.active_support = false
|
201
|
+
Time.any_instance.expects(:in_time_zone).never
|
202
|
+
Timecop::TimeStackItem.new(:freeze, Time.now)
|
199
203
|
end
|
200
204
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timecop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -58,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
58
|
version: '0'
|
59
59
|
requirements: []
|
60
60
|
rubyforge_project: johntrupiano
|
61
|
-
rubygems_version: 1.8.
|
61
|
+
rubygems_version: 1.8.23
|
62
62
|
signing_key:
|
63
63
|
specification_version: 3
|
64
64
|
summary: A gem providing "time travel" and "time freezing" capabilities, making it
|
@@ -70,4 +70,3 @@ test_files:
|
|
70
70
|
- test/timecop_test.rb
|
71
71
|
- test/timecop_without_date_test.rb
|
72
72
|
- test/timecop_without_date_but_with_time_test.rb
|
73
|
-
has_rdoc:
|