Class: NormalizedHash::Matchers::HashKeys
- Inherits:
-
HashMatchers
- Object
- HashMatchers
- NormalizedHash::Matchers::HashKeys
- Defined in:
- lib/normalized_hash/hash_keys.rb
Overview
RSpec::Matchers class for testing hierarchically hashes, that are elements of deep hash.
HashKeys class tests that all keys of the Hash belong to list of Classes.
Usage
it { @hash.should have_keys_in_class [String, Symbol] }
it { @hash.should have_keys_in_class String }
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message_for_should ⇒ Object
- #failure_message_for_should_not ⇒ Object
-
#matches?(target) ⇒ Boolean
Test that all keys of the tested Hash belong to provided list of classes.
Methods inherited from HashMatchers
Constructor Details
This class inherits a constructor from NormalizedHash::Matchers::HashMatchers
Instance Method Details
#description ⇒ Object
29 30 31 |
# File 'lib/normalized_hash/hash_keys.rb', line 29 def description "have (recursively) every key one of class: #{[@expectation].flatten.join ','}" end |
#failure_message_for_should ⇒ Object
58 59 60 |
# File 'lib/normalized_hash/hash_keys.rb', line 58 def "expected #{@target.keys.inspect} #{@actual} to be one of #{@expectation.inspect}" end |
#failure_message_for_should_not ⇒ Object
62 63 64 |
# File 'lib/normalized_hash/hash_keys.rb', line 62 def "expected #{@target.keys.inspect} differ from #{@expectation.inspect}" end |
#matches?(target) ⇒ Boolean
Test that all keys of the tested Hash belong to provided list of classes
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/normalized_hash/hash_keys.rb', line 39 def matches?(target) result = true if target.is_a? Hash @target, @actual = target, target.keys.map(&:class).uniq result &&= (@actual - @expectation).empty? @target.each_value do |val| result &&= HashKeys.new(@expectation).matches?(val) if val.is_a? Hash @target = val unless result end end result end |