timecop 0.7.1 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.markdown +2 -5
- data/lib/timecop/time_extensions.rb +43 -6
- data/lib/timecop/time_stack_item.rb +4 -7
- data/lib/timecop/timecop.rb +3 -1
- data/lib/timecop/version.rb +1 -1
- data/test/test_helper.rb +14 -12
- data/test/time_stack_item_test.rb +4 -8
- data/test/timecop_test.rb +46 -27
- data/test/timecop_without_date_but_with_time_test.rb +4 -6
- data/test/timecop_without_date_test.rb +20 -21
- metadata +9 -11
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f776ac9891fa370f63fc78ad33aa01866e84a298
|
4
|
+
data.tar.gz: 5ebcc9bd5855f97e79d4dd62b7694d99b7449783
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5ca661e7b650a72e902e429c473fb338d3756866d453126f4330a541ca2ba3965566e8ed4a30888948eab9b6d58cd2ddda3640ec4284ef58548ed877013882ee
|
7
|
+
data.tar.gz: 1c678be19de2c126f053908349fb17c1a57ce48f305a0dc079b34257e76f2a9f4d73b83142c0ceecda03288b938f40808095688566058289faa33d1e34d4210e
|
data/README.markdown
CHANGED
@@ -2,9 +2,6 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://secure.travis-ci.org/travisjeffery/timecop.png)](http://travis-ci.org/travisjeffery/timecop)
|
4
4
|
|
5
|
-
- [Source](http://github.com/travisjeffery/timecop)
|
6
|
-
- [Documentation](http://johntrupiano.rubyforge.org/timecop)
|
7
|
-
|
8
5
|
## DESCRIPTION
|
9
6
|
|
10
7
|
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.
|
@@ -111,6 +108,8 @@ Time.now
|
|
111
108
|
# => 2012-09-21 06:22:59 -0500
|
112
109
|
```
|
113
110
|
|
111
|
+
See [#42](https://github.com/travisjeffery/timecop/pull/42) for more information, thanks to Ken Mayer, David Holcomb, and Pivotal Labs.
|
112
|
+
|
114
113
|
### Timecop.safe_mode
|
115
114
|
|
116
115
|
Safe mode forces you to use Timecop with the block syntax since it always puts time back the way it was. If you are running in safe mode and use Timecop without the block syntax `Timecop::SafeModeException` will be raised to tell the user they are not being safe.
|
@@ -128,8 +127,6 @@ Timecop.freeze
|
|
128
127
|
# => Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed.
|
129
128
|
```
|
130
129
|
|
131
|
-
See [#42](https://github.com/travisjeffery/timecop/pull/42) for more information, thanks to Ken Mayer, David Holcomb, and Pivotal Labs.
|
132
|
-
|
133
130
|
## Contribute
|
134
131
|
|
135
132
|
timecop is maintained by [travisjeffery](http://github.com/travisjeffery), and
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require 'date'
|
2
1
|
require 'time'
|
2
|
+
require 'date'
|
3
3
|
|
4
4
|
class Time #:nodoc:
|
5
5
|
class << self
|
@@ -27,6 +27,16 @@ class Time #:nodoc:
|
|
27
27
|
end
|
28
28
|
|
29
29
|
class Date #:nodoc:
|
30
|
+
WEEKDAYS = {
|
31
|
+
"sunday" => 0,
|
32
|
+
"monday" => 1,
|
33
|
+
"tuesday" => 2,
|
34
|
+
"wednesday" => 3,
|
35
|
+
"thursday" => 4,
|
36
|
+
"friday" => 5,
|
37
|
+
"saturday" => 6
|
38
|
+
}
|
39
|
+
|
30
40
|
class << self
|
31
41
|
def mock_date
|
32
42
|
mocked_time_stack_item = Timecop.top_stack_item
|
@@ -46,13 +56,27 @@ class Date #:nodoc:
|
|
46
56
|
def strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY)
|
47
57
|
unless start == Date::ITALY
|
48
58
|
raise ArgumentError, "Timecop's #{self}::#{__method__} only " +
|
49
|
-
|
59
|
+
"supports Date::ITALY for the start argument."
|
50
60
|
end
|
51
61
|
|
52
62
|
Time.strptime(str, fmt).to_date
|
53
63
|
end
|
54
64
|
|
55
65
|
alias_method :strptime, :strptime_with_mock_date
|
66
|
+
|
67
|
+
def parse_with_mock_date(str)
|
68
|
+
if WEEKDAYS.keys.include?(str.downcase)
|
69
|
+
offset = WEEKDAYS[str.downcase] - Date.today.wday
|
70
|
+
|
71
|
+
Date.today + offset
|
72
|
+
else
|
73
|
+
parse_without_mock_date(str)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
alias_method :parse_without_mock_date, :parse
|
78
|
+
alias_method :parse, :parse_with_mock_date
|
79
|
+
|
56
80
|
end
|
57
81
|
end
|
58
82
|
|
@@ -63,14 +87,27 @@ class DateTime #:nodoc:
|
|
63
87
|
mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.datetime(self)
|
64
88
|
end
|
65
89
|
|
66
|
-
def now_without_mock_time
|
67
|
-
Time.now_without_mock_time.to_datetime
|
68
|
-
end
|
69
|
-
|
70
90
|
def now_with_mock_time
|
71
91
|
mock_time || now_without_mock_time
|
72
92
|
end
|
73
93
|
|
94
|
+
alias_method :now_without_mock_time, :now
|
95
|
+
|
74
96
|
alias_method :now, :now_with_mock_time
|
97
|
+
|
98
|
+
def parse_with_mock_date(str)
|
99
|
+
if Date::WEEKDAYS.keys.include?(str.downcase)
|
100
|
+
offset = Date::WEEKDAYS[str.downcase] - DateTime.now.wday
|
101
|
+
|
102
|
+
parsed_weekday =(DateTime.now + offset)
|
103
|
+
|
104
|
+
DateTime.new(parsed_weekday.year, parsed_weekday.month, parsed_weekday.day, 0, 0, 0, 0)
|
105
|
+
else
|
106
|
+
parse_without_mock_date(str)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
alias_method :parse_without_mock_date, :parse
|
111
|
+
alias_method :parse, :parse_with_mock_date
|
75
112
|
end
|
76
113
|
end
|
@@ -6,6 +6,7 @@ class Timecop
|
|
6
6
|
|
7
7
|
def initialize(mock_type, *args)
|
8
8
|
raise "Unknown mock_type #{mock_type}" unless [:freeze, :travel, :scale].include?(mock_type)
|
9
|
+
@travel_offset = @scaling_factor = nil
|
9
10
|
@scaling_factor = args.shift if mock_type == :scale
|
10
11
|
@mock_type = mock_type
|
11
12
|
@time = parse_time(*args)
|
@@ -75,12 +76,8 @@ class Timecop
|
|
75
76
|
|
76
77
|
def datetime(datetime_klass = DateTime)
|
77
78
|
if Float.method_defined?(:to_r)
|
78
|
-
|
79
|
-
|
80
|
-
datetime_klass.new(year, month, day, hour, min, (fractions_of_a_second + sec), utc_offset_to_rational(utc_offset))
|
81
|
-
else
|
82
|
-
datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset))
|
83
|
-
end
|
79
|
+
fractions_of_a_second = time.to_f % 1
|
80
|
+
datetime_klass.new(year, month, day, hour, min, (fractions_of_a_second + sec), utc_offset_to_rational(utc_offset))
|
84
81
|
else
|
85
82
|
datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset))
|
86
83
|
end
|
@@ -137,4 +134,4 @@ class Timecop
|
|
137
134
|
Time.respond_to?(:zone) && Time.zone ? Time.zone : Time
|
138
135
|
end
|
139
136
|
end
|
140
|
-
|
137
|
+
end
|
data/lib/timecop/timecop.rb
CHANGED
@@ -31,6 +31,7 @@ class Timecop
|
|
31
31
|
# 3. Timecop.freeze(date_inst)
|
32
32
|
# 4. Timecop.freeze(offset_in_seconds)
|
33
33
|
# 5. Timecop.freeze(year, month, day, hour=0, minute=0, second=0)
|
34
|
+
# 6. Timecop.freeze() # Defaults to Time.now
|
34
35
|
#
|
35
36
|
# When a block is also passed, Time.now, DateTime.now and Date.today are all reset to their
|
36
37
|
# previous values after the block has finished executing. This allows us to nest multiple
|
@@ -107,7 +108,7 @@ class Timecop
|
|
107
108
|
end
|
108
109
|
|
109
110
|
def safe_mode?
|
110
|
-
|
111
|
+
@safe_mode ||= false
|
111
112
|
end
|
112
113
|
|
113
114
|
private
|
@@ -150,6 +151,7 @@ class Timecop
|
|
150
151
|
current_baseline = @baseline
|
151
152
|
unmock!
|
152
153
|
yield
|
154
|
+
ensure
|
153
155
|
@_stack = current_stack
|
154
156
|
@baseline = current_baseline
|
155
157
|
end
|
data/lib/timecop/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,40 +1,42 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'bundler/setup'
|
3
|
-
require '
|
3
|
+
require 'minitest/autorun'
|
4
|
+
|
5
|
+
$VERBOSE = true # enable ruby warnings
|
6
|
+
|
4
7
|
begin
|
5
8
|
require 'mocha/setup'
|
6
9
|
rescue LoadError
|
7
10
|
require 'mocha'
|
8
11
|
end
|
9
12
|
|
10
|
-
class
|
11
|
-
|
13
|
+
class Minitest::Unit::TestCase
|
12
14
|
private
|
13
15
|
# Tests to see that two times are within the given distance,
|
14
16
|
# in seconds, from each other.
|
15
17
|
def times_effectively_equal(time1, time2, seconds_interval = 1)
|
16
18
|
(time1 - time2).abs <= seconds_interval
|
17
19
|
end
|
18
|
-
|
20
|
+
|
19
21
|
def assert_times_effectively_equal(time1, time2, seconds_interval = 1, msg = nil)
|
20
22
|
assert times_effectively_equal(time1, time2, seconds_interval), "#{msg}: time1 = #{time1.to_s}, time2 = #{time2.to_s}"
|
21
23
|
end
|
22
|
-
|
24
|
+
|
23
25
|
def assert_times_effectively_not_equal(time1, time2, seconds_interval = 1, msg = nil)
|
24
26
|
assert !times_effectively_equal(time1, time2, seconds_interval), "#{msg}: time1 = #{time1.to_s}, time2 = #{time2.to_s}"
|
25
27
|
end
|
26
|
-
|
28
|
+
|
27
29
|
# Gets the local offset (supplied by ENV['TZ'] or your computer's clock)
|
28
30
|
# At the given timestamp, or Time.now if not time is given.
|
29
31
|
def local_offset(time = Time.now)
|
30
32
|
Time.at(time.to_i).to_datetime.offset
|
31
33
|
end
|
32
|
-
|
34
|
+
|
33
35
|
TIMEZONES = ["Europe/Paris", "UTC", "America/Chicago"]
|
34
|
-
|
36
|
+
|
35
37
|
def each_timezone
|
36
38
|
old_tz = ENV["TZ"]
|
37
|
-
|
39
|
+
|
38
40
|
begin
|
39
41
|
TIMEZONES.each do |timezone|
|
40
42
|
ENV["TZ"] = timezone
|
@@ -44,13 +46,13 @@ class Test::Unit::TestCase
|
|
44
46
|
ENV["TZ"] = old_tz
|
45
47
|
end
|
46
48
|
end
|
47
|
-
|
49
|
+
|
48
50
|
def a_time_stack_item
|
49
51
|
Timecop::TimeStackItem.new(:freeze, 2008, 1, 1, 0, 0, 0)
|
50
52
|
end
|
51
|
-
|
53
|
+
|
52
54
|
def assert_date_times_equal(dt1, dt2)
|
53
55
|
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}"
|
54
56
|
end
|
55
|
-
|
57
|
+
|
56
58
|
end
|
@@ -4,7 +4,7 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
|
4
4
|
|
5
5
|
require 'active_support/all'
|
6
6
|
|
7
|
-
class TestTimeStackItem <
|
7
|
+
class TestTimeStackItem < Minitest::Unit::TestCase
|
8
8
|
def teardown
|
9
9
|
Timecop.return
|
10
10
|
Time.zone = nil
|
@@ -204,16 +204,12 @@ class TestTimeStackItem < Test::Unit::TestCase
|
|
204
204
|
end
|
205
205
|
|
206
206
|
def test_parse_date
|
207
|
-
|
208
|
-
Timecop.freeze(Date.new(2012, 6, 9))
|
209
|
-
end
|
207
|
+
Timecop.freeze(Date.new(2012, 6, 9))
|
210
208
|
end
|
211
209
|
|
212
210
|
def test_time_zone_returns_nil
|
213
211
|
Time.zone = nil
|
214
|
-
|
215
|
-
Timecop.freeze
|
216
|
-
end
|
212
|
+
Timecop.freeze
|
217
213
|
end
|
218
214
|
|
219
215
|
def test_nsecs_are_set
|
@@ -273,7 +269,7 @@ class TestTimeStackItem < Test::Unit::TestCase
|
|
273
269
|
|
274
270
|
def test_datetime_timezones
|
275
271
|
dt = DateTime.new(2011,1,3,15,25,0,"-6")
|
276
|
-
Timecop.
|
272
|
+
Timecop.freeze(dt) do
|
277
273
|
now = DateTime.now
|
278
274
|
assert_equal dt, now, "#{dt.to_f}, #{now.to_f}"
|
279
275
|
end
|
data/test/timecop_test.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
require 'date'
|
2
1
|
require File.join(File.dirname(__FILE__), "test_helper")
|
3
2
|
require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
4
3
|
|
5
|
-
class TestTimecop <
|
4
|
+
class TestTimecop < Minitest::Unit::TestCase
|
6
5
|
def teardown
|
7
6
|
Timecop.return
|
8
7
|
end
|
@@ -31,7 +30,7 @@ class TestTimecop < Test::Unit::TestCase
|
|
31
30
|
assert_equal frozen_time, Time.now
|
32
31
|
end
|
33
32
|
end
|
34
|
-
|
33
|
+
|
35
34
|
def test_freeze_then_return_unsets_mock_time
|
36
35
|
Timecop.freeze(1)
|
37
36
|
Timecop.return
|
@@ -59,8 +58,11 @@ class TestTimecop < Test::Unit::TestCase
|
|
59
58
|
def test_travel_does_not_reduce_precision_of_datetime
|
60
59
|
# requires to_r on Float (>= 1.9)
|
61
60
|
if Float.method_defined?(:to_r)
|
62
|
-
Timecop.travel(1)
|
63
|
-
|
61
|
+
Timecop.travel(Time.new(2014, 1, 1, 0, 0, 0))
|
62
|
+
assert DateTime.now != DateTime.now
|
63
|
+
|
64
|
+
Timecop.travel(Time.new(2014, 1, 1, 0, 0, 59))
|
65
|
+
assert DateTime.now != DateTime.now
|
64
66
|
end
|
65
67
|
end
|
66
68
|
|
@@ -113,7 +115,7 @@ class TestTimecop < Test::Unit::TestCase
|
|
113
115
|
end
|
114
116
|
assert_equal t, Time.now
|
115
117
|
end
|
116
|
-
|
118
|
+
assert t != Time.now
|
117
119
|
end
|
118
120
|
|
119
121
|
def test_freeze_with_time_instance_works_as_expected
|
@@ -124,9 +126,9 @@ class TestTimecop < Test::Unit::TestCase
|
|
124
126
|
assert_equal Date.new(2008, 10, 10), Date.today
|
125
127
|
end
|
126
128
|
|
127
|
-
|
128
|
-
|
129
|
-
|
129
|
+
assert t != Time.now
|
130
|
+
assert DateTime.new(2008, 10, 10, 10, 10, 10, local_offset) != DateTime.now
|
131
|
+
assert Date.new(2008, 10, 10) != Date.today
|
130
132
|
end
|
131
133
|
|
132
134
|
def test_freeze_with_datetime_on_specific_timezone_during_dst
|
@@ -185,9 +187,9 @@ class TestTimecop < Test::Unit::TestCase
|
|
185
187
|
assert_equal Time.local(2008, 10, 10, 0, 0, 0), Time.now
|
186
188
|
assert_date_times_equal DateTime.new(2008, 10, 10, 0, 0, 0, local_offset), DateTime.now
|
187
189
|
end
|
188
|
-
|
189
|
-
|
190
|
-
|
190
|
+
assert d != Date.today
|
191
|
+
assert Time.local(2008, 10, 10, 0, 0, 0) != Time.now
|
192
|
+
assert DateTime.new(2008, 10, 10, 0, 0, 0, local_offset) != DateTime.now
|
191
193
|
end
|
192
194
|
|
193
195
|
def test_freeze_with_integer_instance_works_as_expected
|
@@ -202,9 +204,9 @@ class TestTimecop < Test::Unit::TestCase
|
|
202
204
|
assert_equal Date.new(2008, 10, 10), Date.today
|
203
205
|
end
|
204
206
|
end
|
205
|
-
|
206
|
-
|
207
|
-
|
207
|
+
assert t != Time.now
|
208
|
+
assert DateTime.new(2008, 10, 10, 10, 10, 10) != DateTime.now
|
209
|
+
assert Date.new(2008, 10, 10) != Date.today
|
208
210
|
end
|
209
211
|
|
210
212
|
def test_exception_thrown_in_freeze_block_properly_resets_time
|
@@ -215,11 +217,19 @@ class TestTimecop < Test::Unit::TestCase
|
|
215
217
|
raise "blah exception"
|
216
218
|
end
|
217
219
|
rescue
|
218
|
-
|
220
|
+
assert t != Time.now
|
219
221
|
assert_nil Time.send(:mock_time)
|
220
222
|
end
|
221
223
|
end
|
222
224
|
|
225
|
+
def test_exception_thrown_in_return_block_restores_previous_time
|
226
|
+
t = Time.local(2008, 10, 10, 10, 10, 10)
|
227
|
+
Timecop.freeze(t) do
|
228
|
+
Timecop.return { raise 'foobar' } rescue nil
|
229
|
+
assert_equal t, Time.now
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
223
233
|
def test_freeze_freezes_time
|
224
234
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
225
235
|
now = Time.now
|
@@ -252,7 +262,7 @@ class TestTimecop < Test::Unit::TestCase
|
|
252
262
|
if ENV['TZ'] == 'UTC'
|
253
263
|
assert_equal(local_offset, 0, "Local offset not be zero for #{ENV['TZ']}")
|
254
264
|
else
|
255
|
-
|
265
|
+
assert(local_offset, 0 != "Local offset should not be zero for #{ENV['TZ']}")
|
256
266
|
end
|
257
267
|
assert_equal local_offset, DateTime.now.offset, "Failed for timezone: #{ENV['TZ']}"
|
258
268
|
end
|
@@ -283,6 +293,19 @@ class TestTimecop < Test::Unit::TestCase
|
|
283
293
|
end
|
284
294
|
end
|
285
295
|
end
|
296
|
+
|
297
|
+
def test_freeze_without_arguments_instance_works_as_expected
|
298
|
+
t = Time.local(2008, 10, 10, 10, 10, 10)
|
299
|
+
Timecop.freeze(t) do
|
300
|
+
assert_equal t, Time.now
|
301
|
+
Timecop.freeze do
|
302
|
+
assert_equal t, Time.now
|
303
|
+
assert_equal Time.local(2008, 10, 10, 10, 10, 10), Time.now
|
304
|
+
assert_equal Date.new(2008, 10, 10), Date.today
|
305
|
+
end
|
306
|
+
end
|
307
|
+
assert t != Time.now
|
308
|
+
end
|
286
309
|
|
287
310
|
def test_destructive_methods_on_frozen_time
|
288
311
|
# Use any time zone other than UTC.
|
@@ -314,13 +337,13 @@ class TestTimecop < Test::Unit::TestCase
|
|
314
337
|
end
|
315
338
|
|
316
339
|
def test_recursive_travel_yields_correct_time
|
317
|
-
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
340
|
+
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
318
341
|
Timecop.travel(2008, 9, 9, 9, 9, 9) do |inner_freeze|
|
319
342
|
assert_times_effectively_equal inner_freeze, Time.now, 1, "Failed to yield current time back to block"
|
320
343
|
end
|
321
344
|
end
|
322
345
|
end
|
323
|
-
|
346
|
+
|
324
347
|
def test_recursive_travel_then_freeze
|
325
348
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
326
349
|
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
@@ -442,7 +465,7 @@ class TestTimecop < Test::Unit::TestCase
|
|
442
465
|
end
|
443
466
|
|
444
467
|
def test_not_callable_send_travel
|
445
|
-
|
468
|
+
assert_raises NoMethodError do
|
446
469
|
Timecop.send_travel(:travel, Time.now - 100)
|
447
470
|
end
|
448
471
|
end
|
@@ -466,7 +489,7 @@ class TestTimecop < Test::Unit::TestCase
|
|
466
489
|
|
467
490
|
def test_raises_when_safe_mode_and_no_block
|
468
491
|
with_safe_mode do
|
469
|
-
|
492
|
+
assert_raises Timecop::SafeModeException do
|
470
493
|
Timecop.freeze
|
471
494
|
end
|
472
495
|
end
|
@@ -474,17 +497,13 @@ class TestTimecop < Test::Unit::TestCase
|
|
474
497
|
|
475
498
|
def test_no_raise_when_safe_mode_and_block_used
|
476
499
|
with_safe_mode do
|
477
|
-
|
478
|
-
Timecop.freeze {}
|
479
|
-
end
|
500
|
+
Timecop.freeze {}
|
480
501
|
end
|
481
502
|
end
|
482
503
|
|
483
504
|
def test_no_raise_when_not_safe_mode_and_no_block
|
484
505
|
with_safe_mode(false) do
|
485
|
-
|
486
|
-
Timecop.freeze
|
487
|
-
end
|
506
|
+
Timecop.freeze
|
488
507
|
end
|
489
508
|
end
|
490
509
|
|
@@ -1,12 +1,10 @@
|
|
1
|
-
require "
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
2
|
|
3
|
-
class TestTimecopWithoutDateButWithTime <
|
3
|
+
class TestTimecopWithoutDateButWithTime < Minitest::Unit::TestCase
|
4
4
|
TIMECOP_LIB = File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
5
5
|
|
6
6
|
def test_loads_properly_when_time_is_required_instead_of_date
|
7
|
-
|
8
|
-
|
9
|
-
require TIMECOP_LIB
|
10
|
-
end
|
7
|
+
require "time"
|
8
|
+
require TIMECOP_LIB
|
11
9
|
end
|
12
10
|
end
|
@@ -1,34 +1,33 @@
|
|
1
|
-
|
2
1
|
require File.join(File.dirname(__FILE__), "test_helper")
|
3
2
|
require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
4
3
|
|
5
|
-
class TestTimecopWithoutDate <
|
6
|
-
|
4
|
+
class TestTimecopWithoutDate < Minitest::Unit::TestCase
|
5
|
+
|
7
6
|
def setup
|
8
7
|
Object.send(:remove_const, :Date) if Object.const_defined?(:Date)
|
9
8
|
Object.send(:remove_const, :DateTime) if Object.const_defined?(:DateTime)
|
10
9
|
end
|
11
|
-
|
10
|
+
|
12
11
|
# just in case...let's really make sure that Timecop is disabled between tests...
|
13
12
|
def teardown
|
14
13
|
Timecop.return
|
15
14
|
end
|
16
|
-
|
15
|
+
|
17
16
|
def test_freeze_changes_and_resets_time
|
18
17
|
# depending on how we're invoked (individually or via the rake test suite)
|
19
18
|
assert !Time.respond_to?(:zone) || Time.zone.nil?
|
20
|
-
|
19
|
+
|
21
20
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
22
|
-
|
21
|
+
assert t != Time.now
|
23
22
|
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
24
23
|
assert_equal t, Time.now
|
25
24
|
end
|
26
|
-
|
25
|
+
assert t != Time.now
|
27
26
|
end
|
28
|
-
|
27
|
+
|
29
28
|
def test_recursive_freeze
|
30
29
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
31
|
-
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
30
|
+
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
32
31
|
assert_equal t, Time.now
|
33
32
|
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
34
33
|
Timecop.freeze(2008, 9, 9, 9, 9, 9) do
|
@@ -38,7 +37,7 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
38
37
|
end
|
39
38
|
assert_nil Time.send(:mock_time)
|
40
39
|
end
|
41
|
-
|
40
|
+
|
42
41
|
def test_exception_thrown_in_freeze_block_properly_resets_time
|
43
42
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
44
43
|
begin
|
@@ -47,11 +46,11 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
47
46
|
raise "blah exception"
|
48
47
|
end
|
49
48
|
rescue
|
50
|
-
|
49
|
+
assert t != Time.now
|
51
50
|
assert_nil Time.send(:mock_time)
|
52
51
|
end
|
53
52
|
end
|
54
|
-
|
53
|
+
|
55
54
|
def test_freeze_freezes_time
|
56
55
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
57
56
|
now = Time.now
|
@@ -63,7 +62,7 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
63
62
|
assert_equal new_t, Time.now
|
64
63
|
end
|
65
64
|
end
|
66
|
-
|
65
|
+
|
67
66
|
def test_travel_keeps_time_moving
|
68
67
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
69
68
|
now = Time.now
|
@@ -74,10 +73,10 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
74
73
|
assert_times_effectively_not_equal new_now, Time.now, 0.25, "Looks like time is not moving"
|
75
74
|
end
|
76
75
|
end
|
77
|
-
|
76
|
+
|
78
77
|
def test_recursive_travel_maintains_each_context
|
79
78
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
80
|
-
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
79
|
+
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
81
80
|
assert((t - Time.now).abs < 50, "Failed to travel time.")
|
82
81
|
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
83
82
|
Timecop.travel(2008, 9, 9, 9, 9, 9) do
|
@@ -88,10 +87,10 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
88
87
|
end
|
89
88
|
assert_nil Time.send(:mock_time)
|
90
89
|
end
|
91
|
-
|
90
|
+
|
92
91
|
def test_recursive_travel_then_freeze
|
93
92
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
94
|
-
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
93
|
+
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
95
94
|
assert((t - Time.now).abs < 50, "Failed to travel time.")
|
96
95
|
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
97
96
|
Timecop.freeze(2008, 9, 9, 9, 9, 9) do
|
@@ -101,10 +100,10 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
101
100
|
end
|
102
101
|
assert_nil Time.send(:mock_time)
|
103
102
|
end
|
104
|
-
|
103
|
+
|
105
104
|
def test_recursive_freeze_then_travel
|
106
105
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
107
|
-
Timecop.freeze(t) do
|
106
|
+
Timecop.freeze(t) do
|
108
107
|
assert_equal t, Time.now
|
109
108
|
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
110
109
|
Timecop.travel(t2) do
|
@@ -113,7 +112,7 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
|
|
113
112
|
end
|
114
113
|
assert_equal t, Time.now
|
115
114
|
end
|
116
|
-
assert_nil Time.send(:mock_time)
|
115
|
+
assert_nil Time.send(:mock_time)
|
117
116
|
end
|
118
117
|
|
119
118
|
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timecop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
5
|
-
prerelease:
|
4
|
+
version: 0.7.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Travis Jeffery
|
@@ -10,7 +9,7 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 2015-02-19 00:00:00.000000000 Z
|
14
13
|
dependencies: []
|
15
14
|
description: A gem providing "time travel" and "time freezing" capabilities, making
|
16
15
|
it dead simple to test time-dependent code. It provides a unified method to mock
|
@@ -28,37 +27,36 @@ files:
|
|
28
27
|
- lib/timecop.rb
|
29
28
|
- lib/timecop/time_extensions.rb
|
30
29
|
- lib/timecop/time_stack_item.rb
|
31
|
-
- lib/timecop/version.rb
|
32
30
|
- lib/timecop/timecop.rb
|
31
|
+
- lib/timecop/version.rb
|
33
32
|
- test/run_tests.sh
|
34
33
|
- test/test_helper.rb
|
35
34
|
- test/time_stack_item_test.rb
|
36
35
|
- test/timecop_test.rb
|
37
|
-
- test/timecop_without_date_test.rb
|
38
36
|
- test/timecop_without_date_but_with_time_test.rb
|
37
|
+
- test/timecop_without_date_test.rb
|
39
38
|
homepage: https://github.com/travisjeffery/timecop
|
40
39
|
licenses:
|
41
40
|
- MIT
|
41
|
+
metadata: {}
|
42
42
|
post_install_message:
|
43
43
|
rdoc_options:
|
44
|
-
- --charset=UTF-8
|
44
|
+
- "--charset=UTF-8"
|
45
45
|
require_paths:
|
46
46
|
- lib
|
47
47
|
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
-
none: false
|
49
48
|
requirements:
|
50
|
-
- -
|
49
|
+
- - ">="
|
51
50
|
- !ruby/object:Gem::Version
|
52
51
|
version: 1.9.2
|
53
52
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
-
none: false
|
55
53
|
requirements:
|
56
|
-
- -
|
54
|
+
- - ">="
|
57
55
|
- !ruby/object:Gem::Version
|
58
56
|
version: '0'
|
59
57
|
requirements: []
|
60
58
|
rubyforge_project: timecop
|
61
|
-
rubygems_version:
|
59
|
+
rubygems_version: 2.2.2
|
62
60
|
signing_key:
|
63
61
|
specification_version: 3
|
64
62
|
summary: A gem providing "time travel" and "time freezing" capabilities, making it
|