Skip to content

Commit

Permalink
! assert_throws returns the value returned, if any. (volmer)
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//src/minitest/dev/": change = 13123]
  • Loading branch information
zenspider committed May 23, 2021
1 parent e325cc8 commit 5ea4b79
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/minitest/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def assert_silent
def assert_throws sym, msg = nil
default = "Expected #{mu_pp(sym)} to have been thrown"
caught = true
catch(sym) do
value = catch(sym) do
begin
yield
rescue ThreadError => e # wtf?!? 1.8 + threads == suck
Expand All @@ -489,6 +489,7 @@ def assert_throws sym, msg = nil
end

assert caught, message(msg) { default }
value
rescue Assertion
raise
rescue => e
Expand Down
12 changes: 11 additions & 1 deletion test/minitest/test_minitest_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -995,9 +995,19 @@ def test_assert_silent_triggered_out
end

def test_assert_throws
@tc.assert_throws :blah do
v = @tc.assert_throws :blah do
throw :blah
end

assert_nil v
end

def test_assert_throws_value
v = @tc.assert_throws :blah do
throw :blah, 42
end

assert_equal 42, v
end

def test_assert_throws_argument_exception
Expand Down
5 changes: 3 additions & 2 deletions test/minitest/test_minitest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,10 @@ def assert_success spec
end

it "needs to verify throw" do
@assertion_count += 2 # 2 extra tests
@assertion_count += 4 # 2 extra tests

assert_success expect { throw :blah }.must_throw(:blah)
assert_nil expect { throw :blah }.must_throw(:blah)
assert_equal 42, expect { throw :blah, 42 }.must_throw(:blah)

assert_triggered "Expected :blah to have been thrown." do
expect {}.must_throw :blah
Expand Down

0 comments on commit 5ea4b79

Please sign in to comment.