Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: rspec/rspec-mocks
...
head fork: rspec/rspec-mocks
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 18, 2010
@dchelimsky dchelimsky clean up 004b83b
Commits on May 19, 2010
@dchelimsky dchelimsky progress e03b58d
View
74 lib/rspec/mocks/message_expectation.rb
@@ -8,13 +8,12 @@ class BaseExpectation
attr_accessor :error_generator
protected :error_generator, :error_generator=
- def initialize(error_generator, expectation_ordering, expected_from, sym, method_block, expected_received_count=1, opts={}, &implementation)
+ def initialize(error_generator, expectation_ordering, expected_from, sym, expected_received_count=1, opts={}, &implementation)
@error_generator = error_generator
@error_generator.opts = opts
@expected_from = expected_from
@sym = sym
- @method_block = method_block
- @return_block = nil
+ @method_block = @return_block = implementation
@actual_received_count = 0
@expected_received_count = expected_received_count
@args_expectation = ArgumentExpectation.new([ArgumentMatchers::AnyArgsMatcher.new])
@@ -27,14 +26,13 @@ def initialize(error_generator, expectation_ordering, expected_from, sym, method
@args_to_yield = []
@failed_fast = nil
@args_to_yield_were_cloned = false
- @return_block = implementation
@eval_context = nil
end
- def build_child(expected_from, method_block, expected_received_count, opts={})
+ def build_child(expected_from, expected_received_count, opts={}, &implementation)
child = clone
child.expected_from = expected_from
- child.method_block = method_block
+ child.method_block = implementation
child.expected_received_count = expected_received_count
child.clear_actual_received_count!
new_gen = error_generator.clone
@@ -49,17 +47,17 @@ def expected_args
end
def and_return(*values, &return_block)
- Kernel::raise AmbiguousReturnError unless @method_block.nil?
- case values.size
- when 0 then value = nil
- when 1 then value = values[0]
+ Kernel::raise AmbiguousReturnError if @method_block
+ if values.size < 2
+ value = values[0]
else
- value = values
@consecutive = true
- @expected_received_count = values.size if !ignoring_args? &&
- @expected_received_count < values.size
+ unless ignoring_args?
+ @expected_received_count = [values.size, @expected_received_count].max
+ end
+ value = values
end
- @return_block = block_given? ? return_block : lambda { value }
+ @return_block = return_block || lambda { value }
end
# :call-seq:
@@ -98,11 +96,11 @@ 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
@@ -112,23 +110,23 @@ def invoke(args, block)
@order_group.handle_order_constraint self
begin
- Kernel::raise @exception_to_raise unless @exception_to_raise.nil?
- Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil?
+ Kernel::raise @exception_to_raise if @exception_to_raise
+ Kernel::throw @symbol_to_throw if @symbol_to_throw
- 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
+ eval_block(*args, &@method_block)
+ elsif @args_to_yield.size > 0 || @eval_context
+ invoke_with_yield(&block)
+ else
+ nil
+ end
if @consecutive
- return invoke_consecutive_return_block(args, block)
- elsif @return_block
- return invoke_return_block(args, block)
+ invoke_consecutive_return_block(args, block)
+ elsif @return_block && !@method_block
+ eval_block(*args, &@return_block)
else
- return default_return_val
+ default_return_val
end
ensure
@actual_received_count += 1
@@ -142,14 +140,6 @@ def called_max_times?
protected
- def invoke_method_block(args)
- begin
- @method_block.call(*args)
- rescue => detail
- @error_generator.raise_block_failed_error @sym, detail.message
- end
- end
-
def invoke_with_yield(&block)
if block.nil?
@error_generator.raise_missing_block_error @args_to_yield
@@ -170,6 +160,8 @@ def eval_block(*args, &block)
else
block.call(*args)
end
+ rescue => e
+ @error_generator.raise_block_failed_error @sym, e.message
end
def invoke_consecutive_return_block(args, block)
@@ -179,7 +171,7 @@ def invoke_consecutive_return_block(args, block)
end
def invoke_return_block(args, block)
- args << block unless block.nil?
+ args << block if block
# 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
@@ -329,8 +321,8 @@ def clear_actual_received_count!
end
class NegativeMessageExpectation < MessageExpectation
- def initialize(message, expectation_ordering, expected_from, sym, method_block)
- super(message, expectation_ordering, expected_from, sym, method_block, 0)
+ def initialize(message, expectation_ordering, expected_from, sym, &method_block)
+ super(message, expectation_ordering, expected_from, sym, 0, &method_block)
end
def negative_expectation_for?(sym)
View
8 lib/rspec/mocks/method_double.rb
@@ -118,9 +118,9 @@ def clear
def add_expectation(error_generator, expectation_ordering, expected_from, opts, &block)
configure_method
expectation = if existing_stub = stubs.first
- existing_stub.build_child(expected_from, block, 1, opts)
+ existing_stub.build_child(expected_from, 1, opts, &block)
else
- MessageExpectation.new(error_generator, expectation_ordering, expected_from, @method_name, block, 1, opts)
+ MessageExpectation.new(error_generator, expectation_ordering, expected_from, @method_name, 1, opts, &block)
end
expectations << expectation
expectation
@@ -128,14 +128,14 @@ def add_expectation(error_generator, expectation_ordering, expected_from, opts,
def add_negative_expectation(error_generator, expectation_ordering, expected_from, &implementation)
configure_method
- expectation = NegativeMessageExpectation.new(error_generator, expectation_ordering, expected_from, @method_name, implementation)
+ expectation = NegativeMessageExpectation.new(error_generator, expectation_ordering, expected_from, @method_name, &implementation)
expectations.unshift expectation
expectation
end
def add_stub(error_generator, expectation_ordering, expected_from, opts={}, &implementation)
configure_method
- stub = MessageExpectation.new(error_generator, expectation_ordering, expected_from, @method_name, nil, :any, opts, &implementation)
+ stub = MessageExpectation.new(error_generator, expectation_ordering, expected_from, @method_name, :any, opts, &implementation)
stubs.unshift stub
stub
end
View
4 lib/rspec/mocks/methods.rb
@@ -11,7 +11,7 @@ def should_not_receive(sym, &block)
def stub(sym_or_hash, opts={}, &block)
if Hash === sym_or_hash
- sym_or_hash.each {|method, value| stub!(method).and_return value }
+ sym_or_hash.each {|method, value| stub(method).and_return value }
else
__mock_proxy.add_stub(caller(1)[0], sym_or_hash.to_sym, opts, &block)
end
@@ -22,7 +22,7 @@ def stub(sym_or_hash, opts={}, &block)
def stub_chain(*methods)
if methods.length > 1
next_in_chain = Object.new
- stub!(methods.shift) {next_in_chain}
+ stub(methods.shift) {next_in_chain}
next_in_chain.stub_chain(*methods)
else
stub!(methods.shift)
View
12 lib/rspec/mocks/proxy.rb
@@ -52,8 +52,8 @@ def already_proxied_respond_to?
@already_proxied_respond_to
end
- def add_message_expectation(location, method_name, opts={}, &block)
- method_double[method_name].add_expectation @error_generator, @expectation_ordering, location, opts, &block
+ def add_message_expectation(location, method_name, opts={}, &implementation)
+ method_double[method_name].add_expectation @error_generator, @expectation_ordering, location, opts, &implementation
end
def add_negative_message_expectation(location, method_name, &implementation)
@@ -94,16 +94,16 @@ def message_received(method_name, *args, &block)
if expectation = find_almost_matching_expectation(method_name, *args)
expectation.advise(args, block) 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?
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
@@ -112,7 +112,7 @@ def raise_unexpected_message_args_error(expectation, *args)
end
def raise_unexpected_message_error(method_name, *args)
- @error_generator.raise_unexpected_message_error method_name, *args
+ @error_generator.raise_unexpected_message_error(method_name, *args)
end
private
View
2  spec/rspec/mocks/mock_spec.rb
@@ -366,7 +366,7 @@ module Mocks
@mock.yield_me do |x|
raise "Bang"
end
- }.should raise_error(StandardError, "Bang")
+ }.should raise_error(/Bang/)
@mock.rspec_verify
end
View
2  specs.watchr
@@ -20,7 +20,7 @@ end
def run(files_to_run)
puts("Running: #{files_to_run}")
- system("clear;rspec -cfs #{files_to_run}")
+ system("clear;rspec -c #{files_to_run}")
no_int_for_you
end

No commit comments for this range

Something went wrong with that request. Please try again.