Class: NormalizedHash::Matchers::ArrayValues

Inherits:
HashMatchers show all
Defined in:
lib/normalized_hash/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 NormalizedHash::Matchers::HashMatchers

Instance Method Details

#descriptionObject



86
87
88
# File 'lib/normalized_hash/array_values.rb', line 86

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

#failure_message_for_shouldObject



114
115
116
# File 'lib/normalized_hash/array_values.rb', line 114

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

#failure_message_for_should_notObject



118
119
120
# File 'lib/normalized_hash/array_values.rb', line 118

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

#matches?(target) ⇒ Boolean

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 'lib/normalized_hash/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