1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
require 'mocha/configuration'
require 'mocha/deprecation'
require 'mocha/parameter_matchers/base'
module Mocha
module ParameterMatchers
# @private
class PositionalOrKeywordHash < Base
def initialize(value, expectation)
@value = value
@expectation = expectation
end
def matches?(available_parameters)
parameter, is_last_parameter = extract_parameter(available_parameters)
return false unless HasEntries.new(@value).matches?([parameter])
if is_last_parameter && !same_type_of_hash?(parameter, @value)
return false if Mocha.configuration.strict_keyword_argument_matching?
deprecation_warning(parameter, @value) if Mocha::RUBY_V27_PLUS
end
true
end
def mocha_inspect
@value.mocha_inspect
end
private
def extract_parameter(available_parameters)
[available_parameters.shift, available_parameters.empty?]
end
def same_type_of_hash?(actual, expected)
ruby2_keywords_hash?(actual) == ruby2_keywords_hash?(expected)
end
def deprecation_warning(actual, expected)
details1 = "Expectation #{expectation_definition} expected #{hash_type(expected)} (#{expected.mocha_inspect}),".squeeze(' ')
details2 = "but received #{hash_type(actual)} (#{actual.mocha_inspect})."
sentence1 = 'These will stop matching when strict keyword argument matching is enabled.'
sentence2 = 'See the documentation for Mocha::Configuration#strict_keyword_argument_matching=.'
Deprecation.warning([details1, details2, sentence1, sentence2].join(' '))
end
def hash_type(hash)
ruby2_keywords_hash?(hash) ? 'keyword arguments' : 'positional hash'
end
def ruby2_keywords_hash?(hash)
hash.is_a?(Hash) && ::Hash.ruby2_keywords_hash?(hash)
end
def expectation_definition
return nil unless @expectation
"defined at #{@expectation.definition_location}"
end
end
end
end
|