-
Notifications
You must be signed in to change notification settings - Fork 8
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
Add Comparison trait. #24
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
56dbc87
Add Comparison trait.
zainab-ali a14ae24
Require `Comparison` instance for `eql` and `same`.
zainab-ali cf84f3a
Remove failing tests.
zainab-ali 08d12df
Add tests.
zainab-ali b5f6290
Add an implicitNotFoundAnnotation.
zainab-ali a20eed9
Add Result.
zainab-ali cf5a7d2
Rename `Failure.diff` to `report`.
zainab-ali 5e7e5b5
Add more docs.
zainab-ali 226d78b
Add a linebreak.
zainab-ali 027c3f4
Correct docs.
zainab-ali 5e28248
Use implicit comparison instance in `expect.same`.
zainab-ali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
modules/core/shared/src/main/scala/weaver/Comparison.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,77 @@ | ||
package weaver | ||
|
||
import cats.Eq | ||
import cats.Show | ||
import com.eed3si9n.expecty._ | ||
import scala.annotation.implicitNotFound | ||
|
||
/** | ||
* A type class used to compare two instances of the same type and construct an | ||
* informative report. | ||
* | ||
* If the comparison succeeds with [[Result.Success]] then no report is printed. | ||
* If the comparison fails with [[Result.Failure]], then the report is printed | ||
* with the test failure. | ||
* | ||
* The report is generally a diff of the `expected` and `found` values. It may | ||
* use ANSI escape codes to add color. | ||
*/ | ||
@implicitNotFound("Could not find an implicit Comparison[${A}]. Does ${A} have an associated cats.Eq[${A}] instance?") | ||
trait Comparison[A] { | ||
|
||
def diff(expected: A, found: A): Comparison.Result | ||
} | ||
|
||
object Comparison { | ||
sealed trait Result | ||
object Result { | ||
case object Success extends Result | ||
case class Failure(report: String) extends Result | ||
} | ||
|
||
/** | ||
* Create a [[Comparison]] instance from an [[cats.kernel.Eq]] implementation. | ||
* | ||
* Uses the [[cats.Show]] instance or [[cats.Show.fromToString]] to construct | ||
* a string diff of the `expected` and `found` values on failure. | ||
*/ | ||
implicit def fromEq[A]( | ||
implicit eqv: Eq[A], | ||
showA: Show[A] = Show.fromToString[A] | ||
): Comparison[A] = { | ||
new Comparison[A] { | ||
def diff(expected: A, found: A): Result = { | ||
if (eqv.eqv(found, expected)) { | ||
Result.Success | ||
} else { | ||
val expectedLines = showA.show(expected).linesIterator.toSeq | ||
val foundLines = showA.show(found).linesIterator.toSeq | ||
val report = DiffUtil | ||
.mkColoredLineDiff(expectedLines, foundLines) | ||
.linesIterator | ||
.toSeq | ||
.map(str => Console.RESET.toString + str) | ||
.mkString("\n") | ||
Result.Failure(report) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Create a [[Comparison]] instance from a `diff` implementation. | ||
*/ | ||
def instance[A](f: (A, A) => Option[String]): Comparison[A] = | ||
new Comparison[A] { | ||
def diff(expected: A, found: A): Result = f(expected, found) match { | ||
case None => Result.Success | ||
case Some(report) => Result.Failure(report) | ||
} | ||
} | ||
|
||
/** | ||
* Create a [[Comparison]] instance from a `diff` implementation. | ||
*/ | ||
def instance[A](f: PartialFunction[(A, A), String]): Comparison[A] = | ||
instance((expected, found) => f.lift((expected, found))) | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This could potentially extend
Eq
as aComparison
instance does imply anEq
instance (and should satisfyEq
laws).I'm not sure if the inheritance would play well with implicit searches.
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.
Interesting question.
I think
Comparison
should not necessarily implyEq
, because you could have comparisons that verify if an instanceA
is contained in anotherA
and I think that'd be a legitimate use. However there could be a more refinedEqComparison
that would implyEq
. In that case, the MTL treatment of having the implication reified as adef eqInstance: Eq[A]
instead of OO-inheritance would probably be the right course of action, in order to avoid implicit search problems.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 an interesting case. In that vein,
Comparison
is a general predicate that has a string representation on failure? It doesn't have any laws associated with it.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, yes