Skip to content

Commit

Permalink
- Object.stub now passes blocks down to the callable result.
Browse files Browse the repository at this point in the history
- Object.stub no longer calls the passed block if stubbed with a callable.

[git-p4: depot-paths = "//src/minitest/dev/": change = 11463]
  • Loading branch information
zenspider committed Dec 10, 2017
1 parent daf555a commit ac9eaef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
15 changes: 6 additions & 9 deletions lib/minitest/mock.rb
Expand Up @@ -223,15 +223,12 @@ def stub name, val_or_callable, *block_args
metaclass.send :alias_method, new_name, name

metaclass.send :define_method, name do |*args, &blk|
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
if val_or_callable.respond_to? :call then
val_or_callable.call(*args, &blk)
else
blk.call(*block_args) if blk
val_or_callable
end
end

yield self
Expand Down
26 changes: 12 additions & 14 deletions test/minitest/test_minitest_mock.rb
Expand Up @@ -594,11 +594,11 @@ def self.identity arg
end

def test_stub_callable_block_5 # from tenderlove
@assertion_count += 2
@assertion_count += 1
Foo.stub5 :blocking, Bar.new do
@tc.assert_output "hi\n", "" do
Foo.blocking do
@tc.assert true
@tc.flunk "shouldn't ever hit this"
end
end
end
Expand Down Expand Up @@ -630,10 +630,9 @@ def test_stub_lambda_args
end

def test_stub_lambda_block_5
@assertion_count += 1
Thread.stub5 :new, lambda { 21+21 } do
result = Thread.new do # TODO: warn about using both?
@tc.assert true
result = Thread.new do
@tc.flunk "shouldn't ever hit this"
end
@tc.assert_equal 42, result
end
Expand All @@ -651,10 +650,10 @@ def test_stub_lambda_block_6
end

def test_stub_lambda_block_args_5
@assertion_count += 2
@assertion_count += 1
Thingy.stub5 :identity, lambda { |y| @tc.assert_equal :nope, y; 21+21 }, :WTF? do
result = Thingy.identity :nope do |x|
@tc.assert_equal :WTF?, x # TODO: THIS MAKES NO SENSE AT ALL
@tc.flunk "shouldn't reach this"
end
@tc.assert_equal 42, result
end
Expand Down Expand Up @@ -691,8 +690,8 @@ def test_stub_lambda_block_call_5
rs = f && f.write("woot")
end
end
@tc.assert_nil rs
@tc.assert_equal "", io.string
@tc.assert_equal 4, rs
@tc.assert_equal "woot", io.string
end

def test_stub_lambda_block_call_6
Expand All @@ -711,17 +710,16 @@ def test_stub_lambda_block_call_6
end

def test_stub_lambda_block_call_args_5
@assertion_count += 2
@assertion_count += 1
rs = nil
io = StringIO.new "", "w"
File.stub5(:open, lambda { |p, m, &blk| blk and blk.call io }, :WTF?) do
File.open "foo.txt", "r" do |f|
@tc.assert_equal :WTF?, f # HACK: THIS MAKES NO SENSE AT ALL
# TODO: rs = f.write("woot")
rs = f.write("woot")
end
end
@tc.assert_nil rs # TODO
@tc.assert_equal "", io.string # TODO
@tc.assert_equal 4, rs
@tc.assert_equal "woot", io.string
end

def test_stub_lambda_block_call_args_6
Expand Down

0 comments on commit ac9eaef

Please sign in to comment.