Geminstaller C0 Coverage Information - RCov

/Users/woolley/.rvm/gems/ruby-1.8.7-p174@geminstaller/gems/rspec-1.3.0/lib/spec/mocks/message_expectation.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
/Users/woolley/.rvm/gems/ruby-1.8.7-p174@geminstaller/gems/rspec-1.3.0/lib/spec/mocks/message_expectation.rb 344 275
58.14%
49.09%

Key

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.

Coverage Details

1 module Spec
2   module Mocks
3 
4     class BaseExpectation
5       attr_reader :sym
6       attr_writer :expected_received_count, :method_block, :expected_from
7       protected :expected_received_count=, :method_block=, :expected_from=
8       attr_accessor :error_generator
9       protected :error_generator, :error_generator=
10 
11       def initialize(error_generator, expectation_ordering, expected_from, sym, method_block, expected_received_count=1, opts={}, &implementation)
12         @error_generator = error_generator
13         @error_generator.opts = opts
14         @expected_from = expected_from
15         @sym = sym
16         @method_block = method_block
17         @actual_received_count = 0
18         @expected_received_count = expected_received_count
19         @args_expectation = ArgumentExpectation.new([ArgumentMatchers::AnyArgsMatcher.new])
20         @consecutive = false
21         @exception_to_raise = nil
22         @symbol_to_throw = nil
23         @order_group = expectation_ordering
24         @at_least = nil
25         @at_most = nil
26         @args_to_yield = []
27         @failed_fast = nil
28         @args_to_yield_were_cloned = false
29         @return_block = implementation
30         @eval_context = nil
31       end
32 
33       def build_child(expected_from, method_block, expected_received_count, opts={})
34         child = clone
35         child.expected_from = expected_from
36         child.method_block = method_block
37         child.expected_received_count = expected_received_count
38         child.clear_actual_received_count!
39         new_gen = error_generator.clone
40         new_gen.opts = opts
41         child.error_generator = new_gen
42         child.clone_args_to_yield @args_to_yield
43         child
44       end
45 
46       def expected_args
47         @args_expectation.args
48       end
49 
50       def and_return(*values, &return_block)
51         Kernel::raise AmbiguousReturnError unless @method_block.nil?
52         case values.size
53           when 0 then value = nil
54           when 1 then value = values[0]
55         else
56           value = values
57           @consecutive = true
58           @expected_received_count = values.size if !ignoring_args? &&
59                                                     @expected_received_count < values.size
60         end
61         @return_block = block_given? ? return_block : lambda { value }
62       end
63 
64       # :call-seq:
65       #   and_raise()
66       #   and_raise(Exception) #any exception class
67       #   and_raise(exception) #any exception object
68       #
69       # == Warning
70       #
71       # When you pass an exception class, the MessageExpectation will
72       # raise an instance of it, creating it with +new+. If the exception
73       # class initializer requires any parameters, you must pass in an
74       # instance and not the class.
75       def and_raise(exception=Exception)
76         @exception_to_raise = exception
77       end
78 
79       def and_throw(symbol)
80         @symbol_to_throw = symbol
81       end
82 
83       def and_yield(*args, &block)
84         if @args_to_yield_were_cloned
85           @args_to_yield.clear
86           @args_to_yield_were_cloned = false
87         end
88 
89         if block
90           require 'spec/extensions/instance_exec'
91           @eval_context = Object.new
92           @eval_context.extend Spec::Matchers::InstanceExec
93           yield @eval_context
94         end
95         @args_to_yield << args
96         self
97       end
98 
99       def matches(sym, args)
100         @sym == sym and @args_expectation.args_match?(args)
101       end
102 
103       def invoke(*args, &block)
104         if @expected_received_count == 0
105           @failed_fast = true
106           @actual_received_count += 1
107           @error_generator.raise_expectation_error @sym, @expected_received_count, @actual_received_count, *args
108         end
109 
110         @order_group.handle_order_constraint self
111 
112         begin
113           Kernel::raise @exception_to_raise unless @exception_to_raise.nil?
114           Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil?
115 
116 
117           if !@method_block.nil?
118             default_return_val = invoke_method_block(*args)
119           elsif @args_to_yield.size > 0 || @eval_context
120             default_return_val = invoke_with_yield(&block)
121           else
122             default_return_val = nil
123           end
124 
125           if @consecutive
126             return invoke_consecutive_return_block(*args, &block)
127           elsif @return_block
128             return invoke_return_block(*args, &block)
129           else
130             return default_return_val
131           end
132         ensure
133           @actual_received_count += 1
134         end
135       end
136 
137       def called_max_times?
138         @expected_received_count != :any && @expected_received_count > 0 &&
139           @actual_received_count >= @expected_received_count
140       end
141 
142       def invoke_return_block(*args, &block)
143         args << block unless block.nil?
144         # Ruby 1.9 - when we set @return_block to return values
145         # regardless of arguments, any arguments will result in
146         # a "wrong number of arguments" error
147         @return_block.arity == 0 ? @return_block.call : @return_block.call(*args)
148       end
149 
150       protected
151 
152       def invoke_method_block(*args)
153         begin
154           @method_block.call(*args)
155         rescue => detail
156           @error_generator.raise_block_failed_error @sym, detail.message
157         end
158       end
159 
160       def invoke_with_yield(&block)
161         if block.nil?
162           @error_generator.raise_missing_block_error @args_to_yield
163         end
164         value = nil
165         @args_to_yield.each do |args_to_yield_this_time|
166           if block.arity > -1 && args_to_yield_this_time.length != block.arity
167             @error_generator.raise_wrong_arity_error args_to_yield_this_time, block.arity
168           end
169           value = eval_block(*args_to_yield_this_time, &block)
170         end
171         value
172       end
173 
174       def eval_block(*args, &block)
175         if @eval_context
176           @eval_context.instance_exec(*args, &block)
177         else
178           block.call(*args)
179         end
180       end
181 
182       def invoke_consecutive_return_block(*args, &block)
183         value = invoke_return_block(*args, &block)
184         index = [@actual_received_count, value.size-1].min
185         value[index]
186       end
187 
188       def clone_args_to_yield(args)
189         @args_to_yield = args.clone
190         @args_to_yield_were_cloned = true
191       end
192 
193       def failed_fast?
194         @failed_fast
195       end
196     end
197 
198     class MessageExpectation < BaseExpectation
199 
200       def matches_name?(sym)
201         @sym == sym
202       end
203 
204       def matches_name_but_not_args(sym, args)
205         matches_name?(sym) and not @args_expectation.args_match?(args)
206       end
207 
208       def verify_messages_received
209         return if expected_messages_received? || failed_fast?
210 
211         generate_error
212       rescue Spec::Mocks::MockExpectationError => error
213         error.backtrace.insert(0, @expected_from)
214         Kernel::raise error
215       end
216 
217       def expected_messages_received?
218         ignoring_args? || matches_exact_count? ||
219            matches_at_least_count? || matches_at_most_count?
220       end
221 
222       def ignoring_args?
223         @expected_received_count == :any
224       end
225 
226       def matches_at_least_count?
227         @at_least && @actual_received_count >= @expected_received_count
228       end
229 
230       def matches_at_most_count?
231         @at_most && @actual_received_count <= @expected_received_count
232       end
233 
234       def matches_exact_count?
235         @expected_received_count == @actual_received_count
236       end
237 
238       def similar_messages
239         @similar_messages ||= []
240       end
241 
242       def advise(args, block)
243         similar_messages << args
244       end
245 
246       def generate_error
247         if similar_messages.empty?
248           @error_generator.raise_expectation_error(@sym, @expected_received_count, @actual_received_count, *@args_expectation.args)
249         else
250           @error_generator.raise_unexpected_message_args_error(self, *@similar_messages)
251         end
252       end
253 
254       def with(*args, &block)
255         @args_expectation = ArgumentExpectation.new(args, &block)
256         self
257       end
258 
259       def exactly(n)
260         set_expected_received_count :exactly, n
261         self
262       end
263 
264       def at_least(n)
265         set_expected_received_count :at_least, n
266         self
267       end
268 
269       def at_most(n)
270         set_expected_received_count :at_most, n
271         self
272       end
273 
274       def times(&block)
275         @method_block = block if block
276         self
277       end
278 
279       def any_number_of_times(&block)
280         @method_block = block if block
281         @expected_received_count = :any
282         self
283       end
284 
285       def never
286         @expected_received_count = 0
287         self
288       end
289 
290       def once(&block)
291         @method_block = block if block
292         @expected_received_count = 1
293         self
294       end
295 
296       def twice(&block)
297         @method_block = block if block
298         @expected_received_count = 2
299         self
300       end
301 
302       def ordered(&block)
303         @method_block = block if block
304         @order_group.register(self)
305         @ordered = true
306         self
307       end
308 
309       def negative_expectation_for?(sym)
310         return false
311       end
312 
313       protected
314         def set_expected_received_count(relativity, n)
315           @at_least = (relativity == :at_least)
316           @at_most = (relativity == :at_most)
317           @expected_received_count = case n
318             when Numeric
319               n
320             when :once
321               1
322             when :twice
323               2
324           end
325         end
326 
327         def clear_actual_received_count!
328           @actual_received_count = 0
329         end
330 
331     end
332 
333     class NegativeMessageExpectation < MessageExpectation
334       def initialize(message, expectation_ordering, expected_from, sym, method_block)
335         super(message, expectation_ordering, expected_from, sym, method_block, 0)
336       end
337 
338       def negative_expectation_for?(sym)
339         return @sym == sym
340       end
341     end
342 
343   end
344 end

Generated on Mon May 10 23:40:27 -0700 2010 with rcov 0.9.8