timecop 0.4.3 → 0.4.4

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,4 +1,14 @@
1
- === 0.4.1 / 2012-07-27
1
+ === 0.4.4 / 2012-07-29
2
+
3
+ * Maintenance
4
+ * Can now configure Timecop to not use ActiveSupport by setting Timecop.active_support = false
5
+
6
+ === 0.4.3 / 2012-07-27
7
+
8
+ * Maintenance
9
+ * Fix alias of Time#new/Time#now when Time#new given with args
10
+
11
+ === 0.4.2 / 2012-07-27
2
12
 
3
13
  * Maintenance
4
14
  * Mock Time#new the same as Time#now since they're synonyms
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 3
2
+ :patch: 4
3
3
  :major: 0
4
4
  :minor: 4
@@ -82,8 +82,8 @@ class Timecop
82
82
  time_klass = Time
83
83
  time_klass = Time.zone if Time.respond_to? :zone
84
84
  arg = args.shift
85
- if arg.is_a?(Time)
86
- if arg.respond_to?(:in_time_zone)
85
+ if arg.is_a?(Time)
86
+ if Timecop.active_support != false && arg.respond_to?(:in_time_zone)
87
87
  arg.in_time_zone
88
88
  else
89
89
  arg.getlocal
@@ -99,7 +99,7 @@ class Timecop
99
99
  elsif arg.nil?
100
100
  Time.now
101
101
  else
102
- if Time.respond_to?(:parse) && arg.is_a?(String)
102
+ if arg.is_a?(String) && Timecop.active_support != false && Time.respond_to?(:parse)
103
103
  Time.parse(arg)
104
104
  else
105
105
  # we'll just assume it's a list of y/m/d/h/m/s
@@ -13,78 +13,82 @@ require 'timecop/time_stack_item'
13
13
  class Timecop
14
14
  include Singleton
15
15
 
16
- # Allows you to run a block of code and "fake" a time throughout the execution of that block.
17
- # This is particularly useful for writing test methods where the passage of time is critical to the business
18
- # logic being tested. For example:
19
- #
20
- # joe = User.find(1)
21
- # joe.purchase_home()
22
- # assert !joe.mortgage_due?
23
- # Timecop.freeze(2008, 10, 5) do
24
- # assert joe.mortgage_due?
25
- # end
26
- #
27
- # freeze and travel will respond to several different arguments:
28
- # 1. Timecop.freeze(time_inst)
29
- # 2. Timecop.freeze(datetime_inst)
30
- # 3. Timecop.freeze(date_inst)
31
- # 4. Timecop.freeze(offset_in_seconds)
32
- # 5. Timecop.freeze(year, month, day, hour=0, minute=0, second=0)
33
- #
34
- # When a block is also passed, Time.now, DateTime.now and Date.today are all reset to their
35
- # previous values after the block has finished executing. This allows us to nest multiple
36
- # calls to Timecop.travel and have each block maintain it's concept of "now."
37
- #
38
- # * Note: Timecop.freeze will actually freeze time. This can cause unanticipated problems if
39
- # benchmark or other timing calls are executed, which implicitly expect Time to actually move
40
- # forward.
41
- #
42
- # * Rails Users: Be especially careful when setting this in your development environment in a
43
- # rails project. Generators will load your environment, including the migration generator,
44
- # which will lead to files being generated with the timestamp set by the Timecop.freeze call
45
- # in your dev environment
46
- #
47
- # Returns the frozen time.
48
- def self.freeze(*args, &block)
49
- instance().send(:travel, :freeze, *args, &block)
50
- Time.now
51
- end
16
+ class << self
17
+ attr_accessor :active_support
18
+
19
+ # Allows you to run a block of code and "fake" a time throughout the execution of that block.
20
+ # This is particularly useful for writing test methods where the passage of time is critical to the business
21
+ # logic being tested. For example:
22
+ #
23
+ # joe = User.find(1)
24
+ # joe.purchase_home()
25
+ # assert !joe.mortgage_due?
26
+ # Timecop.freeze(2008, 10, 5) do
27
+ # assert joe.mortgage_due?
28
+ # end
29
+ #
30
+ # freeze and travel will respond to several different arguments:
31
+ # 1. Timecop.freeze(time_inst)
32
+ # 2. Timecop.freeze(datetime_inst)
33
+ # 3. Timecop.freeze(date_inst)
34
+ # 4. Timecop.freeze(offset_in_seconds)
35
+ # 5. Timecop.freeze(year, month, day, hour=0, minute=0, second=0)
36
+ #
37
+ # When a block is also passed, Time.now, DateTime.now and Date.today are all reset to their
38
+ # previous values after the block has finished executing. This allows us to nest multiple
39
+ # calls to Timecop.travel and have each block maintain it's concept of "now."
40
+ #
41
+ # * Note: Timecop.freeze will actually freeze time. This can cause unanticipated problems if
42
+ # benchmark or other timing calls are executed, which implicitly expect Time to actually move
43
+ # forward.
44
+ #
45
+ # * Rails Users: Be especially careful when setting this in your development environment in a
46
+ # rails project. Generators will load your environment, including the migration generator,
47
+ # which will lead to files being generated with the timestamp set by the Timecop.freeze call
48
+ # in your dev environment
49
+ #
50
+ # Returns the frozen time.
51
+ def freeze(*args, &block)
52
+ instance().send(:travel, :freeze, *args, &block)
53
+ Time.now
54
+ end
52
55
 
