timecop 0.8.0 → 0.9.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/README.markdown +19 -12
- data/Rakefile +9 -3
- data/lib/timecop/time_extensions.rb +76 -31
- data/lib/timecop/time_stack_item.rb +110 -107
- data/lib/timecop/timecop.rb +85 -27
- data/lib/timecop/version.rb +1 -1
- metadata +12 -24
- data/test/run_tests.sh +0 -10
- data/test/test_helper.rb +0 -58
- data/test/time_stack_item_test.rb +0 -277
- data/test/timecop_test.rb +0 -548
- data/test/timecop_without_date_but_with_time_test.rb +0 -10
- data/test/timecop_without_date_test.rb +0 -118
data/test/timecop_test.rb
DELETED
@@ -1,548 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
-
require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
3
|
-
|
4
|
-
class TestTimecop < Minitest::Unit::TestCase
|
5
|
-
def teardown
|
6
|
-
Timecop.return
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_freeze_changes_and_resets_time
|
10
|
-
outer_freeze_time = Time.local(2001, 01, 01)
|
11
|
-
inner_freeze_block = Time.local(2002, 02, 02)
|
12
|
-
inner_freeze_one = Time.local(2003, 03, 03)
|
13
|
-
inner_freeze_two = Time.local(2004, 04, 04)
|
14
|
-
|
15
|
-
Timecop.freeze(outer_freeze_time) do
|
16
|
-
assert_times_effectively_equal outer_freeze_time, Time.now
|
17
|
-
Timecop.freeze(inner_freeze_block) do
|
18
|
-
assert_times_effectively_equal inner_freeze_block, Time.now
|
19
|
-
Timecop.freeze(inner_freeze_one)
|
20
|
-
assert_times_effectively_equal inner_freeze_one, Time.now
|
21
|
-
Timecop.freeze(inner_freeze_two)
|
22
|
-
assert_times_effectively_equal inner_freeze_two, Time.now
|
23
|
-
end
|
24
|
-
assert_times_effectively_equal outer_freeze_time, Time.now
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_freeze_yields_mocked_time
|
29
|
-
Timecop.freeze(2008, 10, 10, 10, 10, 10) do |frozen_time|
|
30
|
-
assert_equal frozen_time, Time.now
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_freeze_then_return_unsets_mock_time
|
35
|
-
Timecop.freeze(1)
|
36
|
-
Timecop.return
|
37
|
-
assert_nil Time.send(:mock_time)
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_travel_then_return_unsets_mock_time
|
41
|
-
Timecop.travel(1)
|
42
|
-
Timecop.return
|
43
|
-
assert_nil Time.send(:mock_time)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_freeze_with_block_unsets_mock_time
|
47
|
-
assert_nil Time.send(:mock_time), "test is invalid"
|
48
|
-
Timecop.freeze(1) do; end
|
49
|
-
assert_nil Time.send(:mock_time)
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_travel_with_block_unsets_mock_time
|
53
|
-
assert_nil Time.send(:mock_time), "test is invalid"
|
54
|
-
Timecop.travel(1) do; end
|
55
|
-
assert_nil Time.send(:mock_time)
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_travel_does_not_reduce_precision_of_datetime
|
59
|
-
# requires to_r on Float (>= 1.9)
|
60
|
-
if Float.method_defined?(:to_r)
|
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
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_freeze_in_time_subclass_returns_mocked_subclass
|
70
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
71
|
-
custom_timeklass = Class.new(Time) do
|
72
|
-
def custom_format_method() strftime('%F') end
|
73
|
-
end
|
74
|
-
|
75
|
-
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
76
|
-
assert custom_timeklass.now.is_a? custom_timeklass
|
77
|
-
assert Time.now.eql? custom_timeklass.now
|
78
|
-
assert custom_timeklass.now.respond_to? :custom_format_method
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_freeze_in_date_subclass_returns_mocked_subclass
|
83
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
84
|
-
custom_dateklass = Class.new(Date) do
|
85
|
-
def custom_format_method() strftime('%F') end
|
86
|
-
end
|
87
|
-
|
88
|
-
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
89
|
-
assert custom_dateklass.today.is_a? custom_dateklass
|
90
|
-
assert Date.today.eql? custom_dateklass.today
|
91
|
-
assert custom_dateklass.today.respond_to? :custom_format_method
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_freeze_in_datetime_subclass_returns_mocked_subclass
|
96
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
97
|
-
custom_datetimeklass = Class.new(DateTime) do
|
98
|
-
def custom_format_method() strftime('%F') end
|
99
|
-
end
|
100
|
-
|
101
|
-
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
102
|
-
assert custom_datetimeklass.now.is_a? custom_datetimeklass
|
103
|
-
assert DateTime.now.eql? custom_datetimeklass.now
|
104
|
-
assert custom_datetimeklass.now.respond_to? :custom_format_method
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_recursive_freeze
|
109
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
110
|
-
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
111
|
-
assert_equal t, Time.now
|
112
|
-
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
113
|
-
Timecop.freeze(2008, 9, 9, 9, 9, 9) do
|
114
|
-
assert_equal t2, Time.now
|
115
|
-
end
|
116
|
-
assert_equal t, Time.now
|
117
|
-
end
|
118
|
-
assert t != Time.now
|
119
|
-
end
|
120
|
-
|
121
|
-
def test_freeze_with_time_instance_works_as_expected
|
122
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
123
|
-
Timecop.freeze(t) do
|
124
|
-
assert_equal t, Time.now
|
125
|
-
assert_date_times_equal DateTime.new(2008, 10, 10, 10, 10, 10, local_offset), DateTime.now
|
126
|
-
assert_equal Date.new(2008, 10, 10), Date.today
|
127
|
-
end
|
128
|
-
|
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
|
132
|
-
end
|
133
|
-
|
134
|
-
def test_freeze_with_datetime_on_specific_timezone_during_dst
|
135
|
-
each_timezone do
|
136
|
-
# Start from a time that is subject to DST
|
137
|
-
Timecop.freeze(2009, 9, 1)
|
138
|
-
# Travel to a DateTime that is also in DST
|
139
|
-
t = DateTime.parse("2009-10-11 00:38:00 +0200")
|
140
|
-
Timecop.freeze(t) do
|
141
|
-
assert_date_times_equal t, DateTime.now
|
142
|
-
end
|
143
|
-
Timecop.return
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
def test_freeze_with_datetime_on_specific_timezone_not_during_dst
|
148
|
-
each_timezone do
|
149
|
-
# Start from a time that is not subject to DST
|
150
|
-
Timecop.freeze(2009, 12, 1)
|
151
|
-
# Travel to a time that is also not in DST
|
152
|
-
t = DateTime.parse("2009-12-11 00:38:00 +0100")
|
153
|
-
Timecop.freeze(t) do
|
154
|
-
assert_date_times_equal t, DateTime.now
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
def test_freeze_with_datetime_from_a_non_dst_time_to_a_dst_time
|
160
|
-
each_timezone do
|
161
|
-
# Start from a time that is not subject to DST
|
162
|
-
Timecop.freeze(DateTime.parse("2009-12-1 00:00:00 +0100"))
|
163
|
-
# Travel back to a time in DST
|
164
|
-
t = DateTime.parse("2009-10-11 00:38:00 +0200")
|
165
|
-
Timecop.freeze(t) do
|
166
|
-
assert_date_times_equal t, DateTime.now
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
def test_freeze_with_datetime_from_a_dst_time_to_a_non_dst_time
|
172
|
-
each_timezone do
|
173
|
-
# Start from a time that is not subject to DST
|
174
|
-
Timecop.freeze(DateTime.parse("2009-10-11 00:00:00 +0200"))
|
175
|
-
# Travel back to a time in DST
|
176
|
-
t = DateTime.parse("2009-12-1 00:38:00 +0100")
|
177
|
-
Timecop.freeze(t) do
|
178
|
-
assert_date_times_equal t, DateTime.now
|
179
|
-
end
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
def test_freeze_with_date_instance_works_as_expected
|
184
|
-
d = Date.new(2008, 10, 10)
|
185
|
-
Timecop.freeze(d) do
|
186
|
-
assert_equal d, Date.today
|
187
|
-
assert_equal Time.local(2008, 10, 10, 0, 0, 0), Time.now
|
188
|
-
assert_date_times_equal DateTime.new(2008, 10, 10, 0, 0, 0, local_offset), DateTime.now
|
189
|
-
end
|
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
|
193
|
-
end
|
194
|
-
|
195
|
-
def test_freeze_with_integer_instance_works_as_expected
|
196
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
197
|
-
Timecop.freeze(t) do
|
198
|
-
assert_equal t, Time.now
|
199
|
-
assert_date_times_equal DateTime.new(2008, 10, 10, 10, 10, 10, local_offset), DateTime.now
|
200
|
-
assert_equal Date.new(2008, 10, 10), Date.today
|
201
|
-
Timecop.freeze(10) do
|
202
|
-
assert_equal t + 10, Time.now
|
203
|
-
assert_equal Time.local(2008, 10, 10, 10, 10, 20), Time.now
|
204
|
-
assert_equal Date.new(2008, 10, 10), Date.today
|
205
|
-
end
|
206
|
-
end
|
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
|
210
|
-
end
|
211
|
-
|
212
|
-
def test_exception_thrown_in_freeze_block_properly_resets_time
|
213
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
214
|
-
begin
|
215
|
-
Timecop.freeze(t) do
|
216
|
-
assert_equal t, Time.now
|
217
|
-
raise "blah exception"
|
218
|
-
end
|
219
|
-
rescue
|
220
|
-
assert t != Time.now
|
221
|
-
assert_nil Time.send(:mock_time)
|
222
|
-
end
|
223
|
-
end
|
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
|
-
|
233
|
-
def test_freeze_freezes_time
|
234
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
235
|
-
now = Time.now
|
236
|
-
Timecop.freeze(t) do
|
237
|
-
#assert Time.now < now, "If we had failed to freeze, time would have proceeded, which is what appears to have happened."
|
238
|
-
new_t, new_d, new_dt = Time.now, Date.today, DateTime.now
|
239
|
-
assert_equal t, new_t, "Failed to freeze time." # 2 seconds
|
240
|
-
#sleep(10)
|
241
|
-
assert_equal new_t, Time.now
|
242
|
-
assert_equal new_d, Date.today
|
243
|
-
assert_equal new_dt, DateTime.now
|
244
|
-
end
|
245
|
-
end
|
246
|
-
|
247
|
-
def test_travel_keeps_time_moving
|
248
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
249
|
-
now = Time.now
|
250
|
-
Timecop.travel(t) do
|
251
|
-
new_now = Time.now
|
252
|
-
assert_times_effectively_equal(new_now, t, 1, "Looks like we failed to actually travel time")
|
253
|
-
sleep(0.25)
|
254
|
-
assert_times_effectively_not_equal new_now, Time.now, 0.25, "Looks like time is not moving"
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
def test_mocked_date_time_now_is_local
|
259
|
-
each_timezone do
|
260
|
-
t = DateTime.parse("2009-10-11 00:38:00 +0200")
|
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
|
267
|
-
assert_equal local_offset, DateTime.now.offset, "Failed for timezone: #{ENV['TZ']}"
|
268
|
-
end
|
269
|
-
end
|
270
|
-
end
|
271
|
-
|
272
|
-
def test_scaling_keeps_time_moving_at_an_accelerated_rate
|
273
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
274
|
-
Timecop.scale(4, t) do
|
275
|
-
start = Time.now
|
276
|
-
assert_times_effectively_equal start, t, 1, "Looks like we failed to actually travel time"
|
277
|
-
sleep(0.25)
|
278
|
-
assert_times_effectively_equal Time.at((start + 4*0.25).to_f), Time.now, 0.25, "Looks like time is not moving at 4x"
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
def test_scaling_returns_now_if_no_block_given
|
283
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
284
|
-
assert_times_effectively_equal t, Timecop.scale(4, t)
|
285
|
-
end
|
286
|
-
|
287
|
-
def test_freeze_with_utc_time
|
288
|
-
each_timezone do
|
289
|
-
t = Time.utc(2008, 10, 10, 10, 10, 10)
|
290
|
-
local = t.getlocal
|
291
|
-
Timecop.freeze(t) do
|
292
|
-
assert_equal local, Time.now, "Failed for timezone: #{ENV['TZ']}"
|
293
|
-
end
|
294
|
-
end
|
295
|
-
end
|
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
|
-
|
310
|
-
def test_destructive_methods_on_frozen_time
|
311
|
-
# Use any time zone other than UTC.
|
312
|
-
ENV['TZ'] = 'EST'
|
313
|
-
|
314
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
315
|
-
Timecop.freeze(t) do
|
316
|
-
assert !Time.now.utc?, "Time#local failed to return a time in the local time zone."
|
317
|
-
|
318
|
-
# #utc, #gmt, and #localtime are destructive methods.
|
319
|
-
Time.now.utc
|
320
|
-
|
321
|
-
assert !Time.now.utc?, "Failed to thwart destructive methods."
|
322
|
-
end
|
323
|
-
end
|
324
|
-
|
325
|
-
def test_recursive_travel_maintains_each_context
|
326
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
327
|
-
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
328
|
-
assert((t - Time.now).abs < 50, "Failed to travel time.")
|
329
|
-
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
330
|
-
Timecop.travel(2008, 9, 9, 9, 9, 9) do
|
331
|
-
assert_times_effectively_equal(t2, Time.now, 1, "Failed to travel time.")
|
332
|
-
assert_times_effectively_not_equal(t, Time.now, 1000, "Failed to travel time.")
|
333
|
-
end
|
334
|
-
assert_times_effectively_equal(t, Time.now, 2, "Failed to restore previously-traveled time.")
|
335
|
-
end
|
336
|
-
assert_nil Time.send(:mock_time)
|
337
|
-
end
|
338
|
-
|
339
|
-
def test_recursive_travel_yields_correct_time
|
340
|
-
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
341
|
-
Timecop.travel(2008, 9, 9, 9, 9, 9) do |inner_freeze|
|
342
|
-
assert_times_effectively_equal inner_freeze, Time.now, 1, "Failed to yield current time back to block"
|
343
|
-
end
|
344
|
-
end
|
345
|
-
end
|
346
|
-
|
347
|
-
def test_recursive_travel_then_freeze
|
348
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
349
|
-
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
350
|
-
assert((t - Time.now).abs < 50, "Failed to travel time.")
|
351
|
-
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
352
|
-
Timecop.freeze(2008, 9, 9, 9, 9, 9) do
|
353
|
-
assert_equal t2, Time.now
|
354
|
-
end
|
355
|
-
assert_times_effectively_equal(t, Time.now, 2, "Failed to restore previously-traveled time.")
|
356
|
-
end
|
357
|
-
assert_nil Time.send(:mock_time)
|
358
|
-
end
|
359
|
-
|
360
|
-
def test_recursive_freeze_then_travel
|
361
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
362
|
-
Timecop.freeze(t) do
|
363
|
-
assert_equal t, Time.now
|
364
|
-
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
365
|
-
Timecop.travel(t2) do
|
366
|
-
assert_times_effectively_equal(t2, Time.now, 1, "Failed to travel time.")
|
367
|
-
assert_times_effectively_not_equal(t, Time.now, 1000, "Failed to travel time.")
|
368
|
-
end
|
369
|
-
assert_equal t, Time.now
|
370
|
-
end
|
371
|
-
assert_nil Time.send(:mock_time)
|
372
|
-
end
|
373
|
-
|
374
|
-
def test_travel_time_returns_now_if_no_block_given
|
375
|
-
t_future = Time.local(2030, 10, 10, 10, 10, 10)
|
376
|
-
assert_times_effectively_equal t_future, Timecop.travel(t_future)
|
377
|
-
end
|
378
|
-
|
379
|
-
def test_return_temporarily_returns_to_current_time_in_given_block
|
380
|
-
time_after_travel = Time.local(1990, 7, 16)
|
381
|
-
now = Time.now
|
382
|
-
|
383
|
-
Timecop.travel(time_after_travel)
|
384
|
-
|
385
|
-
assert_times_effectively_equal(time_after_travel, Time.now)
|
386
|
-
Timecop.return do
|
387
|
-
assert_times_effectively_equal(now, Time.now)
|
388
|
-
end
|
389
|
-
assert_times_effectively_equal(time_after_travel, Time.now)
|
390
|
-
end
|
391
|
-
|
392
|
-
def test_travel_time_with_block_returns_the_value_of_the_block
|
393
|
-
t_future = Time.local(2030, 10, 10, 10, 10, 10)
|
394
|
-
expected = :foo
|
395
|
-
actual = Timecop.travel(t_future) { expected }
|
396
|
-
|
397
|
-
assert_equal expected, actual
|
398
|
-
end
|
399
|
-
|
400
|
-
def test_freeze_time_returns_now_if_no_block_given
|
401
|
-
t_future = Time.local(2030, 10, 10, 10, 10, 10)
|
402
|
-
assert_times_effectively_equal t_future, Timecop.freeze(t_future)
|
403
|
-
end
|
404
|
-
|
405
|
-
def test_freeze_time_with_block_returns_the_value_of_the_block
|
406
|
-
t_future = Time.local(2030, 10, 10, 10, 10, 10)
|
407
|
-
expected = :foo
|
408
|
-
actual = Timecop.freeze(t_future) { expected }
|
409
|
-
|
410
|
-
assert_equal expected, actual
|
411
|
-
end
|
412
|
-
|
413
|
-
def test_return_returns_nil
|
414
|
-
assert_nil Timecop.return
|
415
|
-
end
|
416
|
-
|
417
|
-
def test_freeze_without_params
|
418
|
-
Timecop.freeze 1 do
|
419
|
-
current_time = Time.now
|
420
|
-
Timecop.freeze do
|
421
|
-
assert_equal Time.now, current_time
|
422
|
-
end
|
423
|
-
end
|
424
|
-
end
|
425
|
-
|
426
|
-
def test_freeze_with_new_date
|
427
|
-
date = Date.new(2012, 6, 9)
|
428
|
-
Timecop.freeze(Date.new(2012, 6, 9)) do
|
429
|
-
assert_equal date, Time.now.__send__(:to_date)
|
430
|
-
end
|
431
|
-
end
|
432
|
-
|
433
|
-
def test_return_to_baseline_without_a_baseline_set_returns_to_current_time
|
434
|
-
time_before_travel = Time.now
|
435
|
-
Timecop.travel Time.now - 60
|
436
|
-
Timecop.return_to_baseline
|
437
|
-
assert times_effectively_equal(time_before_travel, Time.now)
|
438
|
-
end
|
439
|
-
|
440
|
-
def test_return_to_baseline_with_a_baseline_set_returns_to_baseline
|
441
|
-
baseline = Time.local(1945, 10, 10, 10, 10, 10)
|
442
|
-
Timecop.baseline = baseline
|
443
|
-
Timecop.travel Time.now - 60
|
444
|
-
time_now = Timecop.return_to_baseline
|
445
|
-
assert times_effectively_equal(baseline, time_now),
|
446
|
-
"expected to return to #{baseline}, but returned to #{time_now}"
|
447
|
-
end
|
448
|
-
|
449
|
-
def test_return_eliminates_baseline
|
450
|
-
time_before_travel = Time.now
|
451
|
-
Timecop.baseline = Time.local(1937, 9, 9, 9, 9, 9)
|
452
|
-
Timecop.return
|
453
|
-
assert times_effectively_equal(time_before_travel, Time.now)
|
454
|
-
|
455
|
-
Timecop.travel(Time.now - 100)
|
456
|
-
Timecop.return_to_baseline
|
457
|
-
assert times_effectively_equal(time_before_travel, Time.now)
|
458
|
-
end
|
459
|
-
|
460
|
-
def test_mock_time_new_same_as_now
|
461
|
-
date = Time.local(2011, 01, 02)
|
462
|
-
Timecop.freeze date
|
463
|
-
assert_equal date, Time.now
|
464
|
-
assert_equal date, Time.new
|
465
|
-
end
|
466
|
-
|
467
|
-
def test_not_callable_send_travel
|
468
|
-
assert_raises NoMethodError do
|
469
|
-
Timecop.send_travel(:travel, Time.now - 100)
|
470
|
-
end
|
471
|
-
end
|
472
|
-
|
473
|
-
def test_datetime_to_time_for_dst_to_non_dst
|
474
|
-
# Start at a time subject to DST
|
475
|
-
Timecop.travel(2009, 4, 1, 0, 0, 0, -4*60*60) do
|
476
|
-
|
477
|
-
# Then freeze, via DateTime, at a time not subject to DST
|
478
|
-
t = DateTime.new(2009,01,01,0,0,0, "-0500")
|
479
|
-
Timecop.freeze(t) do
|
480
|
-
|
481
|
-
# Check the current time via DateTime.now--should be what we asked for
|
482
|
-
assert_date_times_equal t, DateTime.now
|
483
|
-
|
484
|
-
# Then check the current time via Time.now (not DateTime.now)
|
485
|
-
assert_times_effectively_equal Time.new(2009, 1, 1, 0, 0, 0, -5*60*60), Time.now
|
486
|
-
end
|
487
|
-
end
|
488
|
-
end
|
489
|
-
|
490
|
-
def test_raises_when_safe_mode_and_no_block
|
491
|
-
with_safe_mode do
|
492
|
-
assert_raises Timecop::SafeModeException do
|
493
|
-
Timecop.freeze
|
494
|
-
end
|
495
|
-
end
|
496
|
-
end
|
497
|
-
|
498
|
-
def test_no_raise_when_safe_mode_and_block_used
|
499
|
-
with_safe_mode do
|
500
|
-
Timecop.freeze {}
|
501
|
-
end
|
502
|
-
end
|
503
|
-
|
504
|
-
def test_no_raise_when_not_safe_mode_and_no_block
|
505
|
-
with_safe_mode(false) do
|
506
|
-
Timecop.freeze
|
507
|
-
end
|
508
|
-
end
|
509
|
-
|
510
|
-
def test_date_strptime_without_year
|
511
|
-
Timecop.freeze(Time.new(1984,2,28)) do
|
512
|
-
assert_equal Date.strptime('04-14', '%m-%d'), Date.new(1984, 4, 14)
|
513
|
-
end
|
514
|
-
end
|
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
|
-
|
539
|
-
private
|
540
|
-
|
541
|
-
def with_safe_mode(enabled=true)
|
542
|
-
mode = Timecop.safe_mode?
|
543
|
-
Timecop.safe_mode = enabled
|
544
|
-
yield
|
545
|
-
ensure
|
546
|
-
Timecop.safe_mode = mode
|
547
|
-
end
|
548
|
-
end
|
@@ -1,10 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
-
|
3
|
-
class TestTimecopWithoutDateButWithTime < Minitest::Unit::TestCase
|
4
|
-
TIMECOP_LIB = File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
5
|
-
|
6
|
-
def test_loads_properly_when_time_is_required_instead_of_date
|
7
|
-
require "time"
|
8
|
-
require TIMECOP_LIB
|
9
|
-
end
|
10
|
-
end
|
@@ -1,118 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
-
require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
3
|
-
|
4
|
-
class TestTimecopWithoutDate < Minitest::Unit::TestCase
|
5
|
-
|
6
|
-
def setup
|
7
|
-
Object.send(:remove_const, :Date) if Object.const_defined?(:Date)
|
8
|
-
Object.send(:remove_const, :DateTime) if Object.const_defined?(:DateTime)
|
9
|
-
end
|
10
|
-
|
11
|
-
# just in case...let's really make sure that Timecop is disabled between tests...
|
12
|
-
def teardown
|
13
|
-
Timecop.return
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_freeze_changes_and_resets_time
|
17
|
-
# depending on how we're invoked (individually or via the rake test suite)
|
18
|
-
assert !Time.respond_to?(:zone) || Time.zone.nil?
|
19
|
-
|
20
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
21
|
-
assert t != Time.now
|
22
|
-
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
23
|
-
assert_equal t, Time.now
|
24
|
-
end
|
25
|
-
assert t != Time.now
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_recursive_freeze
|
29
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
30
|
-
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
31
|
-
assert_equal t, Time.now
|
32
|
-
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
33
|
-
Timecop.freeze(2008, 9, 9, 9, 9, 9) do
|
34
|
-
assert_equal t2, Time.now
|
35
|
-
end
|
36
|
-
assert_equal t, Time.now
|
37
|
-
end
|
38
|
-
assert_nil Time.send(:mock_time)
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_exception_thrown_in_freeze_block_properly_resets_time
|
42
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
43
|
-
begin
|
44
|
-
Timecop.freeze(t) do
|
45
|
-
assert_equal t, Time.now
|
46
|
-
raise "blah exception"
|
47
|
-
end
|
48
|
-
rescue
|
49
|
-
assert t != Time.now
|
50
|
-
assert_nil Time.send(:mock_time)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_freeze_freezes_time
|
55
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
56
|
-
now = Time.now
|
57
|
-
Timecop.freeze(t) do
|
58
|
-
sleep(0.25)
|
59
|
-
assert Time.now < now, "If we had failed to freeze, time would have proceeded, which is what appears to have happened."
|
60
|
-
new_t = Time.now
|
61
|
-
assert_equal t, new_t, "Failed to change move time." # 2 seconds
|
62
|
-
assert_equal new_t, Time.now
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_travel_keeps_time_moving
|
67
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
68
|
-
now = Time.now
|
69
|
-
Timecop.travel(t) do
|
70
|
-
new_now = Time.now
|
71
|
-
assert_times_effectively_equal new_now, t, 1, "Looks like we failed to actually travel time" # 0.1 seconds
|
72
|
-
sleep(0.25)
|
73
|
-
assert_times_effectively_not_equal new_now, Time.now, 0.25, "Looks like time is not moving"
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_recursive_travel_maintains_each_context
|
78
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
79
|
-
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
80
|
-
assert((t - Time.now).abs < 50, "Failed to travel time.")
|
81
|
-
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
82
|
-
Timecop.travel(2008, 9, 9, 9, 9, 9) do
|
83
|
-
assert_times_effectively_equal(t2, Time.now, 1, "Failed to travel time.")
|
84
|
-
assert_times_effectively_not_equal(t, Time.now, 1000, "Failed to travel time.")
|
85
|
-
end
|
86
|
-
assert_times_effectively_equal(t, Time.now, 2, "Failed to restore previously-traveled time.")
|
87
|
-
end
|
88
|
-
assert_nil Time.send(:mock_time)
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_recursive_travel_then_freeze
|
92
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
93
|
-
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
94
|
-
assert((t - Time.now).abs < 50, "Failed to travel time.")
|
95
|
-
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
96
|
-
Timecop.freeze(2008, 9, 9, 9, 9, 9) do
|
97
|
-
assert_equal t2, Time.now
|
98
|
-
end
|
99
|
-
assert_times_effectively_equal(t, Time.now, 2, "Failed to restore previously-traveled time.")
|
100
|
-
end
|
101
|
-
assert_nil Time.send(:mock_time)
|
102
|
-
end
|
103
|
-
|
104
|
-
def test_recursive_freeze_then_travel
|
105
|
-
t = Time.local(2008, 10, 10, 10, 10, 10)
|
106
|
-
Timecop.freeze(t) do
|
107
|
-
assert_equal t, Time.now
|
108
|
-
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
109
|
-
Timecop.travel(t2) do
|
110
|
-
assert_times_effectively_equal(t2, Time.now, 1, "Failed to travel time.")
|
111
|
-
assert_times_effectively_not_equal(t, Time.now, 1000, "Failed to travel time.")
|
112
|
-
end
|
113
|
-
assert_equal t, Time.now
|
114
|
-
end
|
115
|
-
assert_nil Time.send(:mock_time)
|
116
|
-
end
|
117
|
-
|
118
|
-
end
|