-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The time steps now fall back to the ActiveSupport time function if the timecop gem is not present. Moved the time steps from timecop.rb to time.rb and deprecated importing timecop.rb
- Loading branch information
1 parent
a952ed8
commit 063b4f0
Showing
17 changed files
with
184 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
# coding: UTF-8 | ||
require 'rspec/matchers' | ||
|
||
ALREADY_LOADED_FILES = %w[all_steps timecop_steps] | ||
|
||
Dir[File.join(File.dirname(__FILE__), '*_steps.rb')].each do |f| | ||
name = File.basename(f, '.rb') | ||
unless name == 'all_steps' | ||
|
||
unless ALREADY_LOADED_FILES.include?(name) | ||
require "spreewald/#{name}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# coding: UTF-8 | ||
|
||
|
||
# Steps to travel through time | ||
# | ||
# This uses [Timecop](https://github.com/jtrupiano/timecop) or Active Support 4.1+ to stub Time.now / Time.current. | ||
# The user is responsible for including one of the two gems. | ||
# | ||
# Please note that the two approaches branch. While ActiveSupport will freeze the time, Timecop will keep it running. | ||
# FILE_COMMENT_END | ||
|
||
major_minor_rails_version = defined?(ActiveSupport) ? [ActiveSupport::VERSION::MAJOR, ActiveSupport::VERSION::MINOR] : [0, 0] | ||
is_at_least_rails_4_1 = (major_minor_rails_version <=> [4, 1]) != -1 | ||
|
||
if defined?(Timecop) || is_at_least_rails_4_1 | ||
|
||
module TimeHelpers | ||
|
||
# When you have to make your rails app time zone aware you have to go 100% | ||
# otherwise you are better off ignoring time zones at all. | ||
# https://makandracards.com/makandra/8723-guide-to-localizing-a-rails-application | ||
|
||
def use_timezones? | ||
active_record_loaded = defined?(ActiveRecord::Base) | ||
(!active_record_loaded || ActiveRecord::Base.default_timezone != :local) && Time.zone | ||
end | ||
|
||
def parse_time(str) | ||
if use_timezones? | ||
Time.zone.parse(str) | ||
else | ||
Time.parse(str) | ||
end | ||
end | ||
|
||
def current_time | ||
if use_timezones? | ||
Time.current | ||
else | ||
Time.now | ||
end | ||
end | ||
|
||
if defined?(Timecop) | ||
# Emulate ActiveSupport time helper methods with Timecop - don't rename these methods | ||
def travel(duration) | ||
Timecop.travel(current_time + duration) | ||
end | ||
|
||
def travel_to(date_or_time) | ||
Timecop.travel(date_or_time) | ||
end | ||
|
||
def travel_back | ||
Timecop.return | ||
end | ||
else | ||
require 'active_support/testing/time_helpers' | ||
include ActiveSupport::Testing::TimeHelpers | ||
end | ||
|
||
end | ||
|
||
World(TimeHelpers) | ||
|
||
# Example: | ||
# | ||
# Given the date is 2012-02-10 | ||
# Given the time is 2012-02-10 13:40 | ||
When /^the (?:date|time) is "?(\d{4}-\d{2}-\d{2}(?: \d{1,2}:\d{2})?)"?$/ do |time| | ||
travel_to parse_time(time) | ||
end.overridable | ||
|
||
# Example: | ||
# | ||
# Given the time is 13:40 | ||
When /^the time is "?(\d{1,2}:\d{2})"?$/ do |time_without_date| | ||
travel_to parse_time(time_without_date) # date will be today | ||
end.overridable | ||
|
||
# Example: | ||
# | ||
# When it is 10 minutes later | ||
# When it is a few hours earlier | ||
When /^it is (\d+|an?|some|a few) (seconds?|minutes?|hours?|days?|weeks?|months?|years?) (later|earlier)$/ do |amount, unit, direction| | ||
amount = case amount | ||
when 'a', 'an' | ||
1 | ||
when 'some', 'a few' | ||
10 | ||
else | ||
amount.to_i | ||
end | ||
amount = -amount if direction == 'earlier' | ||
travel amount.send(unit) | ||
end.overridable | ||
|
||
After do | ||
travel_back | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,9 @@ | ||
# coding: UTF-8 | ||
|
||
|
||
# Steps to travel through time using [Timecop](https://github.com/jtrupiano/timecop). | ||
# | ||
# See [this article](https://makandracards.com/makandra/1222-useful-cucumber-steps-to-travel-through-time-with-timecop) for details. | ||
# nodoc | ||
# FILE_COMMENT_END | ||
|
||
warn <<-WARNING | ||
Warning: The file spreewald/timecop_steps.rb is deprecated. It was moved to | ||
spreewald/time_steps.rb. Please require the new file instead. | ||
WARNING | ||
|
||
if defined?(Timecop) | ||
|
||
module TimecopHarness | ||
|
||
# When you have to make your rails app time zone aware you have to go 100% | ||
# otherwise you are better off ignoring time zones at all. | ||
# https://makandracards.com/makandra/8723-guide-to-localizing-a-rails-application | ||
|
||
def use_timezones? | ||
active_record_loaded = defined?(ActiveRecord::Base) | ||
(!active_record_loaded || ActiveRecord::Base.default_timezone != :local) && Time.zone | ||
end | ||
|
||
def parse_time(str) | ||
if use_timezones? | ||
Time.zone.parse(str) | ||
else | ||
Time.parse(str) | ||
end | ||
end | ||
|
||
def current_time | ||
if use_timezones? | ||
Time.current | ||
else | ||
Time.now | ||
end | ||
end | ||
|
||
end | ||
|
||
World(TimecopHarness) | ||
|
||
# Example: | ||
# | ||
# Given the date is 2012-02-10 | ||
# Given the time is 2012-02-10 13:40 | ||
When /^the (?:date|time) is "?(\d{4}-\d{2}-\d{2}(?: \d{1,2}:\d{2})?)"?$/ do |time| | ||
Timecop.travel(parse_time(time)) | ||
end.overridable | ||
|
||
# Example: | ||
# | ||
# Given the time is 13:40 | ||
When /^the time is "?(\d{1,2}:\d{2})"?$/ do |time_without_date| | ||
Timecop.travel(parse_time(time_without_date)) # date will be today | ||
end.overridable | ||
|
||
# Example: | ||
# | ||
# When it is 10 minutes later | ||
# When it is a few hours earlier | ||
When /^it is (\d+|a|some|a few) (seconds?|minutes?|hours?|days?|weeks?|months?|years?) (later|earlier)$/ do |amount, unit, direction| | ||
amount = case amount | ||
when 'a' | ||
1 | ||
when 'some', 'a few' | ||
10 | ||
else | ||
amount.to_i | ||
end | ||
amount = -amount if direction == 'earlier' | ||
Timecop.travel(current_time + amount.send(unit)) | ||
end.overridable | ||
|
||
After do | ||
Timecop.return | ||
end | ||
|
||
end | ||
require 'spreewald/time_steps' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../shared/features/shared/time_steps.feature |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2.7.1 | ||
2.6.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../shared/features/shared/time_steps.feature |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
= "The current date is #{Time.current.strftime("%Y-%m-%d")}." | ||
= "The current time is #{Time.current.strftime("%H:%M")}." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.