From 3c59ad169513b9e794dfad427b5c36ebd2317cb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Zu=CC=88hlke?= Date: Tue, 1 Aug 2023 08:52:10 +0200 Subject: [PATCH] fix intercept for AssertionError When trying to `intercept`and `AssertionError` till now the test passed successfully. This fix treats the FailException special. --- .../shared/src/main/scala/munit/Assertions.scala | 9 +++++---- .../src/test/scala/munit/FailExceptionSuite.scala | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/munit/shared/src/main/scala/munit/Assertions.scala b/munit/shared/src/main/scala/munit/Assertions.scala index fea92e7a..ef03b49d 100644 --- a/munit/shared/src/main/scala/munit/Assertions.scala +++ b/munit/shared/src/main/scala/munit/Assertions.scala @@ -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())) { diff --git a/tests/shared/src/test/scala/munit/FailExceptionSuite.scala b/tests/shared/src/test/scala/munit/FailExceptionSuite.scala index 0fcfcfae..c2b0e1a2 100644 --- a/tests/shared/src/test/scala/munit/FailExceptionSuite.scala +++ b/tests/shared/src/test/scala/munit/FailExceptionSuite.scala @@ -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" + ) + ) + } + } }