Class: NormalizedHash::Matchers::SameArrayValues

Inherits:
HashMatchers
  • Object
show all
Defined in:
lib/normalized_hash/array_values.rb

Overview

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

SameArrayValues class tests that all values of the Array are on the same class.

Instance Method Summary collapse

Methods inherited from HashMatchers

#initialize

Constructor Details

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

Instance Method Details

#descriptionObject



40
41
42
# File 'lib/normalized_hash/array_values.rb', line 40

def description 
  "have all values of the same class"
end

#failure_message_for_shouldObject



68
69
70
# File 'lib/normalized_hash/array_values.rb', line 68

def failure_message_for_should
  "expected #{@actual.inspect} to be one class"
end

#failure_message_for_should_notObject



72
73
74
# File 'lib/normalized_hash/array_values.rb', line 72

def failure_message_for_should_not
  "expected #{@actual.inspect} be at least 2 different classes"
end

#matches?(target) ⇒ Boolean

Test that all values of the tested Array are of the same class

Parameters:

  • target (Array)

    tested array

Returns:

  • (Boolean)

    true if pass, false if fail



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/normalized_hash/array_values.rb', line 49

def matches? target
  result = true
  case target
  when Array
    @actual = target.map(&:class).uniq
    result = !(@actual.count > 1)
  when Hash

    target.each_value do |val|
      if val.is_a? Array
        result &&= SameArrayValues.new(Object).matches? val
        @actual = val.map(&:class).uniq unless result
      end
    end

  end
  result
end