Skip to content

Commit

Permalink
Use implicit comparison instance in expect.same.
Browse files Browse the repository at this point in the history
  • Loading branch information
zainab-ali committed Apr 8, 2024
1 parent 027c3f4 commit 5e28248
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ private[weaver] trait ExpectSame {
def same[A](
expected: A,
found: A)(
implicit loc: SourceLocation): Expectations = eql(expected, found)(
Comparison.fromEq(Eq.fromUniversalEquals, Show.fromToString),
loc)
implicit comparisonA: Comparison[A] =
Comparison.fromEq[A](Eq.fromUniversalEquals, Show.fromToString),
loc: SourceLocation): Expectations =
eql(expected, found)(comparisonA, loc)
}
29 changes: 27 additions & 2 deletions modules/framework-cats/shared/src/test/scala/DogFoodTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,36 @@ object DogFoodTests extends IOSuite {
case (logs, _) =>
val actual =
extractLogEventAfterFailures(logs) {
case LoggedEvent.Error(msg) if msg.contains("(Comparison)") => msg
case LoggedEvent.Error(msg) if msg.contains("(eql Comparison)") =>
msg
}.get

val expected = """
|- (Comparison) 0ms
|- (eql Comparison) 0ms
| Values not equal: (src/main/DogFoodTests.scala:5)
|
| Foo { | Foo {
| s: foo | s: foo
| i: [1] | i: [2]
| } | }
""".stripMargin.trim

expect.same(actual, expected)
}
}

test(
"expect.same delegates to Comparison show when an instance is found") {
_.runSuite(Meta.Rendering).map {
case (logs, _) =>
val actual =
extractLogEventAfterFailures(logs) {
case LoggedEvent.Error(msg) if msg.contains("(same Comparison)") =>
msg
}.get

val expected = """
|- (same Comparison) 0ms
| Values not equal: (src/main/DogFoodTests.scala:5)
|
| Foo { | Foo {
Expand Down
23 changes: 13 additions & 10 deletions modules/framework-cats/shared/src/test/scala/Meta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,28 @@ object Meta {
cancel("I was cancelled :(")
}

pureTest("(Comparison)") {
import cats.Show
case class Foo(s: String, i: Int)
object Foo {
val show: Show[Foo] = Show.show[Foo] {
case Foo(s, i) =>
s"""
import cats.Show
case class Foo(s: String, i: Int)
object Foo {
val show: Show[Foo] = Show.show[Foo] {
case Foo(s, i) =>
s"""
|Foo {
| s: ${Show[String].show(s)}
| i: ${Show[Int].show(i)}
|}
""".stripMargin.trim()
}
implicit val comparison: Comparison[Foo] =
Comparison.fromEq[Foo](cats.Eq.fromUniversalEquals, show)
}
implicit val comparison: Comparison[Foo] =
Comparison.fromEq[Foo](cats.Eq.fromUniversalEquals, show)
}

pureTest("(eql Comparison)") {
expect.eql(Foo("foo", 1), Foo("foo", 2))
}
pureTest("(same Comparison)") {
expect.same(Foo("foo", 1), Foo("foo", 2))
}
}

object FailingTestStatusReporting extends SimpleIOSuite {
Expand Down

0 comments on commit 5e28248

Please sign in to comment.