Skip to content

Commit

Permalink
Add Result.
Browse files Browse the repository at this point in the history
  • Loading branch information
zainab-ali committed Apr 4, 2024
1 parent b5f6290 commit a20eed9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
37 changes: 32 additions & 5 deletions modules/core/shared/src/main/scala/weaver/Comparison.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@ import scala.annotation.implicitNotFound

@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): Option[String]
def diff(expected: A, found: A): Comparison.Result
}

object Comparison {
implicit def fromEqAndShow[A](
sealed trait Result
object Result {
case object Success extends Result
case class Failure(diff: String) extends Result
}

/**
* Create a `Comparison` instance from an `Eq` implementation. Uses the
* default `Show.fromToString` when no implicit `Show` instance is found.
*/
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): Option[String] = {
def diff(expected: A, found: A): Result = {
if (eqv.eqv(found, expected)) {
None
Result.Success
} else {
val expectedLines = showA.show(expected).linesIterator.toSeq
val foundLines = showA.show(found).linesIterator.toSeq
Expand All @@ -28,9 +38,26 @@ object Comparison {
.toSeq
.map(str => Console.RESET.toString + str)
.mkString("\n")
Some(diff)
Result.Failure(diff)
}
}
}
}

/**
* Create a `Comparison` instance from an `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(diff) => Result.Failure(diff)
}
}

/**
* Create a `Comparison` instance from an `diff` implementation.
*/
def instance[A](f: PartialFunction[(A, A), String]): Comparison[A] =
instance((expected, found) => f.lift((expected, found)))
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ private[weaver] trait ExpectSame {
implicit comparisonA: Comparison[A],
loc: SourceLocation): Expectations = {
comparisonA.diff(expected, found) match {
case None => Expectations(Validated.validNel(()))
case Some(diff) =>
case Comparison.Result.Success => Expectations(Validated.validNel(()))
case Comparison.Result.Failure(diff) =>
val header = "Values not equal:"
val sourceLocs = NonEmptyList.of(loc)
Expectations(
Expand All @@ -30,6 +30,6 @@ private[weaver] trait ExpectSame {
expected: A,
found: A)(
implicit loc: SourceLocation): Expectations = eql(expected, found)(
Comparison.fromEqAndShow(Eq.fromUniversalEquals, Show.fromToString),
Comparison.fromEq(Eq.fromUniversalEquals, Show.fromToString),
loc)
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ object ExpectationsTests extends SimpleIOSuite {
}

pureTest("expect.eql respects weaver.Comparison") {
implicit val comparison: Comparison[Int] =
Comparison.fromEqAndShow(Eq.allEqual)
implicit val comparison: Comparison[Int] = Comparison.fromEq(Eq.allEqual)
expect.eql(0, 1)
}

Expand Down
4 changes: 2 additions & 2 deletions modules/framework-cats/shared/src/test/scala/Meta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ object Meta {
|}
""".stripMargin.trim()
}
implicit val cmp: Comparison[Foo] =
Comparison.fromEqAndShow[Foo](cats.Eq.fromUniversalEquals, show)
implicit val comparison: Comparison[Foo] =
Comparison.fromEq[Foo](cats.Eq.fromUniversalEquals, show)
}

expect.eql(Foo("foo", 1), Foo("foo", 2))
Expand Down

0 comments on commit a20eed9

Please sign in to comment.