timecop 0.4.5 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.rdoc CHANGED
@@ -1,3 +1,18 @@
1
+ === 0.5.0 / 2012-09-12
2
+
3
+ * Maintenance
4
+ * Timecop#travel, Timecop#freeze blocks return nil or block value
5
+
6
+ === 0.4.6 / 2012-09-05
7
+
8
+ * Maintenance
9
+ * Fix rounding support for Ruby < 1.9
10
+
11
+ === 0.4.5 / 2012-08-09
12
+
13
+ * Maintenance
14
+ * Subclasses of Time/Date/DateTime now return proper instances
15
+
1
16
  === 0.4.4 / 2012-07-29
2
17
 
3
18
  * Maintenance
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 5
2
+ :patch: 0
3
3
  :major: 0
4
- :minor: 4
4
+ :minor: 5
@@ -19,10 +19,12 @@ class Time #:nodoc:
19
19
  alias_method :now, :now_with_mock_time
20
20
 
21
21
  alias_method :new_without_mock_time, :new
22
+
22
23
  def new_with_mock_time *args
23
- if args.size > 0
24
+ begin
25
+ raise ArgumentError.new if args.size <= 0
24
26
  new_without_mock_time *args
25
- else
27
+ rescue ArgumentError
26
28
  now
27
29
  end
28
30
  end
@@ -54,15 +54,20 @@ class Timecop
54
54
  end
55
55
 
56
56
  def date(date_klass=Date)
57
- date_klass.jd( time.to_date.jd )
57
+ date_klass.jd(time.__send__(:to_date).jd)
58
58
  end
59
59
 
60
60
  def datetime(datetime_klass=DateTime)
61
61
  # DateTime doesn't know about DST, so let's remove its presence
62
62
  our_offset = utc_offset + dst_adjustment
63
- fractions_of_a_second = time.to_f % 1
64
- datetime_klass.new(year, month, day, hour, min, sec + fractions_of_a_second,
65
- utc_offset_to_rational(our_offset))
63
+ if Float.method_defined?(:to_r)
64
+ fractions_of_a_second = time.to_f % 1
65
+ datetime_klass.new(year, month, day, hour, min, sec + fractions_of_a_second,
66
+ utc_offset_to_rational(our_offset))
67
+ else
68
+ our_offset = utc_offset + dst_adjustment
69
+ datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(our_offset))
70
+ end
66
71
  end
67
72
 
68
73
  def dst_adjustment
@@ -47,10 +47,11 @@ class Timecop
47
47
  # which will lead to files being generated with the timestamp set by the Timecop.freeze call
48
48
  # in your dev environment
49
49
  #
50
- # Returns the frozen time.
50
+ # Returns the value of the block or nil.
51
51
  def freeze(*args, &block)
52
- instance().send(:travel, :freeze, *args, &block)
53
- Time.now
52
+ val = instance().send(:travel, :freeze, *args, &block)
53
+
54
+ block_given? ? val : nil
54
55
  end
55
56
 
56
57
  # Allows you to run a block of code and "fake" a time throughout the execution of that block.
@@ -59,10 +60,11 @@ class Timecop
59
60
  # * Note: Timecop.travel will not freeze time (as opposed to Timecop.freeze). This is a particularly
60
61
  # good candidate for use in environment files in rails projects.
61
62
  #
62
- # Returns the 'new' current Time.
63
+ # Returns the value of the block or nil.
63
64
  def travel(*args, &block)
64
- instance().send(:travel, :travel, *args, &block)
65
- Time.now
65
+ val = instance().send(:travel, :travel, *args, &block)
66
+
67
+ block_given? ? val : nil
66
68
  end
67
69
 
68
70
  def baseline
@@ -74,11 +76,9 @@ class Timecop
74
76
  end
75
77
 
76
78
  # Reverts back to system's Time.now, Date.today and DateTime.now (if it exists).
77
- #
78
- # Returns Time.now, which is now the real current time.
79
79
  def return
80
80
  instance().send(:unmock!)
81
- Time.now
81
+ nil
82
82
  end
83
83
 