53
- # Allows you to run a block of code and "fake" a time throughout the execution of that block.
54
- # See Timecop#freeze for a sample of how to use (same exact usage syntax)
55
- #
56
- # * Note: Timecop.travel will not freeze time (as opposed to Timecop.freeze). This is a particularly
57
- # good candidate for use in environment files in rails projects.
58
- #
59
- # Returns the 'new' current Time.
60
- def self.travel(*args, &block)
61
- instance().send(:travel, :travel, *args, &block)
62
- Time.now
63
- end
56
+ # Allows you to run a block of code and "fake" a time throughout the execution of that block.
57
+ # See Timecop#freeze for a sample of how to use (same exact usage syntax)
58
+ #
59
+ # * Note: Timecop.travel will not freeze time (as opposed to Timecop.freeze). This is a particularly
60
+ # good candidate for use in environment files in rails projects.
61
+ #
62
+ # Returns the 'new' current Time.
63
+ def travel(*args, &block)
64
+ instance().send(:travel, :travel, *args, &block)
65
+ Time.now
66
+ end
64
67
 
65
- def self.baseline
66
- instance().send(:baseline)
67
- end
68
+ def baseline
69
+ instance().send(:baseline)
70
+ end
68
71
 
69
- def self.baseline=(baseline)
70
- instance().send(:baseline=, baseline)
71
- end
72
+ def baseline=(baseline)
73
+ instance().send(:baseline=, baseline)
74
+ end
72
75
 
73
- # Reverts back to system's Time.now, Date.today and DateTime.now (if it exists).
74
- #
75
- # Returns Time.now, which is now the real current time.
76
- def self.return
77
- instance().send(:unmock!)
78
- Time.now
79
- end
76
+ # 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
+ def return
80
+ instance().send(:unmock!)
81
+ Time.now
82
+ end
80
83
 
81
- def self.return_to_baseline
82
- instance().send(:return_to_baseline)
83
- Time.now
84
- end
84
+ def return_to_baseline
85
+ instance().send(:return_to_baseline)
86
+ Time.now
87
+ end
85
88
 
86
- def self.top_stack_item #:nodoc:
87
- instance().instance_variable_get(:@_stack).last
89
+ def top_stack_item #:nodoc:
90
+ instance().instance_variable_get(:@_stack).last
91
+ end
88
92
  end
89
93
 
90
94
  protected
@@ -5,6 +5,7 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
5
5
  class TestTimeStackItem < Test::Unit::TestCase
6
6
 
7
7
  def teardown
8
+ Timecop.active_support = nil
8
9
  Timecop.return
9
10
  end
10
11
 
@@ -182,19 +183,22 @@ class TestTimeStackItem < Test::Unit::TestCase
182
183
  Timecop.freeze(2011, 01, 02, hour=0, minute=0, second=0)
183
184
  end
184
185
 
185
- def test_integration_with_rails_time_zone
186
+ def test_parse_with_active_support_off
187
+ date = '2012-01-02'
188
+ Timecop.active_support = false
189
+ Time.expects(:parse).never
190
+ Timecop.freeze(date)
191
+ end
192
+
193
+ def test_uses_active_supports_in_time_zone
186
194
  time = Time.now
187
- def time.in_time_zone
188
- self - 3600
189
- end
190
- t = time - 3600
191
- stack_item = Timecop::TimeStackItem.new(:freeze, time)
192
- y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
193
- assert_equal y, stack_item.year
194
- assert_equal m, stack_item.month
195
- assert_equal d, stack_item.day
196
- assert_equal h, stack_item.hour
197
- assert_equal min, stack_item.min
198
- assert_equal s, stack_item.sec
195
+ Time.any_instance.expects(:in_time_zone).returns(time)
196
+ Timecop::TimeStackItem.new(:freeze, time)
197
+ end
198
+
199
+ def test_configured_off_active_support_in_time_zone_xxx
200
+ Timecop.active_support = false
201
+ Time.any_instance.expects(:in_time_zone).never
202
+ Timecop::TimeStackItem.new(:freeze, Time.now)
199
203
  end
200
204
  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.4.3
4
+ version: 0.4.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -58,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  version: '0'
59
59
  requirements: []
60
60
  rubyforge_project: johntrupiano
61
- rubygems_version: 1.8.19
61
+ rubygems_version: 1.8.23
62
62
  signing_key:
63
63
  specification_version: 3
64
64
  summary: A gem providing "time travel" and "time freezing" capabilities, making it
@@ -70,4 +70,3 @@ test_files:
70
70
  - test/timecop_test.rb
71
71
  - test/timecop_without_date_test.rb
72
72
  - test/timecop_without_date_but_with_time_test.rb
73
- has_rdoc: