timecop 0.7.0 → 0.8.0
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.
- checksums.yaml +4 -4
- data/README.markdown +4 -6
- data/lib/timecop/time_extensions.rb +50 -6
- data/lib/timecop/time_stack_item.rb +4 -7
- data/lib/timecop/timecop.rb +8 -1
- data/lib/timecop/version.rb +1 -1
- data/test/test_helper.rb +19 -15
- data/test/time_stack_item_test.rb +4 -8
- data/test/timecop_test.rb +73 -26
- data/test/timecop_without_date_but_with_time_test.rb +4 -6
- data/test/timecop_without_date_test.rb +20 -21
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6e96be7760b99a2b26e5d68800d2980190fe22c2
|
|
4
|
+
data.tar.gz: 9c55cc8abe218ff8dd6035452ffe5e1056d592a4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6f5b765cd3bc5ead77b8947e18d8230f164a1b4655f26ccc73bc7715a70666bf6e623a8567274fda5b375955f30a7bbeb825921fa24c2bb046de9cd8b26df429
|
|
7
|
+
data.tar.gz: 2170ebdcf36371775b6b2abd27faf6ed435ba3d7469c405d8dbda47328622a9375b847de7663e25ff23a6dd8fbddc64012246f4127752a21a143d87458ee0f94
|
data/README.markdown
CHANGED
|
@@ -2,9 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
[](http://travis-ci.org/travisjeffery/timecop)
|
|
4
4
|
|
|
5
|
-
- [Source](http://github.com/travisjeffery/timecop)
|
|
6
|
-
- [Documentation](http://johntrupiano.rubyforge.org/timecop)
|
|
7
|
-
|
|
8
5
|
## DESCRIPTION
|
|
9
6
|
|
|
10
7
|
A gem providing "time travel" and "time freezing" capabilities, making it dead simple to test time-dependent code. It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call.
|
|
@@ -54,7 +51,8 @@ describe "some set of tests to mock" do
|
|
|
54
51
|
Timecop.return
|
|
55
52
|
end
|
|
56
53
|
|
|
57
|
-
it "should do blah blah blah"
|
|
54
|
+
it "should do blah blah blah" do
|
|
55
|
+
end
|
|
58
56
|
end
|
|
59
57
|
```
|
|
60
58
|
|
|
@@ -111,6 +109,8 @@ Time.now
|
|
|
111
109
|
# => 2012-09-21 06:22:59 -0500
|
|
112
110
|
```
|
|
113
111
|
|
|
112
|
+
See [#42](https://github.com/travisjeffery/timecop/pull/42) for more information, thanks to Ken Mayer, David Holcomb, and Pivotal Labs.
|
|
113
|
+
|
|
114
114
|
### Timecop.safe_mode
|
|
115
115
|
|
|
116
116
|
Safe mode forces you to use Timecop with the block syntax since it always puts time back the way it was. If you are running in safe mode and use Timecop without the block syntax `Timecop::SafeModeException` will be raised to tell the user they are not being safe.
|
|
@@ -128,8 +128,6 @@ Timecop.freeze
|
|
|
128
128
|
# => Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed.
|
|
129
129
|
```
|
|
130
130
|
|
|
131
|
-
See [#42](https://github.com/travisjeffery/timecop/pull/42) for more information, thanks to Ken Mayer, David Holcomb, and Pivotal Labs.
|
|
132
|
-
|
|
133
131
|
## Contribute
|
|
134
132
|
|
|
135
133
|
timecop is maintained by [travisjeffery](http://github.com/travisjeffery), and
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
require 'date'
|
|
2
1
|
require 'time'
|
|
2
|
+
require 'date'
|
|
3
3
|
|
|
4
4
|
class Time #:nodoc:
|
|
5
5
|
class << self
|
|
@@ -27,6 +27,16 @@ class Time #:nodoc:
|
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
class Date #:nodoc:
|
|
30
|
+
WEEKDAYS = {
|
|
31
|
+
"sunday" => 0,
|
|
32
|
+
"monday" => 1,
|
|
33
|
+
"tuesday" => 2,
|
|
34
|
+
"wednesday" => 3,
|
|
35
|
+
"thursday" => 4,
|
|
36
|
+
"friday" => 5,
|
|
37
|
+
"saturday" => 6
|
|
38
|
+
}
|
|
39
|
+
|
|
30
40
|
class << self
|
|
31
41
|
def mock_date
|
|
32
42
|
mocked_time_stack_item = Timecop.top_stack_item
|
|
@@ -43,11 +53,31 @@ class Date #:nodoc:
|
|
|
43
53
|
|
|
44
54
|
alias_method :strptime_without_mock_date, :strptime
|
|
45
55
|
|
|
46
|
-
def strptime_with_mock_date(str, fmt)
|
|
56
|
+
def strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY)
|
|
57
|
+
unless start == Date::ITALY
|
|
58
|
+
raise ArgumentError, "Timecop's #{self}::#{__method__} only " +
|
|
59
|
+
"supports Date::ITALY for the start argument."
|
|
60
|
+
end
|
|
61
|
+
|
|
47
62
|
Time.strptime(str, fmt).to_date
|
|
48
63
|
end
|
|
49
64
|
|
|
50
65
|
alias_method :strptime, :strptime_with_mock_date
|
|
66
|
+
|
|
67
|
+
def parse_with_mock_date(*args)
|
|
68
|
+
str = args.first
|
|
69
|
+
if str && WEEKDAYS.keys.include?(str.downcase)
|
|
70
|
+
offset = WEEKDAYS[str.downcase] - Date.today.wday
|
|
71
|
+
|
|
72
|
+
Date.today + offset
|
|
73
|
+
else
|
|
74
|
+
parse_without_mock_date(*args)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
alias_method :parse_without_mock_date, :parse
|
|
79
|
+
alias_method :parse, :parse_with_mock_date
|
|
80
|
+
|
|
51
81
|
end
|
|
52
82
|
end
|
|
53
83
|
|
|
@@ -58,14 +88,28 @@ class DateTime #:nodoc:
|
|
|
58
88
|
mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.datetime(self)
|
|
59
89
|
end
|
|
60
90
|
|
|
61
|
-
def now_without_mock_time
|
|
62
|
-
Time.now_without_mock_time.to_datetime
|
|
63
|
-
end
|
|
64
|
-
|
|
65
91
|
def now_with_mock_time
|
|
66
92
|
mock_time || now_without_mock_time
|
|
67
93
|
end
|
|
68
94
|
|
|
95
|
+
alias_method :now_without_mock_time, :now
|
|
96
|
+
|
|
69
97
|
alias_method :now, :now_with_mock_time
|
|
98
|
+
|
|
99
|
+
def parse_with_mock_date(*args)
|
|
100
|
+
str = args.first
|
|
101
|
+
if str && Date::WEEKDAYS.keys.include?(str.downcase)
|
|
102
|
+
offset = Date::WEEKDAYS[str.downcase] - DateTime.now.wday
|
|
103
|
+
|
|
104
|
+
parsed_weekday =(DateTime.now + offset)
|
|
105
|
+
|
|
106
|
+
DateTime.new(parsed_weekday.year, parsed_weekday.month, parsed_weekday.day, 0, 0, 0, 0)
|
|
107
|
+
else
|
|
108
|
+
parse_without_mock_date(*args)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
alias_method :parse_without_mock_date, :parse
|
|
113
|
+
alias_method :parse, :parse_with_mock_date
|
|
70
114
|
end
|
|
71
115
|
end
|
|
@@ -6,6 +6,7 @@ class Timecop
|
|
|
6
6
|
|
|
7
7
|
def initialize(mock_type, *args)
|
|
8
8
|
raise "Unknown mock_type #{mock_type}" unless [:freeze, :travel, :scale].include?(mock_type)
|
|
9
|
+
@travel_offset = @scaling_factor = nil
|
|
9
10
|
@scaling_factor = args.shift if mock_type == :scale
|
|
10
11
|
@mock_type = mock_type
|
|
11
12
|
@time = parse_time(*args)
|
|
@@ -75,12 +76,8 @@ class Timecop
|
|
|
75
76
|
|
|
76
77
|
def datetime(datetime_klass = DateTime)
|
|
77
78
|
if Float.method_defined?(:to_r)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
datetime_klass.new(year, month, day, hour, min, (fractions_of_a_second + sec), utc_offset_to_rational(utc_offset))
|
|
81
|
-
else
|
|
82
|
-
datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset))
|
|
83
|
-
end
|
|
79
|
+
fractions_of_a_second = time.to_f % 1
|
|
80
|
+
datetime_klass.new(year, month, day, hour, min, (fractions_of_a_second + sec), utc_offset_to_rational(utc_offset))
|
|
84
81
|
else
|
|
85
82
|
datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset))
|
|
86
83
|
end
|
|
@@ -137,4 +134,4 @@ class Timecop
|
|
|
137
134
|
Time.respond_to?(:zone) && Time.zone ? Time.zone : Time
|
|
138
135
|
end
|
|
139
136
|
end
|
|
140
|
-
|
|
137
|
+
end
|
data/lib/timecop/timecop.rb
CHANGED
|
@@ -31,6 +31,7 @@ class Timecop
|
|
|
31
31
|
# 3. Timecop.freeze(date_inst)
|
|
32
32
|
# 4. Timecop.freeze(offset_in_seconds)
|
|
33
33
|
# 5. Timecop.freeze(year, month, day, hour=0, minute=0, second=0)
|
|
34
|
+
# 6. Timecop.freeze() # Defaults to Time.now
|
|
34
35
|
#
|
|
35
36
|
# When a block is also passed, Time.now, DateTime.now and Date.today are all reset to their
|
|
36
37
|
# previous values after the block has finished executing. This allows us to nest multiple
|
|
@@ -107,7 +108,12 @@ class Timecop
|
|
|
107
108
|
end
|
|
108
109
|
|
|
109
110
|
def safe_mode?
|
|
110
|
-
|
|
111
|
+
@safe_mode ||= false
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Returns whether or not Timecop is currently frozen/travelled
|
|
115
|
+
def frozen?
|
|
116
|
+
!instance.instance_variable_get(:@_stack).empty?
|
|
111
117
|
end
|
|
112
118
|
|
|
113
119
|
private
|
|
@@ -150,6 +156,7 @@ class Timecop
|
|
|
150
156
|
current_baseline = @baseline
|
|
151
157
|
unmock!
|
|
152
158
|
yield
|
|
159
|
+
ensure
|
|
153
160
|
@_stack = current_stack
|
|
154
161
|
@baseline = current_baseline
|
|
155
162
|
end
|
data/lib/timecop/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
|
@@ -1,38 +1,42 @@
|
|
|
1
1
|
require 'rubygems'
|
|
2
2
|
require 'bundler/setup'
|
|
3
|
-
require '
|
|
3
|
+
require 'minitest/autorun'
|
|
4
|
+
|
|
5
|
+
$VERBOSE = true # enable ruby warnings
|
|
6
|
+
|
|
4
7
|
begin
|
|
5
8
|
require 'mocha/setup'
|
|
6
9
|
rescue LoadError
|
|
7
10
|
require 'mocha'
|
|
8
11
|
end
|
|
9
12
|
|
|
10
|
-
class
|
|
11
|
-
|
|
13
|
+
class Minitest::Unit::TestCase
|
|
12
14
|
private
|
|
13
15
|
# Tests to see that two times are within the given distance,
|
|
14
16
|
# in seconds, from each other.
|
|
15
17
|
def times_effectively_equal(time1, time2, seconds_interval = 1)
|
|
16
18
|
(time1 - time2).abs <= seconds_interval
|
|
17
19
|
end
|
|
18
|
-
|
|
20
|
+
|
|
19
21
|
def assert_times_effectively_equal(time1, time2, seconds_interval = 1, msg = nil)
|
|
20
22
|
assert times_effectively_equal(time1, time2, seconds_interval), "#{msg}: time1 = #{time1.to_s}, time2 = #{time2.to_s}"
|
|
21
23
|
end
|
|
22
|
-
|
|
24
|
+
|
|
23
25
|
def assert_times_effectively_not_equal(time1, time2, seconds_interval = 1, msg = nil)
|
|
24
26
|
assert !times_effectively_equal(time1, time2, seconds_interval), "#{msg}: time1 = #{time1.to_s}, time2 = #{time2.to_s}"
|
|
25
27
|
end
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
|
|
29
|
+
# Gets the local offset (supplied by ENV['TZ'] or your computer's clock)
|
|
30
|
+
# At the given timestamp, or Time.now if not time is given.
|
|
31
|
+
def local_offset(time = Time.now)
|
|
32
|
+
Time.at(time.to_i).to_datetime.offset
|
|
29
33
|
end
|
|
30
|
-
|
|
31
|
-
TIMEZONES = ["Europe/Paris", "UTC", "
|
|
32
|
-
|
|
34
|
+
|
|
35
|
+
TIMEZONES = ["Europe/Paris", "UTC", "America/Chicago"]
|
|
36
|
+
|
|
33
37
|
def each_timezone
|
|
34
38
|
old_tz = ENV["TZ"]
|
|
35
|
-
|
|
39
|
+
|
|
36
40
|
begin
|
|
37
41
|
TIMEZONES.each do |timezone|
|
|
38
42
|
ENV["TZ"] = timezone
|
|
@@ -42,13 +46,13 @@ class Test::Unit::TestCase
|
|
|
42
46
|
ENV["TZ"] = old_tz
|
|
43
47
|
end
|
|
44
48
|
end
|
|
45
|
-
|
|
49
|
+
|
|
46
50
|
def a_time_stack_item
|
|
47
51
|
Timecop::TimeStackItem.new(:freeze, 2008, 1, 1, 0, 0, 0)
|
|
48
52
|
end
|
|
49
|
-
|
|
53
|
+
|
|
50
54
|
def assert_date_times_equal(dt1, dt2)
|
|
51
55
|
assert_in_delta dt1.to_time.to_f, dt2.to_time.to_f, 0.01, "Failed for timezone: #{ENV['TZ']}: #{dt1.to_s} not equal to #{dt2.to_s}"
|
|
52
56
|
end
|
|
53
|
-
|
|
57
|
+
|
|
54
58
|
end
|
|
@@ -4,7 +4,7 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
|
|
4
4
|
|
|
5
5
|
require 'active_support/all'
|
|
6
6
|
|
|
7
|
-
class TestTimeStackItem <
|
|
7
|
+
class TestTimeStackItem < Minitest::Unit::TestCase
|
|
8
8
|
def teardown
|
|
9
9
|
Timecop.return
|
|
10
10
|
Time.zone = nil
|
|
@@ -204,16 +204,12 @@ class TestTimeStackItem < Test::Unit::TestCase
|
|
|
204
204
|
end
|
|
205
205
|
|
|
206
206
|
def test_parse_date
|
|
207
|
-
|
|
208
|
-
Timecop.freeze(Date.new(2012, 6, 9))
|
|
209
|
-
end
|
|
207
|
+
Timecop.freeze(Date.new(2012, 6, 9))
|
|
210
208
|
end
|
|
211
209
|
|
|
212
210
|
def test_time_zone_returns_nil
|
|
213
211
|
Time.zone = nil
|
|
214
|
-
|
|
215
|
-
Timecop.freeze
|
|
216
|
-
end
|
|
212
|
+
Timecop.freeze
|
|
217
213
|
end
|
|
218
214
|
|
|
219
215
|
def test_nsecs_are_set
|
|
@@ -273,7 +269,7 @@ class TestTimeStackItem < Test::Unit::TestCase
|
|
|
273
269
|
|
|
274
270
|
def test_datetime_timezones
|
|
275
271
|
dt = DateTime.new(2011,1,3,15,25,0,"-6")
|
|
276
|
-
Timecop.
|
|
272
|
+
Timecop.freeze(dt) do
|
|
277
273
|
now = DateTime.now
|
|
278
274
|
assert_equal dt, now, "#{dt.to_f}, #{now.to_f}"
|
|
279
275
|
end
|
data/test/timecop_test.rb
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
require 'date'
|
|
2
1
|
require File.join(File.dirname(__FILE__), "test_helper")
|
|
3
2
|
require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
|
4
3
|
|
|
5
|
-
class TestTimecop <
|
|
4
|
+
class TestTimecop < Minitest::Unit::TestCase
|
|
6
5
|
def teardown
|
|
7
6
|
Timecop.return
|
|
8
7
|
end
|
|
@@ -31,7 +30,7 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
31
30
|
assert_equal frozen_time, Time.now
|
|
32
31
|
end
|
|
33
32
|
end
|
|
34
|
-
|
|
33
|
+
|
|
35
34
|
def test_freeze_then_return_unsets_mock_time
|
|
36
35
|
Timecop.freeze(1)
|
|
37
36
|
Timecop.return
|
|
@@ -59,8 +58,11 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
59
58
|
def test_travel_does_not_reduce_precision_of_datetime
|
|
60
59
|
# requires to_r on Float (>= 1.9)
|
|
61
60
|
if Float.method_defined?(:to_r)
|
|
62
|
-
Timecop.travel(1)
|
|
63
|
-
|
|
61
|
+
Timecop.travel(Time.new(2014, 1, 1, 0, 0, 0))
|
|
62
|
+
assert DateTime.now != DateTime.now
|
|
63
|
+
|
|
64
|
+
Timecop.travel(Time.new(2014, 1, 1, 0, 0, 59))
|
|
65
|
+
assert DateTime.now != DateTime.now
|
|
64
66
|
end
|
|
65
67
|
end
|
|
66
68
|
|
|
@@ -113,7 +115,7 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
113
115
|
end
|
|
114
116
|
assert_equal t, Time.now
|
|
115
117
|
end
|
|
116
|
-
|
|
118
|
+
assert t != Time.now
|
|
117
119
|
end
|
|
118
120
|
|
|
119
121
|
def test_freeze_with_time_instance_works_as_expected
|
|
@@ -124,9 +126,9 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
124
126
|
assert_equal Date.new(2008, 10, 10), Date.today
|
|
125
127
|
end
|
|
126
128
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
129
|
+
assert t != Time.now
|
|
130
|
+
assert DateTime.new(2008, 10, 10, 10, 10, 10, local_offset) != DateTime.now
|
|
131
|
+
assert Date.new(2008, 10, 10) != Date.today
|
|
130
132
|
end
|
|
131
133
|
|
|
132
134
|
def test_freeze_with_datetime_on_specific_timezone_during_dst
|
|
@@ -185,9 +187,9 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
185
187
|
assert_equal Time.local(2008, 10, 10, 0, 0, 0), Time.now
|
|
186
188
|
assert_date_times_equal DateTime.new(2008, 10, 10, 0, 0, 0, local_offset), DateTime.now
|
|
187
189
|
end
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
190
|
+
assert d != Date.today
|
|
191
|
+
assert Time.local(2008, 10, 10, 0, 0, 0) != Time.now
|
|
192
|
+
assert DateTime.new(2008, 10, 10, 0, 0, 0, local_offset) != DateTime.now
|
|
191
193
|
end
|
|
192
194
|
|
|
193
195
|
def test_freeze_with_integer_instance_works_as_expected
|
|
@@ -202,9 +204,9 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
202
204
|
assert_equal Date.new(2008, 10, 10), Date.today
|
|
203
205
|
end
|
|
204
206
|
end
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
207
|
+
assert t != Time.now
|
|
208
|
+
assert DateTime.new(2008, 10, 10, 10, 10, 10) != DateTime.now
|
|
209
|
+
assert Date.new(2008, 10, 10) != Date.today
|
|
208
210
|
end
|
|
209
211
|
|
|
210
212
|
def test_exception_thrown_in_freeze_block_properly_resets_time
|
|
@@ -215,11 +217,19 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
215
217
|
raise "blah exception"
|
|
216
218
|
end
|
|
217
219
|
rescue
|
|
218
|
-
|
|
220
|
+
assert t != Time.now
|
|
219
221
|
assert_nil Time.send(:mock_time)
|
|
220
222
|
end
|
|
221
223
|
end
|
|
222
224
|
|
|
225
|
+
def test_exception_thrown_in_return_block_restores_previous_time
|
|
226
|
+
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
227
|
+
Timecop.freeze(t) do
|
|
228
|
+
Timecop.return { raise 'foobar' } rescue nil
|
|
229
|
+
assert_equal t, Time.now
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
223
233
|
def test_freeze_freezes_time
|
|
224
234
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
225
235
|
now = Time.now
|
|
@@ -249,6 +259,11 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
249
259
|
each_timezone do
|
|
250
260
|
t = DateTime.parse("2009-10-11 00:38:00 +0200")
|
|
251
261
|
Timecop.freeze(t) do
|
|
262
|
+
if ENV['TZ'] == 'UTC'
|
|
263
|
+
assert_equal(local_offset, 0, "Local offset not be zero for #{ENV['TZ']}")
|
|
264
|
+
else
|
|
265
|
+
assert(local_offset, 0 != "Local offset should not be zero for #{ENV['TZ']}")
|
|
266
|
+
end
|
|
252
267
|
assert_equal local_offset, DateTime.now.offset, "Failed for timezone: #{ENV['TZ']}"
|
|
253
268
|
end
|
|
254
269
|
end
|
|
@@ -279,6 +294,19 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
279
294
|
end
|
|
280
295
|
end
|
|
281
296
|
|
|
297
|
+
def test_freeze_without_arguments_instance_works_as_expected
|
|
298
|
+
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
299
|
+
Timecop.freeze(t) do
|
|
300
|
+
assert_equal t, Time.now
|
|
301
|
+
Timecop.freeze do
|
|
302
|
+
assert_equal t, Time.now
|
|
303
|
+
assert_equal Time.local(2008, 10, 10, 10, 10, 10), Time.now
|
|
304
|
+
assert_equal Date.new(2008, 10, 10), Date.today
|
|
305
|
+
end
|
|
306
|
+
end
|
|
307
|
+
assert t != Time.now
|
|
308
|
+
end
|
|
309
|
+
|
|
282
310
|
def test_destructive_methods_on_frozen_time
|
|
283
311
|
# Use any time zone other than UTC.
|
|
284
312
|
ENV['TZ'] = 'EST'
|
|
@@ -309,13 +337,13 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
309
337
|
end
|
|
310
338
|
|
|
311
339
|
def test_recursive_travel_yields_correct_time
|
|
312
|
-
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
|
340
|
+
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
|
313
341
|
Timecop.travel(2008, 9, 9, 9, 9, 9) do |inner_freeze|
|
|
314
342
|
assert_times_effectively_equal inner_freeze, Time.now, 1, "Failed to yield current time back to block"
|
|
315
343
|
end
|
|
316
344
|
end
|
|
317
345
|
end
|
|
318
|
-
|
|
346
|
+
|
|
319
347
|
def test_recursive_travel_then_freeze
|
|
320
348
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
321
349
|
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
|
@@ -437,7 +465,7 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
437
465
|
end
|
|
438
466
|
|
|
439
467
|
def test_not_callable_send_travel
|
|
440
|
-
|
|
468
|
+
assert_raises NoMethodError do
|
|
441
469
|
Timecop.send_travel(:travel, Time.now - 100)
|
|
442
470
|
end
|
|
443
471
|
end
|
|
@@ -461,7 +489,7 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
461
489
|
|
|
462
490
|
def test_raises_when_safe_mode_and_no_block
|
|
463
491
|
with_safe_mode do
|
|
464
|
-
|
|
492
|
+
assert_raises Timecop::SafeModeException do
|
|
465
493
|
Timecop.freeze
|
|
466
494
|
end
|
|
467
495
|
end
|
|
@@ -469,17 +497,13 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
469
497
|
|
|
470
498
|
def test_no_raise_when_safe_mode_and_block_used
|
|
471
499
|
with_safe_mode do
|
|
472
|
-
|
|
473
|
-
Timecop.freeze {}
|
|
474
|
-
end
|
|
500
|
+
Timecop.freeze {}
|
|
475
501
|
end
|
|
476
502
|
end
|
|
477
503
|
|
|
478
504
|
def test_no_raise_when_not_safe_mode_and_no_block
|
|
479
505
|
with_safe_mode(false) do
|
|
480
|
-
|
|
481
|
-
Timecop.freeze
|
|
482
|
-
end
|
|
506
|
+
Timecop.freeze
|
|
483
507
|
end
|
|
484
508
|
end
|
|
485
509
|
|
|
@@ -489,6 +513,29 @@ class TestTimecop < Test::Unit::TestCase
|
|
|
489
513
|
end
|
|
490
514
|
end
|
|
491
515
|
|
|
516
|
+
def test_date_strptime_without_specifying_format
|
|
517
|
+
Timecop.freeze(Time.new(1984,2,28)) do
|
|
518
|
+
assert_equal Date.strptime('1999-04-14'), Date.new(1999, 4, 14)
|
|
519
|
+
end
|
|
520
|
+
end
|
|
521
|
+
|
|
522
|
+
def test_frozen_after_freeze
|
|
523
|
+
Timecop.freeze
|
|
524
|
+
assert Timecop.frozen?
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
def test_frozen_inside_freeze
|
|
528
|
+
Timecop.freeze do
|
|
529
|
+
assert Timecop.frozen?
|
|
530
|
+
end
|
|
531
|
+
end
|
|
532
|
+
|
|
533
|
+
def test_not_frozen_after_return
|
|
534
|
+
Timecop.freeze
|
|
535
|
+
Timecop.return
|
|
536
|
+
assert !Timecop.frozen?
|
|
537
|
+
end
|
|
538
|
+
|
|
492
539
|
private
|
|
493
540
|
|
|
494
541
|
def with_safe_mode(enabled=true)
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
require "
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
2
|
|
|
3
|
-
class TestTimecopWithoutDateButWithTime <
|
|
3
|
+
class TestTimecopWithoutDateButWithTime < Minitest::Unit::TestCase
|
|
4
4
|
TIMECOP_LIB = File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
|
5
5
|
|
|
6
6
|
def test_loads_properly_when_time_is_required_instead_of_date
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
require TIMECOP_LIB
|
|
10
|
-
end
|
|
7
|
+
require "time"
|
|
8
|
+
require TIMECOP_LIB
|
|
11
9
|
end
|
|
12
10
|
end
|
|
@@ -1,34 +1,33 @@
|
|
|
1
|
-
|
|
2
1
|
require File.join(File.dirname(__FILE__), "test_helper")
|
|
3
2
|
require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
|
4
3
|
|
|
5
|
-
class TestTimecopWithoutDate <
|
|
6
|
-
|
|
4
|
+
class TestTimecopWithoutDate < Minitest::Unit::TestCase
|
|
5
|
+
|
|
7
6
|
def setup
|
|
8
7
|
Object.send(:remove_const, :Date) if Object.const_defined?(:Date)
|
|
9
8
|
Object.send(:remove_const, :DateTime) if Object.const_defined?(:DateTime)
|
|
10
9
|
end
|
|
11
|
-
|
|
10
|
+
|
|
12
11
|
# just in case...let's really make sure that Timecop is disabled between tests...
|
|
13
12
|
def teardown
|
|
14
13
|
Timecop.return
|
|
15
14
|
end
|
|
16
|
-
|
|
15
|
+
|
|
17
16
|
def test_freeze_changes_and_resets_time
|
|
18
17
|
# depending on how we're invoked (individually or via the rake test suite)
|
|
19
18
|
assert !Time.respond_to?(:zone) || Time.zone.nil?
|
|
20
|
-
|
|
19
|
+
|
|
21
20
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
22
|
-
|
|
21
|
+
assert t != Time.now
|
|
23
22
|
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
|
24
23
|
assert_equal t, Time.now
|
|
25
24
|
end
|
|
26
|
-
|
|
25
|
+
assert t != Time.now
|
|
27
26
|
end
|
|
28
|
-
|
|
27
|
+
|
|
29
28
|
def test_recursive_freeze
|
|
30
29
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
31
|
-
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
|
30
|
+
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
|
32
31
|
assert_equal t, Time.now
|
|
33
32
|
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
|
34
33
|
Timecop.freeze(2008, 9, 9, 9, 9, 9) do
|
|
@@ -38,7 +37,7 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
|
38
37
|
end
|
|
39
38
|
assert_nil Time.send(:mock_time)
|
|
40
39
|
end
|
|
41
|
-
|
|
40
|
+
|
|
42
41
|
def test_exception_thrown_in_freeze_block_properly_resets_time
|
|
43
42
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
44
43
|
begin
|
|
@@ -47,11 +46,11 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
|
47
46
|
raise "blah exception"
|
|
48
47
|
end
|
|
49
48
|
rescue
|
|
50
|
-
|
|
49
|
+
assert t != Time.now
|
|
51
50
|
assert_nil Time.send(:mock_time)
|
|
52
51
|
end
|
|
53
52
|
end
|
|
54
|
-
|
|
53
|
+
|
|
55
54
|
def test_freeze_freezes_time
|
|
56
55
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
57
56
|
now = Time.now
|
|
@@ -63,7 +62,7 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
|
63
62
|
assert_equal new_t, Time.now
|
|
64
63
|
end
|
|
65
64
|
end
|
|
66
|
-
|
|
65
|
+
|
|
67
66
|
def test_travel_keeps_time_moving
|
|
68
67
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
69
68
|
now = Time.now
|
|
@@ -74,10 +73,10 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
|
74
73
|
assert_times_effectively_not_equal new_now, Time.now, 0.25, "Looks like time is not moving"
|
|
75
74
|
end
|
|
76
75
|
end
|
|
77
|
-
|
|
76
|
+
|
|
78
77
|
def test_recursive_travel_maintains_each_context
|
|
79
78
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
80
|
-
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
|
79
|
+
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
|
81
80
|
assert((t - Time.now).abs < 50, "Failed to travel time.")
|
|
82
81
|
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
|
83
82
|
Timecop.travel(2008, 9, 9, 9, 9, 9) do
|
|
@@ -88,10 +87,10 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
|
88
87
|
end
|
|
89
88
|
assert_nil Time.send(:mock_time)
|
|
90
89
|
end
|
|
91
|
-
|
|
90
|
+
|
|
92
91
|
def test_recursive_travel_then_freeze
|
|
93
92
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
94
|
-
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
|
93
|
+
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
|
95
94
|
assert((t - Time.now).abs < 50, "Failed to travel time.")
|
|
96
95
|
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
|
97
96
|
Timecop.freeze(2008, 9, 9, 9, 9, 9) do
|
|
@@ -101,10 +100,10 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
|
101
100
|
end
|
|
102
101
|
assert_nil Time.send(:mock_time)
|
|
103
102
|
end
|
|
104
|
-
|
|
103
|
+
|
|
105
104
|
def test_recursive_freeze_then_travel
|
|
106
105
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
107
|
-
Timecop.freeze(t) do
|
|
106
|
+
Timecop.freeze(t) do
|
|
108
107
|
assert_equal t, Time.now
|
|
109
108
|
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
|
110
109
|
Timecop.travel(t2) do
|
|
@@ -113,7 +112,7 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
|
113
112
|
end
|
|
114
113
|
assert_equal t, Time.now
|
|
115
114
|
end
|
|
116
|
-
assert_nil Time.send(:mock_time)
|
|
115
|
+
assert_nil Time.send(:mock_time)
|
|
117
116
|
end
|
|
118
117
|
|
|
119
118
|
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
|
+
version: 0.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Travis Jeffery
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date:
|
|
12
|
+
date: 2015-07-28 00:00:00.000000000 Z
|
|
13
13
|
dependencies: []
|
|
14
14
|
description: A gem providing "time travel" and "time freezing" capabilities, making
|
|
15
15
|
it dead simple to test time-dependent code. It provides a unified method to mock
|
|
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
56
56
|
version: '0'
|
|
57
57
|
requirements: []
|
|
58
58
|
rubyforge_project: timecop
|
|
59
|
-
rubygems_version: 2.0.
|
|
59
|
+
rubygems_version: 2.0.14
|
|
60
60
|
signing_key:
|
|
61
61
|
specification_version: 3
|
|
62
62
|
summary: A gem providing "time travel" and "time freezing" capabilities, making it
|