Skip to content

Commit

Permalink
remove __add (push down to MethodDouble)
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Mar 16, 2010
1 parent b9ae4ef commit b7dcfa2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
32 changes: 13 additions & 19 deletions lib/rspec/mocks/proxy.rb
Expand Up @@ -3,6 +3,8 @@ module Mocks
class Proxy
DEFAULT_OPTIONS = { :null_object => false }

attr_accessor :already_proxied_respond_to

class << self
def warn_about_expectations_on_nil
defined?(@warn_about_expectations_on_nil) ? @warn_about_expectations_on_nil : true
Expand Down Expand Up @@ -35,8 +37,6 @@ def initialize(target, name=nil, options={})
@already_proxied_respond_to = false
end

attr_accessor :already_proxied_respond_to

def null_object?
@options[:null_object]
end
Expand All @@ -47,23 +47,20 @@ def as_null_object
end

def add_message_expectation(expected_from, sym, opts={}, &block)
__add sym
expectation = if existing_stub = expectations_hash[sym][:stubs].detect {|s| s.sym == sym }
existing_stub.build_child(expected_from, block_given?? block : nil, 1, opts)
else
MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil, 1, opts)
end
expectations_hash[sym].add_expectation expectation
double_for(sym).add_expectation expectation
end

def add_negative_message_expectation(expected_from, sym, &block)
__add sym
expectations_hash[sym].add_expectation NegativeMessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil)
double_for(sym).add_expectation NegativeMessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil)
end

def add_stub(expected_from, sym, opts={}, &implementation)
__add sym
expectations_hash[sym].add_stub MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, nil, :any, opts, &implementation)
double_for(sym).add_stub MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, nil, :any, opts, &implementation)
end

def verify #:nodoc:
Expand All @@ -74,7 +71,6 @@ def verify #:nodoc:

def reset
method_doubles.each {|d| d.reset}
reset_nil_expectations_warning
end

def received_message?(sym, *args, &block)
Expand Down Expand Up @@ -120,11 +116,6 @@ def raise_unexpected_message_error(sym, *args)

private

def __add(sym)
$rspec_mocks.add(@target) if $rspec_mocks
double_for(sym).proxy_method
end

def double_for(sym)
expectations_hash[sym]
end
Expand All @@ -137,10 +128,6 @@ def proxy_for_nil_class?
@target.nil?
end

def reset_nil_expectations_warning
self.class.warn_about_expectations_on_nil = true if proxy_for_nil_class?
end

def find_matching_expectation(sym, *args)
double_for(sym)[:expectations].find {|expectation| expectation.matches(sym, args) && !expectation.called_max_times?} ||
double_for(sym)[:expectations].find {|expectation| expectation.matches(sym, args)}
Expand All @@ -162,8 +149,8 @@ def expectations_hash

class MethodDouble < Hash
def initialize(target, sym, proxy)
@target = target
@sym = sym
@target = target
@proxy = proxy
@proxied = false
store(:expectations, [])
Expand Down Expand Up @@ -230,6 +217,7 @@ def proxy_method
end
redefine
warn_if_nil_class
$rspec_mocks.add(@target) if $rspec_mocks
end

def reset_proxied_method
Expand All @@ -254,6 +242,7 @@ def verify
end

def reset
reset_nil_expectations_warning
reset_proxied_method
clear
end
Expand All @@ -264,11 +253,13 @@ def clear
end

def add_expectation(expectation)
proxy_method
self[:expectations] << expectation
expectation
end

def add_stub(stub)
proxy_method
self[:stubs] << stub
stub
end
Expand All @@ -283,6 +274,9 @@ def warn_if_nil_class
end
end

def reset_nil_expectations_warning
Rspec::Mocks::Proxy.warn_about_expectations_on_nil = true if proxy_for_nil_class?
end
end

end
Expand Down
10 changes: 5 additions & 5 deletions spec/rspec/mocks/stub_spec.rb
Expand Up @@ -124,9 +124,9 @@ def existing_instance_method
end

it "should throw when told to" do
@mock.stub!(:something).and_throw(:up)
@stub.stub(:something).and_throw(:up)
lambda do
@mock.something
@stub.something
end.should throw_symbol(:up)
end

Expand All @@ -143,9 +143,9 @@ def existing_instance_method
end

it "calculates return value by executing block passed to #and_return" do
@mock.stub!(:something).with("a","b","c").and_return { |a,b,c| c+b+a }
@mock.something("a","b","c").should == "cba"
@mock.rspec_verify
@stub.stub(:something).with("a","b","c").and_return { |a,b,c| c+b+a }
@stub.something("a","b","c").should == "cba"
@stub.rspec_verify
end
end

Expand Down

0 comments on commit b7dcfa2

Please sign in to comment.