Class: ProtoPharm::Matchers::HashArgumentMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/proto_pharm/matchers/hash_argument_matcher.rb

Overview

Direct Known Subclasses

RequestIncludingMatcher

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ HashArgumentMatcher

Returns a new instance of HashArgumentMatcher.



26
27
28
29
30
# File 'lib/proto_pharm/matchers/hash_argument_matcher.rb', line 26

def initialize(expected)
  @expected = Hash[
    ProtoPharm::Matchers::HashArgumentMatcher.stringify_keys!(expected, deep: true).sort,
  ]
end

Class Method Details

.from_rspec_matcher(matcher) ⇒ Object



38
39
40
# File 'lib/proto_pharm/matchers/hash_argument_matcher.rb', line 38

def self.from_rspec_matcher(matcher)
  new(matcher.instance_variable_get(:@expected))
end

.stringify_keys!(arg, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/proto_pharm/matchers/hash_argument_matcher.rb', line 8

def self.stringify_keys!(arg, options = {})
  case arg
  when Array
    arg.map do |elem|
      options[:deep] ? stringify_keys!(elem, options) : elem
    end
  when Hash
    Hash[
      *arg.map do |key, value|
        k = key.is_a?(Symbol) ? key.to_s : key
        v = (options[:deep] ? stringify_keys!(value, options) : value)
        [k, v]
      end.inject([]) { |r, x| r + x }]
  else
    arg
  end
end

Instance Method Details

#==(_actual, &block) ⇒ Object



32
33
34
35
36
# File 'lib/proto_pharm/matchers/hash_argument_matcher.rb', line 32

def ==(_actual, &block)
  @expected.all?(&block)
rescue NoMethodError
  false
end