Skip to content
Browse files

splat _this_

  • Loading branch information...
1 parent 7607345 commit ceb77672c251bf924a9ab97b7befd476cab231cc @dchelimsky dchelimsky committed Aug 9, 2010
View
17 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
@@ -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
View
52 lib/rspec/mocks/message_expectation.rb
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -235,7 +235,7 @@ def similar_messages
@similar_messages ||= []
end
- def advise(args, block)
+ def advise(*args)
similar_messages << args
end
@@ -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
View
2 lib/rspec/mocks/methods.rb
@@ -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 }
View
6 lib/rspec/mocks/mock.rb
@@ -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
View
20 lib/rspec/mocks/proxy.rb
@@ -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
@@ -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
@@ -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
View
4 spec/rspec/mocks/argument_expectation_spec.rb
@@ -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)

0 comments on commit ceb7767

Please sign in to comment.
Something went wrong with that request. Please try again.