Class: Minicrest::Equals

Inherits:
Matcher show all
Defined in:
lib/minicrest/equals.rb

Overview

Matcher that checks for value equality using deep comparison.

Uses Ruby’s == operator which provides value equality semantics. For data structures (Arrays, Hashes), this performs deep comparison by value rather than by reference.

Examples:

Basic usage with primitives

equals(42).matches?(42)     # => true
equals("hello").matches?("hello") # => true

Deep equality with arrays

equals([1, 2, 3]).matches?([1, 2, 3]) # => true
equals([1, [2, 3]]).matches?([1, [2, 3]]) # => true (nested)

Deep equality with hashes

equals({a: 1}).matches?({a: 1}) # => true
equals({a: {b: 2}}).matches?({a: {b: 2}}) # => true (nested)

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Constructor Details

#initialize(expected) ⇒ Equals

Creates a new value equality matcher.

Parameters:

  • expected (Object)

    the expected value



25
26
27
28
# File 'lib/minicrest/equals.rb', line 25

def initialize(expected)
  super()
  @expected = expected
end

Instance Method Details

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    description of expected value



41
42
43
# File 'lib/minicrest/equals.rb', line 41

def description
  "equal to #{@expected.inspect}"
end

#failure_message(actual) ⇒ String

Returns the failure message when the match fails.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    detailed message showing both values



49
50
51
52
53
54
55
56
57
# File 'lib/minicrest/equals.rb', line 49

def failure_message(actual)
  message = "expected #{actual.inspect}\n      " \
            "to equal #{@expected.inspect}"

  diff = compute_diff(actual)
  message += "\n\n#{diff}" if diff

  message
end

#matches?(actual) ⇒ Boolean

Checks if actual equals expected by value.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if actual == expected



34
35
36
# File 'lib/minicrest/equals.rb', line 34

def matches?(actual)
  actual == @expected
end

#negated_failure_message(actual) ⇒ String

Returns the failure message when a negated match fails.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    message indicating unexpected equality



63
64
65
66
# File 'lib/minicrest/equals.rb', line 63

def negated_failure_message(actual)
  "expected #{actual.inspect}\n  " \
    "not to equal #{@expected.inspect}, but they are equal"
end