iridesco-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 +99 -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 +59 -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
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
time_warp
|
2
|
+
=========
|
3
|
+
|
4
|
+
When writing tests, it is often desirable to bend time in order to test limits
|
5
|
+
and edges of the day. It is especially useful to warp time to test results
|
6
|
+
across the timezones of the world. Manipulating time is also useful to assure
|
7
|
+
a day of the week, month or year every time the test runs.
|
8
|
+
|
9
|
+
Some may say "Why not just mock Time#now?" I see the point, but I find
|
10
|
+
mocking around with baseline Ruby classes to be asking for trouble. Eventually
|
11
|
+
unusual behavior will rear its head and a day will be lost debugging
|
12
|
+
tests - the most excruciating debugging one can be subjected to.
|
13
|
+
|
14
|
+
|
15
|
+
Installation
|
16
|
+
============
|
17
|
+
|
18
|
+
Plugin:
|
19
|
+
|
20
|
+
$ script/plugin install git://github.com/iridesco/time_warp.git
|
21
|
+
|
22
|
+
Gem:
|
23
|
+
|
24
|
+
$ gem sources -a http://gems.github.com
|
25
|
+
$ sudo gem install iridesco-time-warp
|
26
|
+
|
27
|
+
Gem config in a Rails app. environment.rb:
|
28
|
+
|
29
|
+
config.gem 'iridesco-time-warp', :lib => 'time_warp', :source => "http://gems.github.com"
|
30
|
+
|
31
|
+
Example
|
32
|
+
=======
|
33
|
+
|
34
|
+
And now a contrived example. In this case, the goal is to let the full
|
35
|
+
mechanics of Rails execute. Yes, this test will even hit the database! The
|
36
|
+
goal is to assure a particular day of week when each test method executes:
|
37
|
+
|
38
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
39
|
+
class CompanyTest < Test::Unit::TestCase
|
40
|
+
|
41
|
+
def setup
|
42
|
+
@company = companies(:acme)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_find_company_needing_reminded_today
|
46
|
+
pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
|
47
|
+
@company.reminder_day = 'Thursday'
|
48
|
+
@company.save
|
49
|
+
companies = Company.find_companies_needing_reminded_today
|
50
|
+
assert_equal true, companies.include?(@company)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_should_not_find_company_needing_reminded_tomorrow
|
55
|
+
pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
|
56
|
+
@company.reminder_day = 'Friday'
|
57
|
+
@company.save
|
58
|
+
companies = Company.find_companies_needing_reminded_today
|
59
|
+
assert_equal false, companies.include?(@company)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_should_not_find_company_needing_reminded_yesterday
|
64
|
+
pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
|
65
|
+
@company.reminder_day = 'Wednesday'
|
66
|
+
@company.save
|
67
|
+
companies = Company.find_companies_needing_reminded_today
|
68
|
+
assert_equal false, companies.include?(@company)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
The pretend_now_is method may also be called with the arguments for the
|
74
|
+
Time#utc call, rather than a Time argument. So:
|
75
|
+
|
76
|
+
pretend_now_is(Time.utc(2008,"jul",24,20)) do
|
77
|
+
# Shifted code
|
78
|
+
end
|
79
|
+
|
80
|
+
Becomes:
|
81
|
+
|
82
|
+
pretend_now_is(2008,"jul",24,20) do
|
83
|
+
# Shifted code
|
84
|
+
end
|
85
|
+
|
86
|
+
Credits
|
87
|
+
=======
|
88
|
+
|
89
|
+
The creation of this plugin is a direct result of Jason M. Felice's snippet
|
90
|
+
(and ensuing discussion). The snippet can be found at DZone:
|
91
|
+
|
92
|
+
http://snippets.dzone.com/posts/show/1738
|
93
|
+
|
94
|
+
Further discussion of this snippet's evolution may be found at my blog:
|
95
|
+
|
96
|
+
http://bjhess.com/blog/2007/08/12/time-warp-for-rails-testing/
|
97
|
+
|
98
|
+
|
99
|
+
Copyright (c) 2008 Barry Hess, Iridesco. 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,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: iridesco-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 -08: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
|
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
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.2.0
|
54
|
+
signing_key:
|
55
|
+
specification_version: 2
|
56
|
+
summary: Warp time in your tests
|
57
|
+
test_files:
|
58
|
+
- test/test_helper.rb
|
59
|
+
- test/time_warp_test.rb
|