File: timecop_test.rb

package info (click to toggle)
ruby-timecop 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 220 kB
  • ctags: 281
  • sloc: ruby: 1,138; sh: 8; makefile: 4
file content (548 lines) | stat: -rw-r--r-- 17,085 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
require File.join(File.dirname(__FILE__), "test_helper")
require "timecop"

class TestTimecop < Minitest::Unit::TestCase
  def teardown
    Timecop.return
  end

  def test_freeze_changes_and_resets_time
    outer_freeze_time = Time.local(2001, 01, 01)
    inner_freeze_block = Time.local(2002, 02, 02)
    inner_freeze_one = Time.local(2003, 03, 03)
    inner_freeze_two = Time.local(2004, 04, 04)

    Timecop.freeze(outer_freeze_time) do
      assert_times_effectively_equal outer_freeze_time, Time.now
      Timecop.freeze(inner_freeze_block) do
        assert_times_effectively_equal inner_freeze_block, Time.now
        Timecop.freeze(inner_freeze_one)
        assert_times_effectively_equal inner_freeze_one, Time.now
        Timecop.freeze(inner_freeze_two)
        assert_times_effectively_equal inner_freeze_two, Time.now
      end
      assert_times_effectively_equal outer_freeze_time, Time.now
    end
  end

  def test_freeze_yields_mocked_time
    Timecop.freeze(2008, 10, 10, 10, 10, 10) do |frozen_time|
      assert_equal frozen_time, Time.now
    end
  end

  def test_freeze_then_return_unsets_mock_time
    Timecop.freeze(1)
    Timecop.return
    assert_nil Time.send(:mock_time)
  end

  def test_travel_then_return_unsets_mock_time
    Timecop.travel(1)
    Timecop.return
    assert_nil Time.send(:mock_time)
  end

  def test_freeze_with_block_unsets_mock_time
    assert_nil Time.send(:mock_time), "test is invalid"
    Timecop.freeze(1) do; end
    assert_nil Time.send(:mock_time)
  end

  def test_travel_with_block_unsets_mock_time
    assert_nil Time.send(:mock_time), "test is invalid"
    Timecop.travel(1) do; end
    assert_nil Time.send(:mock_time)
  end

  def test_travel_does_not_reduce_precision_of_datetime
    # requires to_r on Float (>= 1.9)
    if Float.method_defined?(:to_r)
      Timecop.travel(Time.new(2014, 1, 1, 0, 0, 0))
      assert DateTime.now != DateTime.now

      Timecop.travel(Time.new(2014, 1, 1, 0, 0, 59))
      assert DateTime.now != DateTime.now
    end
  end

  def test_freeze_in_time_subclass_returns_mocked_subclass
    t = Time.local(2008, 10, 10, 10, 10, 10)
    custom_timeklass = Class.new(Time) do
      def custom_format_method() strftime('%F') end
    end

    Timecop.freeze(2008, 10, 10, 10, 10, 10) do
      assert custom_timeklass.now.is_a? custom_timeklass
      assert Time.now.eql? custom_timeklass.now
      assert custom_timeklass.now.respond_to? :custom_format_method
    end
  end

  def test_freeze_in_date_subclass_returns_mocked_subclass
    t = Time.local(2008, 10, 10, 10, 10, 10)
    custom_dateklass = Class.new(Date) do
      def custom_format_method() strftime('%F') end
    end

    Timecop.freeze(2008, 10, 10, 10, 10, 10) do
      assert custom_dateklass.today.is_a? custom_dateklass
      assert Date.today.eql? custom_dateklass.today
      assert custom_dateklass.today.respond_to? :custom_format_method
    end
  end

  def test_freeze_in_datetime_subclass_returns_mocked_subclass
    t = Time.local(2008, 10, 10, 10, 10, 10)
    custom_datetimeklass = Class.new(DateTime) do
      def custom_format_method() strftime('%F') end
    end

    Timecop.freeze(2008, 10, 10, 10, 10, 10) do
      assert custom_datetimeklass.now.is_a? custom_datetimeklass
      assert DateTime.now.eql? custom_datetimeklass.now
      assert custom_datetimeklass.now.respond_to? :custom_format_method
    end
  end

  def test_recursive_freeze
    t = Time.local(2008, 10, 10, 10, 10, 10)
    Timecop.freeze(2008, 10, 10, 10, 10, 10) do
      assert_equal t, Time.now
      t2 = Time.local(2008, 9, 9, 9, 9, 9)
      Timecop.freeze(2008, 9, 9, 9, 9, 9) do
        assert_equal t2, Time.now
      end
      assert_equal t, Time.now
    end
    assert t != Time.now
  end

  def test_freeze_with_time_instance_works_as_expected
    t = Time.local(2008, 10, 10, 10, 10, 10)
    Timecop.freeze(t) do
      assert_equal t, Time.now
      assert_date_times_equal DateTime.new(2008, 10, 10, 10, 10, 10, local_offset), DateTime.now
      assert_equal Date.new(2008, 10, 10), Date.today
    end

    assert t != Time.now
    assert DateTime.new(2008, 10, 10, 10, 10, 10, local_offset) != DateTime.now
    assert Date.new(2008, 10, 10) != Date.today
  end

  def test_freeze_with_datetime_on_specific_timezone_during_dst
    each_timezone do
      # Start from a time that is subject to DST
      Timecop.freeze(2009, 9, 1)
      # Travel to a DateTime that is also in DST
      t = DateTime.parse("2009-10-11 00:38:00 +0200")
      Timecop.freeze(t) do
        assert_date_times_equal t, DateTime.now
      end
      Timecop.return
    end
  end

  def test_freeze_with_datetime_on_specific_timezone_not_during_dst
    each_timezone do
      # Start from a time that is not subject to DST
      Timecop.freeze(2009, 12, 1)
      # Travel to a time that is also not in DST
      t = DateTime.parse("2009-12-11 00:38:00 +0100")
      Timecop.freeze(t) do
        assert_date_times_equal t, DateTime.now
      end
    end
  end

  def test_freeze_with_datetime_from_a_non_dst_time_to_a_dst_time
    each_timezone do
      # Start from a time that is not subject to DST
      Timecop.freeze(DateTime.parse("2009-12-1 00:00:00 +0100"))
      # Travel back to a time in DST
      t = DateTime.parse("2009-10-11 00:38:00 +0200")
      Timecop.freeze(t) do
        assert_date_times_equal t, DateTime.now
      end
    end
  end

  def test_freeze_with_datetime_from_a_dst_time_to_a_non_dst_time
    each_timezone do
      # Start from a time that is not subject to DST
      Timecop.freeze(DateTime.parse("2009-10-11 00:00:00 +0200"))
      # Travel back to a time in DST
      t = DateTime.parse("2009-12-1 00:38:00 +0100")
      Timecop.freeze(t) do
        assert_date_times_equal t, DateTime.now
      end
    end
  end

  def test_freeze_with_date_instance_works_as_expected
    d = Date.new(2008, 10, 10)
    Timecop.freeze(d) do
      assert_equal d, Date.today
      assert_equal Time.local(2008, 10, 10, 0, 0, 0), Time.now
      assert_date_times_equal DateTime.new(2008, 10, 10, 0, 0, 0, local_offset), DateTime.now
    end
    assert d != Date.today
    assert Time.local(2008, 10, 10, 0, 0, 0) != Time.now
    assert DateTime.new(2008, 10, 10, 0, 0, 0, local_offset) != DateTime.now
  end

  def test_freeze_with_integer_instance_works_as_expected
    t = Time.local(2008, 10, 10, 10, 10, 10)
    Timecop.freeze(t) do
      assert_equal t, Time.now
      assert_date_times_equal DateTime.new(2008, 10, 10, 10, 10, 10, local_offset), DateTime.now
      assert_equal Date.new(2008, 10, 10), Date.today
      Timecop.freeze(10) do
        assert_equal t + 10, Time.now
        assert_equal Time.local(2008, 10, 10, 10, 10, 20), Time.now
        assert_equal Date.new(2008, 10, 10), Date.today
      end
    end
    assert t != Time.now
    assert DateTime.new(2008, 10, 10, 10, 10, 10) != DateTime.now
    assert Date.new(2008, 10, 10) != Date.today
  end

  def test_exception_thrown_in_freeze_block_properly_resets_time
    t = Time.local(2008, 10, 10, 10, 10, 10)
    begin
      Timecop.freeze(t) do
        assert_equal t, Time.now
        raise "blah exception"
      end
    rescue
      assert t != Time.now
      assert_nil Time.send(:mock_time)
    end
  end

  def test_exception_thrown_in_return_block_restores_previous_time
    t = Time.local(2008, 10, 10, 10, 10, 10)
    Timecop.freeze(t) do
      Timecop.return { raise 'foobar' } rescue nil
      assert_equal t, Time.now
    end
  end

  def test_freeze_freezes_time
    t = Time.local(2008, 10, 10, 10, 10, 10)
    now = Time.now
    Timecop.freeze(t) do
      #assert Time.now < now, "If we had failed to freeze, time would have proceeded, which is what appears to have happened."
      new_t, new_d, new_dt = Time.now, Date.today, DateTime.now
      assert_equal t, new_t, "Failed to freeze time." # 2 seconds
      #sleep(10)
      assert_equal new_t, Time.now
      assert_equal new_d, Date.today
      assert_equal new_dt, DateTime.now
    end
  end

  def test_travel_keeps_time_moving
    t = Time.local(2008, 10, 10, 10, 10, 10)
    now = Time.now
    Timecop.travel(t) do
      new_now = Time.now
      assert_times_effectively_equal(new_now, t, 1, "Looks like we failed to actually travel time")
      sleep(0.25)
      assert_times_effectively_not_equal new_now, Time.now, 0.25, "Looks like time is not moving"
    end
  end

  def test_mocked_date_time_now_is_local
    each_timezone do
      t = DateTime.parse("2009-10-11 00:38:00 +0200")
      Timecop.freeze(t) do
        if ENV['TZ'] == 'UTC'
          assert_equal(local_offset, 0, "Local offset not be zero for #{ENV['TZ']}")
        else
          assert(local_offset, 0 != "Local offset should not be zero for #{ENV['TZ']}")
        end
        assert_equal local_offset, DateTime.now.offset, "Failed for timezone: #{ENV['TZ']}"
      end
    end
  end

  def test_scaling_keeps_time_moving_at_an_accelerated_rate
    t = Time.local(2008, 10, 10, 10, 10, 10)
    Timecop.scale(4, t) do
      start = Time.now
      assert_times_effectively_equal start, t, 1, "Looks like we failed to actually travel time"
      sleep(0.25)
      assert_times_effectively_equal Time.at((start + 4*0.25).to_f), Time.now, 0.25, "Looks like time is not moving at 4x"
    end
  end

  def test_scaling_returns_now_if_no_block_given
    t = Time.local(2008, 10, 10, 10, 10, 10)
    assert_times_effectively_equal t, Timecop.scale(4, t)
  end

  def test_freeze_with_utc_time
    each_timezone do
      t = Time.utc(2008, 10, 10, 10, 10, 10)
      local = t.getlocal
      Timecop.freeze(t) do
        assert_equal local, Time.now, "Failed for timezone: #{ENV['TZ']}"
      end
    end
  end

  def test_freeze_without_arguments_instance_works_as_expected
    t = Time.local(2008, 10, 10, 10, 10, 10)
    Timecop.freeze(t) do
      assert_equal t, Time.now
      Timecop.freeze do
        assert_equal t, Time.now
        assert_equal Time.local(2008, 10, 10, 10, 10, 10), Time.now
        assert_equal Date.new(2008, 10, 10), Date.today
      end
    end
    assert t != Time.now
  end

  def test_destructive_methods_on_frozen_time
    # Use any time zone other than UTC.
    ENV['TZ'] = 'EST'

    t = Time.local(2008, 10, 10, 10, 10, 10)
    Timecop.freeze(t) do
      assert !Time.now.utc?, "Time#local failed to return a time in the local time zone."

      # #utc, #gmt, and #localtime are destructive methods.
      Time.now.utc

      assert !Time.now.utc?, "Failed to thwart destructive methods."
    end
  end

  def test_recursive_travel_maintains_each_context
    t = Time.local(2008, 10, 10, 10, 10, 10)
    Timecop.travel(2008, 10, 10, 10, 10, 10) do
      assert((t - Time.now).abs < 50, "Failed to travel time.")
      t2 = Time.local(2008, 9, 9, 9, 9, 9)
      Timecop.travel(2008, 9, 9, 9, 9, 9) do
        assert_times_effectively_equal(t2, Time.now, 1, "Failed to travel time.")
        assert_times_effectively_not_equal(t, Time.now, 1000, "Failed to travel time.")
      end
      assert_times_effectively_equal(t, Time.now, 2, "Failed to restore previously-traveled time.")
    end
    assert_nil Time.send(:mock_time)
  end

  def test_recursive_travel_yields_correct_time
    Timecop.travel(2008, 10, 10, 10, 10, 10) do
      Timecop.travel(2008, 9, 9, 9, 9, 9) do |inner_freeze|
        assert_times_effectively_equal inner_freeze, Time.now, 1, "Failed to yield current time back to block"
      end
    end
  end

  def test_recursive_travel_then_freeze
    t = Time.local(2008, 10, 10, 10, 10, 10)
    Timecop.travel(2008, 10, 10, 10, 10, 10) do
      assert((t - Time.now).abs < 50, "Failed to travel time.")
      t2 = Time.local(2008, 9, 9, 9, 9, 9)
      Timecop.freeze(2008, 9, 9, 9, 9, 9) do
        assert_equal t2, Time.now
      end
      assert_times_effectively_equal(t, Time.now, 2, "Failed to restore previously-traveled time.")
    end
    assert_nil Time.send(:mock_time)
  end

  def test_recursive_freeze_then_travel
    t = Time.local(2008, 10, 10, 10, 10, 10)
    Timecop.freeze(t) do
      assert_equal t, Time.now
      t2 = Time.local(2008, 9, 9, 9, 9, 9)
      Timecop.travel(t2) do
        assert_times_effectively_equal(t2, Time.now, 1, "Failed to travel time.")
        assert_times_effectively_not_equal(t, Time.now, 1000, "Failed to travel time.")
      end
      assert_equal t, Time.now
    end
    assert_nil Time.send(:mock_time)
  end

  def test_travel_time_returns_now_if_no_block_given
    t_future = Time.local(2030, 10, 10, 10, 10, 10)
    assert_times_effectively_equal t_future, Timecop.travel(t_future)
  end

  def test_return_temporarily_returns_to_current_time_in_given_block
    time_after_travel = Time.local(1990, 7, 16)
    now = Time.now

    Timecop.travel(time_after_travel)

    assert_times_effectively_equal(time_after_travel, Time.now)
    Timecop.return do
      assert_times_effectively_equal(now, Time.now)
    end
    assert_times_effectively_equal(time_after_travel, Time.now)
  end

  def test_travel_time_with_block_returns_the_value_of_the_block
    t_future = Time.local(2030, 10, 10, 10, 10, 10)
    expected = :foo
    actual = Timecop.travel(t_future) { expected }

    assert_equal expected, actual
  end

  def test_freeze_time_returns_now_if_no_block_given
    t_future = Time.local(2030, 10, 10, 10, 10, 10)
    assert_times_effectively_equal t_future, Timecop.freeze(t_future)
  end

  def test_freeze_time_with_block_returns_the_value_of_the_block
    t_future = Time.local(2030, 10, 10, 10, 10, 10)
    expected = :foo
    actual = Timecop.freeze(t_future) { expected }

    assert_equal expected, actual
  end

  def test_return_returns_nil
    assert_nil Timecop.return
  end

  def test_freeze_without_params
    Timecop.freeze 1 do
      current_time = Time.now
      Timecop.freeze do
        assert_equal Time.now, current_time
      end
    end
  end

  def test_freeze_with_new_date
    date = Date.new(2012, 6, 9)
    Timecop.freeze(Date.new(2012, 6, 9)) do
      assert_equal date, Time.now.__send__(:to_date)
    end
  end

  def test_return_to_baseline_without_a_baseline_set_returns_to_current_time
    time_before_travel = Time.now
    Timecop.travel Time.now - 60
    Timecop.return_to_baseline
    assert times_effectively_equal(time_before_travel, Time.now)
  end

  def test_return_to_baseline_with_a_baseline_set_returns_to_baseline
    baseline = Time.local(1945, 10, 10, 10, 10, 10)
    Timecop.baseline = baseline
    Timecop.travel Time.now - 60
    time_now = Timecop.return_to_baseline
    assert times_effectively_equal(baseline, time_now),
      "expected to return to #{baseline}, but returned to #{time_now}"
  end

  def test_return_eliminates_baseline
    time_before_travel = Time.now
    Timecop.baseline = Time.local(1937, 9, 9, 9, 9, 9)
    Timecop.return
    assert times_effectively_equal(time_before_travel, Time.now)

    Timecop.travel(Time.now - 100)
    Timecop.return_to_baseline
    assert times_effectively_equal(time_before_travel, Time.now)
  end

  def test_mock_time_new_same_as_now
    date = Time.local(2011, 01, 02)
    Timecop.freeze date
    assert_equal date, Time.now
    assert_equal date, Time.new
  end

  def test_not_callable_send_travel
    assert_raises NoMethodError do
      Timecop.send_travel(:travel, Time.now - 100)
    end
  end

  def test_datetime_to_time_for_dst_to_non_dst
    # Start at a time subject to DST
    Timecop.travel(2009, 4, 1, 0, 0, 0, -4*60*60) do

      # Then freeze, via DateTime, at a time not subject to DST
      t = DateTime.new(2009,01,01,0,0,0, "-0500")
      Timecop.freeze(t) do

        # Check the current time via DateTime.now--should be what we asked for
        assert_date_times_equal t, DateTime.now

        # Then check the current time via Time.now (not DateTime.now)
        assert_times_effectively_equal Time.new(2009, 1, 1, 0, 0, 0, -5*60*60), Time.now
      end
    end
  end

  def test_raises_when_safe_mode_and_no_block
    with_safe_mode do
      assert_raises Timecop::SafeModeException do
        Timecop.freeze
      end
    end
  end

  def test_no_raise_when_safe_mode_and_block_used
    with_safe_mode do
      Timecop.freeze {}
    end
  end

  def test_no_raise_when_not_safe_mode_and_no_block
    with_safe_mode(false) do
      Timecop.freeze
    end
  end

  def test_date_strptime_without_year
    Timecop.freeze(Time.new(1984,2,28)) do
      assert_equal Date.strptime('04-14', '%m-%d'), Date.new(1984, 4, 14)
    end
  end

  def test_date_strptime_without_specifying_format
    Timecop.freeze(Time.new(1984,2,28)) do
      assert_equal Date.strptime('1999-04-14'), Date.new(1999, 4, 14)
    end
  end

  def test_frozen_after_freeze
    Timecop.freeze
    assert Timecop.frozen?
  end

  def test_frozen_inside_freeze
    Timecop.freeze do
      assert Timecop.frozen?
    end
  end

  def test_not_frozen_after_return
    Timecop.freeze
    Timecop.return
    assert !Timecop.frozen?
  end

  private

  def with_safe_mode(enabled=true)
    mode = Timecop.safe_mode?
    Timecop.safe_mode = enabled
    yield
  ensure
    Timecop.safe_mode = mode
  end
end