File: parameters_matcher.rb

package info (click to toggle)
ruby-mocha 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: ruby: 12,324; javascript: 499; makefile: 14
file content (46 lines) | stat: -rw-r--r-- 1,238 bytes parent folder | download
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
# frozen_string_literal: true

require 'mocha/inspect'
require 'mocha/parameter_matchers'

module Mocha
  class ParametersMatcher
    def initialize(expected_parameters = [ParameterMatchers::AnyParameters.new], expectation = nil, &matching_block)
      @expected_parameters = expected_parameters
      @expectation = expectation
      @matching_block = matching_block
    end

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

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

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

    def matchers
      @expected_parameters.map.with_index do |parameter, index|
        parameter.to_matcher(
          expectation: @expectation,
          top_level: true,
          last: index == @expected_parameters.length - 1
        )
      end
    end
  end
end