time-warp 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +88 -0
- data/Rakefile +22 -0
- data/lib/core_ext.rb +20 -0
- data/lib/time_warp.rb +27 -0
- data/tasks/time_warp_tasks.rake +4 -0
- data/test/test_helper.rb +2 -0
- data/test/time_warp_test.rb +58 -0
- metadata +61 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Barry Hess
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
### time_warp
|
2
|
+
|
3
|
+
When writing tests, it is often desirable to bend time in order to test limits and edges of the day. It is especially useful to warp time to test results across the timezones of the world. Manipulating time is also useful to assure a day of the week, month or year every time the test runs.
|
4
|
+
|
5
|
+
Some may say "Why not just mock Time#now?" I see the point, but I find mocking around with baseline Ruby classes to be asking for trouble. Eventually unusual behavior will rear its head and a day will be lost debugging tests - the most excruciating debugging one can be subjected to.
|
6
|
+
|
7
|
+
|
8
|
+
### Installation
|
9
|
+
|
10
|
+
Plugin:
|
11
|
+
|
12
|
+
$ script/plugin install git://github.com/iridesco/time_warp.git
|
13
|
+
|
14
|
+
Gem:
|
15
|
+
|
16
|
+
$ gem sources -a http://gems.github.com
|
17
|
+
$ sudo gem install iridesco-time-warp
|
18
|
+
|
19
|
+
Gem config in a Rails app. environment.rb:
|
20
|
+
|
21
|
+
config.gem 'iridesco-time-warp', :lib => 'time_warp', :source => "http://gems.github.com"
|
22
|
+
|
23
|
+
### Example
|
24
|
+
|
25
|
+
And now a contrived example. In this case, the goal is to let the full mechanics of Rails execute. Yes, this test will even hit the database! The goal is to assure a particular day of week when each test method executes:
|
26
|
+
|
27
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
28
|
+
class CompanyTest < Test::Unit::TestCase
|
29
|
+
|
30
|
+
def setup
|
31
|
+
@company = companies(:acme)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_should_find_company_needing_reminded_today
|
35
|
+
pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
|
36
|
+
@company.reminder_day = 'Thursday'
|
37
|
+
@company.save
|
38
|
+
companies = Company.find_companies_needing_reminded_today
|
39
|
+
assert_equal true, companies.include?(@company)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_should_not_find_company_needing_reminded_tomorrow
|
44
|
+
pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
|
45
|
+
@company.reminder_day = 'Friday'
|
46
|
+
@company.save
|
47
|
+
companies = Company.find_companies_needing_reminded_today
|
48
|
+
assert_equal false, companies.include?(@company)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_should_not_find_company_needing_reminded_yesterday
|
53
|
+
pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
|
54
|
+
@company.reminder_day = 'Wednesday'
|
55
|
+
@company.save
|
56
|
+
companies = Company.find_companies_needing_reminded_today
|
57
|
+
assert_equal false, companies.include?(@company)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
### Notes
|
63
|
+
|
64
|
+
The pretend\_now\_is method may also be called with the arguments for the Time#utc call, rather than a Time argument. So:
|
65
|
+
|
66
|
+
pretend_now_is(Time.utc(2008,"jul",24,20)) do
|
67
|
+
# Shifted code
|
68
|
+
end
|
69
|
+
|
70
|
+
Becomes:
|
71
|
+
|
72
|
+
pretend_now_is(2008,"jul",24,20) do
|
73
|
+
# Shifted code
|
74
|
+
end
|
75
|
+
|
76
|
+
Also, pretend\_now\_is should impact `ActiveSupport` generated `Date` extensions such as `Date.today`, `Date.tomorrow`, and so on.
|
77
|
+
|
78
|
+
Credits
|
79
|
+
=======
|
80
|
+
|
81
|
+
The creation of this plugin is a direct result of Jason M. Felice's snippet (and ensuing discussion). The snippet can be found [at DZone](http://snippets.dzone.com/posts/show/1738).
|
82
|
+
|
83
|
+
Further discussion of this snippet's evolution may be found [at Barry Hess's blog](http://bjhess.com/blog/2007/08/12/time-warp-for-rails-testing/).
|
84
|
+
|
85
|
+
time_warp is maintained and funded by [Iridesco](http://iridesco.com).
|
86
|
+
|
87
|
+
|
88
|
+
Copyright (c) 2008 [Barry Hess](http://bjhess.com), [Iridesco](http://iridesco.com). Released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the time_warp plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the time_warp plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'TimeWarp'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/lib/core_ext.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
##
|
2
|
+
# Extend Time class to offset the time that 'now' returns. This
|
3
|
+
# provides the opening to "warp time" for any tests checking for
|
4
|
+
# time-based limitations. Perhaps one needs to check hourly
|
5
|
+
# limits, common time borders like midnight, etc.
|
6
|
+
if !Time.respond_to?(:real_now) # assures there is no infinite looping when aliasing #now
|
7
|
+
Time.class_eval do
|
8
|
+
class << self
|
9
|
+
attr_accessor :testing_offset
|
10
|
+
|
11
|
+
alias_method :real_now, :now
|
12
|
+
def now
|
13
|
+
real_now - testing_offset
|
14
|
+
end
|
15
|
+
alias_method :new, :now
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
Time.testing_offset = 0
|
data/lib/time_warp.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'core_ext'
|
2
|
+
|
3
|
+
module Test # :nodoc:
|
4
|
+
module Unit # :nodoc:
|
5
|
+
class TestCase
|
6
|
+
|
7
|
+
##
|
8
|
+
# Time warp to the specified time for the duration of the passed block.
|
9
|
+
def pretend_now_is(*args)
|
10
|
+
begin
|
11
|
+
Time.testing_offset = Time.now - time_from(*args)
|
12
|
+
yield
|
13
|
+
ensure
|
14
|
+
Time.testing_offset = 0
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def time_from(*args)
|
21
|
+
return args[0] if 1 == args.size && args[0].is_a?(Time)
|
22
|
+
Time.utc(*args)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TimeWarpTest < Test::Unit::TestCase
|
4
|
+
def test_test_unit_test_case_should_respond_to_pretend_now_is
|
5
|
+
assert_equal true, self.respond_to?(:pretend_now_is)
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_pretend_now_is_should_set_now_back_in_time
|
9
|
+
pretend_now_is(Time.utc(2008,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 UTC 2008
|
10
|
+
assert_equal 2008, Time.now.utc.year
|
11
|
+
assert_equal 7, Time.now.utc.month
|
12
|
+
assert_equal 25, Time.now.utc.day
|
13
|
+
assert_equal 6, Time.now.utc.hour
|
14
|
+
assert_equal 15, Time.now.utc.min
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_pretend_now_is_should_set_now_forward_in_time
|
19
|
+
future_year = Time.now.year + 1
|
20
|
+
pretend_now_is(Time.utc(future_year,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 UTC future_year
|
21
|
+
assert_equal future_year, Time.now.utc.year
|
22
|
+
assert_equal 7, Time.now.utc.month
|
23
|
+
assert_equal 25, Time.now.utc.day
|
24
|
+
assert_equal 6, Time.now.utc.hour
|
25
|
+
assert_equal 15, Time.now.utc.min
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_pretend_now_should_revert_to_real_now_after_block
|
30
|
+
now = Time.now
|
31
|
+
now_year = now.year
|
32
|
+
now_month = now.month
|
33
|
+
now_day = now.day
|
34
|
+
now_hour = now.hour
|
35
|
+
now_minute = now.min
|
36
|
+
|
37
|
+
pretend_now_is(Time.utc(2008,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 UTC 2008
|
38
|
+
# block of code
|
39
|
+
end
|
40
|
+
|
41
|
+
assert_equal now_year, Time.now.year
|
42
|
+
assert_equal now_month, Time.now.month
|
43
|
+
assert_equal now_day, Time.now.day
|
44
|
+
assert_equal now_hour, Time.now.hour
|
45
|
+
assert_equal now_minute, Time.now.min
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_pretend_now_resolves_to_the_same_value_regardless_of_setting_by_time_argument_or_time_utc_arguments
|
49
|
+
now_with_time_argument = now_with_time_utc_arguments = nil
|
50
|
+
pretend_now_is(Time.utc(2008,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 UTC 2008
|
51
|
+
now_with_time_argument = Time.now.utc
|
52
|
+
end
|
53
|
+
pretend_now_is(2008,"jul",25,6,15) do #=> Fri Jul 25 06:15:00 UTC 2008
|
54
|
+
now_with_time_utc_arguments = Time.now.utc
|
55
|
+
end
|
56
|
+
assert_equal now_with_time_argument.to_s, now_with_time_utc_arguments.to_s
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time-warp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Barry Hess
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-08 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: TimeWarp is a ruby library for manipulating times in automated tests.
|
17
|
+
email: barry@iridesco.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- MIT-LICENSE
|
26
|
+
- Rakefile
|
27
|
+
- README.md
|
28
|
+
- lib/core_ext.rb
|
29
|
+
- lib/time_warp.rb
|
30
|
+
- tasks/time_warp_tasks.rake
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/iridesco/time_warp
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.3.5
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Warp time in your tests
|
59
|
+
test_files:
|
60
|
+
- test/test_helper.rb
|
61
|
+
- test/time_warp_test.rb
|