From 5e7ca88e72e260a95d041456d45cda6b94e9db20 Mon Sep 17 00:00:00 2001 From: Andrius Chamentauskas Date: Sat, 25 Aug 2012 14:09:24 +0300 Subject: [PATCH] Time =~ matcher now works with ruby 1.8.7, jruby and rubinius, sadly no longer supports Date and DateTime objects --- features/built_in_matchers/operators.feature | 4 +-- lib/rspec/matchers.rb | 4 +-- lib/rspec/matchers/built_in/match_time.rb | 11 ++++--- spec/rspec/matchers/match_time_spec.rb | 30 ++++---------------- 4 files changed, 13 insertions(+), 36 deletions(-) diff --git a/features/built_in_matchers/operators.feature b/features/built_in_matchers/operators.feature index 83805d0cd..e9f8c8be3 100644 --- a/features/built_in_matchers/operators.feature +++ b/features/built_in_matchers/operators.feature @@ -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 """ diff --git a/lib/rspec/matchers.rb b/lib/rspec/matchers.rb index 847c523a8..7e9cc6470 100644 --- a/lib/rspec/matchers.rb +++ b/lib/rspec/matchers.rb @@ -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 diff --git a/lib/rspec/matchers/built_in/match_time.rb b/lib/rspec/matchers/built_in/match_time.rb index bc973c6ad..2b2dc5f65 100644 --- a/lib/rspec/matchers/built_in/match_time.rb +++ b/lib/rspec/matchers/built_in/match_time.rb @@ -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 diff --git a/spec/rspec/matchers/match_time_spec.rb b/spec/rspec/matchers/match_time_spec.rb index 544c7f9c3..e276979c5 100644 --- a/spec/rspec/matchers/match_time_spec.rb +++ b/spec/rspec/matchers/match_time_spec.rb @@ -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