class HashMap::Matchers::HashMappedMatcher

Attributes

description_messages[R]
expected[R]
expected_provided[R]
failure_messages[R]
from_key[R]
key[R]
mapped_has_key[R]
mapped_hash[R]
original_hash[R]

Public Class Methods

new(key) click to toggle source
# File lib/hash_map/matchers.rb, line 11
def initialize(key)
  @key = key
  @error = []
  @description_messages = []
  @failure_messages = []
end

Public Instance Methods

and_eq(expected) click to toggle source
# File lib/hash_map/matchers.rb, line 37
def and_eq(expected)
  @expected_provided = true
  @expected = expected
  self
end
description() click to toggle source
# File lib/hash_map/matchers.rb, line 23
def description
  description_messages.join(', ')
end
failure_message() click to toggle source
# File lib/hash_map/matchers.rb, line 27
def failure_message
  failure_messages.join(', ')
end
failure_message_when_negated() click to toggle source
# File lib/hash_map/matchers.rb, line 43
def failure_message_when_negated
  "expect to not #{description}"
end
from(original_hash, *from_key) click to toggle source
# File lib/hash_map/matchers.rb, line 31
def from(original_hash, *from_key)
  @original_hash = original_hash
  @from_key = from_key
  self
end
matches?(hash) click to toggle source
# File lib/hash_map/matchers.rb, line 18
def matches?(hash)
  @mapped_hash = hash
  _has_key && _from && equality
end

Private Instance Methods

_from() click to toggle source
# File lib/hash_map/matchers.rb, line 82
def _from
  return true unless original_hash
  if original_has_key?
    if original_value == mapped_value
      description_messages << "#{key_to_message} and original #{from_key_to_message} are the same"
    else
      failure_messages << "#{key_to_message} and original #{from_key_to_message} are NOT the same"
      false
    end
  else
    failure_messages << "original has no key: #{from_key_to_message}"
    false
  end
end
_has_key() click to toggle source
# File lib/hash_map/matchers.rb, line 73
def _has_key
  if mapped_has_key?
    description_messages << "have key #{key_to_message} after been mapped"
  else
    failure_messages << "has no key #{key_to_message} after been mapped"
    false
  end
end
_nested_keys(hash, keys) click to toggle source
# File lib/hash_map/matchers.rb, line 132
def _nested_keys(hash, keys)
  keys.inject({ value: hash, has_key: true }) do |out, key|
    return { value: nil, has_key: false } unless out[:value]
    return { value: nil, has_key: false } unless out[:has_key]
    { value: out[:value][key], has_key: out[:value].key?(key) }
  end
end
equality() click to toggle source
# File lib/hash_map/matchers.rb, line 114
def equality
  return true unless expected_provided
  if matches_equality?(mapped_value, expected)
    description_messages << "and eq `#{expected}`"
  else
    failure_messages << "key #{key_to_message} expected to eq `#{expected}`"
    false
  end
end
from_key_to_message() click to toggle source
# File lib/hash_map/matchers.rb, line 97
def from_key_to_message
  keys_to_message from_key
end
key_representation(k) click to toggle source
# File lib/hash_map/matchers.rb, line 110
def key_representation(k)
  k.is_a?(Symbol) ? ":#{k}" : "'#{k}'"
end
key_to_message() click to toggle source
# File lib/hash_map/matchers.rb, line 101
def key_to_message
  keys_to_message key
end
keys_to_message(keys) click to toggle source
# File lib/hash_map/matchers.rb, line 105
def keys_to_message(keys)
  children = keys.map { |k| key_representation(k) }.join(' -> ')
  "`#{children}`"
end
mapped_has_key?() click to toggle source
# File lib/hash_map/matchers.rb, line 49
def mapped_has_key?
  mapped_results[:has_key]
end
mapped_results() click to toggle source
# File lib/hash_map/matchers.rb, line 65
def mapped_results
  @mapped_results ||= _nested_keys(mapped_hash, key)
end
mapped_value() click to toggle source
# File lib/hash_map/matchers.rb, line 53
def mapped_value
  mapped_results[:value]
end
matches_equality?(value, expected) click to toggle source
# File lib/hash_map/matchers.rb, line 124
def matches_equality?(value, expected)
  if expected.is_a?(Proc)
    expected.call(value)
  else
    value == expected
  end
end
original_has_key?() click to toggle source
# File lib/hash_map/matchers.rb, line 57
def original_has_key?
  original_results[:has_key]
end
original_results() click to toggle source
# File lib/hash_map/matchers.rb, line 69
def original_results
  @original_results ||= _nested_keys(original_hash, from_key)
end
original_value() click to toggle source
# File lib/hash_map/matchers.rb, line 61
def original_value
  original_results[:value]
end