Skip to content

Commit

Permalink
splat _this_
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Aug 9, 2010
1 parent 7607345 commit ceb7767
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 52 deletions.
17 changes: 7 additions & 10 deletions lib/rspec/mocks/argument_expectation.rb
@@ -1,10 +1,9 @@
module RSpec
module Mocks

class ArgumentExpectation
attr_reader :args

def initialize(args, &block)
def initialize(*args, &block)
@args = args
@matchers_block = block if args.empty?
@match_any_args = false
Expand All @@ -29,23 +28,21 @@ def is_matcher?(obj)
return obj.respond_to?(:matches?) & obj.respond_to?(:description)
end

def args_match?(given_args)
match_any_args? || matchers_block_matches?(given_args) || matchers_match?(given_args)
def args_match?(*args)
match_any_args? || matchers_block_matches?(*args) || matchers_match?(*args)
end

def matchers_block_matches?(given_args)
@matchers_block ? @matchers_block.call(*given_args) : nil
def matchers_block_matches?(*args)
@matchers_block ? @matchers_block.call(*args) : nil
end

def matchers_match?(given_args)
@matchers == given_args
def matchers_match?(*args)
@matchers == args
end

def match_any_args?
@match_any_args
end

end

end
end
52 changes: 26 additions & 26 deletions lib/rspec/mocks/message_expectation.rb
Expand Up @@ -17,7 +17,7 @@ def initialize(error_generator, expectation_ordering, expected_from, sym, method
@return_block = nil
@actual_received_count = 0
@expected_received_count = expected_received_count
@args_expectation = ArgumentExpectation.new([ArgumentMatchers::AnyArgsMatcher.new])
@args_expectation = ArgumentExpectation.new(ArgumentMatchers::AnyArgsMatcher.new)
@consecutive = false
@exception_to_raise = nil
@symbol_to_throw = nil
Expand All @@ -40,7 +40,7 @@ def build_child(expected_from, method_block, expected_received_count, opts={})
new_gen = error_generator.clone
new_gen.opts = opts
child.error_generator = new_gen
child.clone_args_to_yield @args_to_yield
child.clone_args_to_yield *@args_to_yield
child
end

Expand Down Expand Up @@ -97,15 +97,15 @@ def and_yield(*args, &block)
self
end

def matches(sym, args)
@sym == sym and @args_expectation.args_match?(args)
def matches?(sym, *args)
@sym == sym and @args_expectation.args_match?(*args)
end

def invoke(args, block)
def invoke(*args, &block)
if @expected_received_count == 0
@failed_fast = true
@actual_received_count += 1
@error_generator.raise_expectation_error @sym, @expected_received_count, @actual_received_count, *args
@error_generator.raise_expectation_error(@sym, @expected_received_count, @actual_received_count, *args)
end

@order_group.handle_order_constraint self
Expand All @@ -114,20 +114,20 @@ def invoke(args, block)
Kernel::raise @exception_to_raise unless @exception_to_raise.nil?
Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil?

if !@method_block.nil?
default_return_val = invoke_method_block(args)
elsif @args_to_yield.size > 0 || @eval_context
default_return_val = invoke_with_yield(&block)
else
default_return_val = nil
end
default_return_val = if !@method_block.nil?
invoke_method_block(*args)
elsif !@args_to_yield.empty? || @eval_context
invoke_with_yield(&block)
else
nil
end

if @consecutive
return invoke_consecutive_return_block(args, block)
invoke_consecutive_return_block(*args, &block)
elsif @return_block
return invoke_return_block(args, block)
invoke_return_block(*args, &block)
else
return default_return_val
default_return_val
end
ensure
@actual_received_count += 1
Expand All @@ -141,11 +141,11 @@ def called_max_times?

protected

def invoke_method_block(args)
def invoke_method_block(*args)
begin
@method_block.call(*args)
rescue => detail
@error_generator.raise_block_failed_error @sym, detail.message
@error_generator.raise_block_failed_error(@sym, detail.message)
end
end

Expand All @@ -171,21 +171,21 @@ def eval_block(*args, &block)
end
end

def invoke_consecutive_return_block(args, block)
value = invoke_return_block(args, block)
def invoke_consecutive_return_block(*args, &block)
value = invoke_return_block(*args, &block)
index = [@actual_received_count, value.size-1].min
value[index]
end

def invoke_return_block(args, block)
def invoke_return_block(*args, &block)
args << block unless block.nil?
# Ruby 1.9 - when we set @return_block to return values
# regardless of arguments, any arguments will result in
# a "wrong number of arguments" error
@return_block.arity == 0 ? @return_block.call : @return_block.call(*args)
end

def clone_args_to_yield(args)
def clone_args_to_yield(*args)
@args_to_yield = args.clone
@args_to_yield_were_cloned = true
end
Expand All @@ -197,8 +197,8 @@ def failed_fast?

class MessageExpectation < BaseExpectation

def matches_name_but_not_args(sym, args)
@sym == sym and not @args_expectation.args_match?(args)
def matches_name_but_not_args(sym, *args)
@sym == sym and not @args_expectation.args_match?(*args)
end

def verify_messages_received
Expand Down Expand Up @@ -235,7 +235,7 @@ def similar_messages
@similar_messages ||= []
end

def advise(args, block)
def advise(*args)
similar_messages << args
end

Expand All @@ -249,7 +249,7 @@ def generate_error

def with(*args, &block)
@return_block = block if block_given?
@args_expectation = ArgumentExpectation.new(args, &block)
@args_expectation = ArgumentExpectation.new(*args, &block)
self
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/mocks/methods.rb
Expand Up @@ -39,7 +39,7 @@ def stub_chain(*chain)
if methods.length > 1
if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, methods[0].to_sym)
methods.shift
matching_stub.__send__(:invoke, [], nil).stub_chain(*methods)
matching_stub.invoke.stub_chain(*methods)
else
next_in_chain = Object.new
stub(methods.shift) { next_in_chain }
Expand Down
6 changes: 3 additions & 3 deletions lib/rspec/mocks/mock.rb
Expand Up @@ -37,12 +37,12 @@ def to_s
private

