Module: RSpec::GraphqlIntegration::Matchers::DeepEq

Extended by:
Matchers::DSL
Defined in:
lib/rspec/graphql_integration/matchers/deep_eq.rb

Overview

This matcher recursively compares nested Ruby Hashes and Arrays while ignoring the order of elements in Arrays.

Instance Method Summary collapse

Instance Method Details

#arrays_deep_eq?(actual, expected) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rspec/graphql_integration/matchers/deep_eq.rb', line 20

def arrays_deep_eq?(actual, expected)
  expected = expected.clone

  actual.each do |array|
    index = expected.find_index { |element| deep_eq?(array, element) }
    return false if index.nil?

    expected.delete_at(index)
  end

  expected.empty?
end

#deep_eq?(actual, expected) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/rspec/graphql_integration/matchers/deep_eq.rb', line 12

def deep_eq?(actual, expected)
  return arrays_deep_eq?(actual, expected) if expected.is_a?(Array) && actual.is_a?(Array)

  return hashes_deep_eq?(actual, expected) if expected.is_a?(Hash) && actual.is_a?(Hash)

  expected == actual
end

#hashes_deep_eq?(actual, expected) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
# File 'lib/rspec/graphql_integration/matchers/deep_eq.rb', line 33

def hashes_deep_eq?(actual, expected)
  return false if actual.keys.sort != expected.keys.sort

  # TODO: use any or all
  actual.each { |key, value| return false unless deep_eq?(value, expected[key]) }

  true
end