timecop 0.5.2 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,20 @@
1
1
  require 'date'
2
- require 'test_helper'
3
- require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
2
+ require_relative "test_helper"
3
+ require 'timecop'
4
4
 
5
- class TestTimeStackItem < Test::Unit::TestCase
6
-
5
+ require 'active_support/all'
6
+
7
+ class TestTimeStackItem < Minitest::Test
7
8
  def teardown
8
- Timecop.active_support = nil
9
9
  Timecop.return
10
+ Time.zone = nil
10
11
  end
11
-
12
+
12
13
  def test_new_with_time
13
14
  t = Time.now
14
15
  y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
15
16
  stack_item = Timecop::TimeStackItem.new(:freeze, t)
17
+
16
18
  assert_equal y, stack_item.year
17
19
  assert_equal m, stack_item.month
18
20
  assert_equal d, stack_item.day
@@ -25,6 +27,7 @@ class TestTimeStackItem < Test::Unit::TestCase
25
27
  t = Time.new(2012, 7, 28, 20, 0)
26
28
  y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
27
29
  stack_item = Timecop::TimeStackItem.new(:freeze, t)
30
+
28
31
  assert_equal y, stack_item.year
29
32
  assert_equal m, stack_item.month
30
33
  assert_equal d, stack_item.day
@@ -32,11 +35,12 @@ class TestTimeStackItem < Test::Unit::TestCase
32
35
  assert_equal min, stack_item.min
33
36
  assert_equal s, stack_item.sec
34
37
  end
35
-
38
+
36
39
  def test_new_with_datetime_now
37
40
  t = DateTime.now
38
41
  y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
39
42
  stack_item = Timecop::TimeStackItem.new(:freeze, t)
43
+
40
44
  assert_equal y, stack_item.year
41
45
  assert_equal m, stack_item.month
42
46
  assert_equal d, stack_item.day
@@ -44,19 +48,21 @@ class TestTimeStackItem < Test::Unit::TestCase
44
48
  assert_equal min, stack_item.min
45
49
  assert_equal s, stack_item.sec
46
50
  end
47
-
51
+
48
52
  def test_new_with_datetime_in_different_timezone
49
53
  each_timezone do
50
54
  t = DateTime.parse("2009-10-11 00:38:00 +0200")
51
55
  stack_item = Timecop::TimeStackItem.new(:freeze, t)
56
+
52
57
  assert_date_times_equal(t, stack_item.datetime)
53
58
  end
54
59
  end
55
-
60
+
56
61
  def test_new_with_date
57
62
  date = Date.today
58
63
  y, m, d, h, min, s = date.year, date.month, date.day, 0, 0, 0
59
64
  stack_item = Timecop::TimeStackItem.new(:freeze, date)
65
+
60
66
  assert_equal y, stack_item.year
61
67
  assert_equal m, stack_item.month
62
68
  assert_equal d, stack_item.day
@@ -64,7 +70,7 @@ class TestTimeStackItem < Test::Unit::TestCase
64
70
  assert_equal min, stack_item.min
65
71
  assert_equal s, stack_item.sec
66
72
  end
67
-
73
+
68
74
  # Due to the nature of this test (calling Time.now once in this test and
69
75
  # once in #new), this test may fail when two subsequent calls
70
76
  # to Time.now return a different second.
@@ -72,6 +78,7 @@ class TestTimeStackItem < Test::Unit::TestCase
72
78
  t = Time.now
73
79
  y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
74
80
  stack_item = Timecop::TimeStackItem.new(:freeze, 0)
81
+
75
82
  assert_equal y, stack_item.year
76
83
  assert_equal m, stack_item.month
77
84
  assert_equal d, stack_item.day
@@ -79,10 +86,24 @@ class TestTimeStackItem < Test::Unit::TestCase
79
86
  assert_equal min, stack_item.min
80
87
  assert_equal s, stack_item.sec
81
88
  end
82
-
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
+
83
103
  def test_new_with_individual_arguments
84
104
  y, m, d, h, min, s = 2008, 10, 10, 10, 10, 10
85
105
  stack_item = Timecop::TimeStackItem.new(:freeze, y, m, d, h, min, s)
