| Class | Spec::Mocks::BaseExpectation |
| In: |
lib/spec/mocks/message_expectation.rb
|
| Parent: | Object |
| sym | [R] |
# File lib/spec/mocks/message_expectation.rb, line 7
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
# File lib/spec/mocks/message_expectation.rb, line 45
45: def and_raise(exception=Exception)
46: @exception_to_raise = exception
47: end
# File lib/spec/mocks/message_expectation.rb, line 30
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
# File lib/spec/mocks/message_expectation.rb, line 49
49: def and_throw(symbol)
50: @symbol_to_throw = symbol
51: end
# File lib/spec/mocks/message_expectation.rb, line 53
53: def and_yield(*args)
54: @args_to_yield = args
55: end
# File lib/spec/mocks/message_expectation.rb, line 26
26: def expected_args
27: @args_expectation.args
28: end
# File lib/spec/mocks/message_expectation.rb, line 61
61: def invoke(args, block)
62: @order_group.handle_order_constraint self
63:
64: begin
65: if @exception_to_raise.class == Class
66: @exception_instance_to_raise = @exception_to_raise.new
67: else
68: @exception_instance_to_raise = @exception_to_raise
69: end
70: Kernel::raise @exception_to_raise unless @exception_to_raise.nil?
71: Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil?
72:
73: if !@method_block.nil?
74: return invoke_method_block(args)
75: elsif !@args_to_yield.nil?
76: return invoke_with_yield(block)
77: elsif @consecutive
78: return invoke_consecutive_return_block(args, block)
79: else
80: return invoke_return_block(args, block)
81: end
82: ensure
83: @received_count += 1
84: end
85: end
# File lib/spec/mocks/message_expectation.rb, line 57
57: def matches(sym, args)
58: @sym == sym and @args_expectation.check_args(args)
59: end
# File lib/spec/mocks/message_expectation.rb, line 107
107: def invoke_consecutive_return_block(args, block)
108: args << block unless block.nil?
109: value = @return_block.call(*args)
110:
111: index = [@received_count, value.size-1].min
112: value[index]
113: end
# File lib/spec/mocks/message_expectation.rb, line 89
89: def invoke_method_block(args)
90: begin
91: @method_block.call(*args)
92: rescue => detail
93: @error_generator.raise_block_failed_error @sym, detail.message
94: end
95: end
# File lib/spec/mocks/message_expectation.rb, line 115
115: def invoke_return_block(args, block)
116: args << block unless block.nil?
117: value = @return_block.call(*args)
118:
119: value
120: end
# File lib/spec/mocks/message_expectation.rb, line 97
97: def invoke_with_yield(block)
98: if block.nil?
99: @error_generator.raise_missing_block_error @args_to_yield
100: end
101: if block.arity > -1 && @args_to_yield.length != block.arity
102: @error_generator.raise_wrong_arity_error @args_to_yield, block.arity
103: end
104: block.call(*@args_to_yield)
105: end