Permalink
Browse files

move equal matcher to class

  • Loading branch information...
1 parent d9e6258 commit 6ef7b166e74ccb508ec0cc610ea9255235888e3c @dchelimsky dchelimsky committed Oct 19, 2011
Showing with 41 additions and 28 deletions.
  1. +12 −0 lib/rspec/matchers.rb
  2. +0 −1 lib/rspec/matchers/eql.rb
  3. +29 −27 lib/rspec/matchers/equal.rb
View
@@ -196,6 +196,18 @@ def eql(expected)
Eql.new(expected)
end
+ # Passes if <tt>actual.equal?(expected)</tt> (object identity).
+ #
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
+ #
+ # == Examples
+ #
+ # 5.should equal(5) # Fixnums are equal
+ # "5".should_not equal("5") # Strings that look the same are not the same object
+ def equal(expected)
+ Equal.new(expected)
+ end
+
# :call-seq:
# should include(expected)
# should_not include(expected)
@@ -23,6 +23,5 @@ def description
"eql #{@expected.inspect}"
end
end
-
end
end
@@ -1,27 +1,16 @@
module RSpec
module Matchers
- # Passes if <tt>actual.equal?(expected)</tt> (object identity).
- #
- # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
- #
- # == Examples
- #
- # 5.should equal(5) # Fixnums are equal
- # "5".should_not equal("5") # Strings that look the same are not the same object
- def equal(expected)
- Matcher.new :equal, expected do |_expected_|
- match do |actual|
- actual.equal?(_expected_)
- end
-
- def inspect_object(o)
- "#<#{o.class}:#{o.object_id}> => #{o.inspect}"
- end
-
- failure_message_for_should do |actual|
- <<-MESSAGE
-
-expected #{inspect_object(_expected_)}
+ class Equal < BaseMatcher
+ attr_reader :actual
+ def matches?(actual)
+ @actual = actual
+ @actual.equal?(@expected)
+ end
+
+ def failure_message_for_should
+ return <<-MESSAGE
+
+expected #{inspect_object(expected)}
got #{inspect_object(actual)}
Compared using equal?, which compares object identity,
@@ -30,18 +19,31 @@ def inspect_object(o)
object identity in this example.
MESSAGE
- end
+ end
- failure_message_for_should_not do |actual|
- <<-MESSAGE
+ def failure_message_for_should_not
+ return <<-MESSAGE
expected not #{inspect_object(actual)}
- got #{inspect_object(_expected_)}
+ got #{inspect_object(expected)}
Compared using equal?, which compares object identity.
MESSAGE
- end
+ end
+
+ def diffable?
+ true
+ end
+
+ def description
+ "equal #{@expected.inspect}"
+ end
+
+ private
+
+ def inspect_object(o)
+ "#<#{o.class}:#{o.object_id}> => #{o.inspect}"
end
end
end

0 comments on commit 6ef7b16

Please sign in to comment.