timecop 0.9.6 → 0.9.9

Sign up to get free protection for your applications and to get access to all the features.
data/test/timecop_test.rb DELETED
@@ -1,622 +0,0 @@
1
- require_relative "test_helper"
2
- require 'timecop'
3
-
4
- class TestTimecop < Minitest::Test
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_freeze_then_unfreeze_unsets_mock_time
41
- Timecop.freeze(1)
42
- Timecop.unfreeze
43
- assert_nil Time.send(:mock_time)
44
- end
45
-
46
- def test_travel_then_return_unsets_mock_time
47
- Timecop.travel(1)
48
- Timecop.return
49
- assert_nil Time.send(:mock_time)
50
- end
51
-
52
- def test_freeze_with_block_unsets_mock_time
53
- assert_nil Time.send(:mock_time), "test is invalid"
54
- Timecop.freeze(1) do; end
55
- assert_nil Time.send(:mock_time)
56
- end
57
-
58
- def test_travel_with_block_unsets_mock_time
59
- assert_nil Time.send(:mock_time), "test is invalid"
60
- Timecop.travel(1) do; end
61
- assert_nil Time.send(:mock_time)
62
- end
63
-
64
- def test_travel_does_not_reduce_precision_of_datetime
65
- # requires to_r on Float (>= 1.9)
66
- if Float.method_defined?(:to_r)
67
- Timecop.travel(Time.new(2014, 1, 1, 0, 0, 0))
68
- assert DateTime.now != DateTime.now
69
-
70
- Timecop.travel(Time.new(2014, 1, 1, 0, 0, 59))
71
- assert DateTime.now != DateTime.now
72
- end
73
- end
74
-
75
- def test_freeze_in_time_subclass_returns_mocked_subclass
76
- t = Time.local(2008, 10, 10, 10, 10, 10)
77
- custom_timeklass = Class.new(Time) do
78
- def custom_format_method() strftime('%F') end
79
- end
80
-
81
- Timecop.freeze(2008, 10, 10, 10, 10, 10) do
82
- assert custom_timeklass.now.is_a? custom_timeklass
83
- assert Time.now.eql? custom_timeklass.now
84
- assert custom_timeklass.now.respond_to? :custom_format_method
85
- end
86
- end
87
-
88
- def test_freeze_in_date_subclass_returns_mocked_subclass
89
- t = Time.local(2008, 10, 10, 10, 10, 10)
90
- custom_dateklass = Class.new(Date) do
91
- def custom_format_method() strftime('%F') end
92
- end
93
-
94
- Timecop.freeze(2008, 10, 10, 10, 10, 10) do
95
- assert custom_dateklass.today.is_a? custom_dateklass
96
- assert Date.today.eql? custom_dateklass.today
97
- assert custom_dateklass.today.respond_to? :custom_format_method
98
- end
99
- end
100
-
101
- def test_freeze_in_datetime_subclass_returns_mocked_subclass
102
- t = Time.local(2008, 10, 10, 10, 10, 10)
103
- custom_datetimeklass = Class.new(DateTime) do
104
- def custom_format_method() strftime('%F') end
105
- end
106
-
107
- Timecop.freeze(2008, 10, 10, 10, 10, 10) do
108
- assert custom_datetimeklass.now.is_a? custom_datetimeklass
109
- assert DateTime.now.eql? custom_datetimeklass.now
110
- assert custom_datetimeklass.now.respond_to? :custom_format_method
111
- end
112
- end
113
-
114
- def test_recursive_freeze
115
- t = Time.local(2008, 10, 10, 10, 10, 10)
116
- Timecop.freeze(2008, 10, 10, 10, 10, 10) do
117
- assert_equal t, Time.now
118
- t2 = Time.local(2008, 9, 9, 9, 9, 9)
119
- Timecop.freeze(2008, 9, 9, 9, 9, 9) do
120
- assert_equal t2, Time.now
121
- end
122
- assert_equal t, Time.now
123
- end
124
- assert t != Time.now
125
- end
126
-
127
- def test_freeze_with_time_instance_works_as_expected
128
- t = Time.local(2008, 10, 10, 10, 10, 10)
129
- Timecop.freeze(t) do
130
- assert_equal t, Time.now
131
- assert_date_times_equal DateTime.new(2008, 10, 10, 10, 10, 10, local_offset), DateTime.now
132
- assert_equal Date.new(2008, 10, 10), Date.today
133
- end
134
-
135
- assert t != Time.now
136
- assert DateTime.new(2008, 10, 10, 10, 10, 10, local_offset) != DateTime.now
137
- assert Date.new(2008, 10, 10) != Date.today
138
- end
139
-
140
- def test_freeze_with_datetime_on_specific_timezone_during_dst
141
- each_timezone do
142
- # Start from a time that is subject to DST
143
- Timecop.freeze(2009, 9, 1)
144
- # Travel to a DateTime that is also in DST
145
- t = DateTime.parse("2009-10-11 00:38:00 +0200")
146
- Timecop.freeze(t) do
147
- assert_date_times_equal t, DateTime.now
148
- end
149
- Timecop.return
150
- end
151
- end
152
-
153
- def test_freeze_with_datetime_on_specific_timezone_not_during_dst
154
- each_timezone do
155
- # Start from a time that is not subject to DST
156
- Timecop.freeze(2009, 12, 1)
157
- # Travel to a time that is also not in DST
158
- t = DateTime.parse("2009-12-11 00:38:00 +0100")
159
- Timecop.freeze(t) do
160
- assert_date_times_equal t, DateTime.now
161
- end
162
- end
163
- end
164
-
165
- def test_freeze_with_datetime_from_a_non_dst_time_to_a_dst_time
166
- each_timezone do
167
- # Start from a time that is not subject to DST
168
- Timecop.freeze(DateTime.parse("2009-12-1 00:00:00 +0100"))
169
- # Travel back to a time in DST
170
- t = DateTime.parse("2009-10-11 00:38:00 +0200")
171
- Timecop.freeze(t) do
172
- assert_date_times_equal t, DateTime.now
173
- end
174
- end
175
- end
176
-
177
- def test_freeze_with_datetime_from_a_dst_time_to_a_non_dst_time
178
- each_timezone do
179
- # Start from a time that is not subject to DST
180
- Timecop.freeze(DateTime.parse("2009-10-11 00:00:00 +0200"))
181
- # Travel back to a time in DST
182
- t = DateTime.parse("2009-12-1 00:38:00 +0100")
183
- Timecop.freeze(t) do
184
- assert_date_times_equal t, DateTime.now
185
- end
186
- end
187
- end
188
-
189
- def test_freeze_with_date_instance_works_as_expected
190
- d = Date.new(2008, 10, 10)
191
- Timecop.freeze(d) do
192
- assert_equal d, Date.today
193
- assert_equal Time.local(2008, 10, 10, 0, 0, 0), Time.now
194
- assert_date_times_equal DateTime.new(2008, 10, 10, 0, 0, 0, local_offset), DateTime.now
195
- end
196
- assert d != Date.today
197
- assert Time.local(2008, 10, 10, 0, 0, 0) != Time.now
198
- assert DateTime.new(2008, 10, 10, 0, 0, 0, local_offset) != DateTime.now
199
- end
200
-
201
- def test_freeze_with_integer_instance_works_as_expected
202
- t = Time.local(2008, 10, 10, 10, 10, 10)
203
- Timecop.freeze(t) do
204
- assert_equal t, Time.now
205
- assert_date_times_equal DateTime.new(2008, 10, 10, 10, 10, 10, local_offset), DateTime.now
206
- assert_equal Date.new(2008, 10, 10), Date.today
207
- Timecop.freeze(10) do
208
- assert_equal t + 10, Time.now
209
- assert_equal Time.local(2008, 10, 10, 10, 10, 20), Time.now
210
- assert_equal Date.new(2008, 10, 10), Date.today
211
- end
212
- end
213
- assert t != Time.now
214
- assert DateTime.new(2008, 10, 10, 10, 10, 10) != DateTime.now
215
- assert Date.new(2008, 10, 10) != Date.today
216
- end
217
-
218
- def test_exception_thrown_in_freeze_block_properly_resets_time
219
- t = Time.local(2008, 10, 10, 10, 10, 10)
220
- begin
221
- Timecop.freeze(t) do
222
- assert_equal t, Time.now
223
- raise "blah exception"
224
- end
225
- rescue
226
- assert t != Time.now
227
- assert_nil Time.send(:mock_time)
228
- end
229
- end
230
-
231
- def test_exception_thrown_in_return_block_restores_previous_time
232
- t = Time.local(2008, 10, 10, 10, 10, 10)
233
- Timecop.freeze(t) do
234
- Timecop.return { raise 'foobar' } rescue nil
235
- assert_equal t, Time.now
236
- end
237
- end
238
-
239
- def test_freeze_freezes_time
240
- t = Time.local(2008, 10, 10, 10, 10, 10)
241
- now = Time.now
242
- Timecop.freeze(t) do
243
- #assert Time.now < now, "If we had failed to freeze, time would have proceeded, which is what appears to have happened."
244
- new_t, new_d, new_dt = Time.now, Date.today, DateTime.now
245
- assert_equal t, new_t, "Failed to freeze time." # 2 seconds
246
- #sleep(10)
247
- assert_equal new_t, Time.now
248
- assert_equal new_d, Date.today
249
- assert_equal new_dt, DateTime.now
250
- end
251
- end
252
-
253
- def test_travel_keeps_time_moving
254
- t = Time.local(2008, 10, 10, 10, 10, 10)
255
- now = Time.now
256
- Timecop.travel(t) do
257
- new_now = Time.now
258
- assert_times_effectively_equal(new_now, t, 1, "Looks like we failed to actually travel time")
259
- sleep(0.25)
260
- assert_times_effectively_not_equal new_now, Time.now, 0.24, "Looks like time is not moving"
261
- end
262
- end
263
-
264
- def test_mocked_date_time_now_is_local
265
- each_timezone do
266
- t = DateTime.parse("2009-10-11 00:38:00 +0200")
267
- Timecop.freeze(t) do
268
- if ENV['TZ'] == 'UTC'
269
- assert_equal(local_offset, 0, "Local offset not be zero for #{ENV['TZ']}")
270
- else
271
- assert(local_offset, 0 != "Local offset should not be zero for #{ENV['TZ']}")
272
- end
273
- assert_equal local_offset, DateTime.now.offset, "Failed for timezone: #{ENV['TZ']}"
274
- end
275
- end
276
- end
277
-
278
- def test_scaling_keeps_time_moving_at_an_accelerated_rate
279
- t = Time.local(2008, 10, 10, 10, 10, 10)
280
- Timecop.scale(4, t) do
281
- start = Time.now
282
- assert_times_effectively_equal start, t, 1, "Looks like we failed to actually travel time"
283
- sleep(0.25)
284
- assert_times_effectively_equal Time.at((start + 4*0.25).to_f), Time.now, 0.25, "Looks like time is not moving at 4x"
285
- end
286
- end
287
-
288
- def test_scaling_returns_now_if_no_block_given
289
- t = Time.local(2008, 10, 10, 10, 10, 10)
290
- assert_times_effectively_equal t, Timecop.scale(4, t)
291
- end
292
-
293
- def test_scaling_returns_now_if_nil_supplied
294
- assert_times_effectively_equal Time.now, Timecop.scale(nil)
295
- end
296
-
297
- def test_scaling_raises_when_empty_string_supplied
298
- err = assert_raises(TypeError) do
299
- Timecop.scale("")
300
- end
301
- assert_match /String can't be coerced into Float/, err.message
302
- end
303
-
304
- def test_freeze_with_utc_time
305
- each_timezone do
306
- t = Time.utc(2008, 10, 10, 10, 10, 10)
307
- local = t.getlocal
308
- Timecop.freeze(t) do
309
- assert_equal local, Time.now, "Failed for timezone: #{ENV['TZ']}"
310
- end
311
- end
312
- end
313
-
314
- def test_freeze_without_arguments_instance_works_as_expected
315
- t = Time.local(2008, 10, 10, 10, 10, 10)
316
- Timecop.freeze(t) do
317
- assert_equal t, Time.now
318
- Timecop.freeze do
319
- assert_equal t, Time.now
320
- assert_equal Time.local(2008, 10, 10, 10, 10, 10), Time.now
321
- assert_equal Date.new(2008, 10, 10), Date.today
322
- end
323
- end
324
- assert t != Time.now
325
- end
326
-
327
- def test_destructive_methods_on_frozen_time
328
- # Use any time zone other than UTC.
329
- ENV['TZ'] = 'EST'
330
-
331
- t = Time.local(2008, 10, 10, 10, 10, 10)
332
- Timecop.freeze(t) do
333
- assert !Time.now.utc?, "Time#local failed to return a time in the local time zone."
334
-
335
- # #utc, #gmt, and #localtime are destructive methods.
336
- Time.now.utc
337
-
338
- assert !Time.now.utc?, "Failed to thwart destructive methods."
339
- end
340
- end
341
-
342
- def test_recursive_travel_maintains_each_context
343
- t = Time.local(2008, 10, 10, 10, 10, 10)
344
- Timecop.travel(2008, 10, 10, 10, 10, 10) do
345
- assert((t - Time.now).abs < 50, "Failed to travel time.")
346
- t2 = Time.local(2008, 9, 9, 9, 9, 9)
347
- Timecop.travel(2008, 9, 9, 9, 9, 9) do
348
- assert_times_effectively_equal(t2, Time.now, 1, "Failed to travel time.")
349
- assert_times_effectively_not_equal(t, Time.now, 1000, "Failed to travel time.")
350
- end
351
- assert_times_effectively_equal(t, Time.now, 2, "Failed to restore previously-traveled time.")
352
- end
353
- assert_nil Time.send(:mock_time)
354
- end
355
-
356
- def test_recursive_travel_yields_correct_time
357
- Timecop.travel(2008, 10, 10, 10, 10, 10) do
358
- Timecop.travel(2008, 9, 9, 9, 9, 9) do |inner_freeze|
359
- assert_times_effectively_equal inner_freeze, Time.now, 1, "Failed to yield current time back to block"
360
- end
361
- end
362
- end
363
-
364
- def test_recursive_travel_then_freeze
365
- t = Time.local(2008, 10, 10, 10, 10, 10)
366
- Timecop.travel(2008, 10, 10, 10, 10, 10) do
367
- assert((t - Time.now).abs < 50, "Failed to travel time.")
368
- t2 = Time.local(2008, 9, 9, 9, 9, 9)
369
- Timecop.freeze(2008, 9, 9, 9, 9, 9) do
370
- assert_equal t2, Time.now
371
- end
372
- assert_times_effectively_equal(t, Time.now, 2, "Failed to restore previously-traveled time.")
373
- end
374
- assert_nil Time.send(:mock_time)
375
- end
376
-
377
- def test_recursive_freeze_then_travel
378
- t = Time.local(2008, 10, 10, 10, 10, 10)
379
- Timecop.freeze(t) do
380
- assert_equal t, Time.now
381
- t2 = Time.local(2008, 9, 9, 9, 9, 9)
382
- Timecop.travel(t2) do
383
- assert_times_effectively_equal(t2, Time.now, 1, "Failed to travel time.")
384
- assert_times_effectively_not_equal(t, Time.now, 1000, "Failed to travel time.")
385
- end
386
- assert_equal t, Time.now
387
- end
388
- assert_nil Time.send(:mock_time)
389
- end
390
-
391
- def test_travel_time_returns_now_if_no_block_given
392
- t_future = Time.local(2030, 10, 10, 10, 10, 10)
393
- assert_times_effectively_equal t_future, Timecop.travel(t_future)
394
- end
395
-
396
- def test_return_temporarily_returns_to_current_time_in_given_block
397
- time_after_travel = Time.local(1990, 7, 16)
398
- now = Time.now
399
-
400
- Timecop.travel(time_after_travel)
401
-
402
- assert_times_effectively_equal(time_after_travel, Time.now)
403
- Timecop.return do
404
- assert_times_effectively_equal(now, Time.now)
405
- end
406
- assert_times_effectively_equal(time_after_travel, Time.now)
407
- end
408
-
409
- def test_travel_returns_now_if_nil_supplied
410
- assert_times_effectively_equal Time.now, Timecop.travel(nil)
411
- end
412
-
413
- def test_travel_time_with_block_returns_the_value_of_the_block
414
- t_future = Time.local(2030, 10, 10, 10, 10, 10)
415
- expected = :foo
416
- actual = Timecop.travel(t_future) { expected }
417
-
418
- assert_equal expected, actual
419
- end
420
-
421
- def test_travel_raises_when_empty_string_supplied
422
- err = assert_raises(ArgumentError) do
423
- Timecop.travel("")
424
- end
425
- assert_match /no time information in \"\"/, err.message
426
- end
427
-
428
- def test_freeze_time_returns_now_if_no_block_given
429
- t_future = Time.local(2030, 10, 10, 10, 10, 10)
430
- assert_times_effectively_equal t_future, Timecop.freeze(t_future)
431
- end
432
-
433
- def test_freeze_time_with_block_returns_the_value_of_the_block
434
- t_future = Time.local(2030, 10, 10, 10, 10, 10)
435
- expected = :foo
436
- actual = Timecop.freeze(t_future) { expected }
437
-
438
- assert_equal expected, actual
439
- end
440
-
441
- def test_return_returns_nil
442
- assert_nil Timecop.return
443
- end
444
-
445
- def test_freeze_without_params
446
- Timecop.freeze 1 do
447
- current_time = Time.now
448
- Timecop.freeze do
449
- assert_equal Time.now, current_time
450
- end
451
- end
452
- end
453
-
454
- def test_freeze_returns_now_if_nil_supplied
455
- assert_times_effectively_equal Time.now, Timecop.freeze(nil)
456
- end
457
-
458
- def test_freeze_raises_when_empty_string_supplied
459
- err = assert_raises(ArgumentError) do
460
- Timecop.freeze("")
461
- end
462
- assert_match /no time information in \"\"/, err.message
463
- end
464
-
465
- def test_freeze_with_new_date
466
- date = Date.new(2012, 6, 9)
467
- Timecop.freeze(Date.new(2012, 6, 9)) do
468
- assert_equal date, Time.now.__send__(:to_date)
469
- end
470
- end
471
-
472
- def test_return_to_baseline_without_a_baseline_set_returns_to_current_time
473
- time_before_travel = Time.now
474
- Timecop.travel Time.now - 60
475
- Timecop.return_to_baseline
476
- assert times_effectively_equal(time_before_travel, Time.now)
477
- end
478
-
479
- def test_return_to_baseline_with_a_baseline_set_returns_to_baseline
480
- baseline = Time.local(1945, 10, 10, 10, 10, 10)
481
- Timecop.baseline = baseline
482
- Timecop.travel Time.now - 60
483
- time_now = Timecop.return_to_baseline
484
- assert times_effectively_equal(baseline, time_now),
485
- "expected to return to #{baseline}, but returned to #{time_now}"
486
- end
487
-
488
- def test_return_eliminates_baseline
489
- time_before_travel = Time.now
490
- Timecop.baseline = Time.local(1937, 9, 9, 9, 9, 9)
491
- Timecop.return
492
- assert times_effectively_equal(time_before_travel, Time.now)
493
-
494
- Timecop.travel(Time.now - 100)
495
- Timecop.return_to_baseline
496
- assert times_effectively_equal(time_before_travel, Time.now)
497
- end
498
-
499
- def test_mock_time_new_same_as_now
500
- date = Time.local(2011, 01, 02)
501
- Timecop.freeze date
502
- assert_equal date, Time.now
503
- assert_equal date, Time.new
504
- end
505
-
506
- def test_not_callable_send_travel
507
- assert_raises NoMethodError do
508
- Timecop.send_travel(:travel, Time.now - 100)
509
- end
510
- end
511
-
512
- def test_datetime_to_time_for_dst_to_non_dst
513
- # Start at a time subject to DST
514
- Timecop.travel(2009, 4, 1, 0, 0, 0, -4*60*60) do
515
-
516
- # Then freeze, via DateTime, at a time not subject to DST
517
- t = DateTime.new(2009,01,01,0,0,0, "-0500")
518
- Timecop.freeze(t) do
519
-
520
- # Check the current time via DateTime.now--should be what we asked for
521
- assert_date_times_equal t, DateTime.now
522
-
523
- # Then check the current time via Time.now (not DateTime.now)
524
- assert_times_effectively_equal Time.new(2009, 1, 1, 0, 0, 0, -5*60*60), Time.now
525
- end
526
- end
527
- end
528
-
529
- def test_raises_when_safe_mode_and_no_block
530
- with_safe_mode do
531
- assert_raises Timecop::SafeModeException do
532
- Timecop.freeze
533
- end
534
- end
535
- end
536
-
537
- def test_raises_when_safe_mode_and_no_block_though_previously_block_given
538
- Timecop.freeze do
539
- Timecop.freeze
540
- end
541
-
542
- with_safe_mode do
543
- assert_raises Timecop::SafeModeException do
544
- Timecop.freeze
545
- end
546
- end
547
- end
548
-
549
- def test_no_raise_when_safe_mode_and_block_used
550
- with_safe_mode do
551
- Timecop.freeze {}
552
- end
553
- end
554
-
555
- def test_no_raise_when_not_safe_mode_and_no_block
556
- with_safe_mode(false) do
557
- Timecop.freeze
558
- end
559
- end
560
-
561
- def test_no_raise_when_safe_mode_and_no_block_and_in_block_context
562
- with_safe_mode do
563
- Timecop.freeze do
564
- Timecop.freeze
565
- end
566
- end
567
- end
568
-
569
- def test_frozen_after_freeze
570
- Timecop.freeze
571
- assert Timecop.frozen?
572
- end
573
-
574
- def test_frozen_inside_freeze
575
- Timecop.freeze do
576
- assert Timecop.frozen?
577
- end
578
- end
579
-
580
- def test_not_frozen_after_return
581
- Timecop.freeze
582
- Timecop.return
583
- assert !Timecop.frozen?
584
- end
585
-
586
- def test_thread_safe_timecop_in_parallel
587
- Timecop.thread_safe = true
588
- date = Time.local(2011, 01, 02)
589
- thread = Thread.new do
590
- Timecop.freeze(date) do
591
- sleep 1 #give main thread time to run
592
- assert_equal date, Time.now
593
- end
594
- end
595
-
596
- sleep 0.25
597
- assert Time.now != date
598
- thread.join
599
- ensure
600
- Timecop.thread_safe = false
601
- end
602
-
603
- def test_thread_safe_timecop_returns_after_block
604
- Timecop.thread_safe = true
605
- date = Time.local(2017, 10, 8)
606
-
607
- Timecop.freeze(date) { }
608
- assert Time.now != date
609
- ensure
610
- Timecop.thread_safe = false
611
- end
612
-
613
- private
614
-
615
- def with_safe_mode(enabled=true)
616
- mode = Timecop.safe_mode?
617
- Timecop.safe_mode = enabled
618
- yield
619
- ensure
620
- Timecop.safe_mode = mode
621
- end
622
- end
@@ -1,8 +0,0 @@
1
- require_relative "test_helper"
2
- require "time"
3
-
4
- class TestTimecopWithoutDateButWithTime < Minitest::Test
5
- def test_loads_properly_when_time_is_required_instead_of_date
6
- require 'timecop'
7
- end
8
- end