Class: NormalizedHash::Matchers::HashValues
- Inherits:
-
HashMatchers
- Object
- HashMatchers
- NormalizedHash::Matchers::HashValues
- Defined in:
- lib/normalized_hash/hash_values.rb
Overview
RSpec::Matchers class for testing hierarchically hashes, that are elements of deep hash.
HashValues class tests that all values of the Hash belong to list of Classes.
Usage
it { @hash.should have_values_in_class [String, Symbol] }
it { @hash.should have_values_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 values 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
31 32 33 |
# File 'lib/normalized_hash/hash_values.rb', line 31 def description "have (recursively) every value one of class: #{[@expectation].flatten.join ','}" end |
#failure_message_for_should ⇒ Object
59 60 61 |
# File 'lib/normalized_hash/hash_values.rb', line 59 def "expected #{@target.values.inspect} to have values of classes #{@expectation.inspect}" end |
#failure_message_for_should_not ⇒ Object
63 64 65 |
# File 'lib/normalized_hash/hash_values.rb', line 63 def "expected #{@target.values.inspect} differ from #{@expectation.inspect}" end |
#matches?(target) ⇒ Boolean
Test that all values of the tested Hash belong to provided list of classes
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/normalized_hash/hash_values.rb', line 41 def matches?(target) result = true if target.is_a? Hash @target, @actual = target, target.values.map(&:class).uniq result &&= (@actual - @expectation).empty? @target.each_value do |val| result &&= HashValues.new(@expectation).matches?(val) if val.is_a?(Hash) @target = val unless result end end result end |