Class: Minicrest::Is
Overview
Matcher that checks for reference equality (same object identity).
Uses Ruby’s equal? method which checks if two references point to the exact same object in memory.
Instance Method Summary collapse
-
#description ⇒ String
Returns a description of what this matcher expects.
-
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
-
#initialize(expected) ⇒ Is
constructor
Creates a new reference equality matcher.
-
#matches?(actual) ⇒ Boolean
Checks if actual is the same object as expected, or matches if expected is a matcher.
-
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
Methods inherited from Matcher
Constructor Details
#initialize(expected) ⇒ Is
Creates a new reference equality matcher.
25 26 27 28 |
# File 'lib/minicrest/is.rb', line 25 def initialize(expected) super() @expected = expected end |
Instance Method Details
#description ⇒ String
Returns a description of what this matcher expects.
45 46 47 48 49 50 51 |
# File 'lib/minicrest/is.rb', line 45 def description if @expected.is_a?(Matcher) @expected.description else "the same object as #{@expected.inspect} (object_id: #{@expected.object_id})" end end |
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
57 58 59 60 61 62 63 64 65 |
# File 'lib/minicrest/is.rb', line 57 def (actual) if @expected.is_a?(Matcher) @expected.(actual) else <<~MSG.chomp expected #{actual.inspect} (object_id: #{actual.object_id}) to be the same object as #{@expected.inspect} (object_id: #{@expected.object_id}) MSG end end |
#matches?(actual) ⇒ Boolean
Checks if actual is the same object as expected, or matches if expected is a matcher.
34 35 36 37 38 39 40 |
# File 'lib/minicrest/is.rb', line 34 def matches?(actual) if @expected.is_a?(Matcher) @expected.matches?(actual) else actual.equal?(@expected) end end |
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
71 72 73 74 75 76 77 78 79 |
# File 'lib/minicrest/is.rb', line 71 def (actual) if @expected.is_a?(Matcher) @expected.(actual) else <<~MSG.chomp expected #{actual.inspect} (object_id: #{actual.object_id}) not to be the same object as #{@expected.inspect}, but they are the same object MSG end end |