Skip to content

Commit

Permalink
fix intercept for AssertionError
Browse files Browse the repository at this point in the history
When trying to `intercept`and `AssertionError` till now the test passed successfully.
This fix treats the FailException special.
  • Loading branch information
mzuehlke authored and tgodzik committed Apr 16, 2024
1 parent ec2ee68 commit 3c59ad1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 5 additions & 4 deletions munit/shared/src/main/scala/munit/Assertions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,15 @@ trait Assertions extends MacroCompat.CompileErrorMacro {
expectedExceptionMessage: Option[String],
body: => Any
)(implicit T: ClassTag[T], loc: Location): T = {
val expectedExceptionMsg =
s"expected exception of type '${T.runtimeClass.getName()}' but body evaluated successfully"
try {
body
fail(
s"expected exception of type '${T.runtimeClass.getName()}' but body evaluated successfully"
)
fail(expectedExceptionMsg)
} catch {
case e: FailExceptionLike[_]
if !T.runtimeClass.isAssignableFrom(e.getClass()) =>
if !T.runtimeClass.isAssignableFrom(e.getClass()) ||
e.getMessage.contains(expectedExceptionMsg) =>
throw e
case NonFatal(e) =>
if (T.runtimeClass.isAssignableFrom(e.getClass())) {
Expand Down
15 changes: 15 additions & 0 deletions tests/shared/src/test/scala/munit/FailExceptionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,19 @@ class FailExceptionSuite extends BaseSuite {
}
assert(clue(e).isInstanceOf[Serializable])
}

test("assertion-error no exception") {
try {
intercept[AssertionError] {
println("throwing no exception!")
}
} catch {
case e: FailException =>
assert(
e.getMessage.contains(
"expected exception of type 'java.lang.AssertionError' but body evaluated successfully"
)
)
}
}
}

0 comments on commit 3c59ad1

Please sign in to comment.