timecop 0.7.4 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/README.markdown +10 -3
- data/Rakefile +9 -3
- data/lib/timecop/time_extensions.rb +62 -27
- data/lib/timecop/time_stack_item.rb +11 -8
- data/lib/timecop/timecop.rb +81 -18
- data/lib/timecop/version.rb +1 -1
- data/test/test_helper.rb +9 -8
- data/test/time_stack_item_test.rb +27 -5
- data/test/timecop_test.rb +69 -11
- data/test/timecop_without_date_but_with_time_test.rb +4 -6
- data/test/timecop_without_date_test.rb +4 -4
- metadata +6 -8
- data/test/run_tests.sh +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9e107de03899173505259909d8ec9e008082d8fa4cf551a2326a6bc98890285e
|
4
|
+
data.tar.gz: e50a9c462cafa4feb5a1b47fc720f853c1a7ac62bd5d404ecad3f2b749be0ef4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 427b83dd2d0506193c3c4d947cb1281817a531a64f09f73f004d97c17339665311774e775bc7506e4447ed2c66636e0df2cd954447e079409aef347262ab3e30
|
7
|
+
data.tar.gz: 631df874968afa111e81b255d665972085ec64a6c315b424304148a75c3226eb7e0f259ed7d149c6829950394f340b4cd595fa21720149104b2c4c8e2448b586
|
data/LICENSE
CHANGED
data/README.markdown
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# timecop
|
2
2
|
|
3
|
-
[![Build Status](https://secure.travis-ci.org/travisjeffery/timecop.
|
3
|
+
[![Build Status](https://secure.travis-ci.org/travisjeffery/timecop.svg)](http://travis-ci.org/travisjeffery/timecop)
|
4
4
|
|
5
5
|
## DESCRIPTION
|
6
6
|
|
@@ -51,7 +51,8 @@ describe "some set of tests to mock" do
|
|
51
51
|
Timecop.return
|
52
52
|
end
|
53
53
|
|
54
|
-
it "should do blah blah blah"
|
54
|
+
it "should do blah blah blah" do
|
55
|
+
end
|
55
56
|
end
|
56
57
|
```
|
57
58
|
|
@@ -103,7 +104,7 @@ being able to simulate activity via subsequent calls to your application.
|
|
103
104
|
Timecop.scale(3600)
|
104
105
|
Time.now
|
105
106
|
# => 2012-09-20 21:23:25 -0500
|
106
|
-
# seconds later, hours have
|
107
|
+
# seconds later, hours have passed and it's gone from 9pm at night to 6am in the morning
|
107
108
|
Time.now
|
108
109
|
# => 2012-09-21 06:22:59 -0500
|
109
110
|
```
|
@@ -127,6 +128,12 @@ Timecop.freeze
|
|
127
128
|
# => Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed.
|
128
129
|
```
|
129
130
|
|
131
|
+
### Rails v Ruby Date/Time libraries
|
132
|
+
|
133
|
+
Sometimes [Rails Date/Time methods don't play nicely with Ruby Date/Time methods.](https://rails.lighthouseapp.com/projects/8994/tickets/6410-dateyesterday-datetoday)
|
134
|
+
|
135
|
+
Be careful mixing Ruby `Date.today` with Rails `Date.tomorrow` / `Date.yesterday` as things might break.
|
136
|
+
|
130
137
|
## Contribute
|
131
138
|
|
132
139
|
timecop is maintained by [travisjeffery](http://github.com/travisjeffery), and
|
data/Rakefile
CHANGED
@@ -3,8 +3,6 @@ require 'bundler/gem_tasks'
|
|
3
3
|
require 'rake/testtask'
|
4
4
|
require 'rdoc/task'
|
5
5
|
|
6
|
-
$LOAD_PATH.unshift("lib")
|
7
|
-
|
8
6
|
Rake::RDocTask.new do |rdoc|
|
9
7
|
if File.exist?('VERSION')
|
10
8
|
version = File.read('VERSION')
|
@@ -21,7 +19,15 @@ Rake::RDocTask.new do |rdoc|
|
|
21
19
|
end
|
22
20
|
|
23
21
|
task :test do
|
24
|
-
|
22
|
+
failed = Dir["test/*_test.rb"].map do |test|
|
23
|
+
command = "ruby #{test}"
|
24
|
+
puts
|
25
|
+
puts command
|
26
|
+
command unless system(command)
|
27
|
+
end.compact
|
28
|
+
if failed.any?
|
29
|
+
abort "#{failed.count} Tests failed\n#{failed.join("\n")}"
|
30
|
+
end
|
25
31
|
end
|
26
32
|
|
27
33
|
desc 'Default: run tests'
|
@@ -22,24 +22,15 @@ class Time #:nodoc:
|
|
22
22
|
args.size <= 0 ? now : new_without_mock_time(*args)
|
23
23
|
end
|
24
24
|
|
25
|
+
ruby2_keywords :new_with_mock_time if Module.private_method_defined?(:ruby2_keywords)
|
26
|
+
|
25
27
|
alias_method :new, :new_with_mock_time
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
29
31
|
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
|
-
|
40
32
|
class << self
|
41
33
|
def mock_date
|
42
|
-
mocked_time_stack_item = Timecop.top_stack_item
|
43
34
|
mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.date(self)
|
44
35
|
end
|
45
36
|
|
@@ -59,32 +50,67 @@ class Date #:nodoc:
|
|
59
50
|
"supports Date::ITALY for the start argument."
|
60
51
|
end
|
61
52
|
|
62
|
-
|
53
|
+
d = Date._strptime(str, fmt) || Date.strptime_without_mock_date(str, fmt)
|
54
|
+
now = Time.now.to_date
|
55
|
+
year = d[:year] || now.year
|
56
|
+
mon = d[:mon] || now.mon
|
57
|
+
if d[:mday]
|
58
|
+
Date.new(year, mon, d[:mday])
|
59
|
+
elsif d[:wday]
|
60
|
+
Date.new(year, mon, now.mday) + (d[:wday] - now.wday)
|
61
|
+
elsif d[:yday]
|
62
|
+
Date.new(year).next_day(d[:yday] - 1)
|
63
|
+
elsif d[:cwyear] && d[:cweek]
|
64
|
+
if d[:cwday]
|
65
|
+
Date.commercial(d[:cwyear], d[:cweek], d[:cwday])
|
66
|
+
else
|
67
|
+
Date.commercial(d[:cwyear], d[:cweek])
|
68
|
+
end
|
69
|
+
elsif d[:seconds]
|
70
|
+
Time.at(d[:seconds]).to_date
|
71
|
+
else
|
72
|
+
Date.new(year, mon)
|
73
|
+
end
|
63
74
|
end
|
64
75
|
|
65
76
|
alias_method :strptime, :strptime_with_mock_date
|
66
77
|
|
67
78
|
def parse_with_mock_date(*args)
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
79
|
+
parsed_date = parse_without_mock_date(*args)
|
80
|
+
return parsed_date unless mocked_time_stack_item
|
81
|
+
date_hash = Date._parse(*args)
|
82
|
+
|
83
|
+
case
|
84
|
+
when date_hash[:year] && date_hash[:mon]
|
85
|
+
parsed_date
|
86
|
+
when date_hash[:mon] && date_hash[:mday]
|
87
|
+
Date.new(mocked_time_stack_item.year, date_hash[:mon], date_hash[:mday])
|
88
|
+
when date_hash[:wday]
|
89
|
+
closest_wday(date_hash[:wday])
|
73
90
|
else
|
74
|
-
|
91
|
+
parsed_date + mocked_time_stack_item.travel_offset_days
|
75
92
|
end
|
76
93
|
end
|
77
94
|
|
78
95
|
alias_method :parse_without_mock_date, :parse
|
79
96
|
alias_method :parse, :parse_with_mock_date
|
80
97
|
|
98
|
+
def mocked_time_stack_item
|
99
|
+
Timecop.top_stack_item
|
100
|
+
end
|
101
|
+
|
102
|
+
def closest_wday(wday)
|
103
|
+
today = Date.today
|
104
|
+
result = today - today.wday
|
105
|
+
result += 1 until wday == result.wday
|
106
|
+
result
|
107
|
+
end
|
81
108
|
end
|
82
109
|
end
|
83
110
|
|
84
111
|
class DateTime #:nodoc:
|
85
112
|
class << self
|
86
113
|
def mock_time
|
87
|
-
mocked_time_stack_item = Timecop.top_stack_item
|
88
114
|
mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.datetime(self)
|
89
115
|
end
|
90
116
|
|
@@ -97,19 +123,28 @@ class DateTime #:nodoc:
|
|
97
123
|
alias_method :now, :now_with_mock_time
|
98
124
|
|
99
125
|
def parse_with_mock_date(*args)
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
126
|
+
date_hash = Date._parse(*args)
|
127
|
+
parsed_date = parse_without_mock_date(*args)
|
128
|
+
return parsed_date unless mocked_time_stack_item
|
129
|
+
date_hash = DateTime._parse(*args)
|
130
|
+
|
131
|
+
case
|
132
|
+
when date_hash[:year] && date_hash[:mon]
|
133
|
+
parsed_date
|
134
|
+
when date_hash[:mon] && date_hash[:mday]
|
135
|
+
DateTime.new(mocked_time_stack_item.year, date_hash[:mon], date_hash[:mday])
|
136
|
+
when date_hash[:wday]
|
137
|
+
Date.closest_wday(date_hash[:wday]).to_datetime
|
107
138
|
else
|
108
|
-
|
139
|
+
parsed_date + mocked_time_stack_item.travel_offset_days
|
109
140
|
end
|
110
141
|
end
|
111
142
|
|
112
143
|
alias_method :parse_without_mock_date, :parse
|
113
144
|
alias_method :parse, :parse_with_mock_date
|
145
|
+
|
146
|
+
def mocked_time_stack_item
|
147
|
+
Timecop.top_stack_item
|
148
|
+
end
|
114
149
|
end
|
115
150
|
end
|
@@ -43,7 +43,11 @@ class Timecop
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def travel_offset
|
46
|
-
@travel_offset
|
46
|
+
@travel_offset unless mock_type == :freeze
|
47
|
+
end
|
48
|
+
|
49
|
+
def travel_offset_days
|
50
|
+
(@travel_offset / 60 / 60 / 24).round
|
47
51
|
end
|
48
52
|
|
49
53
|
def scaling_factor
|
@@ -52,7 +56,7 @@ class Timecop
|
|
52
56
|
|
53
57
|
def time(time_klass = Time) #:nodoc:
|
54
58
|
if @time.respond_to?(:in_time_zone)
|
55
|
-
time = time_klass.at(@time.dup.
|
59
|
+
time = time_klass.at(@time.dup.localtime)
|
56
60
|
else
|
57
61
|
time = time_klass.at(@time)
|
58
62
|
end
|
@@ -60,7 +64,7 @@ class Timecop
|
|
60
64
|
if travel_offset.nil?
|
61
65
|
time
|
62
66
|
elsif scaling_factor.nil?
|
63
|
-
time_klass.at(
|
67
|
+
time_klass.at(Time.now_without_mock_time + travel_offset)
|
64
68
|
else
|
65
69
|
time_klass.at(scaled_time)
|
66
70
|
end
|
@@ -101,13 +105,13 @@ class Timecop
|
|
101
105
|
time_klass.at(arg.to_time.to_f).getlocal
|
102
106
|
elsif Object.const_defined?(:Date) && arg.is_a?(Date)
|
103
107
|
time_klass.local(arg.year, arg.month, arg.day, 0, 0, 0)
|
104
|
-
elsif args.empty? && arg.kind_of?(Integer)
|
105
|
-
|
108
|
+
elsif args.empty? && (arg.kind_of?(Integer) || arg.kind_of?(Float))
|
109
|
+
time_klass.now + arg
|
106
110
|
elsif arg.nil?
|
107
|
-
|
111
|
+
time_klass.now
|
108
112
|
else
|
109
113
|
if arg.is_a?(String) && Time.respond_to?(:parse)
|
110
|
-
|
114
|
+
time_klass.parse(arg)
|
111
115
|
else
|
112
116
|
# we'll just assume it's a list of y/m/d/h/m/s
|
113
117
|
year = arg || 2000
|
@@ -122,7 +126,6 @@ class Timecop
|
|
122
126
|
end
|
123
127
|
|
124
128
|
def compute_travel_offset
|
125
|
-
return nil if mock_type == :freeze
|
126
129
|
time - Time.now_without_mock_time
|
127
130
|
end
|
128
131
|
|
data/lib/timecop/timecop.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'singleton'
|
2
|
-
require File.join(File.dirname(__FILE__), "time_extensions")
|
3
2
|
require File.join(File.dirname(__FILE__), "time_stack_item")
|
4
3
|
|
5
4
|
# Timecop
|
@@ -93,6 +92,7 @@ class Timecop
|
|
93
92
|
nil
|
94
93
|
end
|
95
94
|
end
|
95
|
+
alias :unfreeze :return
|
96
96
|
|
97
97
|
def return_to_baseline
|
98
98
|
instance.send(:return_to_baseline)
|
@@ -100,7 +100,7 @@ class Timecop
|
|
100
100
|
end
|
101
101
|
|
102
102
|
def top_stack_item #:nodoc:
|
103
|
-
instance.
|
103
|
+
instance.send(:stack).last
|
104
104
|
end
|
105
105
|
|
106
106
|
def safe_mode=(safe)
|
@@ -111,6 +111,19 @@ class Timecop
|
|
111
111
|
@safe_mode ||= false
|
112
112
|
end
|
113
113
|
|
114
|
+
def thread_safe=(t)
|
115
|
+
instance.send(:thread_safe=, t)
|
116
|
+
end
|
117
|
+
|
118
|
+
def thread_safe
|
119
|
+
instance.send(:thread_safe)
|
120
|
+
end
|
121
|
+
|
122
|
+
# Returns whether or not Timecop is currently frozen/travelled
|
123
|
+
def frozen?
|
124
|
+
!instance.send(:stack).empty?
|
125
|
+
end
|
126
|
+
|
114
127
|
private
|
115
128
|
def send_travel(mock_type, *args, &block)
|
116
129
|
val = instance.send(:travel, mock_type, *args, &block)
|
@@ -120,50 +133,97 @@ class Timecop
|
|
120
133
|
|
121
134
|
private
|
122
135
|
|
123
|
-
def baseline=(
|
124
|
-
|
125
|
-
|
136
|
+
def baseline=(b)
|
137
|
+
set_baseline(b)
|
138
|
+
stack << TimeStackItem.new(:travel, b)
|
139
|
+
end
|
140
|
+
|
141
|
+
def baseline
|
142
|
+
if @thread_safe
|
143
|
+
Thread.current[:timecop_baseline]
|
144
|
+
else
|
145
|
+
@baseline
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def set_baseline(b)
|
150
|
+
if @thread_safe
|
151
|
+
Thread.current[:timecop_baseline] = b
|
152
|
+
else
|
153
|
+
@baseline = b
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def stack
|
158
|
+
if @thread_safe
|
159
|
+
Thread.current[:timecop_stack] ||= []
|
160
|
+
Thread.current[:timecop_stack]
|
161
|
+
else
|
162
|
+
@stack
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def set_stack(s)
|
167
|
+
if @thread_safe
|
168
|
+
Thread.current[:timecop_stack] = s
|
169
|
+
else
|
170
|
+
@stack = s
|
171
|
+
end
|
126
172
|
end
|
127
173
|
|
128
174
|
def initialize #:nodoc:
|
129
|
-
@
|
175
|
+
@stack = []
|
176
|
+
@safe = nil
|
177
|
+
@thread_safe = false
|
178
|
+
end
|
179
|
+
|
180
|
+
def thread_safe=(t)
|
181
|
+
initialize
|
182
|
+
@thread_safe = t
|
183
|
+
end
|
184
|
+
|
185
|
+
def thread_safe
|
186
|
+
@thread_safe
|
130
187
|
end
|
131
188
|
|
132
189
|
def travel(mock_type, *args, &block) #:nodoc:
|
133
|
-
raise SafeModeException if Timecop.safe_mode? && !block_given?
|
190
|
+
raise SafeModeException if Timecop.safe_mode? && !block_given? && !@safe
|
134
191
|
|
135
192
|
stack_item = TimeStackItem.new(mock_type, *args)
|
136
193
|
|
137
|
-
stack_backup =
|
138
|
-
|
194
|
+
stack_backup = stack.dup
|
195
|
+
stack << stack_item
|
139
196
|
|
140
197
|
if block_given?
|
198
|
+
safe_backup = @safe
|
199
|
+
@safe = true
|
141
200
|
begin
|
142
201
|
yield stack_item.time
|
143
202
|
ensure
|
144
|
-
|
203
|
+
stack.replace stack_backup
|
204
|
+
@safe = safe_backup
|
145
205
|
end
|
146
206
|
end
|
147
207
|
end
|
148
208
|
|
149
209
|
def return(&block)
|
150
|
-
current_stack =
|
151
|
-
current_baseline =
|
210
|
+
current_stack = stack
|
211
|
+
current_baseline = baseline
|
152
212
|
unmock!
|
153
213
|
yield
|
154
214
|
ensure
|
155
|
-
|
156
|
-
|
215
|
+
set_stack current_stack
|
216
|
+
set_baseline current_baseline
|
157
217
|
end
|
158
218
|
|
159
219
|
def unmock! #:nodoc:
|
160
|
-
|
161
|
-
|
220
|
+
set_baseline nil
|
221
|
+
set_stack []
|
162
222
|
end
|
163
223
|
|
164
224
|
def return_to_baseline
|
165
|
-
if
|
166
|
-
|
225
|
+
if baseline
|
226
|
+
set_stack [stack.shift]
|
167
227
|
else
|
168
228
|
unmock!
|
169
229
|
end
|
@@ -175,3 +235,6 @@ class Timecop
|
|
175
235
|
end
|
176
236
|
end
|
177
237
|
end
|
238
|
+
|
239
|
+
# This must be done after TimeCop is available
|
240
|
+
require File.join(File.dirname(__FILE__), "time_extensions")
|
data/lib/timecop/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,16 +1,13 @@
|
|
1
|
-
require 'rubygems'
|
2
1
|
require 'bundler/setup'
|
3
2
|
require 'minitest/autorun'
|
3
|
+
require 'minitest/rg'
|
4
|
+
require 'pry'
|
4
5
|
|
5
6
|
$VERBOSE = true # enable ruby warnings
|
6
7
|
|
7
|
-
|
8
|
-
require 'mocha/setup'
|
9
|
-
rescue LoadError
|
10
|
-
require 'mocha'
|
11
|
-
end
|
8
|
+
require 'mocha/minitest'
|
12
9
|
|
13
|
-
class Minitest::
|
10
|
+
class Minitest::Test
|
14
11
|
private
|
15
12
|
# Tests to see that two times are within the given distance,
|
16
13
|
# in seconds, from each other.
|
@@ -32,7 +29,7 @@ class Minitest::Unit::TestCase
|
|
32
29
|
Time.at(time.to_i).to_datetime.offset
|
33
30
|
end
|
34
31
|
|
35
|
-
TIMEZONES = ["Europe/Paris", "UTC", "America/Chicago"]
|
32
|
+
TIMEZONES = ["Pacific/Midway", "Europe/Paris", "UTC", "America/Chicago"]
|
36
33
|
|
37
34
|
def each_timezone
|
38
35
|
old_tz = ENV["TZ"]
|
@@ -55,4 +52,8 @@ class Minitest::Unit::TestCase
|
|
55
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}"
|
56
53
|
end
|
57
54
|
|
55
|
+
def jruby?
|
56
|
+
RUBY_PLATFORM == "java"
|
57
|
+
end
|
58
|
+
|
58
59
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'date'
|
2
|
-
|
3
|
-
require
|
2
|
+
require_relative "test_helper"
|
3
|
+
require 'timecop'
|
4
4
|
|
5
5
|
require 'active_support/all'
|
6
6
|
|
7
|
-
class TestTimeStackItem < Minitest::
|
7
|
+
class TestTimeStackItem < Minitest::Test
|
8
8
|
def teardown
|
9
9
|
Timecop.return
|
10
10
|
Time.zone = nil
|
@@ -87,6 +87,19 @@ class TestTimeStackItem < Minitest::Unit::TestCase
|
|
87
87
|
assert_equal s, stack_item.sec
|
88
88
|
end
|
89
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
|
+
|
90
103
|
def test_new_with_individual_arguments
|
91
104
|
y, m, d, h, min, s = 2008, 10, 10, 10, 10, 10
|
92
105
|
stack_item = Timecop::TimeStackItem.new(:freeze, y, m, d, h, min, s)
|
@@ -168,7 +181,7 @@ class TestTimeStackItem < Minitest::Unit::TestCase
|
|
168
181
|
t = Time.local(2009, 10, 1, 0, 0, 30)
|
169
182
|
tsi = Timecop::TimeStackItem.new(:freeze, t)
|
170
183
|
|
171
|
-
|
184
|
+
assert_nil tsi.send(:travel_offset)
|
172
185
|
end
|
173
186
|
|
174
187
|
def test_timezones
|
@@ -179,12 +192,21 @@ class TestTimeStackItem < Minitest::Unit::TestCase
|
|
179
192
|
end
|
180
193
|
end
|
181
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
|
+
|
182
204
|
def test_timezones_apply_dates
|
183
205
|
Time.zone = "Central Time (US & Canada)"
|
184
206
|
time = Time.zone.local(2013,1,3)
|
185
207
|
|
186
208
|
Timecop.freeze(time) do
|
187
|
-
assert_equal time.to_date, Time.now.to_date
|
209
|
+
assert_equal time.to_date, Time.zone.now.to_date
|
188
210
|
end
|
189
211
|
end
|
190
212
|
|
data/test/timecop_test.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
|
2
|
-
require
|
1
|
+
require_relative "test_helper"
|
2
|
+
require 'timecop'
|
3
3
|
|
4
|
-
class TestTimecop < Minitest::
|
4
|
+
class TestTimecop < Minitest::Test
|
5
5
|
def teardown
|
6
6
|
Timecop.return
|
7
7
|
end
|
@@ -37,6 +37,12 @@ class TestTimecop < Minitest::Unit::TestCase
|
|
37
37
|
assert_nil Time.send(:mock_time)
|
38
38
|
end
|
39
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
|
+
|
40
46
|
def test_travel_then_return_unsets_mock_time
|
41
47
|
Timecop.travel(1)
|
42
48
|
Timecop.return
|
@@ -251,7 +257,7 @@ class TestTimecop < Minitest::Unit::TestCase
|
|
251
257
|
new_now = Time.now
|
252
258
|
assert_times_effectively_equal(new_now, t, 1, "Looks like we failed to actually travel time")
|
253
259
|
sleep(0.25)
|
254
|
-
assert_times_effectively_not_equal new_now, Time.now, 0.
|
260
|
+
assert_times_effectively_not_equal new_now, Time.now, 0.24, "Looks like time is not moving"
|
255
261
|
end
|
256
262
|
end
|
257
263
|
|
@@ -293,7 +299,7 @@ class TestTimecop < Minitest::Unit::TestCase
|
|
293
299
|
end
|
294
300
|
end
|
295
301
|
end
|
296
|
-
|
302
|
+
|
297
303
|
def test_freeze_without_arguments_instance_works_as_expected
|
298
304
|
t = Time.local(2008, 10, 10, 10, 10, 10)
|
299
305
|
Timecop.freeze(t) do
|
@@ -495,6 +501,18 @@ class TestTimecop < Minitest::Unit::TestCase
|
|
495
501
|
end
|
496
502
|
end
|
497
503
|
|
504
|
+
def test_raises_when_safe_mode_and_no_block_though_previously_block_given
|
505
|
+
Timecop.freeze do
|
506
|
+
Timecop.freeze
|
507
|
+
end
|
508
|
+
|
509
|
+
with_safe_mode do
|
510
|
+
assert_raises Timecop::SafeModeException do
|
511
|
+
Timecop.freeze
|
512
|
+
end
|
513
|
+
end
|
514
|
+
end
|
515
|
+
|
498
516
|
def test_no_raise_when_safe_mode_and_block_used
|
499
517
|
with_safe_mode do
|
500
518
|
Timecop.freeze {}
|
@@ -507,18 +525,58 @@ class TestTimecop < Minitest::Unit::TestCase
|
|
507
525
|
end
|
508
526
|
end
|
509
527
|
|
510
|
-
def
|
511
|
-
|
512
|
-
|
528
|
+
def test_no_raise_when_safe_mode_and_no_block_and_in_block_context
|
529
|
+
with_safe_mode do
|
530
|
+
Timecop.freeze do
|
531
|
+
Timecop.freeze
|
532
|
+
end
|
513
533
|
end
|
514
534
|
end
|
515
535
|
|
516
|
-
def
|
517
|
-
Timecop.freeze
|
518
|
-
|
536
|
+
def test_frozen_after_freeze
|
537
|
+
Timecop.freeze
|
538
|
+
assert Timecop.frozen?
|
539
|
+
end
|
540
|
+
|
541
|
+
def test_frozen_inside_freeze
|
542
|
+
Timecop.freeze do
|
543
|
+
assert Timecop.frozen?
|
519
544
|
end
|
520
545
|
end
|
521
546
|
|
547
|
+
def test_not_frozen_after_return
|
548
|
+
Timecop.freeze
|
549
|
+
Timecop.return
|
550
|
+
assert !Timecop.frozen?
|
551
|
+
end
|
552
|
+
|
553
|
+
def test_thread_safe_timecop_in_parallel
|
554
|
+
Timecop.thread_safe = true
|
555
|
+
date = Time.local(2011, 01, 02)
|
556
|
+
thread = Thread.new do
|
557
|
+
Timecop.freeze(date) do
|
558
|
+
sleep 1 #give main thread time to run
|
559
|
+
assert_equal date, Time.now
|
560
|
+
end
|
561
|
+
end
|
562
|
+
|
563
|
+
sleep 0.25
|
564
|
+
assert Time.now != date
|
565
|
+
thread.join
|
566
|
+
ensure
|
567
|
+
Timecop.thread_safe = false
|
568
|
+
end
|
569
|
+
|
570
|
+
def test_thread_safe_timecop_returns_after_block
|
571
|
+
Timecop.thread_safe = true
|
572
|
+
date = Time.local(2017, 10, 8)
|
573
|
+
|
574
|
+
Timecop.freeze(date) { }
|
575
|
+
assert Time.now != date
|
576
|
+
ensure
|
577
|
+
Timecop.thread_safe = false
|
578
|
+
end
|
579
|
+
|
522
580
|
private
|
523
581
|
|
524
582
|
def with_safe_mode(enabled=true)
|
@@ -1,10 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
class TestTimecopWithoutDateButWithTime < Minitest::Unit::TestCase
|
4
|
-
TIMECOP_LIB = File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
1
|
+
require_relative "test_helper"
|
2
|
+
require "time"
|
5
3
|
|
4
|
+
class TestTimecopWithoutDateButWithTime < Minitest::Test
|
6
5
|
def test_loads_properly_when_time_is_required_instead_of_date
|
7
|
-
require
|
8
|
-
require TIMECOP_LIB
|
6
|
+
require 'timecop'
|
9
7
|
end
|
10
8
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
|
2
|
-
require
|
1
|
+
require_relative "test_helper"
|
2
|
+
require 'timecop'
|
3
3
|
|
4
|
-
class TestTimecopWithoutDate < Minitest::
|
4
|
+
class TestTimecopWithoutDate < Minitest::Test
|
5
5
|
|
6
6
|
def setup
|
7
7
|
Object.send(:remove_const, :Date) if Object.const_defined?(:Date)
|
@@ -70,7 +70,7 @@ class TestTimecopWithoutDate < Minitest::Unit::TestCase
|
|
70
70
|
new_now = Time.now
|
71
71
|
assert_times_effectively_equal new_now, t, 1, "Looks like we failed to actually travel time" # 0.1 seconds
|
72
72
|
sleep(0.25)
|
73
|
-
assert_times_effectively_not_equal new_now, Time.now, 0.
|
73
|
+
assert_times_effectively_not_equal new_now, Time.now, 0.24, "Looks like time is not moving"
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timecop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Travis Jeffery
|
8
8
|
- John Trupiano
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-02-09 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,7 +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/run_tests.sh
|
33
32
|
- test/test_helper.rb
|
34
33
|
- test/time_stack_item_test.rb
|
35
34
|
- test/timecop_test.rb
|
@@ -39,7 +38,7 @@ homepage: https://github.com/travisjeffery/timecop
|
|
39
38
|
licenses:
|
40
39
|
- MIT
|
41
40
|
metadata: {}
|
42
|
-
post_install_message:
|
41
|
+
post_install_message:
|
43
42
|
rdoc_options:
|
44
43
|
- "--charset=UTF-8"
|
45
44
|
require_paths:
|
@@ -55,9 +54,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
54
|
- !ruby/object:Gem::Version
|
56
55
|
version: '0'
|
57
56
|
requirements: []
|
58
|
-
|
59
|
-
|
60
|
-
signing_key:
|
57
|
+
rubygems_version: 3.0.8
|
58
|
+
signing_key:
|
61
59
|
specification_version: 3
|
62
60
|
summary: A gem providing "time travel" and "time freezing" capabilities, making it
|
63
61
|
dead simple to test time-dependent code. It provides a unified method to mock Time.now,
|