File: includes.rb

package info (click to toggle)
ruby-mocha 0.11.3-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,300 kB
  • sloc: ruby: 9,935; makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,216 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
47
48
49
50
51
require 'mocha/parameter_matchers/base'

module Mocha

  module ParameterMatchers

    # Matches any object that responds with +true+ to +include?(item)+.
    #
    # @param [Object] item expected item.
    # @return [Includes] parameter matcher.
    #
    # @see Expectation#with
    #
    # @example Actual parameter includes item.
    #   object = mock()
    #   object.expects(:method_1).with(includes('foo'))
    #   object.method_1(['foo', 'bar'])
    #   # no error raised
    #
    # @example Actual parameter does not include item.
    #   object.method_1(['baz'])
    #   # error raised, because ['baz'] does not include 'foo'.
    def includes(item)
      Includes.new(item)
    end

    # Parameter matcher which matches when actual parameter includes expected value.
    class Includes < Base

      # @private
      def initialize(item)
        @item = item
      end

      # @private
      def matches?(available_parameters)
        parameter = available_parameters.shift
        return false unless parameter.respond_to?(:include?)
        return parameter.include?(@item)
      end

      # @private
      def mocha_inspect
        "includes(#{@item.mocha_inspect})"
      end

    end

  end

end