84
84
  def return_to_baseline
data/test/timecop_test.rb CHANGED
@@ -56,8 +56,47 @@ class TestTimecop < Test::Unit::TestCase
56
56
  end
57
57
 
58
58
  def test_travel_does_not_reduce_precision_of_datetime
59
- Timecop.travel(1)
60
- assert_not_equal DateTime.now, DateTime.now
59
+ # requires to_r on Float (>= 1.9)
60
+ if Float.method_defined?(:to_r)
61
+ Timecop.travel(1)
62
+ assert_not_equal DateTime.now, DateTime.now
63
+ end
64
+ end
65
+
66
+ def test_freeze_in_time_subclass_returns_mocked_subclass
67
+ t = Time.local(2008, 10, 10, 10, 10, 10)
68
+ custom_timeklass = Class.new(Time) do
69
+ def custom_format_method() strftime('%F') end
70
+ end
71
+ Timecop.freeze(2008, 10, 10, 10, 10, 10) do
72
+ assert custom_timeklass.now.is_a? custom_timeklass
73
+ assert Time.now.eql? custom_timeklass.now
74
+ assert custom_timeklass.now.respond_to? :custom_format_method
75
+ end
76
+ end
77
+
78
+ def test_freeze_in_date_subclass_returns_mocked_subclass
79
+ t = Time.local(2008, 10, 10, 10, 10, 10)
80
+ custom_dateklass = Class.new(Date) do
81
+ def custom_format_method() strftime('%F') end
82
+ end
83
+ Timecop.freeze(2008, 10, 10, 10, 10, 10) do
84
+ assert custom_dateklass.today.is_a? custom_dateklass
85
+ assert Date.today.eql? custom_dateklass.today
86
+ assert custom_dateklass.today.respond_to? :custom_format_method
87
+ end
88
+ end
89
+
90
+ def test_freeze_in_datetime_subclass_returns_mocked_subclass
91
+ t = Time.local(2008, 10, 10, 10, 10, 10)
92
+ custom_datetimeklass = Class.new(DateTime) do
93
+ def custom_format_method() strftime('%F') end
94
+ end
95
+ Timecop.freeze(2008, 10, 10, 10, 10, 10) do
96
+ assert custom_datetimeklass.now.is_a? custom_datetimeklass
97
+ assert DateTime.now.eql? custom_datetimeklass.now
98
+ assert custom_datetimeklass.now.respond_to? :custom_format_method
99
+ end
61
100
  end
62
101
 
63
102
  def test_recursive_freeze
@@ -285,29 +324,34 @@ class TestTimecop < Test::Unit::TestCase
285
324
  assert_nil Time.send(:mock_time)
286
325
  end
287
326
 
288
- def test_return_values_are_Time_instances
289
- assert Timecop.freeze.is_a?(Time)
290
- assert Timecop.travel.is_a?(Time)
291
- assert Timecop.return.is_a?(Time)
327
+ def test_travel_time_returns_nil
328
+ t_future = Time.local(2030, 10, 10, 10, 10, 10)
329
+ assert_nil Timecop.travel(t_future)
330
+ end
331
+
332
+ def test_travel_time_with_block_returns_the_value_of_the_block
333
+ t_future = Time.local(2030, 10, 10, 10, 10, 10)
334
+ expected = :foo
335
+ actual = Timecop.travel(t_future) { expected }
336
+
337
+ assert_equal expected, actual
292
338
  end
293
339
 
294
- def test_travel_time_returns_passed_value
340
+ def test_freeze_time_returns_nil
295
341
  t_future = Time.local(2030, 10, 10, 10, 10, 10)
296
- t_travel = Timecop.travel t_future
297
- assert times_effectively_equal(t_future, t_travel)
342
+ assert_nil Timecop.freeze(t_future)
298
343
  end
299
344
 
300
- def test_freeze_time_returns_passed_value
345
+ def test_freeze_time_with_block_returns_the_value_of_the_block
301
346
  t_future = Time.local(2030, 10, 10, 10, 10, 10)
