Class: Deep::Matchers::SameArrayValues

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.

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 Deep::Matchers::HashMatchers

Instance Method Details

- (Object) description



40
41
42
# File 'parser/spec/lib/deep/array_values.rb', line 40

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

- (Object) failure_message_for_should



68
69
70
# File 'parser/spec/lib/deep/array_values.rb', line 68

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

- (Object) failure_message_for_should_not



72
73
74
# File 'parser/spec/lib/deep/array_values.rb', line 72

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

- (Boolean) matches?(target)

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 'parser/spec/lib/deep/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