From 1773ca869687c3dd4390b2f7747d374ad6b72ff1 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 1/3] 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 ec3e058b..40d72498 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" + ) + ) + } + } } From 30e97c3028c8f060e8ab6143e58ac90c7ea0783c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Z=C3=BChlke?= Date: Tue, 16 Apr 2024 09:21:22 +0200 Subject: [PATCH 2/3] Update tests/shared/src/test/scala/munit/FailExceptionSuite.scala Co-authored-by: Tomasz Godzik --- tests/shared/src/test/scala/munit/FailExceptionSuite.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/shared/src/test/scala/munit/FailExceptionSuite.scala b/tests/shared/src/test/scala/munit/FailExceptionSuite.scala index c2b0e1a2..33237a4c 100644 --- a/tests/shared/src/test/scala/munit/FailExceptionSuite.scala +++ b/tests/shared/src/test/scala/munit/FailExceptionSuite.scala @@ -8,11 +8,12 @@ class FailExceptionSuite extends BaseSuite { assert(clue(e).isInstanceOf[Serializable]) } - test("assertion-error no exception") { +test("assertion-error-no-exception") { try { intercept[AssertionError] { println("throwing no exception!") } + throw new Exception("should not reach here") } catch { case e: FailException => assert( @@ -20,6 +21,8 @@ class FailExceptionSuite extends BaseSuite { "expected exception of type 'java.lang.AssertionError' but body evaluated successfully" ) ) + case _: Throwable => + fail("No FailException was thrown") } } } From 767a55388a719cb82ba4d89bc6b07caf6f989242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Zu=CC=88hlke?= Date: Tue, 16 Apr 2024 09:32:46 +0200 Subject: [PATCH 3/3] adopted suggestion from @tgodzik --- munit/shared/src/main/scala/munit/Assertions.scala | 6 ++++-- tests/shared/src/test/scala/munit/FailExceptionSuite.scala | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/munit/shared/src/main/scala/munit/Assertions.scala b/munit/shared/src/main/scala/munit/Assertions.scala index 40d72498..2ac37118 100644 --- a/munit/shared/src/main/scala/munit/Assertions.scala +++ b/munit/shared/src/main/scala/munit/Assertions.scala @@ -196,8 +196,10 @@ trait Assertions extends MacroCompat.CompileErrorMacro { fail(expectedExceptionMsg) } catch { case e: FailExceptionLike[_] - if !T.runtimeClass.isAssignableFrom(e.getClass()) || - e.getMessage.contains(expectedExceptionMsg) => + if !T.runtimeClass.isAssignableFrom(e.getClass()) => + throw e + case e: FailExceptionLike[_] + if 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 33237a4c..1c4ce12c 100644 --- a/tests/shared/src/test/scala/munit/FailExceptionSuite.scala +++ b/tests/shared/src/test/scala/munit/FailExceptionSuite.scala @@ -8,7 +8,7 @@ class FailExceptionSuite extends BaseSuite { assert(clue(e).isInstanceOf[Serializable]) } -test("assertion-error-no-exception") { + test("assertion-error-no-exception") { try { intercept[AssertionError] { println("throwing no exception!")