Class: Mocha::ParametersMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/mocha/parameters_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected_parameters = [ParameterMatchers::AnyParameters.new], &matching_block) ⇒ ParametersMatcher

Returns a new instance of ParametersMatcher.



8
9
10
# File 'lib/mocha/parameters_matcher.rb', line 8

def initialize(expected_parameters = [ParameterMatchers::AnyParameters.new], &matching_block)
  @expected_parameters, @matching_block = expected_parameters, matching_block
end

Instance Method Details

#match?(actual_parameters = []) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/mocha/parameters_matcher.rb', line 12

def match?(actual_parameters = [])
  if @matching_block
    return @matching_block.call(*actual_parameters)
  else
    return parameters_match?(actual_parameters)
  end
end

#matchersObject



31
32
33
# File 'lib/mocha/parameters_matcher.rb', line 31

def matchers
  @expected_parameters.map { |parameter| parameter.to_matcher }
end

#mocha_inspectObject



24
25
26
27
28
29
# File 'lib/mocha/parameters_matcher.rb', line 24

def mocha_inspect
  signature = matchers.mocha_inspect
  signature = signature.gsub(/^\[|\]$/, '')
  signature = signature.gsub(/^\{|\}$/, '') if matchers.length == 1
  "(#{signature})"
end

#parameters_match?(actual_parameters) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/mocha/parameters_matcher.rb', line 20

def parameters_match?(actual_parameters)
  matchers.all? { |matcher| matcher.matches?(actual_parameters) } && (actual_parameters.length == 0)
end