C0 code coverage information
Generated on Mon Aug 13 01:18:54 -0400 2007 with rcov 0.8.0
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 module Spec
2 module Mocks
3
4 class BaseExpectation
5 attr_reader :sym
6
7 def initialize(error_generator, expectation_ordering, expected_from, sym, method_block, expected_received_count=1, opts={})
8 @error_generator = error_generator
9 @error_generator.opts = opts
10 @expected_from = expected_from
11 @sym = sym
12 @method_block = method_block
13 @return_block = lambda {}
14 @received_count = 0
15 @expected_received_count = expected_received_count
16 @args_expectation = ArgumentExpectation.new([AnyArgsConstraint.new])
17 @consecutive = false
18 @exception_to_raise = nil
19 @symbol_to_throw = nil
20 @order_group = expectation_ordering
21 @at_least = nil
22 @at_most = nil
23 @args_to_yield = nil
24 end
25
26 def expected_args
27 @args_expectation.args
28 end
29
30 def and_return(*values, &return_block)
31 Kernel::raise AmbiguousReturnError unless @method_block.nil?
32 if values.size == 0
33 value = nil
34 elsif values.size == 1
35 value = values[0]
36 else
37 value = values
38 @consecutive = true
39 @expected_received_count = values.size if @expected_received_count != :any &&
40 @expected_received_count < values.size
41 end
42 @return_block = block_given? ? return_block : lambda { value }
43 end
44
45 # :call-seq:
46 # and_raise()
47 # and_raise(Exception) #any exception class
48 # and_raise(exception) #any exception object
49 #
50 # == Warning
51 #
52 # When you pass an exception class, the MessageExpectation will
53 # raise an instance of it, creating it with +new+. If the exception
54 # class initializer requires any parameters, you must pass in an
55 # instance and not the class.
56 def and_raise(exception=Exception)
57 @exception_to_raise = exception
58 end
59
60 def and_throw(symbol)
61 @symbol_to_throw = symbol
62 end
63
64 def and_yield(*args)
65 @args_to_yield = args
66 end
67
68 def matches(sym, args)
69 @sym == sym and @args_expectation.check_args(args)
70 end
71
72 def invoke(args, block)
73 @order_group.handle_order_constraint self
74
75 begin
76 if @exception_to_raise.class == Class
77 @exception_instance_to_raise = @exception_to_raise.new
78 else
79 @exception_instance_to_raise = @exception_to_raise
80 end
81 Kernel::raise @exception_to_raise unless @exception_to_raise.nil?
82 Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil?
83
84 if !@method_block.nil?
85 return invoke_method_block(args)
86 elsif !@args_to_yield.nil?
87 return invoke_with_yield(block)
88 elsif @consecutive
89 return invoke_consecutive_return_block(args, block)
90 else
91 return invoke_return_block(args, block)
92 end
93 ensure
94 @received_count += 1
95 end
96 end
97
98 protected
99
100 def invoke_method_block(args)
101 begin
102 @method_block.call(*args)
103 rescue => detail
104 @error_generator.raise_block_failed_error @sym, detail.message
105 end
106 end
107
108 def invoke_with_yield(block)
109 if block.nil?
110 @error_generator.raise_missing_block_error @args_to_yield
111 end
112 if block.arity > -1 && @args_to_yield.length != block.arity
113 @error_generator.raise_wrong_arity_error @args_to_yield, block.arity
114 end
115 block.call(*@args_to_yield)
116 end
117
118 def invoke_consecutive_return_block(args, block)
119 args << block unless block.nil?
120 value = @return_block.call(*args)
121
122 index = [@received_count, value.size-1].min
123 value[index]
124 end
125
126 def invoke_return_block(args, block)
127 args << block unless block.nil?
128 value = @return_block.call(*args)
129
130 value
131 end
132 end
133
134 class MessageExpectation < BaseExpectation
135
136 def matches_name_but_not_args(sym, args)
137 @sym == sym and not @args_expectation.check_args(args)
138 end
139
140 def verify_messages_received
141 return if @expected_received_count == :any
142 return if (@at_least) && (@received_count >= @expected_received_count)
143 return if (@at_most) && (@received_count <= @expected_received_count)
144 return if @expected_received_count == @received_count
145
146 begin
147 @error_generator.raise_expectation_error(@sym, @expected_received_count, @received_count, *@args_expectation.args)
148 rescue => error
149 error.backtrace.insert(0, @expected_from)
150 Kernel::raise error
151 end
152 end
153
154 def with(*args, &block)
155 @method_block = block if block
156 @args_expectation = ArgumentExpectation.new(args)
157 self
158 end
159
160 def exactly(n)
161 set_expected_received_count :exactly, n
162 self
163 end
164
165 def at_least(n)
166 set_expected_received_count :at_least, n
167 self
168 end
169
170 def at_most(n)
171 set_expected_received_count :at_most, n
172 self
173 end
174
175 def times(&block)
176 @method_block = block if block
177 self
178 end
179
180 def any_number_of_times(&block)
181 @method_block = block if block
182 @expected_received_count = :any
183 self
184 end
185
186 def never
187 @expected_received_count = 0
188 self
189 end
190
191 def once(&block)
192 @method_block = block if block
193 @expected_received_count = 1
194 self
195 end
196
197 def twice(&block)
198 @method_block = block if block
199 @expected_received_count = 2
200 self
201 end
202
203 def ordered(&block)
204 @method_block = block if block
205 @order_group.register(self)
206 @ordered = true
207 self
208 end
209
210 def negative_expectation_for?(sym)
211 return false
212 end
213
214 protected
215 def set_expected_received_count(relativity, n)
216 @at_least = (relativity == :at_least)
217 @at_most = (relativity == :at_most)
218 @expected_received_count = 1 if n == :once
219 @expected_received_count = 2 if n == :twice
220 @expected_received_count = n if n.kind_of? Numeric
221 end
222
223 end
224
225 class NegativeMessageExpectation < MessageExpectation
226 def initialize(message, expectation_ordering, expected_from, sym, method_block)
227 super(message, expectation_ordering, expected_from, sym, method_block, 0)
228 end
229
230 def negative_expectation_for?(sym)
231 return @sym == sym
232 end
233 end
234
235 class MethodStub < BaseExpectation
236 def initialize(message, expectation_ordering, expected_from, sym, method_block)
237 super(message, expectation_ordering, expected_from, sym, method_block, 0)
238 @expected_received_count = :any
239 end
240 end
241 end
242 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.0.