Class: Deep::Matchers::DeepEql

Inherits:
Object
  • Object
show all
Defined in:
lib/deep/matchers/deep_eql.rb

Instance Method Summary collapse

Constructor Details

#initialize(expectation) ⇒ DeepEql

Returns a new instance of DeepEql.



10
11
12
# File 'lib/deep/matchers/deep_eql.rb', line 10

def initialize(expectation)
  @expectation = expectation
end

Instance Method Details

#failure_message_for_shouldObject



35
36
37
# File 'lib/deep/matchers/deep_eql.rb', line 35

def failure_message_for_should
  "expected #{@target.inspect} to be deep_eql with #{@expectation.inspect}"
end

#failure_message_for_should_notObject



39
40
41
# File 'lib/deep/matchers/deep_eql.rb', line 39

def failure_message_for_should_not
  "expected #{@target.inspect} not to be in deep_eql with #{@expectation.inspect}"
end

#matches?(target) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/deep/matchers/deep_eql.rb', line 14

def matches?(target)
  result = true
  @target = target
  case @expectation
  when Hash
    result &&= @target.is_a?(Hash) && @target.keys.count == @expectation.keys.count
    @expectation.keys.each do |key|
      result &&= @target.has_key?(key) &&
      DeepEql.new(@expectation[key]).matches?(@target[key])
    end
  when Array
    result &&= @target.is_a?(Array) && @target.count == @expectation.count
    @expectation.each_index do |index|
      result &&= DeepEql.new(@expectation[index]).matches?(@target[index])
    end
  else
    result &&= @target == @expectation
  end
  result
end