Class: Deep::Matchers::KeyNamesOfEnclosedHash

Inherits:
KeyNameMatchers show all
Defined in:
parser/spec/lib/deep/hash_enclosed_in_array.rb

Overview

RSpec::Matchers class for testing hashes enclosed in an array.

Array of Hashes: each Hash should have key :name or :<collection.singularize>

Hash here (subject) is an instance of Hash, which is an element of an Array, and Array itself is a value of a Hash:

{ :foos => 
      [                               <--- subject
            { :foo (or :name) => ...,
              :bar => ...,
            },
            { :foo (or :name) => ...,
              :bar => ...,
            },
           ]
}

Instance Method Summary (collapse)

Methods inherited from KeyNameMatchers

#initialize

Constructor Details

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

Instance Method Details

- (Object) description



50
51
52
# File 'parser/spec/lib/deep/hash_enclosed_in_array.rb', line 50

def description 
  "have key :name or '#{@expectation.singularize}' in every enclosed Hash "
end

- (Object) failure_message_for_should



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

def failure_message_for_should
  "expected each of #{@target.map(&:keys)} include '#{@expectation.singularize}'"
end

- (Object) failure_message_for_should_not



76
77
78
# File 'parser/spec/lib/deep/hash_enclosed_in_array.rb', line 76

def failure_message_for_should_not
  "expected #{@target.map(&:keys)} not include '#{@expectation.singularize}'"
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



59
60
61
62
63
64
65
66
67
68
69
70
# File 'parser/spec/lib/deep/hash_enclosed_in_array.rb', line 59

def matches? target
  @target = target 
  result = true

  target.each do |hash|
    next unless hash.is_a? Hash
    keys = hash.keys.map(&:to_s).map(&:downcase)
    result &&= false unless (keys.include? @expectation || keys.include?("name"))
  end

  result
end