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
dradis-mediawiki-4.14.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.13.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.11.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.10.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.9.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.8.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.7.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.6.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.5.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.4.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.3.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.2.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.1.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-4.0.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-3.22.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-3.21.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-3.20.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-3.19.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-3.18.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb
dradis-mediawiki-3.17.0 test/vendor/mocha-0.9.5/lib/mocha/parameter_matchers/optionally.rb