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