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
gsterndale-warrant-0.3.0 test/rails_root/vendor/gems/mocha-0.9.4/lib/mocha/parameter_matchers/optionally.rb
hashrocket-clearance-0.4.0 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/parameter_matchers/optionally.rb
hashrocket-clearance-0.4.1 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/parameter_matchers/optionally.rb
hashrocket-clearance-0.4.2 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/parameter_matchers/optionally.rb
hashrocket-clearance-0.4.3 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/parameter_matchers/optionally.rb
jferris-mocha-0.9.5.0.1240002286 lib/mocha/parameter_matchers/optionally.rb
jferris-mocha-0.9.5.0.1240351621 lib/mocha/parameter_matchers/optionally.rb
jferris-mocha-0.9.5.0.1241126838 lib/mocha/parameter_matchers/optionally.rb
jferris-mocha-0.9.7.0.1247796736 lib/mocha/parameter_matchers/optionally.rb
jferris-mocha-0.9.7.20090701124354 lib/mocha/parameter_matchers/optionally.rb
jferris-mocha-0.9.7.20090911190113 lib/mocha/parameter_matchers/optionally.rb
thewoolleyman-mocha-0.9.3.20081219131457 lib/mocha/parameter_matchers/optionally.rb
thoughtbot-clearance-0.2.2 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/parameter_matchers/optionally.rb
thoughtbot-clearance-0.2.3 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/parameter_matchers/optionally.rb
thoughtbot-clearance-0.2.4 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/parameter_matchers/optionally.rb
thoughtbot-clearance-0.2.5 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/parameter_matchers/optionally.rb
thoughtbot-clearance-0.2.6 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/parameter_matchers/optionally.rb
thoughtbot-clearance-0.2.7 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/parameter_matchers/optionally.rb
thoughtbot-clearance-0.2.8 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/parameter_matchers/optionally.rb
thoughtbot-clearance-0.2.9 test/rails_root/vendor/gems/mocha-0.9.1/lib/mocha/parameter_matchers/optionally.rb