Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Compare: use implicit Location #881

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
29 changes: 22 additions & 7 deletions munit/shared/src/main/scala/munit/Compare.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ trait Compare[A, B] {
obtained: A,
expected: B,
title: Any,
loc: Location,
assertions: Assertions,
): Nothing = {
)(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 +62,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 +74,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 +86,20 @@ trait Compare[A, B] {
s"values are not equal, even if $why: $obtained",
obtained,
expected,
)(loc)
)
}

// for MIMA compatibility
@deprecated("Use version with implicit Location", "1.0.4")
protected def failEqualsComparison(
obtained: A,
expected: B,
title: Any,
loc: Location,
assertions: Assertions,
): Nothing = {
implicit val _loc: Location = loc
failEqualsComparison(obtained, expected, title, assertions)
}

}
Expand Down
Loading