timecop 0.9.6 → 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c86a01ea95f5019cb15a6a66c5668e68e7f1e293ca14b910a10cbe1cdd70d668
4
- data.tar.gz: e5c6987d3a452fa9ead841021f93bf4ea6bac713c1f6cea38cfc1c1d49baf6ee
3
+ metadata.gz: ad1963ee820d4d3519b4e078879aaa3914b2e9802460480fd9ca3c4a239677e4
4
+ data.tar.gz: 49a827fc02c8580d33d6861f51974dd046996e7281442c33f89c527d4d4c5af0
5
5
  SHA512:
6
- metadata.gz: 4abbcd2d37b9fdf6dfb1ed1f63f945a882cd62a47ae2ae237da003ccac0bc660572be0800933ea484fb1632707bdc2de457b96f9aad8a62b1dfd96d766220692
7
- data.tar.gz: a19cc9026478a29161a832454edebf76105d56d3841575af71e9dbaa305a40a68af285d17b7473369ae913e463cadd7b67ab2e099d13d3162b713d2c3cd87d05
6
+ metadata.gz: d18895efbc69f3768fcf68fa372759e727c30869e5ff1a77c8b7a2901e4a89dd49fe61df38c0a5c57dd9604250e0c49733c990c32757ac9a374a56fb3962d441
7
+ data.tar.gz: 6938b5dfc45c545d8c0e83b757f1b99d9b69b2cfe23d5e1d0f9b4fc4ff56282af201a982bcf52084f05b0de88ea36256ecbe54e2988fc944693940f5179dcb97
data/README.markdown CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  ## DESCRIPTION
7
7
 
8
- 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.
8
+ 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.
9
9
 
10
10
  ## INSTALL
11
11
 
@@ -17,13 +17,13 @@ A gem providing "time travel" and "time freezing" capabilities, making it dead s
17
17
  - Travel back to a specific point in time, but allow time to continue moving forward from there.
18
18
  - Scale time by a given scaling factor that will cause time to move at an accelerated pace.
19
19
  - No dependencies, can be used with _any_ ruby project
20
- - Timecop api allows arguments to be passed into #freeze and #travel as one of the following:
20
+ - Timecop api allows arguments to be passed into `#freeze` and `#travel` as one of the following:
21
21
  - Time instance
22
22
  - DateTime instance
23
23
  - Date instance
24
24
  - individual arguments (year, month, day, hour, minute, second)
25
- - a single integer argument that is interpreted as an offset in seconds from Time.now
26
- - Nested calls to Timecop#travel and Timecop#freeze are supported -- each block will maintain its interpretation of now.
25
+ - a single integer argument that is interpreted as an offset in seconds from `Time.now`
26
+ - Nested calls to `Timecop#travel` and `Timecop#freeze` are supported -- each block will maintain its interpretation of now.
27
27
  - Works with regular Ruby projects, and Ruby on Rails projects
28
28
 
29
29
  ## USAGE
@@ -62,7 +62,7 @@ helpful if your whole application is time-sensitive. It allows you to build
62
62
  your test data at a single point in time, and to move in/out of that time as
63
63
  appropriate (within your tests)
64
64
 
65
- in config/environments/test.rb
65
+ in `config/environments/test.rb`
66
66
 