def method_missing(sym, *args, &block)
__mock_proxy.record_message_received(sym, args, block)
__mock_proxy.record_message_received(sym, *args, &block)
begin
return self if __mock_proxy.null_object?
super(sym, *args, &block)
super
rescue NameError
__mock_proxy.raise_unexpected_message_error sym, *args
__mock_proxy.raise_unexpected_message_error(sym, *args)
end
end

Expand Down
20 changes: 10 additions & 10 deletions lib/rspec/mocks/proxy.rb
Expand Up @@ -87,7 +87,7 @@ def has_negative_expectation?(method_name)
method_double[method_name].expectations.detect {|expectation| expectation.negative_expectation_for?(method_name)}
end

def record_message_received(method_name, args, block)
def record_message_received(method_name, *args, &block)
@messages_received << [method_name, args, block]
end

Expand All @@ -97,18 +97,18 @@ def message_received(method_name, *args, &block)

if (stub && expectation && expectation.called_max_times?) || (stub && !expectation)
if expectation = find_almost_matching_expectation(method_name, *args)
expectation.advise(args, block) unless expectation.expected_messages_received?
expectation.advise(*args) unless expectation.expected_messages_received?
end
stub.invoke(args, block)
stub.invoke(*args, &block)
elsif expectation
expectation.invoke(args, block)
expectation.invoke(*args, &block)
elsif expectation = find_almost_matching_expectation(method_name, *args)
expectation.advise(args, block) if null_object? unless expectation.expected_messages_received?
expectation.advise(*args) if null_object? unless expectation.expected_messages_received?
raise_unexpected_message_args_error(expectation, *args) unless (has_negative_expectation?(method_name) or null_object?)
elsif @object.is_a?(Class)
@object.superclass.send(method_name, *args, &block)
else
@object.__send__ :method_missing, method_name, *args, &block
@object.__send__(:method_missing, method_name, *args, &block)
end
end

Expand All @@ -133,16 +133,16 @@ def method_doubles
end

def find_matching_expectation(method_name, *args)
method_double[method_name].expectations.find {|expectation| expectation.matches(method_name, args) && !expectation.called_max_times?} ||
method_double[method_name].expectations.find {|expectation| expectation.matches(method_name, args)}
method_double[method_name].expectations.find {|expectation| expectation.matches?(method_name, *args) && !expectation.called_max_times?} ||
method_double[method_name].expectations.find {|expectation| expectation.matches?(method_name, *args)}
end

def find_almost_matching_expectation(method_name, *args)
method_double[method_name].expectations.find {|expectation| expectation.matches_name_but_not_args(method_name, args)}
method_double[method_name].expectations.find {|expectation| expectation.matches_name_but_not_args(method_name, *args)}
end

def find_matching_method_stub(method_name, *args)
method_double[method_name].stubs.find {|stub| stub.matches(method_name, args)}
method_double[method_name].stubs.find {|stub| stub.matches?(method_name, *args)}
end

end
Expand Down
4 changes: 2 additions & 2 deletions spec/rspec/mocks/argument_expectation_spec.rb
Expand Up @@ -4,15 +4,15 @@ module RSpec
module Mocks
describe ArgumentExpectation do
it "should consider an object that responds to #matches? and #description to be a matcher" do
argument_expecatation = RSpec::Mocks::ArgumentExpectation.new([])
argument_expecatation = RSpec::Mocks::ArgumentExpectation.new
obj = double("matcher")
obj.should_receive(:respond_to?).with(:matches?).and_return(true)
obj.should_receive(:respond_to?).with(:description).and_return(true)
argument_expecatation.is_matcher?(obj).should be_true
end

it "should NOT consider an object that only responds to #matches? to be a matcher" do
argument_expecatation = RSpec::Mocks::ArgumentExpectation.new([])
argument_expecatation = RSpec::Mocks::ArgumentExpectation.new
obj = double("matcher")
obj.should_receive(:respond_to?).with(:matches?).and_return(true)
obj.should_receive(:respond_to?).with(:description).and_return(false)
Expand Down

0 comments on commit ceb7767

Please sign in to comment.