Skip to content

Commit

Permalink
Add instances for Option and Either
Browse files Browse the repository at this point in the history
  • Loading branch information
jatcwang committed Jul 27, 2021
1 parent bab17d7 commit 5d08d1b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/core/src/main/scala/difflicious/Differ.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ object Differ extends DifferTupleInstances with DifferGen with DifferTimeInstanc
implicit val bigDecimalDiffer: NumericDiffer[BigDecimal] = NumericDiffer.make[BigDecimal]
implicit val bigIntDiffer: NumericDiffer[BigInt] = NumericDiffer.make[BigInt]

implicit def optionDiffer[T: Differ]: Differ[Option[T]] = derived[Option[T]]
implicit def eitherDiffer[A: Differ, B: Differ]: Differ[Either[A, B]] = derived[Either[A, B]]

implicit def mapDiffer[M[_, _], K, V](
implicit keyDiffer: ValueDiffer[K],
valueDiffer: Differ[V],
Expand Down
65 changes: 65 additions & 0 deletions modules/coretest/src/test/scala/difflicious/DifferSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,71 @@ class DifferSpec extends ScalaCheckSuite {
)
}

test("Option: fail if one is Some and one is None") {
assertConsoleDiffOutput(
Differ[Option[CC]],
Some(CC(2, "2", 3.0)),
None,
s"""${R}Some$X != ${G}None$X
|${R}=== Obtained ===
|Some(
| value: CC(
| i: 2,
| s: "2",
| dd: 3.0,
| ),
|)$X
|${G}=== Expected ===
|None(
|)$X""".stripMargin,
)
}

test("Option: isOk == true if two values are equal") {
assertOkIfValuesEqualProp(Differ[Option[CC]])
}

test("Option: isOk == false if two values are NOT equal") {
assertNotOkIfNotEqualProp(Differ[Option[CC]])
}

test("Option: isOk always true if differ is marked ignored") {
assertIsOkIfIgnoredProp(Differ[Option[CC]])
}

test("Either: fail if one is Some and one is None") {
assertConsoleDiffOutput(
Differ[Either[String, CC]],
Right(CC(2, "2", 3.0)),
Left("nope"),
s"""${R}Right$X != ${G}Left$X
|${R}=== Obtained ===
|Right(
| value: CC(
| i: 2,
| s: "2",
| dd: 3.0,
| ),
|)$X
|${G}=== Expected ===
|Left(
| value: "nope",
|)$X""".stripMargin,
)
}

test("Either: isOk == true if two values are equal") {
assertOkIfValuesEqualProp(Differ[Either[String, CC]])
}

test("Either: isOk == false if two values are NOT equal") {
assertNotOkIfNotEqualProp(Differ[Either[String, CC]])
}

test("Either: isOk always true if differ is marked ignored") {
assertIsOkIfIgnoredProp(Differ[Either[String, CC]])
}

test("Map: isOk == true if two values are equal") {
assertOkIfValuesEqualProp(Differ.mapDiffer[Map, MapKey, CC])
}
Expand Down

0 comments on commit 5d08d1b

Please sign in to comment.