Class: Deep::Matchers::ArrayValues

Inherits:
HashMatchers show all
Defined in:
parser/spec/lib/deep/array_values.rb

Overview

RSpec::Matchers class for testing hierarchically arrays, that are elements of deep hash.

ArrayValues class ensures that all values of the tested Array belong to expected classes.

Instance Method Summary (collapse)

Methods inherited from HashMatchers

#initialize

Constructor Details

This class inherits a constructor from Deep::Matchers::HashMatchers

Instance Method Details

- (Object) description



86
87
88
# File 'parser/spec/lib/deep/array_values.rb', line 86

def description 
  "have every value one of class: #{@expectation.join ','}"
end

- (Object) failure_message_for_should



114
115
116
# File 'parser/spec/lib/deep/array_values.rb', line 114

def failure_message_for_should
  "expected #{@target.inspect} to have values of classes #{@expectation.inspect}"
end

- (Object) failure_message_for_should_not



118
119
120
# File 'parser/spec/lib/deep/array_values.rb', line 118

def failure_message_for_should_not
  "expected #{@target.inspect} differ from  #{@expectation.inspect}"
end

- (Boolean) matches?(target)

Test that all values of the tested Array belong to expecetd class(es).

Parameters:

  • target (Array)

    array to test

Returns:

  • (Boolean)

    true if pass, false if fail



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'parser/spec/lib/deep/array_values.rb', line 96

def matches?(target)
  result = true
  @target, @actual = target, target.map(&:class).uniq

  case @target
  when Array
    result &&= (@actual - @expectation).empty?
  when Hash
    @target.each_value do |val|
      result &&= ArrayValues.new(@expectation).matches?(val) if [Hash, Array].include? val.class
      @target = val unless result
    end
  end

  result
end