Skip to content

Commit

Permalink
Compare: use implicit Location
Browse files Browse the repository at this point in the history
Part 1 of 2 in modifying assert failure output.
  • Loading branch information
kitbellew committed Jan 20, 2025
1 parent 15df253 commit 3125eeb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion munit/shared/src/main/scala/munit/Assertions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ trait Assertions extends MacroCompat.CompileErrorMacro {
)
case _ =>
}
compare.failEqualsComparison(obtained, expected, clue, loc, this)
compare.failEqualsComparison(obtained, expected, clue, this)
}
}

Expand Down
25 changes: 20 additions & 5 deletions munit/shared/src/main/scala/munit/Compare.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,35 @@ trait Compare[A, B] {
* @return should ideally throw a org.junit.ComparisonFailException in order
* to support the IntelliJ diff viewer.
*/
// for source/binary compatibility, preserve old signature
@deprecated("Use version with implicit Location", "1.0.4")
def failEqualsComparison(
obtained: A,
expected: B,
title: Any,
loc: Location,
assertions: Assertions,
): Nothing = {
implicit val _loc: Location = loc
failEqualsComparison(obtained, expected, title, assertions)
}

def failEqualsComparison(
obtained: A,
expected: 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 = assertions.failComparison(message, obtained, expected)(loc)
_loc: Location,
): Nothing = {
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.
Expand All @@ -60,7 +75,7 @@ trait Compare[A, B] {
diffHandler,
title = assertions.munitPrint(title),
printObtainedAsStripMargin = false,
)(loc)
)

// Attempt 2: try with `.toString` in case `munitPrint()` produces identical
// formatting for both values.
Expand All @@ -72,7 +87,7 @@ trait Compare[A, B] {
diffHandler,
title = assertions.munitPrint(title),
printObtainedAsStripMargin = false,
)(loc)
)

// Attempt 3: string comparison is not working, unconditionally fail the test.
val why =
Expand All @@ -84,7 +99,7 @@ trait Compare[A, B] {
s"values are not equal, even if $why: $obtained",
obtained,
expected,
)(loc)
)
}

}
Expand Down

0 comments on commit 3125eeb

Please sign in to comment.