jtrupiano-timecop 0.1.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.txt +9 -0
- data/Manifest.txt +10 -0
- data/README.txt +75 -0
- data/Rakefile +33 -0
- data/lib/timecop.rb +3 -0
- data/lib/timecop/time_extensions.rb +78 -0
- data/lib/timecop/timecop.rb +121 -0
- data/lib/timecop/version.rb +20 -0
- data/test/test_timecop.rb +138 -0
- data/timecop.gemspec +35 -0
- metadata +77 -0
data/History.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
=== 0.1.0 / 2008-11-09
|
|
2
|
+
|
|
3
|
+
* Initial Feature Set
|
|
4
|
+
|
|
5
|
+
* Temporarily (or permanently if you prefer) change the concept of Time.now, DateTime.now (if defined), and Date.today (if defined)
|
|
6
|
+
* Timecop#travel api allows an argument to be passed in as one of: 1) Time instance, 2) DateTime instance, 3) Date instance,
|
|
7
|
+
4) individual arguments (year, month, day, hour, minute, second)
|
|
8
|
+
* Nested calls to Timecop#travel are supported -- each block will maintain it's interpretation of now.
|
|
9
|
+
|
data/Manifest.txt
ADDED
data/README.txt
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
= timecop
|
|
2
|
+
|
|
3
|
+
* http://github.com/jtrupiano/timecop
|
|
4
|
+
|
|
5
|
+
== DESCRIPTION:
|
|
6
|
+
|
|
7
|
+
A gem providing simple ways to (temporarily) override Time.now, Date.today, and DateTime.now. It provides "time travel" capabilities, making it dead simple to write test time-dependent code.
|
|
8
|
+
|
|
9
|
+
== FEATURES/PROBLEMS:
|
|
10
|
+
|
|
11
|
+
* Temporarily (or permanently if you prefer) change the concept of Time.now, DateTime.now (if defined), and Date.today (if defined)
|
|
12
|
+
* Timecop#travel api allows an argument to be passed in as one of: 1) Time instance, 2) DateTime instance, 3) Date instance,
|
|
13
|
+
4) individual arguments (year, month, day, hour, minute, second)
|
|
14
|
+
* Nested calls to Timecop#travel are supported -- each block will maintain it's interpretation of now.
|
|
15
|
+
|
|
16
|
+
== SYNOPSIS:
|
|
17
|
+
|
|
18
|
+
* Run a time-sensitive test:
|
|
19
|
+
<code>
|
|
20
|
+
joe = User.find(1)
|
|
21
|
+
joe.purchase_home()
|
|
22
|
+
assert !joe.mortgage_due?
|
|
23
|
+
# move ahead a month and assert that the mortgage is due
|
|
24
|
+
Timecop.travel(Date.today + 30) do
|
|
25
|
+
assert joe.mortgage_due?
|
|
26
|
+
end
|
|
27
|
+
</code>
|
|
28
|
+
|
|
29
|
+
* Set the time for the test environment of a rails app -- this is particularly helpful if your whole application
|
|
30
|
+
is time-sensitive. It allows you to build your test data at a single point in time, and to move in/out of that
|
|
31
|
+
time as appropriate (within your tests)
|
|
32
|
+
|
|
33
|
+
in config/environments/test.rb
|
|
34
|
+
|
|
35
|
+
<code>
|
|
36
|
+
config.after_initialize do
|
|
37
|
+
# Set Time.now to September 1, 2008 10:05:00 AM
|
|
38
|
+
t = Time.local(2008, 9, 1, 10, 5, 0)
|
|
39
|
+
Timecop.travel(t)
|
|
40
|
+
end
|
|
41
|
+
</code>
|
|
42
|
+
|
|
43
|
+
== REQUIREMENTS:
|
|
44
|
+
|
|
45
|
+
* None
|
|
46
|
+
|
|
47
|
+
== INSTALL:
|
|
48
|
+
|
|
49
|
+
* sudo gem install timecop (latest stable version from rubyforge)
|
|
50
|
+
* sudo gem install jtrupiano-timecop (HEAD of the repo from github)
|
|
51
|
+
|
|
52
|
+
== LICENSE:
|
|
53
|
+
|
|
54
|
+
(The MIT License)
|
|
55
|
+
|
|
56
|
+
Copyright (c) 2008 FIX
|
|
57
|
+
|
|
58
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
59
|
+
a copy of this software and associated documentation files (the
|
|
60
|
+
'Software'), to deal in the Software without restriction, including
|
|
61
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
62
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
63
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
64
|
+
the following conditions:
|
|
65
|
+
|
|
66
|
+
The above copyright notice and this permission notice shall be
|
|
67
|
+
included in all copies or substantial portions of the Software.
|
|
68
|
+
|
|
69
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
70
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
71
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
72
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
73
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
74
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
75
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# -*- ruby -*-
|
|
2
|
+
|
|
3
|
+
require 'rubygems'
|
|
4
|
+
require 'hoe'
|
|
5
|
+
require './lib/timecop/version.rb'
|
|
6
|
+
|
|
7
|
+
PKG_NAME = "timecop"
|
|
8
|
+
PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
|
|
9
|
+
version = Timecop::Version::STRING.dup
|
|
10
|
+
if ENV['SNAPSHOT'].to_i == 1
|
|
11
|
+
version << "." << Time.now.utc.strftime("%Y%m%d%H%M%S")
|
|
12
|
+
end
|
|
13
|
+
PKG_VERSION = version
|
|
14
|
+
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
|
15
|
+
|
|
16
|
+
Hoe.new(PKG_NAME, PKG_VERSION) do |p|
|
|
17
|
+
p.rubyforge_name = 'johntrupiano' # if different than lowercase project name
|
|
18
|
+
p.developer('John Trupiano', 'jtrupiano@gmail.com')
|
|
19
|
+
p.name = PKG_NAME
|
|
20
|
+
p.version = PKG_VERSION
|
|
21
|
+
#p.platform = Gem::Platform::RUBY
|
|
22
|
+
p.author = "John Trupiano"
|
|
23
|
+
p.email = "jtrupiano@gmail.com"
|
|
24
|
+
p.description = %q(A gem providing simple ways to temporarily override Time.now, Date.today, and DateTime.now. It provides "time travel" capabilities, making it dead simple to write test time-dependent code.)
|
|
25
|
+
p.summary = p.description # More details later??
|
|
26
|
+
p.remote_rdoc_dir = PKG_NAME # Release to /PKG_NAME
|
|
27
|
+
# p.changes = p.paragraphs_of('CHANGELOG', 0..1).join("\n\n")
|
|
28
|
+
p.need_zip = true
|
|
29
|
+
p.need_tar = false
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# vim: syntax=Ruby
|
|
33
|
+
|
data/lib/timecop.rb
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# 1. Extensions to the Time, Date, and DateTime objects
|
|
2
|
+
# 2. Allows us to "freeze" time in our Ruby applications.
|
|
3
|
+
# 3. This is very useful when your app's functionality is dependent on time (e.g.
|
|
4
|
+
# anything that might expire). This will allow us to alter the return value of
|
|
5
|
+
# Date.today, Time.now, and DateTime.now, such that our application code _never_ has to change.
|
|
6
|
+
|
|
7
|
+
class Time
|
|
8
|
+
class << self
|
|
9
|
+
# Time we might be behaving as
|
|
10
|
+
attr_reader :mock_time
|
|
11
|
+
|
|
12
|
+
# Set new time to pretend we are.
|
|
13
|
+
def mock_time=(new_now)
|
|
14
|
+
@mock_time = new_now
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Alias the original now
|
|
18
|
+
alias_method :now_without_mock_time, :now
|
|
19
|
+
|
|
20
|
+
# Define now_with_mock_time
|
|
21
|
+
def now_with_mock_time
|
|
22
|
+
mock_time || now_without_mock_time
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Alias now to now_with_mock_time
|
|
26
|
+
alias_method :now, :now_with_mock_time
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
if Object.const_defined?(:Date)
|
|
31
|
+
class Date
|
|
32
|
+
class << self
|
|
33
|
+
# Date we might be behaving as
|
|
34
|
+
attr_reader :mock_date
|
|
35
|
+
|
|
36
|
+
# Set new date to pretend we are.
|
|
37
|
+
def mock_date=(new_date)
|
|
38
|
+
@mock_date = new_date
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Alias the original today
|
|
42
|
+
alias_method :today_without_mock_date, :today
|
|
43
|
+
|
|
44
|
+
# Define today_with_mock_date
|
|
45
|
+
def today_with_mock_date
|
|
46
|
+
mock_date || today_without_mock_date
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Alias today to today_with_mock_date
|
|
50
|
+
alias_method :today, :today_with_mock_date
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
if Object.const_defined?(:DateTime)
|
|
56
|
+
class DateTime
|
|
57
|
+
class << self
|
|
58
|
+
# Time we might be behaving as
|
|
59
|
+
attr_reader :mock_time
|
|
60
|
+
|
|
61
|
+
# Set new time to pretend we are.
|
|
62
|
+
def mock_time=(new_now)
|
|
63
|
+
@mock_time = new_now
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Alias the original now
|
|
67
|
+
alias_method :now_without_mock_time, :now
|
|
68
|
+
|
|
69
|
+
# Define now_with_mock_time
|
|
70
|
+
def now_with_mock_time
|
|
71
|
+
mock_time || now_without_mock_time
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Alias now to now_with_mock_time
|
|
75
|
+
alias_method :now, :now_with_mock_time
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'time_extensions')
|
|
2
|
+
|
|
3
|
+
# 1. Wrapper class for manipulating the extensions to the Time, Date, and DateTime objects
|
|
4
|
+
# 2. Allows us to "freeze" time in our Ruby applications.
|
|
5
|
+
# 3. This is very useful when your app's functionality is dependent on time (e.g.
|
|
6
|
+
# anything that might expire). This will allow us to alter the return value of
|
|
7
|
+
# Date.today, Time.now, and DateTime.now, such that our application code _never_ has to change.
|
|
8
|
+
class Timecop
|
|
9
|
+
|
|
10
|
+
# Re-bases Time.now, Date.today and DateTime.now (if it exists) to use the time passed in.
|
|
11
|
+
# When using this method directly, it is up to the developer to call unset_all to return us
|
|
12
|
+
# to sanity.
|
|
13
|
+
#
|
|
14
|
+
# * If being consumed in a rails app, Time.zone.local will be used to instantiate the time.
|
|
15
|
+
# Otherwise, Time.local will be used.
|
|
16
|
+
def self.set_all(year, month, day, hour=0, minute=0, second=0)
|
|
17
|
+
if Time.respond_to?(:zone) && !Time.zone.nil?
|
|
18
|
+
# ActiveSupport loaded
|
|
19
|
+
time = Time.zone.local(year, month, day, hour, minute, second)
|
|
20
|
+
else
|
|
21
|
+
# ActiveSupport not loaded
|
|
22
|
+
time = Time.local(year, month, day, hour, minute, second)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
Time.mock_time = time
|
|
26
|
+
Date.mock_date = Date.new(time.year, time.month, time.day) if Object.const_defined?(:Date)
|
|
27
|
+
DateTime.mock_time = DateTime.new(time.year, time.month, time.day, time.hour, time.min, time.sec) if Object.const_defined?(:DateTime)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Reverts back to system's Time.now, Date.today and DateTime.now (if it exists). If set_all
|
|
31
|
+
# was never called in the first place, this method will have no effect.
|
|
32
|
+
def self.unset_all
|
|
33
|
+
Date.mock_date = nil
|
|
34
|
+
DateTime.mock_time = nil if Object.const_defined?(:Date)
|
|
35
|
+
Time.mock_time = nil if Object.const_defined?(:DateTime)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Allows you to run a block of code and "fake" a time throughout the execution of that block.
|
|
39
|
+
# This is particularly useful for writing test methods where the passage of time is critical to the business
|
|
40
|
+
# logic being tested. For example:
|
|
41
|
+
#
|
|
42
|
+
# <code>
|
|
43
|
+
# joe = User.find(1)
|
|
44
|
+
# joe.purchase_home()
|
|
45
|
+
# assert !joe.mortgage_due?
|
|
46
|
+
# Timecop.travel(2008, 10, 5) do
|
|
47
|
+
# assert joe.mortgage_due?
|
|
48
|
+
# end
|
|
49
|
+
# </code>
|
|
50
|
+
#
|
|
51
|
+
# travel will respond to several different arguments:
|
|
52
|
+
# 1. Timecop.travel(time_inst)
|
|
53
|
+
# 2. Timecop.travel(datetime_inst)
|
|
54
|
+
# 3. Timecop.travel(date_inst)
|
|
55
|
+
# 4. Timecop.travel(year, month, day, hour=0, minute=0, second=0)
|
|
56
|
+
#
|
|
57
|
+
# When a block is also passed, the Time.now, DateTime.now and Date.today are all reset to their
|
|
58
|
+
# previous values. This allows us to nest multiple calls to Timecop.travel and have each block
|
|
59
|
+
# maintain it's concept of "now."
|
|
60
|
+
#def self.travel(year, month, day, hour=0, minute=0, second=0, &block)
|
|
61
|
+
def self.travel(*args, &block)
|
|
62
|
+
year, month, day, hour, minute, second = parse_travel_args(*args)
|
|
63
|
+
|
|
64
|
+
old_time = Time.mock_time
|
|
65
|
+
old_date = Date.mock_date if Object.const_defined?(:Date)
|
|
66
|
+
old_datetime = DateTime.mock_time if Object.const_defined?(:DateTime)
|
|
67
|
+
|
|
68
|
+
set_all(year, month, day, hour, minute, second)
|
|
69
|
+
|
|
70
|
+
if block_given?
|
|
71
|
+
begin
|
|
72
|
+
yield
|
|
73
|
+
ensure
|
|
74
|
+
Time.mock_time = old_time
|
|
75
|
+
Date.mock_date = old_date if Object.const_defined?(:Date)
|
|
76
|
+
DateTime.mock_time = old_datetime if Object.const_defined?(:DateTime)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
def self.parse_travel_args(*args)
|
|
83
|
+
arg = args.shift
|
|
84
|
+
if arg.is_a?(Time) || (Object.const_defined?(:DateTime) && arg.is_a?(DateTime))
|
|
85
|
+
year, month, day, hour, minute, second = arg.year, arg.month, arg.day, arg.hour, arg.min, arg.sec
|
|
86
|
+
elsif Object.const_defined?(:Date) && arg.is_a?(Date)
|
|
87
|
+
year, month, day, hour, minute, second = arg.year, arg.month, arg.day, 0, 0, 0
|
|
88
|
+
puts "#{year}-#{month}-#{day} #{hour}:#{minute}:#{second}"
|
|
89
|
+
else # we'll just assume it's a list of y/m/h/d/m/s
|
|
90
|
+
year = arg || 0
|
|
91
|
+
month = args.shift || 1
|
|
92
|
+
day = args.shift || 1
|
|
93
|
+
hour = args.shift || 0
|
|
94
|
+
minute = args.shift || 0
|
|
95
|
+
second = args.shift || 0
|
|
96
|
+
end
|
|
97
|
+
return year, month, day, hour, minute, second
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
#def with_dates(*dates, &block)
|
|
102
|
+
# dates.flatten.each do |date|
|
|
103
|
+
# begin
|
|
104
|
+
# DateTime.forced_now = case date
|
|
105
|
+
# when String: DateTime.parse(date)
|
|
106
|
+
# when Time: DateTime.parse(date.to_s)
|
|
107
|
+
# else
|
|
108
|
+
# date
|
|
109
|
+
# end
|
|
110
|
+
# Date.forced_today = Date.new(DateTime.forced_now.year,
|
|
111
|
+
#DateTime.forced_now.month, DateTime.forced_now.day)
|
|
112
|
+
# yield
|
|
113
|
+
# rescue Exception => e
|
|
114
|
+
# raise e
|
|
115
|
+
# ensure
|
|
116
|
+
# DateTime.forced_now = nil
|
|
117
|
+
# Date.forced_today = nil
|
|
118
|
+
# end
|
|
119
|
+
# end
|
|
120
|
+
#end
|
|
121
|
+
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Timecop
|
|
2
|
+
module Version #:nodoc:
|
|
3
|
+
# A method for comparing versions of required modules. It expects two
|
|
4
|
+
# arrays of integers as parameters, the first being the minimum version
|
|
5
|
+
# required, and the second being the actual version available. It returns
|
|
6
|
+
# true if the actual version is at least equal to the required version.
|
|
7
|
+
def self.check(required, actual) #:nodoc:
|
|
8
|
+
required = required.map { |v| "%06d" % v }.join(".")
|
|
9
|
+
actual = actual.map { |v| "%06d" % v }.join(".")
|
|
10
|
+
return actual >= required
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
MAJOR = 0
|
|
14
|
+
MINOR = 1
|
|
15
|
+
TINY = 0
|
|
16
|
+
|
|
17
|
+
STRING = [MAJOR, MINOR, TINY].join(".")
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
|
|
2
|
+
require 'date'
|
|
3
|
+
require 'test/unit'
|
|
4
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')
|
|
5
|
+
|
|
6
|
+
class TestTimecop < Test::Unit::TestCase
|
|
7
|
+
|
|
8
|
+
def setup
|
|
9
|
+
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# just in case...let's really make sure that Timecop is disabled between tests...
|
|
13
|
+
def teardown
|
|
14
|
+
Timecop.unset_all
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def test_travel_changes_and_resets_time
|
|
18
|
+
# depending on how we're invoked (individually or via the rake test suite)
|
|
19
|
+
assert !Time.respond_to?(:zone) || Time.zone.nil?
|
|
20
|
+
|
|
21
|
+
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
22
|
+
assert_not_equal t, Time.now
|
|
23
|
+
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
|
24
|
+
assert_equal t, Time.now
|
|
25
|
+
end
|
|
26
|
+
assert_not_equal t, Time.now
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_recursive_travel
|
|
30
|
+
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
31
|
+
Timecop.travel(2008, 10, 10, 10, 10, 10) do
|
|
32
|
+
assert_equal t, Time.now
|
|
33
|
+
t2 = Time.local(2008, 9, 9, 9, 9, 9)
|
|
34
|
+
Timecop.travel(2008, 9, 9, 9, 9, 9) do
|
|
35
|
+
assert_equal t2, Time.now
|
|
36
|
+
end
|
|
37
|
+
assert_equal t, Time.now
|
|
38
|
+
end
|
|
39
|
+
assert_nil Time.send(:mock_time)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def test_travel_with_time_instance_works_as_expected
|
|
43
|
+
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
44
|
+
Timecop.travel(t) do
|
|
45
|
+
assert_equal t, Time.now
|
|
46
|
+
assert_equal DateTime.new(2008, 10, 10, 10, 10, 10), DateTime.now
|
|
47
|
+
assert_equal Date.new(2008, 10, 10), Date.today
|
|
48
|
+
end
|
|
49
|
+
assert_not_equal t, Time.now
|
|
50
|
+
assert_not_equal DateTime.new(2008, 10, 10, 10, 10, 10), DateTime.now
|
|
51
|
+
assert_not_equal Date.new(2008, 10, 10), Date.today
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_travel_with_datetime_instance_works_as_expected
|
|
55
|
+
t = DateTime.new(2008, 10, 10, 10, 10, 10)
|
|
56
|
+
Timecop.travel(t) do
|
|
57
|
+
assert_equal t, DateTime.now
|
|
58
|
+
assert_equal Time.local(2008, 10, 10, 10, 10, 10), Time.now
|
|
59
|
+
assert_equal Date.new(2008, 10, 10), Date.today
|
|
60
|
+
end
|
|
61
|
+
assert_not_equal t, DateTime.now
|
|
62
|
+
assert_not_equal Time.local(2008, 10, 10, 10, 10, 10), Time.now
|
|
63
|
+
assert_not_equal Date.new(2008, 10, 10), Date.today
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def test_travel_with_date_instance_works_as_expected
|
|
67
|
+
d = Date.new(2008, 10, 10)
|
|
68
|
+
Timecop.travel(d) do
|
|
69
|
+
assert_equal d, Date.today
|
|
70
|
+
assert_equal Time.local(2008, 10, 10, 0, 0, 0), Time.now
|
|
71
|
+
assert_equal DateTime.new(2008, 10, 10, 0, 0, 0), DateTime.now
|
|
72
|
+
end
|
|
73
|
+
assert_not_equal d, Date.today
|
|
74
|
+
assert_not_equal Time.local(2008, 10, 10, 0, 0, 0), Time.now
|
|
75
|
+
assert_not_equal DateTime.new(2008, 10, 10, 0, 0, 0), DateTime.now
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def test_exception_thrown_in_travel_block_properly_resets_time
|
|
79
|
+
t = Time.local(2008, 10, 10, 10, 10, 10)
|
|
80
|
+
begin
|
|
81
|
+
Timecop.travel(t) do
|
|
82
|
+
assert_equal t, Time.now
|
|
83
|
+
raise "blah exception"
|
|
84
|
+
end
|
|
85
|
+
rescue
|
|
86
|
+
assert_not_equal t, Time.now
|
|
87
|
+
assert_nil Time.send(:mock_time)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def test_parse_travel_args_with_time
|
|
93
|
+
t = Time.now
|
|
94
|
+
y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
|
|
95
|
+
ty, tm, td, th, tmin, ts = Timecop.send(:parse_travel_args, t)
|
|
96
|
+
assert_equal y, ty
|
|
97
|
+
assert_equal m, tm
|
|
98
|
+
assert_equal d, td
|
|
99
|
+
assert_equal h, th
|
|
100
|
+
assert_equal min, tmin
|
|
101
|
+
assert_equal s, ts
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def test_parse_travel_args_with_datetime
|
|
105
|
+
t = DateTime.now
|
|
106
|
+
y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
|
|
107
|
+
ty, tm, td, th, tmin, ts = Timecop.send(:parse_travel_args, t)
|
|
108
|
+
assert_equal y, ty
|
|
109
|
+
assert_equal m, tm
|
|
110
|
+
assert_equal d, td
|
|
111
|
+
assert_equal h, th
|
|
112
|
+
assert_equal min, tmin
|
|
113
|
+
assert_equal s, ts
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def test_parse_travel_args_with_date
|
|
117
|
+
date = Date.today
|
|
118
|
+
y, m, d, h, min, s = date.year, date.month, date.day, 0, 0, 0
|
|
119
|
+
ty, tm, td, th, tmin, ts = Timecop.send(:parse_travel_args, date)
|
|
120
|
+
assert_equal y, ty
|
|
121
|
+
assert_equal m, tm
|
|
122
|
+
assert_equal d, td
|
|
123
|
+
assert_equal h, th
|
|
124
|
+
assert_equal min, tmin
|
|
125
|
+
assert_equal s, ts
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def test_parse_travel_args_with_individual_arguments
|
|
129
|
+
y, m, d, h, min, s = 2008, 10, 10, 10, 10, 10
|
|
130
|
+
ty, tm, td, th, tmin, ts = Timecop.send(:parse_travel_args, y, m, d, h, min, s)
|
|
131
|
+
assert_equal y, ty
|
|
132
|
+
assert_equal m, tm
|
|
133
|
+
assert_equal d, td
|
|
134
|
+
assert_equal h, th
|
|
135
|
+
assert_equal min, tmin
|
|
136
|
+
assert_equal s, ts
|
|
137
|
+
end
|
|
138
|
+
end
|
data/timecop.gemspec
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = %q{timecop}
|
|
5
|
+
s.version = "0.1.0"
|
|
6
|
+
|
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
8
|
+
s.authors = ["John Trupiano"]
|
|
9
|
+
s.date = %q{2008-11-09}
|
|
10
|
+
s.description = %q{A gem providing simple ways to temporarily override Time.now, Date.today, and DateTime.now. It provides "time travel" capabilities, making it dead simple to write test time-dependent code.}
|
|
11
|
+
s.email = %q{jtrupiano@gmail.com}
|
|
12
|
+
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
|
13
|
+
s.files = ["History.txt", "Manifest.txt", "README.txt", "Rakefile", "lib/timecop.rb", "lib/timecop/time_extensions.rb", "lib/timecop/timecop.rb", "lib/timecop/version.rb", "test/test_timecop.rb", "timecop.gemspec", "test/test_timecop_with_rails.rb", "test/test_timecop_without_date.rb"]
|
|
14
|
+
s.has_rdoc = true
|
|
15
|
+
s.homepage = %q{http://github.com/jtrupiano/timecop}
|
|
16
|
+
s.rdoc_options = ["--main", "README.txt"]
|
|
17
|
+
s.require_paths = ["lib"]
|
|
18
|
+
s.rubyforge_project = %q{johntrupiano}
|
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
|
20
|
+
s.summary = %q{A gem providing simple ways to temporarily override Time.now, Date.today, and DateTime.now. It provides "time travel" capabilities, making it dead simple to write test time-dependent code.}
|
|
21
|
+
s.test_files = ["test/test_timecop.rb", "test/test_timecop_with_rails.rb", "test/test_timecop_without_date.rb"]
|
|
22
|
+
|
|
23
|
+
if s.respond_to? :specification_version then
|
|
24
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
25
|
+
s.specification_version = 2
|
|
26
|
+
|
|
27
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
28
|
+
s.add_development_dependency(%q<hoe>, [">= 1.8.2"])
|
|
29
|
+
else
|
|
30
|
+
s.add_dependency(%q<hoe>, [">= 1.8.2"])
|
|
31
|
+
end
|
|
32
|
+
else
|
|
33
|
+
s.add_dependency(%q<hoe>, [">= 1.8.2"])
|
|
34
|
+
end
|
|
35
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: jtrupiano-timecop
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- John Trupiano
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2008-11-09 00:00:00 -08:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: hoe
|
|
17
|
+
version_requirement:
|
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
19
|
+
requirements:
|
|
20
|
+
- - ">="
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: 1.8.2
|
|
23
|
+
version:
|
|
24
|
+
description: A gem providing simple ways to temporarily override Time.now, Date.today, and DateTime.now. It provides "time travel" capabilities, making it dead simple to write test time-dependent code.
|
|
25
|
+
email: jtrupiano@gmail.com
|
|
26
|
+
executables: []
|
|
27
|
+
|
|
28
|
+
extensions: []
|
|
29
|
+
|
|
30
|
+
extra_rdoc_files:
|
|
31
|
+
- History.txt
|
|
32
|
+
- Manifest.txt
|
|
33
|
+
- README.txt
|
|
34
|
+
files:
|
|
35
|
+
- History.txt
|
|
36
|
+
- Manifest.txt
|
|
37
|
+
- README.txt
|
|
38
|
+
- Rakefile
|
|
39
|
+
- lib/timecop.rb
|
|
40
|
+
- lib/timecop/time_extensions.rb
|
|
41
|
+
- lib/timecop/timecop.rb
|
|
42
|
+
- lib/timecop/version.rb
|
|
43
|
+
- test/test_timecop.rb
|
|
44
|
+
- timecop.gemspec
|
|
45
|
+
- test/test_timecop_with_rails.rb
|
|
46
|
+
- test/test_timecop_without_date.rb
|
|
47
|
+
has_rdoc: true
|
|
48
|
+
homepage: http://github.com/jtrupiano/timecop
|
|
49
|
+
post_install_message:
|
|
50
|
+
rdoc_options:
|
|
51
|
+
- --main
|
|
52
|
+
- README.txt
|
|
53
|
+
require_paths:
|
|
54
|
+
- lib
|
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: "0"
|
|
60
|
+
version:
|
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: "0"
|
|
66
|
+
version:
|
|
67
|
+
requirements: []
|
|
68
|
+
|
|
69
|
+
rubyforge_project: johntrupiano
|
|
70
|
+
rubygems_version: 1.2.0
|
|
71
|
+
signing_key:
|
|
72
|
+
specification_version: 2
|
|
73
|
+
summary: A gem providing simple ways to temporarily override Time.now, Date.today, and DateTime.now. It provides "time travel" capabilities, making it dead simple to write test time-dependent code.
|
|
74
|
+
test_files:
|
|
75
|
+
- test/test_timecop.rb
|
|
76
|
+
- test/test_timecop_with_rails.rb
|
|
77
|
+
- test/test_timecop_without_date.rb
|