Skip to content

Commit

Permalink
+ Block-assertions (eg assert_output) now error if raised inside the …
Browse files Browse the repository at this point in the history
…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 96ba1da
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/minitest/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand Down Expand Up @@ -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

##
Expand Down
136 changes: 133 additions & 3 deletions test/minitest/test_minitest_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 96ba1da

Please sign in to comment.