Class: RSpec::SleepingKingStudios::Matchers::Core::DeepMatcher

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/rspec/sleeping_king_studios/matchers/core/deep_matcher.rb

Overview

Matcher for performing a deep comparison between two objects.

Since:

  • 2.5.0

Constant Summary

Constants included from Description

Description::DEFAULT_EXPECTED_ITEMS

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ DeepMatcher

Returns a new instance of DeepMatcher.

Parameters:

  • expected (Object)

    The expected object.

Since:

  • 2.5.0



14
15
16
# File 'lib/rspec/sleeping_king_studios/matchers/core/deep_matcher.rb', line 14

def initialize(expected)
  @expected = expected
end

Instance Method Details

#descriptionObject

Since:

  • 2.5.0



19
20
21
# File 'lib/rspec/sleeping_king_studios/matchers/core/deep_matcher.rb', line 19

def description
  "match #{format_expected(@expected)}"
end

#does_not_match?(actual) ⇒ Boolean

Inverse of #matches? method.

Parameters:

  • actual (Object)

    The object to check.

Returns:

  • (Boolean)

    true if the actual object does not match the expectation, otherwise true.

See Also:

Since:

  • 2.5.0



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rspec/sleeping_king_studios/matchers/core/deep_matcher.rb', line 31

def does_not_match? actual
  super

  if matcher?(@expected)
    delegate_to_negated_matcher(@expected)
  elsif @expected.is_a?(Array) && actual.is_a?(Array)
    diff_arrays_negated
  elsif @expected.is_a?(Hash) && actual.is_a?(Hash)
    diff_hashes_negated
  else
    delegate_to_negated_matcher(equality_matcher)
  end

  !@matches
end

#failure_messageObject

Message for when the object does not match, but was expected to. Make sure to always call #matches? first to set up the matcher state.

Since:

  • 2.5.0



48
49
50
# File 'lib/rspec/sleeping_king_studios/matchers/core/deep_matcher.rb', line 48

def failure_message
  @failure_message
end

#failure_message_when_negatedObject

Message for when the object matches, but was expected not to. Make sure to always call #matches? first to set up the matcher state.

Since:

  • 2.5.0



53
54
55
# File 'lib/rspec/sleeping_king_studios/matchers/core/deep_matcher.rb', line 53

def failure_message_when_negated
  @failure_message_when_negated
end

#matches?(actual) ⇒ Boolean

Performs a deep comparison between the actual object and the expected object. The type of comparison depends on the type of the expected object:

  • If the expected object is an RSpec matcher, the #matches? method on the matcher is called with the expected object.

  • If the expected object is an Array, then each item is compared based on the type of the expected item.

  • If the expected object is a Hash, then the keys must match and each value is compared based on the type of the expected value.

  • Otherwise, the two objects are compared using an equality comparison.

Parameters:

  • actual (Object)

    The object to check.

Returns:

  • (Boolean)

    true if the actual object matches the expectation, otherwise false.

Since:

  • 2.5.0



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rspec/sleeping_king_studios/matchers/core/deep_matcher.rb', line 72

def matches?(actual)
  super

  if matcher?(@expected)
    delegate_to_matcher(@expected)
  elsif @expected.is_a?(Array) && actual.is_a?(Array)
    diff_arrays
  elsif @expected.is_a?(Hash)  && actual.is_a?(Hash)
    diff_hashes
  else
    delegate_to_matcher(equality_matcher)
  end

  @matches
end