class Mocha::ParameterMatchers::HasEntry

Parameter matcher which matches when actual parameter contains expected Hash entry.

Public Class Methods

new(key, value) click to toggle source

@private

# File lib/mocha/parameter_matchers/has_entry.rb, line 67
def initialize(key, value)
  @key = key
  @value = value
end
parse_option(option) click to toggle source

@private

# File lib/mocha/parameter_matchers/has_entry.rb, line 86
def self.parse_option(option)
  case option
  when Hash
    case option.length
    when 0
      raise ArgumentError, 'Argument has no entries.'
    when 1
      option.first
    else
      raise ArgumentError, 'Argument has multiple entries. Use Mocha::ParameterMatchers#has_entries instead.'
    end
  else
    raise ArgumentError, 'Argument is not a Hash.'
  end
end

Public Instance Methods

matches?(available_parameters) click to toggle source

@private

# File lib/mocha/parameter_matchers/has_entry.rb, line 73
def matches?(available_parameters)
  parameter = available_parameters.shift
  return false unless parameter.respond_to?(:keys) && parameter.respond_to?(:[])
  matching_keys = parameter.keys.select { |key| @key.to_matcher.matches?([key]) }
  matching_keys.any? { |key| @value.to_matcher.matches?([parameter[key]]) }
end
mocha_inspect() click to toggle source

@private

# File lib/mocha/parameter_matchers/has_entry.rb, line 81
def mocha_inspect
  "has_entry(#{@key.mocha_inspect} => #{@value.mocha_inspect})"
end