106
+
86
107
  assert_equal y, stack_item.year
87
108
  assert_equal m, stack_item.month
88
109
  assert_equal d, stack_item.day
@@ -90,130 +111,189 @@ class TestTimeStackItem < Test::Unit::TestCase
90
111
  assert_equal min, stack_item.min
91
112
  assert_equal s, stack_item.sec
92
113
  end
93
-
114
+
94
115
  def test_rational_to_utc_offset
95
116
  assert_equal -14400, a_time_stack_item.send(:rational_to_utc_offset, Rational(-1, 6))
96
117
  assert_equal -18000, a_time_stack_item.send(:rational_to_utc_offset, Rational(-5, 24))
97
118
  assert_equal 0, a_time_stack_item.send(:rational_to_utc_offset, Rational(0, 1))
98
119
  assert_equal 3600, a_time_stack_item.send(:rational_to_utc_offset, Rational(1, 24))
99
120
  end
100
-
121
+
101
122
  def test_utc_offset_to_rational
102
123
  assert_equal Rational(-1, 6), a_time_stack_item.send(:utc_offset_to_rational, -14400)
103
124
  assert_equal Rational(-5, 24), a_time_stack_item.send(:utc_offset_to_rational, -18000)
104
125
  assert_equal Rational(0, 1), a_time_stack_item.send(:utc_offset_to_rational, 0)
105
126
  assert_equal Rational(1, 24), a_time_stack_item.send(:utc_offset_to_rational, 3600)
106
127
  end
107
-
108
- # Ensure DST adjustment is calculated properly for DateTime's
109
- def test_compute_dst_adjustment_for_dst_to_dst
110
- Timecop.freeze(DateTime.parse("2009-10-1 00:38:00 -0400"))
111
- t = DateTime.parse("2009-10-11 00:00:00 -0400")
112
- tsi = Timecop::TimeStackItem.new(:freeze, t)
113
- return if !(Time.now.dst? && tsi.time.dst?)
114
- assert_equal 0, tsi.send(:dst_adjustment)
115
- end
116
-
117
- def test_compute_dst_adjustment_for_non_dst_to_non_dst
118
- Timecop.freeze(DateTime.parse("2009-12-1 00:38:00 -0400"))
119
- t = DateTime.parse("2009-12-11 00:00:00 -0400")
120
- tsi = Timecop::TimeStackItem.new(:freeze, t)
121
- return if Time.now.dst? || tsi.time.dst?
122
- assert_equal 0, tsi.send(:dst_adjustment)
123
- end
124
-
125
- def test_compute_dst_adjustment_for_dst_to_non_dst
126
- Timecop.freeze(DateTime.parse("2009-10-1 00:38:00 -0400"))
127
- t = DateTime.parse("2009-12-11 00:00:00 -0400")
128
- tsi = Timecop::TimeStackItem.new(:freeze, t)
129
- return if !Time.now.dst? || tsi.time.dst?
130
- assert_equal 60 * 60, tsi.send(:dst_adjustment)
131
- end
132
-
133
- def test_compute_dst_adjustment_for_non_dst_to_dst
134
- Timecop.freeze(DateTime.parse("2009-12-1 00:38:00 -0400"))
135
- t = DateTime.parse("2009-10-11 00:00:00 -0400")
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)
136
135
  tsi = Timecop::TimeStackItem.new(:freeze, t)
137
- return if Time.now.dst? || !tsi.time.dst?
138
- assert_equal -1 * 60 * 60, tsi.send(:dst_adjustment)
136
+
137
+ assert_date_times_equal t, tsi.datetime
138
+ ensure
139
+ Time.zone, ENV['TZ'] = backed_up_zone, backed_up_tzvar
139
140
  end
140
-
141
+
141
142
  # Ensure DateTimes handle changing DST properly
142
143
  def test_datetime_for_dst_to_non_dst
143
144
  Timecop.freeze(DateTime.parse("2009-12-1 00:38:00 -0500"))
144
145
  t = DateTime.parse("2009-10-11 00:00:00 -0400")
