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
|
# File 'lib/minitest/moar/stubbing.rb', line 11
def instance_stub klass, name, val_or_callable = nil, *block_args, &block
@invocations ||= Hash.new { |h, k| h[k] = Hash.new { |_h, _k| _h[_k] = 0 } }
invoker = Proc.new { |klass, name| @invocations[klass][name] += 1 }
new_name = "__minitest_stub__#{name}"
if respond_to? name and not methods.map(&:to_s).include? name.to_s then
klass.send :define_method, name do |*args|
super(*args)
end
end
klass.send :alias_method, new_name, name
klass.send :define_method, name do |*args, &blk|
if self.is_a?(Class)
invoker.call(self, name)
else
invoker.call("Instance of #{self.class}", name)
end
ret = if val_or_callable.respond_to? :call then
val_or_callable.call(*args)
else
val_or_callable
end
blk.call(*block_args) if blk
ret
end
yield klass
ensure
klass.send :undef_method, name
klass.send :alias_method, name, new_name
klass.send :undef_method, new_name
end
|