Merge pull request #1582 from mojombo/seconds_agnostic_time

Get some nice Regexp which is agnostic about the seconds.
This commit is contained in:
Matt Rogers 2013-10-23 22:57:18 -05:00
commit bd825924b2
2 changed files with 26 additions and 1 deletions

View File

@ -181,7 +181,7 @@ Then /^the "(.*)" file should not exist$/ do |file|
end
Then /^I should see today's time in "(.*)"$/ do |file|
assert_match Regexp.new(Regexp.escape(Time.now.to_s)), file_contents(file)
assert_match Regexp.new(seconds_agnostic_time(Time.now)), file_contents(file)
end
Then /^I should see today's date in "(.*)"$/ do |file|

View File

@ -46,5 +46,30 @@ def file_contents(path)
end
end
def seconds_agnostic_datetime(datetime = Time.now)
pieces = datetime.to_s.split(" ")
if pieces.size == 6 # Ruby 1.8.7
date = pieces[0..2].join(" ")
time = seconds_agnostic_time(pieces[3])
zone = pieces[4..5].join(" ")
else # Ruby 1.9.1 or greater
date, time, zone = pieces
time = seconds_agnostic_time(time)
end
[
Regexp.escape(date),
"#{time}:\\d{2}",
Regexp.escape(zone)
].join("\\ ")
end
def seconds_agnostic_time(time)
if time.is_a? Time
time = time.strftime("%H:%M:%S")
end
hour, minutes, _ = time.split(":")
"#{hour}:#{minutes}"
end
# work around "invalid option: --format" cucumber bug (see #296)
Test::Unit.run = true if RUBY_VERSION < '1.9'