From 9d52271cfd3429bff208dcfbde68e866ad6ced2c Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sat, 18 Jan 2025 21:16:10 -0800 Subject: [PATCH] Compare: refactor, re-use string representations --- .../shared/src/main/scala/munit/Compare.scala | 30 ++++++++++--------- .../munit/AssertionsFrameworkSuite.scala | 4 +-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/munit/shared/src/main/scala/munit/Compare.scala b/munit/shared/src/main/scala/munit/Compare.scala index 8d6af568..4208f931 100644 --- a/munit/shared/src/main/scala/munit/Compare.scala +++ b/munit/shared/src/main/scala/munit/Compare.scala @@ -8,11 +8,11 @@ import scala.annotation.implicitNotFound * By default, uses == and allows comparison between any two types as long * they have a supertype/subtype relationship. For example: * - * - Compare[T, T] OK - * - Compare[Some[Int], Option[Int]] OK, subtype - * - Compare[Option[Int], Some[Int]] OK, supertype - * - Compare[List[Int], collection.Seq[Int]] OK, subtype - * - Compare[List[Int], Vector[Int]] Error, requires upcast to `Seq[Int]` + * - `Compare[T, T]` OK + * - `Compare[Some[Int], Option[Int]]` OK, subtype + * - `Compare[Option[Int], Some[Int]]` OK, supertype + * - `Compare[List[Int], collection.Seq[Int]]` OK, subtype + * - `Compare[List[Int], Vector[Int]]` Error, requires upcast to Seq[Int]` */ @implicitNotFound( // NOTE: Dotty ignores this message if the string is formatted as a multiline string """...""" @@ -64,22 +64,24 @@ trait Compare[A, B] { // Attempt 2: try with `.toString` in case `munitPrint()` produces identical // formatting for both values. + val obtainedStr = obtained.toString + val expectedStr = expected.toString Diffs.assertNoDiff( - obtained.toString(), - expected.toString(), + obtainedStr, + expectedStr, diffHandler, title = assertions.munitPrint(title), printObtainedAsStripMargin = false, )(loc) // Attempt 3: string comparison is not working, unconditionally fail the test. - if (obtained.toString() == expected.toString()) assertions.failComparison( - s"values are not equal even if they have the same `toString()`: $obtained", - obtained, - expected, - )(loc) - else assertions.failComparison( - s"values are not equal, even if their text representation only differs in leading/trailing whitespace and ANSI escape characters: $obtained", + val why = + if (obtainedStr == expectedStr) "they have the same `toString()`" + else + "their text representation only differs in leading/trailing whitespace and ANSI escape characters" + + assertions.failComparison( + s"values are not equal, even if $why: $obtained", obtained, expected, )(loc) diff --git a/tests/shared/src/main/scala/munit/AssertionsFrameworkSuite.scala b/tests/shared/src/main/scala/munit/AssertionsFrameworkSuite.scala index 16c170ee..7fdfa944 100644 --- a/tests/shared/src/main/scala/munit/AssertionsFrameworkSuite.scala +++ b/tests/shared/src/main/scala/munit/AssertionsFrameworkSuite.scala @@ -43,11 +43,11 @@ class AssertionsFrameworkSuite extends FunSuite { object AssertionsFrameworkSuite extends FrameworkTest( classOf[AssertionsFrameworkSuite], - """|==> failure munit.AssertionsFrameworkSuite.equal-tostring - tests/shared/src/main/scala/munit/AssertionsFrameworkSuite.scala:11 values are not equal even if they have the same `toString()`: C + """|==> failure munit.AssertionsFrameworkSuite.equal-tostring - tests/shared/src/main/scala/munit/AssertionsFrameworkSuite.scala:11 values are not equal, even if they have the same `toString()`: C |10: } |11: assertEquals[Any, Any](new A(), new B()) |12: } - |==> failure munit.AssertionsFrameworkSuite.case-class-productPrefix - tests/shared/src/main/scala/munit/AssertionsFrameworkSuite.scala:21 values are not equal even if they have the same `toString()`: A() + |==> failure munit.AssertionsFrameworkSuite.case-class-productPrefix - tests/shared/src/main/scala/munit/AssertionsFrameworkSuite.scala:21 values are not equal, even if they have the same `toString()`: A() |20: } |21: assertEquals[Any, Any](a.A(), b.A()) |22: }