From 721ae807c1cc259fb2e64836e5429aafb779f556 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 19 Jan 2025 10:04:21 -0800 Subject: [PATCH] ComparisonFailExceptionHandler: instantiate w/ SAM --- munit/shared/src/main/scala/munit/Assertions.scala | 11 ++--------- munit/shared/src/main/scala/munit/Compare.scala | 10 ++-------- .../main/scala/munit/StackTraceFrameworkSuite.scala | 2 +- 3 files changed, 5 insertions(+), 18 deletions(-) diff --git a/munit/shared/src/main/scala/munit/Assertions.scala b/munit/shared/src/main/scala/munit/Assertions.scala index d8e62cfb..d69a5ad6 100644 --- a/munit/shared/src/main/scala/munit/Assertions.scala +++ b/munit/shared/src/main/scala/munit/Assertions.scala @@ -252,15 +252,8 @@ trait Assertions extends MacroCompat.CompileErrorMacro { private def exceptionHandlerFromAssertions( assertions: Assertions, clues: => Clues, - ): ComparisonFailExceptionHandler = new ComparisonFailExceptionHandler { - def handle( - message: String, - obtained: String, - expected: String, - loc: Location, - ): Nothing = assertions - .failComparison(message, obtained, expected, clues)(loc) - } + ): ComparisonFailExceptionHandler = + assertions.failComparison(_, _, _, clues)(_) private val munitCapturedClues: mutable.ListBuffer[Clue[_]] = mutable.ListBuffer.empty diff --git a/munit/shared/src/main/scala/munit/Compare.scala b/munit/shared/src/main/scala/munit/Compare.scala index 0fa91eb6..8f383788 100644 --- a/munit/shared/src/main/scala/munit/Compare.scala +++ b/munit/shared/src/main/scala/munit/Compare.scala @@ -56,16 +56,10 @@ trait Compare[A, B] { title: Any, assertions: Assertions, )(implicit loc: Location): Nothing = { - val diffHandler = new ComparisonFailExceptionHandler { - override def handle( - message: String, - _obtained: String, - _expected: String, - _loc: Location, - ): Nothing = { + val diffHandler: ComparisonFailExceptionHandler = { + (message: String, _obtained: String, _expected: String, _loc: Location) => implicit val loc: Location = _loc assertions.failComparison(message, obtained, expected) - } } // Attempt 1: custom pretty-printer that produces multiline output, which is // optimized for line-by-line diffing. diff --git a/tests/shared/src/main/scala/munit/StackTraceFrameworkSuite.scala b/tests/shared/src/main/scala/munit/StackTraceFrameworkSuite.scala index 88056439..142d42d5 100644 --- a/tests/shared/src/main/scala/munit/StackTraceFrameworkSuite.scala +++ b/tests/shared/src/main/scala/munit/StackTraceFrameworkSuite.scala @@ -25,7 +25,7 @@ object FullStackTraceFrameworkSuite """|at munit.Assertions:failComparison | at munit.Assertions:failComparison$ | at munit.FunSuite:failComparison - | at munit.Assertions$$anon$1:handle + | at munit.Assertions:$anonfun$exceptionHandlerFromAssertions$1 |==> failure munit.StackTraceFrameworkSuite.fail - tests/shared/src/main/scala/munit/StackTraceFrameworkSuite.scala:4 |3:class StackTraceFrameworkSuite extends FunSuite { |4: test("fail")(assertNoDiff("a", "b"))