From 5d08d1b8736f718c751966dc48849cdffbf716ee Mon Sep 17 00:00:00 2001 From: Jacob Wang Date: Tue, 27 Jul 2021 21:57:46 +0100 Subject: [PATCH] Add instances for Option and Either --- .../src/main/scala/difflicious/Differ.scala | 3 + .../test/scala/difflicious/DifferSpec.scala | 65 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/modules/core/src/main/scala/difflicious/Differ.scala b/modules/core/src/main/scala/difflicious/Differ.scala index 5a51f4d..e95f276 100644 --- a/modules/core/src/main/scala/difflicious/Differ.scala +++ b/modules/core/src/main/scala/difflicious/Differ.scala @@ -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], diff --git a/modules/coretest/src/test/scala/difflicious/DifferSpec.scala b/modules/coretest/src/test/scala/difflicious/DifferSpec.scala index 2b4fa79..5a84a89 100644 --- a/modules/coretest/src/test/scala/difflicious/DifferSpec.scala +++ b/modules/coretest/src/test/scala/difflicious/DifferSpec.scala @@ -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]) }