-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sets diff context size and optionally outputs line offsets. This is part 2, following #881.
- Loading branch information
Showing
10 changed files
with
292 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
munit-diff/shared/src/main/scala/munit/diff/DiffOptions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package munit.diff | ||
|
||
class DiffOptions private ( | ||
val contextSize: Int, | ||
val showLines: Boolean, | ||
val obtainedAsStripMargin: Boolean, | ||
) { | ||
private def privateCopy( | ||
contextSize: Int = this.contextSize, | ||
showLines: Boolean = this.showLines, | ||
obtainedAsStripMargin: Boolean = this.obtainedAsStripMargin, | ||
): DiffOptions = new DiffOptions( | ||
contextSize = contextSize, | ||
showLines = showLines, | ||
obtainedAsStripMargin = obtainedAsStripMargin, | ||
) | ||
|
||
def withContextSize(value: Int): DiffOptions = privateCopy(contextSize = value) | ||
def withShowLines(value: Boolean): DiffOptions = privateCopy(showLines = value) | ||
def withObtainedAsStripMargin(value: Boolean): DiffOptions = | ||
privateCopy(obtainedAsStripMargin = value) | ||
} | ||
|
||
object DiffOptions | ||
extends DiffOptions( | ||
contextSize = 1, | ||
showLines = false, | ||
obtainedAsStripMargin = false, | ||
) { | ||
implicit val default: DiffOptions = this | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,39 @@ | ||
package munit | ||
|
||
import munit.diff.Diff | ||
import munit.diff.DiffOptions | ||
|
||
object Diffs { | ||
|
||
// for MIMA compatibility | ||
@deprecated("Use version with implicit DiffOptions", "1.0.4") | ||
def assertNoDiff( | ||
obtained: String, | ||
expected: String, | ||
handler: ComparisonFailExceptionHandler, | ||
title: String, | ||
printObtainedAsStripMargin: Boolean, | ||
)(implicit loc: Location): Boolean = { | ||
if (obtained.isEmpty && !expected.isEmpty) { | ||
implicit val diffOptions: DiffOptions = DiffOptions | ||
.withObtainedAsStripMargin(printObtainedAsStripMargin) | ||
assertNoDiff(obtained, expected, handler, title) | ||
} | ||
|
||
def assertNoDiff( | ||
obtained: String, | ||
expected: String, | ||
handler: ComparisonFailExceptionHandler, | ||
title: String, | ||
)(implicit loc: Location, options: DiffOptions): Boolean = { | ||
if (obtained.isEmpty && expected.nonEmpty) { | ||
val msg = s"""|Obtained empty output! | ||
|=> Expected: | ||
|$expected""".stripMargin | ||
handler.handle(msg, obtained, expected, loc) | ||
} | ||
val diff = new Diff(obtained, expected) | ||
val diff = Diff(obtained, expected) | ||
if (diff.isEmpty) true | ||
else handler.handle( | ||
diff.createReport(title, printObtainedAsStripMargin), | ||
obtained, | ||
expected, | ||
loc, | ||
) | ||
else handler.handle(diff.createReport(title), obtained, expected, loc) | ||
} | ||
|
||
} |
Oops, something went wrong.