Permalink
Browse files

Time =~ matcher now works with ruby 1.8.7, jruby and rubinius, sadly …

…no longer supports Date and DateTime objects
  • Loading branch information...
1 parent 9fa2bf5 commit 5e7ca88e72e260a95d041456d45cda6b94e9db20 @andriusch andriusch committed Aug 25, 2012
@@ -238,10 +238,10 @@ Feature: operator matchers
And the output should contain:
"""
Failure/Error: it { should_not =~ subject + 0.999 }
- expected 2012-08-25 10:37:42 UTC not to be same time as 2012-08-25 10:37:42 UTC but it was
+ expected 2012-08-25 10:37:42 +0000 not to be same time as 2012-08-25 10:37:42 +0000 but it was
"""
And the output should contain:
"""
Failure/Error: it { should =~ subject + 1 }
- expected 2012-08-25 10:37:42 UTC to be same time as 2012-08-25 10:37:43 UTC but it wasn't
+ expected 2012-08-25 10:37:42 +0000 to be same time as 2012-08-25 10:37:43 +0000 but it wasn't
"""
View
@@ -699,9 +699,7 @@ def match_time(time)
BuiltIn::MatchTime.new(time)
end
- OperatorMatcher.register(Time, '=~', BuiltIn::MatchTime) if defined?(Time)
- OperatorMatcher.register(Date, '=~', BuiltIn::MatchTime) if defined?(Date)
- OperatorMatcher.register(DateTime, '=~', BuiltIn::MatchTime) if defined?(DateTime)
+ OperatorMatcher.register(Time, '=~', BuiltIn::MatchTime)
OperatorMatcher.register(ActiveSupport::TimeWithZone, '=~', BuiltIn::MatchTime) if defined?(ActiveSupport::TimeWithZone)
end
end
@@ -3,15 +3,15 @@ module Matchers
module BuiltIn
class MatchTime < BaseMatcher
def match(expected, actual)
- (seconds_of(expected) - seconds_of(actual)).abs < 1
+ (expected - actual).abs < 1
end
def failure_message_for_should
- "expected #{actual} to be same time as #{expected} but it wasn't"
+ "expected #{format_time(actual)} to be same time as #{format_time(expected)} but it wasn't"
end
def failure_message_for_should_not
- "expected #{actual} not to be same time as #{expected} but it was"
+ "expected #{format_time(actual)} not to be same time as #{format_time(expected)} but it was"
end
def description
@@ -20,9 +20,8 @@ def description
private
- def seconds_of(time)
- time = time.to_time if time.respond_to?(:to_time)
- time.to_f
+ def format_time(time)
+ time.strftime("%Y-%m-%d %H:%M:%S %z")
end
end
end
@@ -8,15 +8,7 @@
describe "time.should =~ other_time" do
let :time do
- Time.now.to_date.to_time
- end
-
- let :date do
- time.to_date
- end
-
- let :datetime do
- time.to_datetime
+ Time.utc(2012, 8, 25)
end
it "passes if actual value differs from expected by less than 1 second" do
@@ -26,33 +18,21 @@
it "fails if actual value is lower than expected by at least 1 second" do
expect {
time.should =~ time + 1
- }.to fail_with("expected #{time} to be same time as #{time + 1} but it wasn't")
+ }.to fail_with("expected 2012-08-25 00:00:00 +0000 to be same time as 2012-08-25 00:00:01 +0000 but it wasn't")
end
it "fails if actual value is higher than expected by at least 1 second" do
expect {
time.should =~ time - 1
- }.to fail_with("expected #{time} to be same time as #{time - 1} but it wasn't")
- end
-
- it "can be called on Date objects" do
- date.should =~ time - 0.999
- end
-
- it "can be called on DateTime objects" do
- datetime.should =~ time - 0.999
- end
-
- it "can be compared to Date objects" do
- time.should =~ date
+ }.to fail_with("expected 2012-08-25 00:00:00 +0000 to be same time as 2012-08-24 23:59:59 +0000 but it wasn't")
end
end
describe "time.should_not =~ other_time" do
it "fails if actual value differs from expected by less than 1 second" do
- time = Time.mktime(2012, 8, 25)
+ time = Time.utc(2012, 8, 25)
expect {
time.should_not =~ time + 0.999
- }.to fail_with("expected #{time} not to be same time as #{time} but it was")
+ }.to fail_with("expected 2012-08-25 00:00:00 +0000 not to be same time as 2012-08-25 00:00:00 +0000 but it was")
end
end

0 comments on commit 5e7ca88

Please sign in to comment.