Skip to content
This repository
Browse code

Merge pull request #85 from MicahChalmer/timecop_before_datetime

Make TimeCop work with Date/DateTime even if Timecop was loaded first
  • Loading branch information...
commit f3a2bbf56a1af99dd6de4d4a7b9ae8e1601c0543 2 parents 966b160 + 0d5d4d5
Travis Jeffery authored
71 lib/timecop/time_extensions.rb
... ... @@ -1,3 +1,4 @@
  1 +require 'date'
1 2
2 3 class Time #:nodoc:
3 4 class << self
@@ -24,52 +25,48 @@ def new_with_mock_time(*args)
24 25 end
25 26 end
26 27
27   -if Object.const_defined?(:Date) && Date.respond_to?(:today)
28   - class Date #:nodoc:
29   - class << self
30   - def mock_date
31   - mocked_time_stack_item = Timecop.top_stack_item
32   - mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.date(self)
33   - end
34   -
35   - alias_method :today_without_mock_date, :today
  28 +class Date #:nodoc:
  29 + class << self
  30 + def mock_date
  31 + mocked_time_stack_item = Timecop.top_stack_item
  32 + mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.date(self)
  33 + end
36 34
37   - def today_with_mock_date
38   - mock_date || today_without_mock_date
39   - end
  35 + alias_method :today_without_mock_date, :today
40 36
41   - alias_method :today, :today_with_mock_date
  37 + def today_with_mock_date
  38 + mock_date || today_without_mock_date
42 39 end
  40 +
  41 + alias_method :today, :today_with_mock_date
43 42 end
44 43 end
45 44
46   -if Object.const_defined?(:DateTime) && DateTime.respond_to?(:now)
47   - class DateTime #:nodoc:
48   - class << self
49   - def mock_time
50   - mocked_time_stack_item = Timecop.top_stack_item
51   - mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.datetime(self)
52   - end
53   -
54   - def now_without_mock_time
55   - Time.now_without_mock_time.to_datetime
56   - end
57   -
58   - def now_with_mock_time
59   - mock_time || now_without_mock_time
60   - end
61   -
62   - alias_method :now, :now_with_mock_time
  45 +class DateTime #:nodoc:
  46 + class << self
  47 + def mock_time
  48 + mocked_time_stack_item = Timecop.top_stack_item
  49 + mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.datetime(self)
  50 + end
  51 +
  52 + def now_without_mock_time
  53 + Time.now_without_mock_time.to_datetime
63 54 end
  55 +
  56 + def now_with_mock_time
  57 + mock_time || now_without_mock_time
  58 + end
  59 +
  60 + alias_method :now, :now_with_mock_time
64 61 end
  62 +end
65 63
66   - # for ruby1.8
67   - unless Time::public_instance_methods.include? :to_datetime
68   - class DateTime
69   - class << self
70   - def now_without_mock_time
71   - Time.now_without_mock_time.send(:to_datetime)
72   - end
  64 +# for ruby1.8
  65 +unless Time::public_instance_methods.include? :to_datetime
  66 + class DateTime
  67 + class << self
  68 + def now_without_mock_time
  69 + Time.now_without_mock_time.send(:to_datetime)
73 70 end
74 71 end
75 72 end
40 test/timecop_loaded_before_date_test.rb
... ... @@ -0,0 +1,40 @@
  1 +# When this test is running under 'bundle exec', it "helpfully" adds
  2 +# '-rbundler/setup' to RUBYOPT, which in turn loads the Date/DateTime classes
  3 +# (in some versions of Bundler.) But the bug we're trying to test for here only
  4 +# occurs if timecop was loaded before Date/DateTime. So we have to detect if
  5 +# 'date' was already loaded here and rip it out before running the test.
  6 +if Object.const_defined?(:Date) && Date.respond_to?(:today)
  7 + Object.send(:remove_const, :Date)
  8 + Object.send(:remove_const, :DateTime)
  9 + $LOADED_FEATURES.delete('date')
  10 +end
  11 +
  12 +# Require timecop first, before anything else that would load 'date'
  13 +require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
  14 +require File.join(File.dirname(__FILE__), "test_helper")
  15 +require 'date'
  16 +
  17 +class TestTimecopWithoutDate < Test::Unit::TestCase
  18 + # just in case...let's really make sure that Timecop is disabled between tests...
  19 + def teardown
  20 + Timecop.return
  21 + end
  22 +
  23 + def test_freeze_changes_and_resets_date
  24 + t = Date.new(2008, 10, 10)
  25 + assert_not_equal t, Date.today
  26 + Timecop.freeze(2008, 10, 10) do
  27 + assert_equal t, Date.today
  28 + end
  29 + assert_not_equal t, Date.today
  30 + end
  31 +
  32 + def test_freeze_changes_and_resets_datetime
  33 + t = DateTime.new(2008, 10, 10, 0, 0, 0, 0)
  34 + assert_not_equal t, DateTime.now
  35 + Timecop.freeze(t) do
  36 + assert_date_times_equal t, DateTime.now
  37 + end
  38 + assert_not_equal t, DateTime.now
  39 + end
  40 +end

0 comments on commit f3a2bbf

Please sign in to comment.
Something went wrong with that request. Please try again.