-
Notifications
You must be signed in to change notification settings - Fork 92
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
Extract diff module #756
Extract diff module #756
Conversation
private def exceptionHandlerFromAssertions( | ||
assertions: Assertions, | ||
clues: => Clues | ||
): ComparisonFailExceptionHandler = | ||
new ComparisonFailExceptionHandler { | ||
def handle( | ||
message: String, | ||
obtained: String, | ||
expected: String, | ||
loc: Location | ||
): Nothing = { | ||
assertions.failComparison(message, obtained, expected, clues)(loc) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to extract this from ComparisonFailExceptionHandler
so that the scala-diff
package doesn't need to depend on Assertions
build.sbt
Outdated
.jsConfigure(sharedJSConfigure) | ||
.jsSettings(sharedJSSettings) | ||
|
||
lazy val munitDiff = crossProject(JSPlatform, JVMPlatform, NativePlatform) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally munitDiff would not depend on anything, it should not be about testing really.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I understand it, both diff and munit need things like Location
along with macros that generate it. Do you prefer code duplication over shared core module?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not use Location for diff if possible, seems like it's not really core functionality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can try getting rid of Location
, I'll give it a try. What about AnsiColors
? Should it be a part of diff module?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, since it might be useful for showing the diff.
Extracting munitDiff will break mima anyway. It's probably the last chance to do it. We should work on actual release later on. Any objections? |
I first want to note that the ecosystem is eager for a 1.0 release. However, I wanted to at least bump us to the latest Scala 3 LTS before cutting 1.0. All this to say, I don't have strong objections to the plan here. |
@@ -1,4 +1,4 @@ | |||
package munit.internal.difflib | |||
package munit.diff | |||
|
|||
import munit.Location | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if assertNoDiff
should still be part of this Diffs
object? Each testing library does assertions differently.
It might be better to have something like createReportIfDiff
and use this in Assertions
:
def createReportIfDiff(
obtained: String,
expected: String,
title: String,
printObtainedAsStripMargin: Boolean
): Option[String] = {
if (obtained.isEmpty && !expected.isEmpty) {
val msg =
s"""|Obtained empty output!
|=> Expected:
|$expected""".stripMargin
Some(msg)
} else {
val diff = new Diff(obtained, expected)
if (diff.isEmpty) None
else {
Some(diff.createReport(title, printObtainedAsStripMargin))
}
}
}
However, I'm not sure how we feel about removing assertNoDiff
from here entirely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's totally fine, asserts can live elsewhere
d2a90bd
to
3aad768
Compare
@@ -0,0 +1,13 @@ | |||
package munit.diff | |||
|
|||
class Clue[+T]( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think Clue
should be a type in the diff library. From what I understand, it's been moved here so that we can give it a custom representation in Printers
.
That being said, I haven't thought of alternative approaches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me neither, I did this to satisfy the compiler, I also don't have a better idea yet
@@ -51,10 +54,10 @@ trait Assertions extends MacroCompat.CompileErrorMacro { | |||
clue: => Any = "diff assertion failed" | |||
)(implicit loc: Location): Unit = { | |||
StackTraces.dropInside { | |||
Diffs.assertNoDiff( | |||
DiffsAssetion.assertNoDiff( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DiffsAssetion.assertNoDiff( | |
DiffsAssertion.assertNoDiff( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you fix this one? It's super minor
case x: Float => out.append(x.toString()) | ||
case x: Double => out.append(x.toString()) | ||
case x: String => printString(x, out, printer) | ||
case x: Clues => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we just make Printer a class and add support for Clues in a child class?
Or maybe a custom class like:
trait CustomPrintable{
def append(out: StringBuilder)
}
and make Clue implement it?
def empty: Clues = new Clues(List()) | ||
def fromValue[T](value: T): Clues = new Clues(List(Clue.fromValue(value))) | ||
} | ||
// class Clues(val values: List[Clue[_]]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO remove if we decide to leave Clue in the current position
package munit.diff | ||
|
||
/** | ||
* Don't override this ever |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be great to mention why a bit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer relevant, shouldn't be pushed in the first place 😉
@@ -51,10 +54,10 @@ trait Assertions extends MacroCompat.CompileErrorMacro { | |||
clue: => Any = "diff assertion failed" | |||
)(implicit loc: Location): Unit = { | |||
StackTraces.dropInside { | |||
Diffs.assertNoDiff( | |||
DiffsAssetion.assertNoDiff( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you fix this one? It's super minor
You might want to rebase the PR to see if it passes. |
374926c
to
e728d92
Compare
e728d92
to
f0a004c
Compare
After brainstorming with @zainab-ali and inspired by this commit I did an overhaul of this change. Most of the |
Looks like the compilation is failing, could you take a look? |
I just run |
build.sbt
Outdated
@@ -214,7 +214,7 @@ lazy val munit = crossProject(JSPlatform, JVMPlatform, NativePlatform) | |||
.settings( | |||
sharedSettings, | |||
Compile / unmanagedSourceDirectories ++= | |||
crossBuildingDirectories("munit", "main").value, | |||
crossBuildingDirectories("scala-diff", "main").value, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right, missed it during cleanup
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM from me! Thanks for the great work!
I added 3 more commits:
- rename to munit-diff since we have package munit.diff everywhere so it makes much more sense
- fix mdoc docs generation
- fix mima exceptions
@zainab-ali what do you think?
My next plan is to remove any deprecation we can find and release an RC1
Thanks @majk-p and @tgodzik, the PR itself looks great! Unfortunately the rencent changes to munit include a Native upgrade to I'm brainstorming approaches we could take. Would it be viable to release this on |
I can do that manually later on. Hopefully, this time it will work reasonably. |
changes because munit-diff is now an independent artifact: scalameta/munit#756 * add dependency for munit-diff * adapt imports
This PR aims to extract diff calculation to a separate module to resolve #745
Since diff had to share a bit of code with the rest of munit, I've also extracted
munit-core
that provides the minimal common part.