67
67
  ```ruby
68
68
  config.after_initialize do
@@ -74,10 +74,10 @@ end
74
74
 
75
75
  ### The difference between Timecop.freeze and Timecop.travel
76
76
 
77
- freeze is used to statically mock the concept of now. As your program executes,
78
- Time.now will not change unless you make subsequent calls into the Timecop API.
79
- travel, on the other hand, computes an offset between what we currently think
80
- Time.now is (recall that we support nested traveling) and the time passed in.
77
+ `freeze` is used to statically mock the concept of now. As your program executes,
78
+ `Time.now` will not change unless you make subsequent calls into the Timecop API.
79
+ `travel`, on the other hand, computes an offset between what we currently think
80
+ `Time.now` is (recall that we support nested traveling) and the time passed in.
81
81
  It uses this offset to simulate the passage of time. To demonstrate, consider
82
82
  the following code snippets:
83
83
 
@@ -50,21 +50,28 @@ class Date #:nodoc:
50
50
 
51
51
  d = Date._strptime(str, fmt)
52
52
  now = Time.now.to_date
53
- year = d[:year] || now.year
53
+ year = d[:year] || d[:cwyear] || now.year
54
54
  mon = d[:mon] || now.mon
55
55
  if d.keys == [:year]
56
56
  Date.new(year, 1, 1, start)
57
57
  elsif d[:mday]
58
58
  Date.new(year, mon, d[:mday], start)
59
- elsif d[:wday]
60
- Date.new(year, mon, now.mday, start) + (d[:wday] - now.wday)
61
59
  elsif d[:yday]
62
60
  Date.new(year, 1, 1, start).next_day(d[:yday] - 1)
63
- elsif d[:cwyear] && d[:cweek]
64
- if d[:cwday]
65
- Date.commercial(d[:cwyear], d[:cweek], d[:cwday], start)
66
- else
67
- Date.commercial(d[:cwyear], d[:cweek], 1, start)
61
+ elsif d[:cwyear] || d[:cweek] || d[:wnum0] || d[:wnum1] || d[:wday] || d[:cwday]
62
+ week = d[:cweek] || d[:wnum1] || d[:wnum0] || now.strftime('%W').to_i
63
+ if d[:wnum0] #Week of year where week starts on sunday
64
+ if d[:cwday] #monday based day of week
65
+ Date.strptime_without_mock_date("#{year} #{week} #{d[:cwday]}", '%Y %U %u', start)
66
+ else
67
+ Date.strptime_without_mock_date("#{year} #{week} #{d[:wday] || 0}", '%Y %U %w', start)
68
+ end
69
+ else #Week of year where week starts on monday
70
+ if d[:wday] #sunday based day of week
71
+ Date.strptime_without_mock_date("#{year} #{week} #{d[:wday]}", '%Y %W %w', start)
72
+ else
73
+ Date.strptime_without_mock_date("#{year} #{week} #{d[:cwday] || 1}", '%Y %W %u', start)
74
+ end
68
75
  end
69
76
  elsif d[:seconds]
70
77
  Time.at(d[:seconds]).to_date
@@ -125,7 +132,6 @@ class DateTime #:nodoc:
125
132
  alias_method :now, :now_with_mock_time
126
133
 
127
134
  def parse_with_mock_date(*args)
128
- date_hash = Date._parse(*args)
129
135
  parsed_date = parse_without_mock_date(*args)
130
136
  return parsed_date unless mocked_time_stack_item
131
137
  date_hash = DateTime._parse(*args)
@@ -121,9 +121,9 @@ class Timecop
121
121
  instance.thread_safe
122
122
  end
123
123
 
124
- # Returns whether or not Timecop is currently frozen/travelled
124
+ # Returns whether or not Timecop is currently frozen
125
125
  def frozen?
126
- !instance.stack.empty?
126
+ !instance.stack.empty? && instance.stack.last.mock_type == :freeze
127
127
  end
128
128
 
129
129
  private
@@ -1,3 +1,3 @@
1
1
  class Timecop
2
- VERSION = "0.9.6"
2
+ VERSION = "0.9.8"
3
3
  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.9.6
4
+ version: 0.9.8
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: 2022-11-29 00:00:00.000000000 Z
12
+ date: 2023-08-14 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
@@ -29,11 +29,6 @@ files:
29
29
  - lib/timecop/time_stack_item.rb
30
30
  - lib/timecop/timecop.rb
31
31
  - lib/timecop/version.rb
32
- - test/test_helper.rb
33
- - test/time_stack_item_test.rb
34
- - test/timecop_test.rb
35
- - test/timecop_without_date_but_with_time_test.rb
36
- - test/timecop_without_date_test.rb
37
32
  homepage: https://github.com/travisjeffery/timecop
38
33
  licenses:
39
34
  - MIT
@@ -56,13 +51,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
51
  requirements: []
57
52
  rubygems_version: 3.2.15
58
53
  signing_key:
59
- specification_version: 3
54
+ specification_version: 4
60
55
  summary: A gem providing "time travel" and "time freezing" capabilities, making it
61
56
  dead simple to test time-dependent code. It provides a unified method to mock Time.now,
62
57
  Date.today, and DateTime.now in a single call.
63
- test_files:
64
- - test/test_helper.rb
65
- - test/time_stack_item_test.rb
66
- - test/timecop_test.rb
67
- - test/timecop_without_date_test.rb
68
- - test/timecop_without_date_but_with_time_test.rb
58
+ test_files: []
data/test/test_helper.rb DELETED
@@ -1,59 +0,0 @@
1
- require 'bundler/setup'
2
- require 'minitest/autorun'
3
- require 'minitest/rg'
4
- require 'pry'
5
-
6
- $VERBOSE = true # enable ruby warnings
7
-
8
- require 'mocha/minitest'
9
-
10
- class Minitest::Test
11
- private
12
- # Tests to see that two times are within the given distance,
13
- # in seconds, from each other.
14
- def times_effectively_equal(time1, time2, seconds_interval = 1)
15
- (time1 - time2).abs <= seconds_interval
16
- end
17
-
18
- def assert_times_effectively_equal(time1, time2, seconds_interval = 1, msg = nil)
19
- assert times_effectively_equal(time1, time2, seconds_interval), "#{msg}: time1 = #{time1.to_s}, time2 = #{time2.to_s}"
20
- end
21
-
22
- def assert_times_effectively_not_equal(time1, time2, seconds_interval = 1, msg = nil)
23
- assert !times_effectively_equal(time1, time2, seconds_interval), "#{msg}: time1 = #{time1.to_s}, time2 = #{time2.to_s}"
24
- end
25
-
26
- # Gets the local offset (supplied by ENV['TZ'] or your computer's clock)
27
- # At the given timestamp, or Time.now if not time is given.
28
- def local_offset(time = Time.now)
29
- Time.at(time.to_i).to_datetime.offset
30
- end
31
-
32
- TIMEZONES = ["Pacific/Midway", "Europe/Paris", "UTC", "America/Chicago"]
33
-
34
- def each_timezone
35
- old_tz = ENV["TZ"]
36
-
37
- begin
38
- TIMEZONES.each do |timezone|
39
- ENV["TZ"] = timezone
40
- yield
41
- end
42
- ensure
43
- ENV["TZ"] = old_tz
44
- end
45
- end
46
-
47
- def a_time_stack_item
48
- Timecop::TimeStackItem.new(:freeze, 2008, 1, 1, 0, 0, 0)
49
- end
50
-
51
- def assert_date_times_equal(dt1, dt2)
52
- 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}"
53
- end
54
-
55
- def jruby?
56
- RUBY_PLATFORM == "java"
57
- end
58
-
59
- end
@@ -1,299 +0,0 @@
1
- require 'date'
2
- require_relative "test_helper"
3
- require 'timecop'
4
-
5
- require 'active_support/all'
6
-
7
- class TestTimeStackItem < Minitest::Test
8
- def teardown
9
- Timecop.return
10
- Time.zone = nil
11
- end
12
-
13
- def test_new_with_time
14
- t = Time.now
15
- y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
16
- stack_item = Timecop::TimeStackItem.new(:freeze, t)
17
-
18
- assert_equal y, stack_item.year
19
- assert_equal m, stack_item.month
20
- assert_equal d, stack_item.day
21
- assert_equal h, stack_item.hour
22
- assert_equal min, stack_item.min
23
- assert_equal s, stack_item.sec
24
- end
25
-
26
- def test_new_with_time_and_arguments
27
- t = Time.new(2012, 7, 28, 20, 0)
28
- y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
29
- stack_item = Timecop::TimeStackItem.new(:freeze, t)
30
-
31
- assert_equal y, stack_item.year
32
- assert_equal m, stack_item.month
33
- assert_equal d, stack_item.day
34
- assert_equal h, stack_item.hour
35
- assert_equal min, stack_item.min
36
- assert_equal s, stack_item.sec
37
- end
38
-
39
- def test_new_with_datetime_now
40
- t = DateTime.now
41
- y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
42
- stack_item = Timecop::TimeStackItem.new(:freeze, t)
43
-
44
- assert_equal y, stack_item.year
45
- assert_equal m, stack_item.month
46
- assert_equal d, stack_item.day
47
- assert_equal h, stack_item.hour
48
- assert_equal min, stack_item.min
49
- assert_equal s, stack_item.sec
50
- end
51
-
52
- def test_new_with_datetime_in_different_timezone
53
- each_timezone do
54
- t = DateTime.parse("2009-10-11 00:38:00 +0200")
55
- stack_item = Timecop::TimeStackItem.new(:freeze, t)
56
-
57
- assert_date_times_equal(t, stack_item.datetime)
58
- end
59
- end
60
-
61
- def test_new_with_date
62
- date = Date.today
63
- y, m, d, h, min, s = date.year, date.month, date.day, 0, 0, 0
64
- stack_item = Timecop::TimeStackItem.new(:freeze, date)
65
-
66
- assert_equal y, stack_item.year
67
- assert_equal m, stack_item.month
68
- assert_equal d, stack_item.day
69
- assert_equal h, stack_item.hour
70
- assert_equal min, stack_item.min
71
- assert_equal s, stack_item.sec
72
- end
73
-
74
- # Due to the nature of this test (calling Time.now once in this test and
75
- # once in #new), this test may fail when two subsequent calls
76
- # to Time.now return a different second.
77
- def test_new_with_integer
78
- t = Time.now
79
- y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
80
- stack_item = Timecop::TimeStackItem.new(:freeze, 0)
81
-
82
- assert_equal y, stack_item.year
83
- assert_equal m, stack_item.month
84
- assert_equal d, stack_item.day
85
- assert_equal h, stack_item.hour
86
- assert_equal min, stack_item.min
87
- assert_equal s, stack_item.sec
88
- end
89
-
90
- def test_new_with_float
91
- t = Time.now
92
- y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
93
- stack_item = Timecop::TimeStackItem.new(:freeze, 0.0)
94
-
95
- assert_equal y, stack_item.year
96
- assert_equal m, stack_item.month
97
- assert_equal d, stack_item.day
98
- assert_equal h, stack_item.hour
99
- assert_equal min, stack_item.min
100
- assert_equal s, stack_item.sec
101
- end
102
-
103
- def test_new_with_individual_arguments
104
- y, m, d, h, min, s = 2008, 10, 10, 10, 10, 10
105
- stack_item = Timecop::TimeStackItem.new(:freeze, y, m, d, h, min, s)
106
-
107
- assert_equal y, stack_item.year
108
- assert_equal m, stack_item.month
109
- assert_equal d, stack_item.day
110
- assert_equal h, stack_item.hour
111
- assert_equal min, stack_item.min
112
- assert_equal s, stack_item.sec
113
- end
114
-
115
- def test_rational_to_utc_offset
116
- assert_equal -14400, a_time_stack_item.send(:rational_to_utc_offset, Rational(-1, 6))
117
- assert_equal -18000, a_time_stack_item.send(:rational_to_utc_offset, Rational(-5, 24))
118
- assert_equal 0, a_time_stack_item.send(:rational_to_utc_offset, Rational(0, 1))
119
- assert_equal 3600, a_time_stack_item.send(:rational_to_utc_offset, Rational(1, 24))
120
- end
121
-
122
- def test_utc_offset_to_rational
123
- assert_equal Rational(-1, 6), a_time_stack_item.send(:utc_offset_to_rational, -14400)
124
- assert_equal Rational(-5, 24), a_time_stack_item.send(:utc_offset_to_rational, -18000)
125
- assert_equal Rational(0, 1), a_time_stack_item.send(:utc_offset_to_rational, 0)
126
- assert_equal Rational(1, 24), a_time_stack_item.send(:utc_offset_to_rational, 3600)
127
- end
128
-
129
- def test_datetime_in_presence_of_activesupport_timezone
130
- skip('requires ActiveSupport') unless Time.respond_to? :zone
131
- backed_up_zone, backed_up_tzvar = Time.zone, ENV['TZ']
132
-
133
- Time.zone = ENV['TZ'] = 'America/Los_Angeles'
134
- t = DateTime.new(2001, 2, 28, 23, 59, 59.5)
135
- tsi = Timecop::TimeStackItem.new(:freeze, t)
136
-
137
- assert_date_times_equal t, tsi.datetime
138
- ensure
139
- Time.zone, ENV['TZ'] = backed_up_zone, backed_up_tzvar
140
- end
141
-
142
- # Ensure DateTimes handle changing DST properly
143
- def test_datetime_for_dst_to_non_dst
144
- Timecop.freeze(DateTime.parse("2009-12-1 00:38:00 -0500"))
145
- t = DateTime.parse("2009-10-11 00:00:00 -0400")
146
- tsi = Timecop::TimeStackItem.new(:freeze, t)
147
-
148
- assert_date_times_equal t, tsi.datetime
149
- end
150
-
151
- # Ensure DateTimes handle changing DST properly when changing from DateTime to Time
152
- def test_datetime_for_dst_to_time_for_non_dst
153
- Timecop.freeze(DateTime.parse("2009-12-1 00:38:00 -0500"))
154
- t = DateTime.parse("2009-10-11 00:00:00 -0400")
155
- tsi = Timecop::TimeStackItem.new(:freeze, t)
156
-
157
- assert_date_times_equal t.to_time, tsi.time
158
- end
159
-
160
- def test_datetime_for_non_dst_to_dst
161
- Timecop.freeze(DateTime.parse("2009-10-11 00:00:00 -0400"))
162
- t = DateTime.parse("2009-11-30 23:38:00 -0500")
163
- tsi = Timecop::TimeStackItem.new(:freeze, t)
164
- return if !tsi.time.dst?
165
-
166
- assert_date_times_equal t, tsi.datetime
167
- assert_equal Date.new(2009, 12, 1), tsi.date
168
- end
169
-
170
- def test_set_travel_offset_for_travel
171
- t_now = Time.now
172
- t = Time.local(2009, 10, 1, 0, 0, 30)
173
- expected_offset = t - t_now
174
- tsi = Timecop::TimeStackItem.new(:travel, t)
175
-
176
- assert_times_effectively_equal expected_offset, tsi.send(:travel_offset), 1, "Offset not calculated correctly"
177
- end
178
-
179
- def test_set_travel_offset_for_freeze
180
- Timecop.freeze(2009, 10, 1, 0, 0, 0)
181
- t = Time.local(2009, 10, 1, 0, 0, 30)
182
- tsi = Timecop::TimeStackItem.new(:freeze, t)
183
-
184
- assert_nil tsi.send(:travel_offset)
185
- end
186
-
187
- def test_timezones
188
- Time.zone = "Europe/Zurich"
189
- time = Time.zone.parse("2012-12-27T12:12:12+08:00")
190
- Timecop.freeze(time) do |frozen_time|
191
- assert_equal time, frozen_time
192
- end
193
- end
194
-
195
- def test_timezones_with_parsed_string
196
- Time.zone = "Europe/Zurich"
197
- time_string = "2012-12-27 12:12"
198
- expected_time = Time.zone.parse(time_string)
199
- Timecop.freeze(time_string) do |frozen_time|
200
- assert_equal expected_time, frozen_time
201
- end
202
- end
203
-
204
- def test_timezones_apply_dates
205
- Time.zone = "Central Time (US & Canada)"
206
- time = Time.zone.local(2013,1,3)
207
-
208
- Timecop.freeze(time) do
209
- assert_equal time.to_date, Time.zone.now.to_date
210
- end
211
- end
212
-
213
- def test_set_scaling_factor_for_scale
214
- t_now = Time.now
215
- t = Time.local(2009, 10, 1, 0, 0, 30)
216
- expected_offset = t - t_now
217
- tsi = Timecop::TimeStackItem.new(:scale, 4, t)
218
-
219
- assert_times_effectively_equal expected_offset, tsi.send(:travel_offset), 1, "Offset not calculated correctly"
220
- assert_equal tsi.send(:scaling_factor), 4, "Scaling factor not set"
221
- end
222
-
223
- def test_parse_only_string_with_active_support
224
- Time.expects(:parse).never
225
- Timecop.freeze(2011, 01, 02, hour=0, minute=0, second=0)
226
- end
227
-
228
- def test_parse_date
229
- Timecop.freeze(Date.new(2012, 6, 9))
230
- end
231
-
232
- def test_time_zone_returns_nil
233
- Time.zone = nil
234
- Timecop.freeze
235
- end
236
-
237
- def test_nsecs_are_set
238
- time = Time.now
239
- Timecop.freeze time
240
- assert_equal time, Time.now
241
- assert_equal time.nsec, Time.now.nsec if (Time.now.respond_to?(:nsec))
242
- end
243
-
244
- def test_time_with_different_timezone_keeps_nsec
245
- Time.zone = "Tokyo"
246
- t = Time.now
247
- Timecop.freeze(t) do
248
- assert_equal t, Time.now
249
- assert_equal t.nsec, Time.now.nsec if (Time.now.respond_to?(:nsec))
250
- end
251
- end
252
-
253
- def test_time_now_always_returns_local_time
254
- Time.zone = "Tokyo"
255
- t = Time.utc(2000, 1, 1)
256
- Timecop.freeze(t) do
257
- assert_equal t.getlocal.zone, Time.now.zone
258
- end
259
- end
260
-
261
- def test_time_zone_now_returns_time_in_that_zone
262
- Time.zone = "Hawaii"
263
- t = Time.utc(2000, 1, 1)
264
- Timecop.freeze(t) do
265
- assert_equal t, Time.zone.now
266
- assert_equal 'HST', Time.zone.now.zone
267
- end
268
- end
269
-
270
- def test_freezing_a_time_leaves_timezone_intact
271
- Time.zone = "Tokyo"
272
- t = Time.now
273
- t_dup = t.dup
274
- Timecop.freeze(t) {}
275
- assert_equal t_dup.zone, t.zone
276
- end
277
-
278
- def test_freezing_a_time_with_zone_returns_proper_zones
279
- Time.zone = "Hawaii"
280
- t = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1), ActiveSupport::TimeZone['Tokyo'])
281
- Timecop.freeze(t) do
282
- local_now = Time.now
283
- assert_equal t, local_now
284
- assert_equal t.getlocal.zone, local_now.zone
285
-
286
- zoned_now = Time.zone.now
287
- assert_equal t, zoned_now
288
- assert_equal 'HST', zoned_now.zone
289
- end
290
- end
291
-
292
- def test_datetime_timezones
293
- dt = DateTime.new(2011,1,3,15,25,0,"-6")
294
- Timecop.freeze(dt) do
295
- now = DateTime.now
296
- assert_equal dt, now, "#{dt.to_f}, #{now.to_f}"
297
- end
298
- end
299
- end
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
@@ -1,118 +0,0 @@
1
- require_relative "test_helper"
2
- require 'timecop'
3
-
4
- class TestTimecopWithoutDate < Minitest::Test
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.24, "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