145
146
  tsi = Timecop::TimeStackItem.new(:freeze, t)
147
+
146
148
  assert_date_times_equal t, tsi.datetime
147
149
  end
148
-
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
+
149
160
  def test_datetime_for_non_dst_to_dst
150
161
  Timecop.freeze(DateTime.parse("2009-10-11 00:00:00 -0400"))
151
162
  t = DateTime.parse("2009-11-30 23:38:00 -0500")
152
163
  tsi = Timecop::TimeStackItem.new(:freeze, t)
153
164
  return if !tsi.time.dst?
165
+
154
166
  assert_date_times_equal t, tsi.datetime
155
167
  assert_equal Date.new(2009, 12, 1), tsi.date
156
168
  end
157
-
158
- # Ensure @travel_offset is set properly
169
+
159
170
  def test_set_travel_offset_for_travel
160
- # Timecop.freeze(2009, 10, 1, 0, 0, 0)
161
171
  t_now = Time.now
162
172
  t = Time.local(2009, 10, 1, 0, 0, 30)
163
173
  expected_offset = t - t_now
164
174
  tsi = Timecop::TimeStackItem.new(:travel, t)
175
+
165
176
  assert_times_effectively_equal expected_offset, tsi.send(:travel_offset), 1, "Offset not calculated correctly"
166
177
  end
167
-
178
+
168
179
  def test_set_travel_offset_for_freeze
169
180
  Timecop.freeze(2009, 10, 1, 0, 0, 0)
170
181
  t = Time.local(2009, 10, 1, 0, 0, 30)
171
182
  tsi = Timecop::TimeStackItem.new(:freeze, t)
172
- assert_equal nil, tsi.send(:travel_offset)
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
173
202
  end
174
203
 
175
- def test_set_scaling_factor_for_lens
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
176
214
  t_now = Time.now
177
215
  t = Time.local(2009, 10, 1, 0, 0, 30)
178
216
  expected_offset = t - t_now
179
217
  tsi = Timecop::TimeStackItem.new(:scale, 4, t)
218
+
180
219
  assert_times_effectively_equal expected_offset, tsi.send(:travel_offset), 1, "Offset not calculated correctly"
181
220
  assert_equal tsi.send(:scaling_factor), 4, "Scaling factor not set"
182
221
  end
183
222
 
184
- def test_parse_string_date_with_active_support
185
- date = '2012-01-02'
186
- Time.expects(:parse).with(date).returns(Time.local(2012, 01, 02))
187
- Timecop.freeze(date)
188
- end
189
-
190
223
  def test_parse_only_string_with_active_support
191
224
  Time.expects(:parse).never
192
225
  Timecop.freeze(2011, 01, 02, hour=0, minute=0, second=0)
193
226
  end
194
227
 
195
- def test_parse_with_active_support_off
196
- date = '2012-01-02'
197
- Timecop.active_support = false
198
- Time.expects(:parse).never
199
- Timecop.freeze(date)
228
+ def test_parse_date
229
+ Timecop.freeze(Date.new(2012, 6, 9))
200
230
  end
201
231
 
202
- def test_uses_active_supports_in_time_zone
232
+ def test_time_zone_returns_nil
233
+ Time.zone = nil
234
+ Timecop.freeze
235
+ end
236
+
237
+ def test_nsecs_are_set
203
238
  time = Time.now
204
- Time.any_instance.expects(:in_time_zone).returns(time)
205
- Timecop::TimeStackItem.new(:freeze, time)
239
+ Timecop.freeze time
240
+ assert_equal time, Time.now
241
+ assert_equal time.nsec, Time.now.nsec if (Time.now.respond_to?(:nsec))
206
242
  end
207
243
 
208
- def test_configured_off_active_support_in_time_zone_xxx
209
- Timecop.active_support = false
210
- Time.any_instance.expects(:in_time_zone).never
211
- Timecop::TimeStackItem.new(:freeze, Time.now)
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
212
251
  end
213
252
 
214
- def test_parse_date
215
- assert_nothing_raised do
216
- Timecop.freeze(Date.new(2012, 6, 9))
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}"
217
297
  end
218
298
  end
219
299
  end