Skip to content

Commit

Permalink
move equal matcher to class
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Oct 19, 2011
1 parent d9e6258 commit 6ef7b16
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
12 changes: 12 additions & 0 deletions lib/rspec/matchers.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ def eql(expected)
Eql.new(expected) Eql.new(expected)
end 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: # :call-seq:
# should include(expected) # should include(expected)
# should_not include(expected) # should_not include(expected)
Expand Down
1 change: 0 additions & 1 deletion lib/rspec/matchers/eql.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ def description
"eql #{@expected.inspect}" "eql #{@expected.inspect}"
end end
end end

end end
end end
56 changes: 29 additions & 27 deletions lib/rspec/matchers/equal.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,27 +1,16 @@
module RSpec module RSpec
module Matchers module Matchers
# Passes if <tt>actual.equal?(expected)</tt> (object identity). class Equal < BaseMatcher
# attr_reader :actual
# See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby. def matches?(actual)
# @actual = actual
# == Examples @actual.equal?(@expected)
# end
# 5.should equal(5) # Fixnums are equal
# "5".should_not equal("5") # Strings that look the same are not the same object def failure_message_for_should
def equal(expected) return <<-MESSAGE
Matcher.new :equal, expected do |_expected_|
match do |actual| expected #{inspect_object(expected)}
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_)}
got #{inspect_object(actual)} got #{inspect_object(actual)}
Compared using equal?, which compares object identity, Compared using equal?, which compares object identity,
Expand All @@ -30,18 +19,31 @@ def inspect_object(o)
object identity in this example. object identity in this example.
MESSAGE MESSAGE
end end


failure_message_for_should_not do |actual| def failure_message_for_should_not
<<-MESSAGE return <<-MESSAGE
expected not #{inspect_object(actual)} expected not #{inspect_object(actual)}
got #{inspect_object(_expected_)} got #{inspect_object(expected)}
Compared using equal?, which compares object identity. Compared using equal?, which compares object identity.
MESSAGE 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 end
end end
Expand Down

0 comments on commit 6ef7b16

Please sign in to comment.