Skip to content
Permalink
Browse files

+ Block-assertions (eg assert_output) now error if raised inside the …

…block. (casperisfine)

This will potentially cause some of your tests to fail:

```ruby
  assert_raises do            # 4) bypassed
    assert_output "blah\n" do # 3) rescues and raises UnexpectedError
      puts "not_blah"         # 1) output
      raise "boom!"           # 2) raised
    end
  end
```

Re-order to:

```ruby
  assert_output "blah\n" do       # 4) captured and compared
    assert_raises RuntimeError do # 3) caught
      puts "blah"                 # 1) output
      raise "boom!"               # 2) raised
    end
  end
```

Closes #809.

[git-p4: depot-paths = "//src/minitest/dev/": change = 12414]
  • Loading branch information
zenspider committed Nov 24, 2019
1 parent 02e35ce commit 96ba1dac6141599cce47ec298e7e72dc7d49812d
Showing with 141 additions and 3 deletions.
  1. +8 −0 lib/minitest/assertions.rb
  2. +133 −3 test/minitest/test_minitest_assertions.rb
@@ -341,6 +341,10 @@ def assert_output stdout = nil, stderr = nil
x = send out_msg, stdout, out, "In stdout" if out_msg

(!stdout || x) && (!stderr || y)
rescue Assertion

This comment has been minimized.

Copy link
@byroot

byroot Nov 24, 2019

Not sure if you added this for explictness, but if not, since Assertion inherits from Exception, the rescue => e won't catch it.

raise
rescue => e
raise UnexpectedError, e
end

##
@@ -485,6 +489,10 @@ def assert_throws sym, msg = nil
end

assert caught, message(msg) { default }
rescue Assertion
raise
rescue => e
raise UnexpectedError, e
end

##
@@ -77,6 +77,14 @@ def assert_triggered expected, klass = Minitest::Assertion
self.send assert_msg, expected, msg
end

def assert_unexpected expected
expected = Regexp.new expected if String === expected

assert_triggered expected, Minitest::UnexpectedError do
yield
end
end

def clean s
s.gsub(/^ {6,10}/, "")
end
@@ -603,12 +611,117 @@ def test_assert_output_triggered_out
end
end

def test_assert_output_without_block
def test_assert_output_no_block
assert_triggered "assert_output requires a block to capture output." do
@tc.assert_output "blah"
end
end

def test_assert_output_nested_assert_uncaught
@assertion_count = 1

assert_triggered "Epic Fail!" do
@tc.assert_output "blah\n" do
puts "blah"
@tc.flunk
end
end
end

def test_assert_output_nested_raise
@assertion_count = 2

@tc.assert_output "blah\n" do
@tc.assert_raises RuntimeError do
puts "blah"
raise "boom!"
end
end
end

def test_assert_output_nested_raise_bad
@assertion_count = 0

assert_unexpected "boom!" do
@tc.assert_raises do # 2) bypassed via UnexpectedError
@tc.assert_output "blah\n" do # 1) captures and raises UnexpectedError
puts "not_blah"
raise "boom!"
end
end
end
end

def test_assert_output_nested_raise_mismatch
# this test is redundant, but illustrative
@assertion_count = 0

assert_unexpected "boom!" do
@tc.assert_raises RuntimeError do # 2) bypassed via UnexpectedError
@tc.assert_output "blah\n" do # 1) captures and raises UnexpectedError
puts "not_blah"
raise ArgumentError, "boom!"
end
end
end
end

def test_assert_output_nested_throw_caught
@assertion_count = 2

@tc.assert_output "blah\n" do
@tc.assert_throws :boom! do
puts "blah"
throw :boom!
end
end
end

def test_assert_output_nested_throw_caught_bad
@assertion_count = 1 # want 0; can't prevent throw from escaping :(

@tc.assert_throws :boom! do # 2) captured via catch
@tc.assert_output "blah\n" do # 1) bypassed via throw
puts "not_blah"
throw :boom!
end
end
end

def test_assert_output_nested_throw_mismatch
@assertion_count = 0

assert_unexpected "uncaught throw :boom!" do
@tc.assert_throws :not_boom! do # 2) captured via assert_throws+rescue
@tc.assert_output "blah\n" do # 1) bypassed via throw
puts "not_blah"
throw :boom!
end
end
end
end

def test_assert_output_uncaught_raise
@assertion_count = 0

assert_unexpected "RuntimeError: boom!" do
@tc.assert_output "blah\n" do
puts "not_blah"
raise "boom!"
end
end
end

def test_assert_output_uncaught_throw
@assertion_count = 0

assert_unexpected "uncaught throw :boom!" do
@tc.assert_output "blah\n" do
puts "not_blah"
throw :boom!
end
end
end
def test_assert_predicate
@tc.assert_predicate "", :empty?
end
@@ -671,6 +784,19 @@ def test_assert_raises_signals
end
end

def test_assert_raises_throw_nested_bad
@assertion_count = 0

assert_unexpected "RuntimeError: boom!" do
@tc.assert_raises do
@tc.assert_throws :blah do
raise "boom!"
throw :not_blah
end
end
end
end

##
# *sigh* This is quite an odd scenario, but it is from real (albeit
# ugly) test code in ruby-core:
@@ -875,7 +1001,9 @@ def test_assert_throws
end

def test_assert_throws_argument_exception
@tc.assert_raises ArgumentError do
@assertion_count = 0

assert_unexpected "ArgumentError" do
@tc.assert_throws :blah do
raise ArgumentError
end
@@ -891,7 +1019,9 @@ def test_assert_throws_different
end

def test_assert_throws_name_error
@tc.assert_raises NameError do
@assertion_count = 0

assert_unexpected "NameError" do
@tc.assert_throws :blah do
raise NameError
end

0 comments on commit 96ba1da

Please sign in to comment.
You can’t perform that action at this time.