Skip to content

Commit

Permalink
Compare: refactor, re-use string representations
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 19, 2025
1 parent e9e548e commit 9d52271
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
30 changes: 16 additions & 14 deletions munit/shared/src/main/scala/munit/Compare.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 """..."""
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: }
Expand Down

0 comments on commit 9d52271

Please sign in to comment.