302
- t_frozen = Timecop.freeze t_future
303
- assert times_effectively_equal(t_future, t_frozen)
347
+ expected = :foo
348
+ actual = Timecop.freeze(t_future) { expected }
349
+
350
+ assert_equal expected, actual
304
351
  end
305
352
 
306
- def test_return_time_returns_actual_time
307
- t_real = Time.now
308
- Timecop.freeze Time.local(2030, 10, 10, 10, 10, 10)
309
- t_return = Timecop.return
310
- assert times_effectively_equal(t_real, t_return)
353
+ def test_return_returns_nil
354
+ assert_nil Timecop.return
311
355
  end
312
356
 
313
357
  def test_freeze_without_params
@@ -319,6 +363,13 @@ class TestTimecop < Test::Unit::TestCase
319
363
  end
320
364
  end
321
365
 
366
+ def test_freeze_with_new_date
367
+ date = Date.new(2012, 6, 9)
368
+ Timecop.freeze(Date.new(2012, 6, 9)) do
369
+ assert_equal date, Time.now.__send__(:to_date)
370
+ end
371
+ end
372
+
322
373
  def test_return_to_baseline_without_a_baseline_set_returns_to_current_time
323
374
  time_before_travel = Time.now
324
375
  Timecop.travel Time.now - 60
metadata CHANGED
@@ -1,27 +1,35 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: timecop
3
- version: !ruby/object:Gem::Version
4
- version: 0.4.5
3
+ version: !ruby/object:Gem::Version
4
+ hash: 11
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Travis Jeffery
9
14
  - John Trupiano
10
15
  autorequire:
11
16
  bindir: bin
12
17
  cert_chain: []
13
- date: 2012-07-09 00:00:00.000000000 Z
18
+
19
+ date: 2012-09-05 00:00:00 -05:00
20
+ default_executable:
14
21
  dependencies: []
15
- description: A gem providing "time travel" and "time freezing" capabilities, making
16
- it dead simple to test time-dependent code. It provides a unified method to mock
17
- Time.now, Date.today, and DateTime.now in a single call.
22
+
23
+ description: 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.
18
24
  email: travisjeffery@gmail.com
19
25
  executables: []
26
+
20
27
  extensions: []
21
- extra_rdoc_files:
28
+
29
+ extra_rdoc_files:
22
30
  - LICENSE
23
31
  - README.markdown
24
- files:
32
+ files:
25
33
  - History.rdoc
26
34
  - LICENSE
27
35
  - README.markdown
@@ -37,34 +45,41 @@ files:
37
45
  - test/timecop_test.rb
38
46
  - test/timecop_without_date_test.rb
39
47
  - test/timecop_without_date_but_with_time_test.rb
48
+ has_rdoc: true
40
49
  homepage: http://github.com/jtrupiano/timecop
41
50
  licenses: []
51
+
42
52
  post_install_message:
43
- rdoc_options:
53
+ rdoc_options:
44
54
  - --charset=UTF-8
45
- require_paths:
55
+ require_paths:
46
56
  - lib
47
- required_ruby_version: !ruby/object:Gem::Requirement
57
+ required_ruby_version: !ruby/object:Gem::Requirement
48
58
  none: false
49
- requirements:
50
- - - ! '>='
51
- - !ruby/object:Gem::Version
52
- version: '0'
53
- required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
67
  none: false
55
- requirements:
56
- - - ! '>='
57
- - !ruby/object:Gem::Version
58
- version: '0'
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
59
75
  requirements: []
76
+
60
77
  rubyforge_project: johntrupiano
61
- rubygems_version: 1.8.10
78
+ rubygems_version: 1.6.2
62
79
  signing_key:
63
80
  specification_version: 3
64
- summary: A gem providing "time travel" and "time freezing" capabilities, making it
65
- dead simple to test time-dependent code. It provides a unified method to mock Time.now,
66
- Date.today, and DateTime.now in a single call.
67
- test_files:
81
+ summary: 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.
82
+ test_files:
68
83
  - test/test_helper.rb
69
84
  - test/time_stack_item_test.rb
70
85
  - test/timecop_test.rb