Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Compare: refactor, re-use string representations #879

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading