/usr/lib/ruby/vendor_ruby/rspec/mocks/message_chain.rb is in ruby-rspec-mocks 3.4.0c3e0m1s1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | module RSpec
module Mocks
# @private
class MessageChain
attr_reader :object, :chain, :block
def initialize(object, *chain, &blk)
@object = object
@chain, @block = format_chain(*chain, &blk)
end
# @api private
def setup_chain
if chain.length > 1
if (matching_stub = find_matching_stub)
chain.shift
chain_on(matching_stub.invoke(nil), *chain, &@block)
elsif (matching_expectation = find_matching_expectation)
chain.shift
chain_on(matching_expectation.invoke_without_incrementing_received_count(nil), *chain, &@block)
else
next_in_chain = Double.new
expectation(object, chain.shift) { next_in_chain }
chain_on(next_in_chain, *chain, &@block)
end
else
expectation(object, chain.shift, &@block)
end
end
private
def chain_on(object, *chain, &block)
initialize(object, *chain, &block)
setup_chain
end
def format_chain(*chain, &blk)
if Hash === chain.last
hash = chain.pop
hash.each do |k, v|
chain << k
blk = Proc.new { v }
end
end
return chain.join('.').split('.'), blk
end
def find_matching_stub
::RSpec::Mocks.space.proxy_for(object).
__send__(:find_matching_method_stub, chain.first.to_sym)
end
def find_matching_expectation
::RSpec::Mocks.space.proxy_for(object).
__send__(:find_matching_expectation, chain.first.to_sym)
end
end
# @private
class ExpectChain < MessageChain
# @api private
def self.expect_chain_on(object, *chain, &blk)
new(object, *chain, &blk).setup_chain
end
private
def expectation(object, message, &return_block)
::RSpec::Mocks.expect_message(object, message, {}, &return_block)
end
end
# @private
class StubChain < MessageChain
def self.stub_chain_on(object, *chain, &blk)
new(object, *chain, &blk).setup_chain
end
private
def expectation(object, message, &return_block)
::RSpec::Mocks.allow_message(object, message, {}, &return_block)
end
end
end
end
|