Sha256: 2da179a77a0182726bb18344779d20eb86e289341c7eaa83a6a4b7dcd0b261df

Contents?: true

Size: 1.49 KB

Versions: 117

Compression:

Stored size: 1.49 KB

Contents

module Mocha
  
  module ParameterMatchers

    # :call-seq: optionally(*parameter_matchers) -> parameter_matcher
    #
    # Matches optional parameters if available.
    #   object = mock()
    #   object.expects(:method_1).with(1, 2, optionally(3, 4))
    #   object.method_1(1, 2)
    #   # no error raised
    #
    #   object = mock()
    #   object.expects(:method_1).with(1, 2, optionally(3, 4))
    #   object.method_1(1, 2, 3)
    #   # no error raised
    #
    #   object = mock()
    #   object.expects(:method_1).with(1, 2, optionally(3, 4))
    #   object.method_1(1, 2, 3, 4)
    #   # no error raised
    #
    #   object = mock()
    #   object.expects(:method_1).with(1, 2, optionally(3, 4))
    #   object.method_1(1, 2, 3, 5)
    #   # error raised, because optional parameters did not match
    def optionally(*matchers)
      Optionally.new(*matchers)
    end
    
    class Optionally < Base # :nodoc:
      
      def initialize(*parameters)
        @matchers = parameters.map { |parameter| parameter.to_matcher }
      end
      
      def matches?(available_parameters)
        index = 0
        while (available_parameters.length > 0) && (index < @matchers.length) do
          matcher = @matchers[index]
          return false unless matcher.matches?(available_parameters)
          index += 1
        end
        return true
      end
      
      def mocha_inspect
        "optionally(#{@matchers.map { |matcher| matcher.mocha_inspect }.join(", ") })"
      end
      
    end
    
  end
  
end

Version data entries

117 entries across 112 versions & 13 rubygems

Version Path
challah-0.5.2 vendor/bundle/gems/mocha-0.10.5/lib/mocha/parameter_matchers/optionally.rb
challah-0.5.1 vendor/bundle/gems/mocha-0.10.5/lib/mocha/parameter_matchers/optionally.rb
mocha-0.10.5 lib/mocha/parameter_matchers/optionally.rb
challah-0.5.0 vendor/bundle/gems/mocha-0.10.3/lib/mocha/parameter_matchers/optionally.rb
mocha-0.10.4 lib/mocha/parameter_matchers/optionally.rb
challah-0.4.1 vendor/bundle/gems/mocha-0.10.3/lib/mocha/parameter_matchers/optionally.rb
challah-0.4.0 vendor/bundle/gems/mocha-0.10.3/lib/mocha/parameter_matchers/optionally.rb
challah-0.3.5 vendor/bundle/gems/mocha-0.10.3/lib/mocha/parameter_matchers/optionally.rb
challah-0.3.4 vendor/bundle/gems/mocha-0.10.3/lib/mocha/parameter_matchers/optionally.rb
challah-0.3.3 vendor/bundle/gems/mocha-0.10.3/lib/mocha/parameter_matchers/optionally.rb
challah-0.3.2 vendor/bundle/gems/mocha-0.10.3/lib/mocha/parameter_matchers/optionally.rb
challah-0.3.1 vendor/bundle/gems/mocha-0.10.3/lib/mocha/parameter_matchers/optionally.rb
challah-0.3.0 vendor/bundle/gems/mocha-0.10.3/lib/mocha/parameter_matchers/optionally.rb
challah-0.2.1 vendor/bundle/gems/mocha-0.10.3/lib/mocha/parameter_matchers/optionally.rb
challah-0.2.0 vendor/bundle/gems/mocha-0.10.3/lib/mocha/parameter_matchers/optionally.rb
mocha-0.10.3 lib/mocha/parameter_matchers/optionally.rb
mocha-0.10.2 lib/mocha/parameter_matchers/optionally.rb
mocha-0.10.1 lib/mocha/parameter_matchers/optionally.rb
vanity-1.7.1 vendor/ruby/1.9.1/gems/mocha-0.9.12/lib/mocha/parameter_matchers/optionally.rb
mocha-0.10.0 lib/mocha/parameter_matchers/optionally.rb