Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
View
@@ -1,3 +1,4 @@
+require 'date'
class Time #:nodoc:
class << self
@@ -24,52 +25,48 @@ def new_with_mock_time(*args)
end
end
-if Object.const_defined?(:Date) && Date.respond_to?(:today)
- class Date #:nodoc:
- class << self
- def mock_date
- mocked_time_stack_item = Timecop.top_stack_item
- mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.date(self)
- end
-
- alias_method :today_without_mock_date, :today
+class Date #:nodoc:
+ class << self
+ def mock_date
+ mocked_time_stack_item = Timecop.top_stack_item
+ mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.date(self)
+ end
- def today_with_mock_date
- mock_date || today_without_mock_date
- end
+ alias_method :today_without_mock_date, :today
- alias_method :today, :today_with_mock_date
+ def today_with_mock_date
+ mock_date || today_without_mock_date
end
+
+ alias_method :today, :today_with_mock_date
end
end
-if Object.const_defined?(:DateTime) && DateTime.respond_to?(:now)
- class DateTime #:nodoc:
- class << self
- def mock_time
- mocked_time_stack_item = Timecop.top_stack_item
- mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.datetime(self)
- end
-
- def now_without_mock_time
- Time.now_without_mock_time.to_datetime
- end
-
- def now_with_mock_time
- mock_time || now_without_mock_time
- end
-
- alias_method :now, :now_with_mock_time
+class DateTime #:nodoc:
+ class << self
+ def mock_time
+ mocked_time_stack_item = Timecop.top_stack_item
+ mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.datetime(self)
+ end
+
+ def now_without_mock_time
+ Time.now_without_mock_time.to_datetime
end
+
+ def now_with_mock_time
+ mock_time || now_without_mock_time
+ end
+
+ alias_method :now, :now_with_mock_time
end
+end
- # for ruby1.8
- unless Time::public_instance_methods.include? :to_datetime
- class DateTime
- class << self
- def now_without_mock_time
- Time.now_without_mock_time.send(:to_datetime)
- end
+# for ruby1.8
+unless Time::public_instance_methods.include? :to_datetime
+ class DateTime
+ class << self
+ def now_without_mock_time
+ Time.now_without_mock_time.send(:to_datetime)
end
end
end
40 test/timecop_loaded_before_date_test.rb
View
@@ -0,0 +1,40 @@
+# When this test is running under 'bundle exec', it "helpfully" adds
+# '-rbundler/setup' to RUBYOPT, which in turn loads the Date/DateTime classes
+# (in some versions of Bundler.) But the bug we're trying to test for here only
+# occurs if timecop was loaded before Date/DateTime. So we have to detect if
+# 'date' was already loaded here and rip it out before running the test.
+if Object.const_defined?(:Date) && Date.respond_to?(:today)
+ Object.send(:remove_const, :Date)
+ Object.send(:remove_const, :DateTime)
+ $LOADED_FEATURES.delete('date')
+end
+
+# Require timecop first, before anything else that would load 'date'
+require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
+require File.join(File.dirname(__FILE__), "test_helper")
+require 'date'
+
+class TestTimecopWithoutDate < Test::Unit::TestCase
+ # just in case...let's really make sure that Timecop is disabled between tests...
+ def teardown
+ Timecop.return
+ end
+
+ def test_freeze_changes_and_resets_date
+ t = Date.new(2008, 10, 10)
+ assert_not_equal t, Date.today
+ Timecop.freeze(2008, 10, 10) do
+ assert_equal t, Date.today
+ end
+ assert_not_equal t, Date.today
+ end
+
+ def test_freeze_changes_and_resets_datetime
+ t = DateTime.new(2008, 10, 10, 0, 0, 0, 0)
+ assert_not_equal t, DateTime.now
+ Timecop.freeze(t) do
+ assert_date_times_equal t, DateTime.now
+ end
+ assert_not_equal t, DateTime.now
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.