Skip to content
This repository was archived by the owner on Oct 27, 2021. It is now read-only.

Commit 386e334

Browse files
committed
cleanup mock proxy
1 parent 73ff09a commit 386e334

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

lib/spec/mocks/proxy.rb

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ def as_null_object
4040
def add_message_expectation(expected_from, sym, opts={}, &block)
4141
__add sym
4242
warn_if_nil_class sym
43-
if existing_stub = @stubs.detect {|s| s.sym == sym }
44-
expectation = existing_stub.build_child(expected_from, block_given?? block : nil, 1, opts)
43+
@expectations << build_expectation(expected_from, sym, opts, &block)
44+
@expectations.last
45+
end
46+
47+
def build_expectation(expected_from, sym, opts, &block)
48+
if stub = find_matching_method_stub(sym)
49+
stub.build_child(expected_from, block_given?? block : nil, 1, opts)
4550
else
46-
expectation = MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil, 1, opts)
51+
MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil, 1, opts)
4752
end
48-
@expectations << expectation
49-
@expectations.last
5053
end
5154

5255
def add_negative_message_expectation(expected_from, sym, &block)
@@ -64,7 +67,6 @@ def add_stub(expected_from, sym, opts={}, &implementation)
6467

6568
def remove_stub(message)
6669
message = message.to_sym
67-
6870
if stub_to_remove = @stubs.detect { |s| s.matches_name?(message) }
6971
reset_proxied_method(message)
7072
@stubs.delete(stub_to_remove)
@@ -92,7 +94,7 @@ def received_message?(sym, *args, &block)
9294
end
9395

9496
def has_negative_expectation?(sym)
95-
@expectations.detect {|expectation| expectation.negative_expectation_for?(sym)}
97+
@expectations.any? {|expectation| expectation.negative_expectation_for?(sym)}
9698
end
9799

98100
def record_message_received(sym, args, block)
@@ -103,21 +105,39 @@ def message_received(sym, *args, &block)
103105
expectation = find_matching_expectation(sym, *args)
104106
stub = find_matching_method_stub(sym, *args)
105107

106-
if (stub && expectation && expectation.called_max_times?) || (stub && !expectation)
107-
if expectation = find_almost_matching_expectation(sym, *args)
108-
expectation.advise(args, block) unless expectation.expected_messages_received?
109-
end
110-
stub.invoke(*args, &block)
108+
if ok_to_invoke_stub?(stub, expectation)
109+
record_stub(stub, sym, args, &block)
111110
elsif expectation
112-
expectation.invoke(*args, &block)
111+
invoke_expectation(expectation, *args, &block)
113112
elsif expectation = find_almost_matching_expectation(sym, *args)
114-
expectation.advise(args, block) if null_object? unless expectation.expected_messages_received?
115-
raise_unexpected_message_args_error(expectation, *args) unless (has_negative_expectation?(sym) or null_object?)
113+
record_almost_matching_expectation(expectation, sym, *args, &block)
116114
else
117115
@target.__send__ :method_missing, sym, *args, &block
118116
end
119117
end
120118

119+
def record_stub(stub, sym, args, &block)
120+
almost_matching_expectation(sym, *args) do |e|
121+
e.advise(args, block)
122+
end
123+
stub.invoke(*args, &block)
124+
end
125+
126+
def invoke_expectation(expectation, *args, &block)
127+
expectation.invoke(*args, &block)
128+
end
129+
130+
def record_almost_matching_expectation(expectation, sym, *args, &block)
131+
expectation.advise(args, block)
132+
unless (null_object? or has_negative_expectation?(sym))
133+
raise_unexpected_message_args_error(expectation, *args)
134+
end
135+
end
136+
137+
def ok_to_invoke_stub?(stub, expectation)
138+
stub && (!expectation || expectation.called_max_times?)
139+
end
140+
121141
def raise_unexpected_message_args_error(expectation, *args)
122142
@error_generator.raise_unexpected_message_args_error expectation, *args
123143
end
@@ -202,15 +222,11 @@ class << @target; self; end
202222
end
203223

204224
def verify_expectations
205-
@expectations.each do |expectation|
206-
expectation.verify_messages_received
207-
end
225+
@expectations.map {|e| e.verify_messages_received}
208226
end
209227

210228
def reset_proxied_methods
211-
@proxied_methods.each do |sym|
212-
reset_proxied_method(sym)
213-
end
229+
@proxied_methods.map {|sym| reset_proxied_method(sym)}
214230
end
215231

216232
def reset_proxied_method(sym)
@@ -237,10 +253,15 @@ def find_matching_expectation(sym, *args)
237253
@expectations.find {|expectation| expectation.matches(sym, args)}
238254
end
239255

256+
def almost_matching_expectation(sym, *args, &block)
257+
if e = find_almost_matching_expectation(sym, *args)
258+
yield e
259+
end
260+
end
261+
240262
def find_almost_matching_expectation(sym, *args)
241263
@expectations.find {|expectation| expectation.matches_name_but_not_args(sym, args)}
242264
end
243-
244265
end
245266
end
246267
end

0 commit comments

Comments
 (0)