timecop 0.7.0 → 0.9.0

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
  SHA1:
3
- metadata.gz: a736f9ef7fd96e85e0575f736c0a507095012f41
4
- data.tar.gz: c9819e7dca7ace18be6d27445538a0c8a0a36511
3
+ metadata.gz: 580525a47a6026f3d83689807d96949dd5269fa5
4
+ data.tar.gz: ffb9cc8cf58489f231f3348656f99597b3c79989
5
5
  SHA512:
6
- metadata.gz: 5f58a4a57151a9fa501ca0dfbcd960ac0f12ccc0bee9a37198599f71fb9dd2b1ffa9781805cd8187ff2901992ea3df107845417eff4f9609b0ff6a6bd9f71810
7
- data.tar.gz: 674f4a2d227d6aacbe0d8c4920c12ee9cd44597157b92fe16bafa1d4ed5839a3a61af66760f66144b7dc1df3a8ef07a6bc2a3a1aa283c7ca98ef2702443318d1
6
+ metadata.gz: dc3c024856bf2bbaab09a0734aa1d87712e26c8b1e47774edf2d65153ceb22ae6bbe3b04499a4f12c68acf5f9352fb26e614c8c4996c80e14353f75049899598
7
+ data.tar.gz: c759c9cc704c61d2bd1cf13b4d44a6ad8a1b36a03fc398b4796f1dd1bc0d055a6ddda86aa0a31addc753eb6accc57049648d3fd5334477ccb0cc8cbc594076b7
data/README.markdown CHANGED
@@ -1,9 +1,6 @@
1
1
  # timecop
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/travisjeffery/timecop.png)](http://travis-ci.org/travisjeffery/timecop)
4
-
5
- - [Source](http://github.com/travisjeffery/timecop)
6
- - [Documentation](http://johntrupiano.rubyforge.org/timecop)
3
+ [![Build Status](https://secure.travis-ci.org/travisjeffery/timecop.svg)](http://travis-ci.org/travisjeffery/timecop)
7
4
 
8
5
  ## DESCRIPTION
9
6
 
@@ -54,7 +51,8 @@ describe "some set of tests to mock" do
54
51
  Timecop.return
55
52
  end
56
53
 
57
- it "should do blah blah blah" {}
54
+ it "should do blah blah blah" do
55
+ end
58
56
  end
59
57
  ```
60
58
 
@@ -106,11 +104,13 @@ being able to simulate activity via subsequent calls to your application.
106
104
  Timecop.scale(3600)
107
105
  Time.now
108
106
  # => 2012-09-20 21:23:25 -0500
109
- # seconds later, hours have past it's gone from 9pm at night to 6am in the morning
107
+ # seconds later, hours have passed and it's gone from 9pm at night to 6am in the morning
110
108
  Time.now
111
109
  # => 2012-09-21 06:22:59 -0500
112
110
  ```
113
111
 
112
+ See [#42](https://github.com/travisjeffery/timecop/pull/42) for more information, thanks to Ken Mayer, David Holcomb, and Pivotal Labs.
113
+
114
114
  ### Timecop.safe_mode
115
115
 
116
116
  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,7 +128,11 @@ Timecop.freeze
128
128
  # => Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed.
129
129
  ```
130
130
 
131
- See [#42](https://github.com/travisjeffery/timecop/pull/42) for more information, thanks to Ken Mayer, David Holcomb, and Pivotal Labs.
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.
132
136
 
133
137
  ## Contribute
134
138
 
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
- system "cd test && ./run_tests.sh" or fail
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'
@@ -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
@@ -29,7 +29,6 @@ end
29
29
  class Date #:nodoc:
30
30
  class << self
31
31
  def mock_date
32
- mocked_time_stack_item = Timecop.top_stack_item
33
32
  mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.date(self)
34
33
  end
35
34
 
@@ -43,29 +42,87 @@ class Date #:nodoc:
43
42
 
44
43
  alias_method :strptime_without_mock_date, :strptime
45
44
 
46
- def strptime_with_mock_date(str, fmt)
45
+ def strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY)
46
+ unless start == Date::ITALY
47
+ raise ArgumentError, "Timecop's #{self}::#{__method__} only " +
48
+ "supports Date::ITALY for the start argument."
49
+ end
50
+
47
51
  Time.strptime(str, fmt).to_date
48
52
  end
49
53
 
50
54
  alias_method :strptime, :strptime_with_mock_date
55
+
56
+ def parse_with_mock_date(*args)
57
+ parsed_date = parse_without_mock_date(*args)
58
+ return parsed_date unless mocked_time_stack_item
59
+ date_hash = Date._parse(*args)
60
+
61
+ case
62
+ when date_hash[:year] && date_hash[:mon] && date_hash[:mday]
63
+ parsed_date
64
+ when date_hash[:mon] && date_hash[:mday]
65
+ Date.new(mocked_time_stack_item.year, date_hash[:mon], date_hash[:mday])
66
+ when date_hash[:wday]
67
+ closest_wday(date_hash[:wday])
68
+ else
69
+ parsed_date + mocked_time_stack_item.travel_offset_days
70
+ end
71
+ end
72
+
73
+ alias_method :parse_without_mock_date, :parse
74
+ alias_method :parse, :parse_with_mock_date
75
+
76
+ def mocked_time_stack_item
77
+ Timecop.top_stack_item
78
+ end
79
+
80
+ def closest_wday(wday)
81
+ today = Date.today
82
+ result = today - today.wday
83
+ result += 1 until wday == result.wday
84
+ result
85
+ end
51
86
  end
52
87
  end
53
88
 
54
89
  class DateTime #:nodoc:
55
90
  class << self
56
91
  def mock_time
57
- mocked_time_stack_item = Timecop.top_stack_item
58
92
  mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.datetime(self)
59
93
  end
60
94
 
61
- def now_without_mock_time
62
- Time.now_without_mock_time.to_datetime
63
- end
64
-
65
95
  def now_with_mock_time
66
96
  mock_time || now_without_mock_time
67
97
  end
68
98
 
99
+ alias_method :now_without_mock_time, :now
100
+
69
101
  alias_method :now, :now_with_mock_time
102
+
103
+ def parse_with_mock_date(*args)
104
+ date_hash = Date._parse(*args)
105
+ parsed_date = parse_without_mock_date(*args)
106
+ return parsed_date unless mocked_time_stack_item
107
+ date_hash = DateTime._parse(*args)
108
+
109
+ case
110
+ when date_hash[:year] && date_hash[:mon] && date_hash[:mday]
111
+ parsed_date
112
+ when date_hash[:mon] && date_hash[:mday]
113
+ DateTime.new(mocked_time_stack_item.year, date_hash[:mon], date_hash[:mday])
114
+ when date_hash[:wday]
115
+ Date.closest_wday(date_hash[:wday]).to_datetime
116
+ else
117
+ parsed_date + mocked_time_stack_item.travel_offset_days
118
+ end
119
+ end
120
+
121
+ alias_method :parse_without_mock_date, :parse
122
+ alias_method :parse, :parse_with_mock_date
123
+
124
+ def mocked_time_stack_item
125
+ Timecop.top_stack_item
126
+ end
70
127
  end
71
128
  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)
@@ -42,7 +43,11 @@ class Timecop
42
43
  end
43
44
 
44
45
  def travel_offset
45
- @travel_offset
46
+ @travel_offset unless mock_type == :freeze
47
+ end
48
+
49
+ def travel_offset_days
50
+ (@travel_offset / 60 / 60 / 24).round
46
51
  end
47
52
 
48
53
  def scaling_factor
@@ -51,7 +56,7 @@ class Timecop
51
56
 
52
57
  def time(time_klass = Time) #:nodoc:
53
58
  if @time.respond_to?(:in_time_zone)
54
- time = time_klass.at(@time.dup.utc.to_r)
59
+ time = time_klass.at(@time.dup.localtime)
55
60
  else
56
61
  time = time_klass.at(@time)
57
62
  end
@@ -59,7 +64,7 @@ class Timecop
59
64
  if travel_offset.nil?
60
65
  time
61
66
  elsif scaling_factor.nil?
62
- time_klass.at((Time.now_without_mock_time + travel_offset).to_f)
67
+ time_klass.at(Time.now_without_mock_time + travel_offset)
63
68
  else
64
69
  time_klass.at(scaled_time)
65
70
  end
@@ -75,12 +80,8 @@ class Timecop
75
80
 
76
81
  def datetime(datetime_klass = DateTime)
77
82
  if Float.method_defined?(:to_r)
78
- if !sec.zero?
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))
81
- else
82
- datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset))
83
- end
83
+ fractions_of_a_second = time.to_f % 1
84
+ datetime_klass.new(year, month, day, hour, min, (fractions_of_a_second + sec), utc_offset_to_rational(utc_offset))
84
85
  else
85
86
  datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset))
86
87
  end
@@ -104,13 +105,13 @@ class Timecop
104
105
  time_klass.at(arg.to_time.to_f).getlocal
105
106
  elsif Object.const_defined?(:Date) && arg.is_a?(Date)
106
107
  time_klass.local(arg.year, arg.month, arg.day, 0, 0, 0)
107
- elsif args.empty? && arg.kind_of?(Integer)
108
- Time.now + arg
108
+ elsif args.empty? && (arg.kind_of?(Integer) || arg.kind_of?(Float))
109
+ time_klass.now + arg
109
110
  elsif arg.nil?
110
- Time.now
111
+ time_klass.now
111
112
  else
112
113
  if arg.is_a?(String) && Time.respond_to?(:parse)
113
- Time.parse(arg)
114
+ time_klass.parse(arg)
114
115
  else
115
116
  # we'll just assume it's a list of y/m/d/h/m/s
116
117
  year = arg || 2000
@@ -125,7 +126,6 @@ class Timecop
125
126
  end
126
127
 
127
128
  def compute_travel_offset
128
- return nil if mock_type == :freeze
129
129
  time - Time.now_without_mock_time
130
130
  end
131
131
 
@@ -137,4 +137,4 @@ class Timecop
137
137
  Time.respond_to?(:zone) && Time.zone ? Time.zone : Time
138
138
  end
139
139
  end
140
- end
140
+ end
@@ -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
@@ -99,7 +100,7 @@ class Timecop
99
100
  end
100
101
 
101
102
  def top_stack_item #:nodoc:
102
- instance.instance_variable_get(:@_stack).last
103
+ instance.send(:stack).last
103
104
  end
104
105
 
105
106
  def safe_mode=(safe)
@@ -107,7 +108,20 @@ class Timecop
107
108
  end
108
109
 
109
110
  def safe_mode?
110
- false || @safe_mode
111
+ @safe_mode ||= false
112
+ end
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?
111
125
  end
112
126
 
113
127
  private
@@ -119,49 +133,97 @@ class Timecop
119
133
 
120
134
  private
121
135
 
122
- def baseline=(baseline)
123
- @baseline = baseline
124
- @_stack << TimeStackItem.new(:travel, baseline)
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
125
172
  end
126
173
 
127
174
  def initialize #:nodoc:
128
- @_stack = []
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
129
187
  end
130
188
 
131
189
  def travel(mock_type, *args, &block) #:nodoc:
132
- raise SafeModeException if Timecop.safe_mode? && !block_given?
190
+ raise SafeModeException if Timecop.safe_mode? && !block_given? && !@safe
133
191
 
134
192
  stack_item = TimeStackItem.new(mock_type, *args)
135
193
 
136
- stack_backup = @_stack.dup
137
- @_stack << stack_item
194
+ stack_backup = stack.dup
195
+ stack << stack_item
138
196
 
139
197
  if block_given?
198
+ safe_backup = @safe
199
+ @safe = true
140
200
  begin
141
201
  yield stack_item.time
142
202
  ensure
143
- @_stack.replace stack_backup
203
+ @stack.replace stack_backup
204
+ @safe = safe_backup
144
205
  end
145
206
  end
146
207
  end
147
208
 
148
209
  def return(&block)
149
- current_stack = @_stack
150
- current_baseline = @baseline
210
+ current_stack = stack
211
+ current_baseline = baseline
151
212
  unmock!
152
213
  yield
153
- @_stack = current_stack
154
- @baseline = current_baseline
214
+ ensure
215
+ set_stack current_stack
216
+ set_baseline current_baseline
155
217
  end
156
218
 
157
219
  def unmock! #:nodoc:
158
- @baseline = nil
159
- @_stack = []
220
+ set_baseline nil
221
+ set_stack []
160
222
  end
161
223
 
162
224
  def return_to_baseline
163
- if @baseline
164
- @_stack = [@_stack.shift]
225
+ if baseline
226
+ set_stack [stack.shift]
165
227
  else
166
228
  unmock!
167
229
  end
@@ -1,3 +1,3 @@
1
1
  class Timecop
2
- VERSION = "0.7.0"
2
+ VERSION = "0.9.0"
3
3
  end
data/test/test_helper.rb CHANGED
@@ -1,38 +1,38 @@
1
- require 'rubygems'
2
1
  require 'bundler/setup'
3
- require 'test/unit'
4
- begin
5
- require 'mocha/setup'
6
- rescue LoadError
7
- require 'mocha'
8
- end
2
+ require 'minitest/autorun'
3
+ require 'minitest/rg'
4
+
5
+ $VERBOSE = true # enable ruby warnings
9
6
 
10
- class Test::Unit::TestCase
7
+ require 'mocha/setup'
11
8
 
9
+ class Minitest::Test
12
10
  private
13
11
  # Tests to see that two times are within the given distance,
14
12
  # in seconds, from each other.
15
13
  def times_effectively_equal(time1, time2, seconds_interval = 1)
16
14
  (time1 - time2).abs <= seconds_interval
17
15
  end
18
-
16
+
19
17
  def assert_times_effectively_equal(time1, time2, seconds_interval = 1, msg = nil)
20
18
  assert times_effectively_equal(time1, time2, seconds_interval), "#{msg}: time1 = #{time1.to_s}, time2 = #{time2.to_s}"
21
19
  end
22
-
20
+
23
21
  def assert_times_effectively_not_equal(time1, time2, seconds_interval = 1, msg = nil)
24
22
  assert !times_effectively_equal(time1, time2, seconds_interval), "#{msg}: time1 = #{time1.to_s}, time2 = #{time2.to_s}"
25
23
  end
26
-
27
- def local_offset
28
- DateTime.now_without_mock_time.offset
24
+
25
+ # Gets the local offset (supplied by ENV['TZ'] or your computer's clock)
26
+ # At the given timestamp, or Time.now if not time is given.
27
+ def local_offset(time = Time.now)
28
+ Time.at(time.to_i).to_datetime.offset
29
29
  end
30
-
31
- TIMEZONES = ["Europe/Paris", "UTC", "EDT"]
32
-
30
+
31
+ TIMEZONES = ["Europe/Paris", "UTC", "America/Chicago"]
32
+
33
33
  def each_timezone
34
34
  old_tz = ENV["TZ"]
35
-
35
+
36
36
  begin
37
37
  TIMEZONES.each do |timezone|
38
38
  ENV["TZ"] = timezone
@@ -42,13 +42,17 @@ class Test::Unit::TestCase
42
42
  ENV["TZ"] = old_tz
43
43
  end
44
44
  end
45
-
45
+
46
46
  def a_time_stack_item
47
47
  Timecop::TimeStackItem.new(:freeze, 2008, 1, 1, 0, 0, 0)
48
48
  end
49
-
49
+
50
50
  def assert_date_times_equal(dt1, dt2)
51
51
  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}"
52
52
  end
53
-
53
+
54
+ def jruby?
55
+ RUBY_PLATFORM == "java"
56
+ end
57
+
54
58
  end
@@ -1,10 +1,10 @@
1
1
  require 'date'
2
- require File.join(File.dirname(__FILE__), "test_helper")
3
- require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
2
+ require_relative "test_helper"
3
+ require 'timecop'
4
4
 
5
5
  require 'active_support/all'
6
6
 
7
- class TestTimeStackItem < Test::Unit::TestCase
7
+ class TestTimeStackItem < Minitest::Test
8
8
  def teardown
9
9
  Timecop.return
10
10
  Time.zone = nil
@@ -87,6 +87,19 @@ class TestTimeStackItem < Test::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)
@@ -179,6 +192,15 @@ class TestTimeStackItem < Test::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)
@@ -204,16 +226,12 @@ class TestTimeStackItem < Test::Unit::TestCase
204
226
  end
205
227
 
206
228
  def test_parse_date
207
- assert_nothing_raised do
208
- Timecop.freeze(Date.new(2012, 6, 9))
209
- end
229
+ Timecop.freeze(Date.new(2012, 6, 9))
210
230
  end
211
231
 
212
232
  def test_time_zone_returns_nil
213
233
  Time.zone = nil
214
- assert_nothing_raised do
215
- Timecop.freeze
216
- end
234
+ Timecop.freeze
217
235
  end
218
236
 
219
237
  def test_nsecs_are_set
@@ -273,7 +291,7 @@ class TestTimeStackItem < Test::Unit::TestCase
273
291
 
274
292
  def test_datetime_timezones
275
293
  dt = DateTime.new(2011,1,3,15,25,0,"-6")
276
- Timecop.travel(dt) do
294
+ Timecop.freeze(dt) do
277
295
  now = DateTime.now
278
296
  assert_equal dt, now, "#{dt.to_f}, #{now.to_f}"
279
297
  end
data/test/timecop_test.rb CHANGED
@@ -1,8 +1,7 @@
1
- require 'date'
2
- require File.join(File.dirname(__FILE__), "test_helper")
3
- require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
1
+ require_relative "test_helper"
2
+ require 'timecop'
4
3
 
5
- class TestTimecop < Test::Unit::TestCase
4
+ class TestTimecop < Minitest::Test
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
- assert_not_equal DateTime.now, DateTime.now
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
- assert_not_equal t, Time.now
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
- assert_not_equal t, Time.now
128
- assert_not_equal DateTime.new(2008, 10, 10, 10, 10, 10, local_offset), DateTime.now
129
- assert_not_equal Date.new(2008, 10, 10), Date.today
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
- assert_not_equal d, Date.today
189
- assert_not_equal Time.local(2008, 10, 10, 0, 0, 0), Time.now
190
- assert_not_equal DateTime.new(2008, 10, 10, 0, 0, 0, local_offset), DateTime.now
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
- assert_not_equal t, Time.now
206
- assert_not_equal DateTime.new(2008, 10, 10, 10, 10, 10), DateTime.now
207
- assert_not_equal Date.new(2008, 10, 10), Date.today
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
- assert_not_equal t, Time.now
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
@@ -241,7 +251,7 @@ class TestTimecop < Test::Unit::TestCase
241
251
  new_now = Time.now
242
252
  assert_times_effectively_equal(new_now, t, 1, "Looks like we failed to actually travel time")
243
253
  sleep(0.25)
244
- assert_times_effectively_not_equal new_now, Time.now, 0.25, "Looks like time is not moving"
254
+ assert_times_effectively_not_equal new_now, Time.now, 0.24, "Looks like time is not moving"
245
255
  end
246
256
  end
247
257
 
@@ -249,6 +259,11 @@ class TestTimecop < Test::Unit::TestCase
249
259
  each_timezone do
250
260
  t = DateTime.parse("2009-10-11 00:38:00 +0200")
251
261
  Timecop.freeze(t) do
262
+ if ENV['TZ'] == 'UTC'
263
+ assert_equal(local_offset, 0, "Local offset not be zero for #{ENV['TZ']}")
264
+ else
265
+ assert(local_offset, 0 != "Local offset should not be zero for #{ENV['TZ']}")
266
+ end
252
267
  assert_equal local_offset, DateTime.now.offset, "Failed for timezone: #{ENV['TZ']}"
253
268
  end
254
269
  end
@@ -279,6 +294,19 @@ class TestTimecop < Test::Unit::TestCase
279
294
  end
280
295
  end
281
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
309
+
282
310
  def test_destructive_methods_on_frozen_time
283
311
  # Use any time zone other than UTC.
284
312
  ENV['TZ'] = 'EST'
@@ -309,13 +337,13 @@ class TestTimecop < Test::Unit::TestCase
309
337
  end
310
338
 
311
339
  def test_recursive_travel_yields_correct_time
312
- Timecop.travel(2008, 10, 10, 10, 10, 10) do
340
+ Timecop.travel(2008, 10, 10, 10, 10, 10) do
313
341
  Timecop.travel(2008, 9, 9, 9, 9, 9) do |inner_freeze|
314
342
  assert_times_effectively_equal inner_freeze, Time.now, 1, "Failed to yield current time back to block"
315
343
  end
316
344
  end
317
345
  end
318
-
346
+
319
347
  def test_recursive_travel_then_freeze
320
348
  t = Time.local(2008, 10, 10, 10, 10, 10)
321
349
  Timecop.travel(2008, 10, 10, 10, 10, 10) do
@@ -437,7 +465,7 @@ class TestTimecop < Test::Unit::TestCase
437
465
  end
438
466
 
439
467
  def test_not_callable_send_travel
440
- assert_raise NoMethodError do
468
+ assert_raises NoMethodError do
441
469
  Timecop.send_travel(:travel, Time.now - 100)
442
470
  end
443
471
  end
@@ -461,23 +489,39 @@ class TestTimecop < Test::Unit::TestCase
461
489
 
462
490
  def test_raises_when_safe_mode_and_no_block
463
491
  with_safe_mode do
464
- assert_raise Timecop::SafeModeException do
492
+ assert_raises Timecop::SafeModeException do
465
493
  Timecop.freeze
466
494
  end
467
495
  end
468
496
  end
469
497
 
470
- def test_no_raise_when_safe_mode_and_block_used
498
+ def test_raises_when_safe_mode_and_no_block_though_previously_block_given
499
+ Timecop.freeze do
500
+ Timecop.freeze
501
+ end
502
+
471
503
  with_safe_mode do
472
- assert_nothing_raised do
473
- Timecop.freeze {}
504
+ assert_raises Timecop::SafeModeException do
505
+ Timecop.freeze
474
506
  end
475
507
  end
476
508
  end
477
509
 
510
+ def test_no_raise_when_safe_mode_and_block_used
511
+ with_safe_mode do
512
+ Timecop.freeze {}
513
+ end
514
+ end
515
+
478
516
  def test_no_raise_when_not_safe_mode_and_no_block
479
517
  with_safe_mode(false) do
480
- assert_nothing_raised do
518
+ Timecop.freeze
519
+ end
520
+ end
521
+
522
+ def test_no_raise_when_safe_mode_and_no_block_and_in_block_context
523
+ with_safe_mode do
524
+ Timecop.freeze do
481
525
  Timecop.freeze
482
526
  end
483
527
  end
@@ -489,6 +533,46 @@ class TestTimecop < Test::Unit::TestCase
489
533
  end
490
534
  end
491
535
 
536
+ def test_date_strptime_without_specifying_format
537
+ Timecop.freeze(Time.new(1984,2,28)) do
538
+ assert_equal Date.strptime('1999-04-14'), Date.new(1999, 4, 14)
539
+ end
540
+ end
541
+
542
+ def test_frozen_after_freeze
543
+ Timecop.freeze
544
+ assert Timecop.frozen?
545
+ end
546
+
547
+ def test_frozen_inside_freeze
548
+ Timecop.freeze do
549
+ assert Timecop.frozen?
550
+ end
551
+ end
552
+
553
+ def test_not_frozen_after_return
554
+ Timecop.freeze
555
+ Timecop.return
556
+ assert !Timecop.frozen?
557
+ end
558
+
559
+ def test_thread_safe_timecop
560
+ Timecop.thread_safe = true
561
+ date = Time.local(2011, 01, 02)
562
+ thread = Thread.new do
563
+ Timecop.freeze(date) do
564
+ sleep 1 #give main thread time to run
565
+ assert_equal date, Time.now
566
+ end
567
+ end
568
+
569
+ sleep 0.25
570
+ assert Time.now != date
571
+ thread.join
572
+ ensure
573
+ Timecop.thread_safe = false
574
+ end
575
+
492
576
  private
493
577
 
494
578
  def with_safe_mode(enabled=true)
@@ -1,12 +1,8 @@
1
- require "test/unit"
2
-
3
- class TestTimecopWithoutDateButWithTime < Test::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
- assert_nothing_raised do
8
- require "time"
9
- require TIMECOP_LIB
10
- end
6
+ require 'timecop'
11
7
  end
12
8
  end
@@ -1,34 +1,33 @@
1
+ require_relative "test_helper"
2
+ require 'timecop'
1
3
 
2
- require File.join(File.dirname(__FILE__), "test_helper")
3
- require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
4
+ class TestTimecopWithoutDate < Minitest::Test
4
5
 
5
- class TestTimecopWithoutDate < Test::Unit::TestCase
6
-
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
- assert_not_equal t, Time.now
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
- assert_not_equal t, Time.now
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
- assert_not_equal t, Time.now
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
@@ -71,13 +70,13 @@ class TestTimecopWithoutDate < Test::Unit::TestCase
71
70
  new_now = Time.now
72
71
  assert_times_effectively_equal new_now, t, 1, "Looks like we failed to actually travel time" # 0.1 seconds
73
72
  sleep(0.25)
74
- assert_times_effectively_not_equal new_now, Time.now, 0.25, "Looks like time is not moving"
73
+ assert_times_effectively_not_equal new_now, Time.now, 0.24, "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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timecop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.9.0
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: 2013-11-29 00:00:00.000000000 Z
12
+ date: 2017-06-22 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
@@ -27,36 +27,35 @@ files:
27
27
  - lib/timecop.rb
28
28
  - lib/timecop/time_extensions.rb
29
29
  - lib/timecop/time_stack_item.rb
30
- - lib/timecop/version.rb
31
30
  - lib/timecop/timecop.rb
32
- - test/run_tests.sh
31
+ - lib/timecop/version.rb
33
32
  - test/test_helper.rb
34
33
  - test/time_stack_item_test.rb
35
34
  - test/timecop_test.rb
36
- - test/timecop_without_date_test.rb
37
35
  - test/timecop_without_date_but_with_time_test.rb
36
+ - test/timecop_without_date_test.rb
38
37
  homepage: https://github.com/travisjeffery/timecop
39
38
  licenses:
40
39
  - MIT
41
40
  metadata: {}
42
41
  post_install_message:
43
42
  rdoc_options:
44
- - --charset=UTF-8
43
+ - "--charset=UTF-8"
45
44
  require_paths:
46
45
  - lib
47
46
  required_ruby_version: !ruby/object:Gem::Requirement
48
47
  requirements:
49
- - - '>='
48
+ - - ">="
50
49
  - !ruby/object:Gem::Version
51
50
  version: 1.9.2
52
51
  required_rubygems_version: !ruby/object:Gem::Requirement
53
52
  requirements:
54
- - - '>='
53
+ - - ">="
55
54
  - !ruby/object:Gem::Version
56
55
  version: '0'
57
56
  requirements: []
58
57
  rubyforge_project: timecop
59
- rubygems_version: 2.0.3
58
+ rubygems_version: 2.4.5
60
59
  signing_key:
61
60
  specification_version: 3
62
61
  summary: A gem providing "time travel" and "time freezing" capabilities, making it
@@ -68,3 +67,4 @@ test_files:
68
67
  - test/timecop_test.rb
69
68
  - test/timecop_without_date_test.rb
70
69
  - test/timecop_without_date_but_with_time_test.rb
70
+ has_rdoc:
data/test/run_tests.sh DELETED
@@ -1,10 +0,0 @@
1
- #!/bin/sh
2
-
3
- FAILEDCASES=0
4
- for f in *_test.rb; do
5
- if ! ${RUBY:-ruby} -I../lib:. $f; then
6
- FAILEDCASES=`expr "$FAILEDCASES" + 1`
7
- fi
8
- done
9
- echo "$FAILEDCASES test cases had failures"
10
- exit $FAILEDCASES