freeze_time 0.1

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 ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Wes Oldenbeuving
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/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require "rake"
2
+ require "rake/clean"
3
+ require "rake/gempackagetask"
4
+ require 'rubygems'
5
+
6
+ ################################################################################
7
+ ### Gem
8
+ ################################################################################
9
+
10
+ begin
11
+ # Parse gemspec using the github safety level.
12
+ file = Dir['*.gemspec'].first
13
+ data = File.read(file)
14
+ spec = nil
15
+ # FIXME: Lowered SAFE from 3 to 2 to work with Ruby 1.9 due to rubygems
16
+ # performing a require internally
17
+ Thread.new { spec = eval("$SAFE = 2\n%s" % data)}.join
18
+
19
+ # Create the gem tasks
20
+ Rake::GemPackageTask.new(spec) do |package|
21
+ package.gem_spec = spec
22
+ end
23
+ rescue Exception => e
24
+ printf "WARNING: Error caught (%s): %s\n%s", e.class.name, e.message, e.backtrace[0...5].map {|l| ' %s' % l}.join("\n")
25
+ end
26
+
27
+ desc 'Package and install the gem for the current version'
28
+ task :install => :gem do
29
+ system "sudo gem install -l pkg/%s-%s.gem" % [spec.name, spec.version]
30
+ end
31
+
32
+ desc 'Show files missing from gemspec'
33
+ task :diff do
34
+ files = %w[
35
+ Rakefile
36
+ *README* *readme*
37
+ *LICENSE*
38
+ *.gemspec deps.rip
39
+ bin/*
40
+ lib/**/*
41
+ spec/**/*
42
+ ].map {|pattern| Dir.glob(pattern)}.flatten.select{|f| File.file?(f)}
43
+ missing_files = files - spec.files
44
+ extra_files = spec.files - files
45
+ puts "Missing files:"
46
+ puts missing_files.join(" ")
47
+ puts "Extra files:"
48
+ puts extra_files.join(" ")
49
+ end
@@ -0,0 +1,33 @@
1
+ Gem::Specification.new do |s|
2
+ # Project
3
+ s.name = 'freeze_time'
4
+ s.summary = "FreezeTime can freeze time in tests."
5
+ s.description = s.summary
6
+ s.version = '0.1'
7
+ s.date = '2009-09-28'
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Wes Oldenbeuving"]
10
+ s.email = "narnach@gmail.com"
11
+ s.homepage = "http://www.github.com/Narnach/freeze_time"
12
+
13
+ # Files
14
+ root_files = %w[MIT-LICENSE readme.rdoc Rakefile freeze_time.gemspec]
15
+ bin_files = %w[]
16
+ lib_files = %w[freeze_time]
17
+ test_files = %w[]
18
+ spec_files = %w[freeze_time]
19
+ other_files = %w[spec/spec.opts spec/spec_helper.rb]
20
+ s.bindir = "bin"
21
+ s.require_path = "lib"
22
+ s.executables = bin_files
23
+ s.test_files = test_files.map {|f| 'test/%s_test.rb' % f} + spec_files.map {|f| 'spec/%s_spec.rb' % f}
24
+ s.files = root_files + s.test_files + other_files + bin_files.map {|f| 'bin/%s' % f} + lib_files.map {|f| 'lib/%s.rb' % f}
25
+
26
+ # rdoc
27
+ s.has_rdoc = true
28
+ s.extra_rdoc_files = %w[ readme.rdoc MIT-LICENSE]
29
+ s.rdoc_options << '--inline-source' << '--line-numbers' << '--main' << 'README.rdoc'
30
+
31
+ # Requirements
32
+ s.required_ruby_version = ">= 1.8.0"
33
+ end
@@ -0,0 +1,38 @@
1
+ module FreezeTime
2
+ VERSION = '0.1'
3
+ module FreezeTime
4
+ # Within the block, time is frozen.
5
+ # t1 = nil
6
+ # freeze_time do |time|
7
+ # t1 = Time.now
8
+ # sleep 1
9
+ # t2 = Time.now
10
+ # t1 == t2 # => true
11
+ # end
12
+ # t1 == Time.now # => false
13
+ #
14
+ # The value of Time.now is captured before the block is run and then all
15
+ # subsequent calls to Time.now will yield the same Time object. After the
16
+ # block, Time.now returns to its previous functionality.
17
+ def freeze_time(&block) # :yields: time
18
+ old_time = Time
19
+ new_time = old_time.clone
20
+ now = old_time.now
21
+ class << new_time
22
+ attr_accessor :now
23
+ end
24
+ new_time.now = now
25
+ Object.send(:remove_const, :Time)
26
+ Object.send(:const_set, :Time, new_time)
27
+ block.call(now)
28
+ ensure
29
+ Object.send(:remove_const, :Time)
30
+ Object.send(:const_set, :Time, old_time)
31
+ end
32
+ end
33
+ end
34
+
35
+ class Object
36
+ extend FreezeTime::FreezeTime
37
+ include FreezeTime::FreezeTime
38
+ end
data/readme.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = FreezeTime
2
+
3
+ FreezeTime is a helper for testing code that uses time in some way. It implements Kernel#freeze_time, which freezes time within the block passed to it.
4
+
5
+ Within the block, time is frozen.
6
+ t1 = nil
7
+ freeze_time do |time|
8
+ t1 = Time.now
9
+ sleep 1
10
+ t2 = Time.now
11
+ t1 == t2 # => true
12
+ end
13
+ t1 == Time.now # => false
14
+
15
+ The value of Time.now is captured before the block is run and then all
16
+ subsequent calls to Time.now will yield the same Time object. After the
17
+ block, Time.now returns to its previous functionality.
18
+
19
+ == Usage
20
+
21
+ All you need to do to use it in your tests is:
22
+ require 'freeze_time'
23
+ freeze_time do |time|
24
+ # ...
25
+ end
26
+ There are no external dependencies.
27
+
28
+ == Contributors
29
+
30
+ Wes Oldenbeuving [http://github.com/Narnach]
@@ -0,0 +1,25 @@
1
+ require File.join(File.dirname(__FILE__), %w[spec_helper])
2
+ require 'freeze_time'
3
+
4
+ describe FreezeTime do
5
+ it 'should be included in Object' do
6
+ Object.ancestors.should include(FreezeTime::FreezeTime)
7
+ Object.should respond_to(:freeze_time)
8
+ end
9
+
10
+ it 'should freeze Time.now within the block' do
11
+ freeze_time do |time|
12
+ t1 = Time.now
13
+ t2 = Time.now
14
+ t1.should == t2
15
+ end
16
+ end
17
+
18
+ it 'should not freeze Time.now outside of the block' do
19
+ t1 = nil
20
+ freeze_time do |time|
21
+ t1 = Time.now
22
+ end
23
+ t1.should_not == Time.now
24
+ end
25
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,3 @@
1
+ --diff unified
2
+ --colour
3
+ --format specdoc
@@ -0,0 +1,2 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(lib_dir)
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: freeze_time
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Wes Oldenbeuving
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-28 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: FreezeTime can freeze time in tests.
17
+ email: narnach@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - readme.rdoc
24
+ - MIT-LICENSE
25
+ files:
26
+ - MIT-LICENSE
27
+ - readme.rdoc
28
+ - Rakefile
29
+ - freeze_time.gemspec
30
+ - spec/freeze_time_spec.rb
31
+ - spec/spec.opts
32
+ - spec/spec_helper.rb
33
+ - lib/freeze_time.rb
34
+ has_rdoc: true
35
+ homepage: http://www.github.com/Narnach/freeze_time
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --inline-source
41
+ - --line-numbers
42
+ - --main
43
+ - README.rdoc
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 1.8.0
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.4
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: FreezeTime can freeze time in tests.
65
+ test_files:
66
+ - spec/freeze_time_spec.rb