pax_global_header00006660000000000000000000000064121161330310014501gustar00rootroot0000000000000052 comment=35c11b4faaf73c1377a478fa265daf4ca5a0d909 ruby-delorean-2.0.0/000077500000000000000000000000001211613303100142505ustar00rootroot00000000000000ruby-delorean-2.0.0/.gitignore000066400000000000000000000000501211613303100162330ustar00rootroot00000000000000*.gem pkg/* rdoc/* .bundle/* .bin *.swp ruby-delorean-2.0.0/.travis.yml000066400000000000000000000002271211613303100163620ustar00rootroot00000000000000rvm: - 1.8.7 - 1.9.2 - 1.9.3 gemfile: - gemfiles/Gemfile.2.3.x - gemfiles/Gemfile.3.0.x - gemfiles/Gemfile.3.1.x - gemfiles/Gemfile.3.2.xruby-delorean-2.0.0/Gemfile000066400000000000000000000001501211613303100155370ustar00rootroot00000000000000source 'http://rubygems.org' gemspec gem 'rspec', '~> 2.9.0' gem 'activesupport', '> 2.3.0' gem 'rake' ruby-delorean-2.0.0/Gemfile.lock000066400000000000000000000011151211613303100164700ustar00rootroot00000000000000PATH remote: . specs: delorean (1.2.0) chronic GEM remote: http://rubygems.org/ specs: activesupport (3.2.6) i18n (~> 0.6) multi_json (~> 1.0) chronic (0.6.7) diff-lcs (1.1.3) i18n (0.6.0) multi_json (1.3.6) rake (0.9.2.2) rspec (2.9.0) rspec-core (~> 2.9.0) rspec-expectations (~> 2.9.0) rspec-mocks (~> 2.9.0) rspec-core (2.9.0) rspec-expectations (2.9.1) diff-lcs (~> 1.1.3) rspec-mocks (2.9.0) PLATFORMS ruby DEPENDENCIES activesupport (> 2.3.0) delorean! rake rspec (~> 2.9.0) ruby-delorean-2.0.0/HISTORY.md000066400000000000000000000021141211613303100157310ustar00rootroot00000000000000# Delorean changelog ## Version 2.0.0 (2012-07-04) * Update dependencies, get rid of deprecations, test against different versions of activesupport (by brain-geek and jwo) * Added flux capacitor (by nashby) ## Version 1.2.0 (2011-12-17) * Support for DateTime (by tomykaira) ## Version 1.1.1 (2011-11-11) * Fixes for Ruby 1.9.3 (by Otto Hilska) * Enable Travis CI * Create this file ;) ## Version 1.1.0 (2011-06-24) * Delorean.jump accepts passing blocks (by David Calavera) ## Version 1.0.1 (2011-06-03) * Bugfix: time\_travel\_to now works fine with DateTime ## Version 1.0.0 (2011-03-01) * Added back_to_1985 and removed trailing whitespace (by @retr0h) ## Version 0.2.1 (2010-11-16) * Added explicit test to ensure expected time_travel_to block form return value (by Nathaniel Bibler) * Delorean now uses Bundler (by Nathaniel Bibler) ## Version 0.2.0 (2010-05-25) * Allow `time_travel` with Chronic options (by Les Hill and Rafa García) * Require `active_support` for specs * Not include Delorean module in Object * Update docs ## Version 0.1.0 (2010-02-24) * Initial version ruby-delorean-2.0.0/MIT-LICENSE000066400000000000000000000020601211613303100157020ustar00rootroot00000000000000Copyright (c) 2008 Luismi Cavallé & Sergio Gil Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ruby-delorean-2.0.0/README.md000066400000000000000000000043551211613303100155360ustar00rootroot00000000000000# Delorean [![Build Status](https://secure.travis-ci.org/bebanjo/delorean.png)](http://travis-ci.org/bebanjo/delorean) Delorean lets you travel in time with Ruby by mocking `Time.now` ![](http://dl.dropbox.com/u/645329/delorean.png) > Marty:: Wait a minute, Doc. Ah... Are you telling me that you built a time machine... out of a DeLorean? > > Doc:: The way I see it, if you're gonna build a time machine into a car, why not do it with some style? ## Install $ [sudo] gem install delorean Or add it to your `Gemfile`, etc. ## Usage Let's travel in time! require 'delorean' # Date.today => Wed Feb 24 Delorean.time_travel_to "1 month ago" # Date.today => Sun Jan 24 Delorean.back_to_the_present # Date.today => Wed Feb 24 With a block: Delorean.time_travel_to("1 month ago") do # Inside the block, Time.now => Sun Jan 24 00:34:32 +0100 2010 sleep(5) # And the time still goes by... Time.now => Sun Jan 24 00:34:37 +0100 2010 end # Outside the block, Time.now => Wed Feb 24 00:34:35 +0100 2010 You can also `jump` which is like `sleep` but without losing time # Time.now => Wed Feb 24 00:34:04 +0100 2010 Delorean.jump 30 # Time.now => Wed Feb 24 00:34:34 +0100 2010 ## Testing Time-travelling can be extremely useful when you're testing your application. For example, in RSpec you may find convenient to include Delorean's DSL in your `spec_helper.rb`: RSpec.configure do |config| config.include Delorean ... Now you can time-travel in your examples, like this: it "should show latest created user" do time_travel_to(3.minutes.ago) { create_user :name => "John" } time_travel_to(5.minutes.ago) { create_user :name => "Chris" } get 'show' response.should have_text("John") response.should_not have_text("Chris") end Don't forget to go back to the present after each example: after(:each) { back_to_the_present } or its alternate syntax: after(:each) { back_to_1985 } ## Credits Delorean image based on [an original](http://www.gocarlo.com/lagalerie/archives/march05.htm) by [Giancarlo Pitocco](http://www.gocarlo.com/). Copyright (c) 2010-2011 Luismi Cavallé, Sergio Gil and BeBanjo S.L. released under the MIT license ruby-delorean-2.0.0/Rakefile000066400000000000000000000002401211613303100157110ustar00rootroot00000000000000require "rubygems" require "rspec" require "rspec/core/rake_task" RSpec::Core::RakeTask.new do |t| t.rspec_opts = %w(--colour) end task :default => ["spec"]ruby-delorean-2.0.0/delorean.gemspec000066400000000000000000000010311211613303100174010ustar00rootroot00000000000000# encoding: utf-8 Gem::Specification.new do |gem| gem.name = "delorean" gem.version = "2.0.0" gem.date = Date.today.to_s gem.summary = "Delorean lets you travel in time with Ruby by mocking Time.now" gem.authors = ["Luismi Cavallé", "Sergio Gil"] gem.email = "ballsbreaking@bebanjo.com" gem.homepage = "http://github.com/bebanjo/delorean" gem.extra_rdoc_files = ["README.md"] gem.files = ["lib/delorean.rb", "lib/flux_capacitor.rb", "README.md", "MIT-LICENSE"] gem.add_runtime_dependency 'chronic', '>= 0' end ruby-delorean-2.0.0/gemfiles/000077500000000000000000000000001211613303100160435ustar00rootroot00000000000000ruby-delorean-2.0.0/gemfiles/Gemfile.2.3.x000066400000000000000000000001661211613303100201100ustar00rootroot00000000000000source 'http://rubygems.org' gemspec :path => '..' gem 'rspec', '~> 2.9.0' gem 'activesupport', '~>2.3.0' gem 'rake' ruby-delorean-2.0.0/gemfiles/Gemfile.3.0.x000066400000000000000000000002011211613303100200740ustar00rootroot00000000000000source 'http://rubygems.org' gemspec :path => '..' gem 'rspec', '~> 2.9.0' gem 'rake' gem 'activesupport', '~>3.0.0' gem 'i18n' ruby-delorean-2.0.0/gemfiles/Gemfile.3.1.x000066400000000000000000000002011211613303100200750ustar00rootroot00000000000000source 'http://rubygems.org' gemspec :path => '..' gem 'rspec', '~> 2.9.0' gem 'rake' gem 'activesupport', '~>3.1.0' gem 'i18n' ruby-delorean-2.0.0/gemfiles/Gemfile.3.2.x000066400000000000000000000001661211613303100201100ustar00rootroot00000000000000source 'http://rubygems.org' gemspec :path => '..' gem 'rspec', '~> 2.9.0' gem 'rake' gem 'activesupport', '~>3.2.0' ruby-delorean-2.0.0/lib/000077500000000000000000000000001211613303100150165ustar00rootroot00000000000000ruby-delorean-2.0.0/lib/delorean.rb000066400000000000000000000010221211613303100171270ustar00rootroot00000000000000require 'chronic' require 'flux_capacitor' module Delorean include FluxCapacitor extend self end class << Time alias_method :now_without_delorean, :now def now; Delorean.now; end end if RUBY_VERSION >= "1.9.3" class << Date alias_method :today_without_delorean, :today def today(sg=Date::ITALY) t = Time.now Date.civil(t.year, t.mon, t.mday) end end class << DateTime alias_method :now_without_delorean, :now def now(sg=Date::ITALY) Time.now.to_datetime end end end ruby-delorean-2.0.0/lib/flux_capacitor.rb000066400000000000000000000021331211613303100203450ustar00rootroot00000000000000module Delorean module FluxCapacitor def time_travel_to(time, options={}) mock_current_time(time, options) return unless block_given? begin yield ensure restore_previous_time end end def back_to_the_present reset end alias :back_to_1985 :back_to_the_present def jump(seconds) mock_current_time Time.now + seconds return unless block_given? begin yield ensure restore_previous_time end end def now Time.now_without_delorean - time_travel_offsets.inject(0){ |sum, val| sum + val } end private def time_travel_offsets @@time_travel_offsets ||= [] end def reset @@time_travel_offsets = [] end def mock_current_time(time, options={}) time = Chronic.parse(time, options) if time.is_a?(String) time = Time.local(time.year, time.month, time.day) if time.is_a?(Date) && !time.is_a?(DateTime) time_travel_offsets.push Time.now - time end def restore_previous_time time_travel_offsets.pop end end end ruby-delorean-2.0.0/spec/000077500000000000000000000000001211613303100152025ustar00rootroot00000000000000ruby-delorean-2.0.0/spec/delorean_spec.rb000066400000000000000000000115351211613303100203370ustar00rootroot00000000000000require 'rubygems' require 'active_support/all' require File.expand_path("../lib/delorean", File.dirname(__FILE__)) describe Delorean do before(:all) do @default_tz = ENV['TZ'] ENV['TZ'] = 'UTC' end after(:all) do ENV['TZ'] = @default_tz end after(:each) do Delorean.back_to_the_present end describe "time_travel_to" do it "should travel through time" do past_date = Time.utc(2009,1,1,10,30) Delorean.time_travel_to past_date Time.now.should be_within(1).of(past_date) end it "should travel through time several times" do past_date = Date.new(2009,1,1) Delorean.time_travel_to Date.new(2009,2,20) Delorean.time_travel_to past_date Date.today.should == past_date end it "should travel to string times" do two_minutes_from_now = Time.now + 120 Delorean.time_travel_to "2 minutes from now" Time.now.should be_within(1).of(two_minutes_from_now) end it "should travel to string times with options" do two_days_ago = 2.days.ago yesterday = Chronic.parse '1 day ago' Delorean.time_travel_to "1 day ago", :now => yesterday Time.now.should be_within(1).of(two_days_ago) end it "should return the final value of the block given" do Delorean.time_travel_to(Date.new(1955,11,12)) do 'You\'re safe and sound now' 'Back in good old 1955.' end.should eql 'Back in good old 1955.' end it "should work with DateTime" do datetime = DateTime.strptime("2011-05-25 18:00", "%Y-%m-%d %H:%M") Delorean.time_travel_to(datetime) do Time.now.should be_within(1).of(datetime) end end it "should change the result of DateTime" do datetime = DateTime.strptime("2011-05-25 18:00", "%Y-%m-%d %H:%M") Delorean.time_travel_to(datetime) do DateTime.now.should be_within(1).of(datetime) end end end describe "back_to_the_present" do it "should stay in the present if in the present" do today = Date.today Delorean.back_to_the_present Date.today.should == today end it "should go back to the present if not in the present" do today = Date.today Delorean.time_travel_to Time.local(2009,2,2,1,1) Delorean.back_to_the_present Date.today.should == today end it "should go back to the original present if travelled through time several times" do today = Date.today 2.times { Delorean.time_travel_to Time.local(2009,2,2,1,1) } Delorean.back_to_the_present Date.today.should == today end end describe "time_travel_to with block" do it "should travel through time" do past_date = Time.utc(2009,1,1,10,30) Delorean.time_travel_to(past_date) do Time.now.should be_within(1).of(past_date) end end it "should return to the future" do today = Date.today Delorean.time_travel_to(Time.utc(2009,2,2,10,30)) {} Date.today.should == today end it "should travel through time several times" do Delorean.time_travel_to(Time.utc(2009,2,2,10,40)) do Delorean.time_travel_to(Time.utc(2009,1,1,22,45)) do Date.today.should == Date.new(2009,1,1) end end end it "should still return to the future" do today = Date.today Delorean.time_travel_to(Time.utc(2009,2,2,10,40)) do Delorean.time_travel_to(Time.utc(2009,1,1,10,40)) {} Date.today.should == Date.new(2009,2,2) end Date.today.should == today end it "should travel to string times" do two_minutes_ago = Time.now - 120 Delorean.time_travel_to("2 minutes ago") do Time.now.should be_within(1).of(two_minutes_ago) end end it "should travel to string times with options" do two_days_ago = 2.days.ago yesterday = Chronic.parse '1 day ago' Delorean.time_travel_to("1 day ago", :now => yesterday) do Time.now.should be_within(1).of(two_days_ago) end end end describe "jump" do it "should jump the given number of seconds to the future" do expected = Time.now + 60 Delorean.jump 60 Time.now.should be_within(1).of(expected) end it "should return the final value of the block given" do to_the_future = Date.new(2015,10,21).to_time - Time.now Delorean.jump(to_the_future) do 'You\'re safe and sound now' 'Marti, I need to your help to save your future children' end.should eql 'Marti, I need to your help to save your future children' end it "should jump to future using rails date helpers" do expected = Time.now + 60 Delorean.jump 1.minute Time.now.should be_within(1).of(expected) end it "should jump to future using rails date helpers" do expected = Time.now + 3600 Delorean.jump 1.hour Time.now.should be_within(1).of(expected) end end end