bjhess-time_warp 1.0.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/MIT-LICENSE +20 -0
- data/README +77 -0
- data/Rakefile +22 -0
- data/lib/core_ext.rb +20 -0
- data/lib/time_warp.rb +18 -0
- data/tasks/time_warp_tasks.rake +4 -0
- data/test/test_helper.rb +2 -0
- data/test/time_warp_test.rb +47 -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,77 @@
|
|
|
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
|
+
script/plugin install git://github.com/bjhess/time_warp.git
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
Example
|
|
22
|
+
=======
|
|
23
|
+
|
|
24
|
+
And now a contrived example. In this case, the goal is to let the full
|
|
25
|
+
mechanics of Rails execute. Yes, this test will even hit the database! The
|
|
26
|
+
goal is to assure a particular day of week when each test method executes:
|
|
27
|
+
|
|
28
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
|
29
|
+
class CompanyTest < Test::Unit::TestCase
|
|
30
|
+
|
|
31
|
+
def setup
|
|
32
|
+
@company = companies(:acme)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_should_find_company_needing_reminded_today
|
|
36
|
+
pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
|
|
37
|
+
@company.reminder_day = 'Thursday'
|
|
38
|
+
@company.save
|
|
39
|
+
companies = Company.find_companies_needing_reminded_today
|
|
40
|
+
assert_equal true, companies.include?(@company)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def test_should_not_find_company_needing_reminded_tomorrow
|
|
45
|
+
pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
|
|
46
|
+
@company.reminder_day = 'Friday'
|
|
47
|
+
@company.save
|
|
48
|
+
companies = Company.find_companies_needing_reminded_today
|
|
49
|
+
assert_equal false, companies.include?(@company)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_should_not_find_company_needing_reminded_yesterday
|
|
54
|
+
pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
|
|
55
|
+
@company.reminder_day = 'Wednesday'
|
|
56
|
+
@company.save
|
|
57
|
+
companies = Company.find_companies_needing_reminded_today
|
|
58
|
+
assert_equal false, companies.include?(@company)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
Credits
|
|
65
|
+
=======
|
|
66
|
+
|
|
67
|
+
The creation of this plugin is a direct result of Jason M. Felice's snippet
|
|
68
|
+
(and ensuing discussion). The snippet can be found at DZone:
|
|
69
|
+
|
|
70
|
+
http://snippets.dzone.com/posts/show/1738
|
|
71
|
+
|
|
72
|
+
Further discussion of this snippet's evolution may be found at my blog:
|
|
73
|
+
|
|
74
|
+
http://bjhess.com/blog/2007/08/12/time-warp-for-rails-testing/
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
Copyright (c) 2008 Barry Hess, 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,18 @@
|
|
|
1
|
+
module Test # :nodoc:
|
|
2
|
+
module Unit # :nodoc:
|
|
3
|
+
class TestCase
|
|
4
|
+
|
|
5
|
+
##
|
|
6
|
+
# Time warp to the specified time for the duration of the passed block.
|
|
7
|
+
def pretend_now_is(time)
|
|
8
|
+
begin
|
|
9
|
+
Time.testing_offset = Time.now - time
|
|
10
|
+
yield
|
|
11
|
+
ensure
|
|
12
|
+
Time.testing_offset = 0
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: bjhess-time_warp
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
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@bjhess.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/bjhess/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
|