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

Inherits:
BaseMatcher
  • Object
show all
Includes:
Matchers::Composable
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



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

def initialize(expected)
  super()

  @expected = expected
end

Instance Method Details

#descriptionObject

Since:

  • 2.5.0



23
24
25
# File 'lib/rspec/sleeping_king_studios/matchers/core/deep_matcher.rb', line 23

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rspec/sleeping_king_studios/matchers/core/deep_matcher.rb', line 35

def does_not_match?(actual) # rubocop:disable Metrics/MethodLength, Naming/PredicatePrefix
  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



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

def failure_message # rubocop:disable Style/TrivialAccessors
  @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



57
58
59
# File 'lib/rspec/sleeping_king_studios/matchers/core/deep_matcher.rb', line 57

def failure_message_when_negated # rubocop:disable Style/TrivialAccessors
  @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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rspec/sleeping_king_studios/matchers/core/deep_matcher.rb', line 76

def matches?(actual) # rubocop:disable Metrics/MethodLength
  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