Skip to content

Commit b837d40

Browse files
authored
Merge pull request #14 from jatcwang/option_either_instances
Add instances for Option and Either
2 parents bab17d7 + 5d08d1b commit b837d40

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

modules/core/src/main/scala/difflicious/Differ.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ object Differ extends DifferTupleInstances with DifferGen with DifferTimeInstanc
7272
implicit val bigDecimalDiffer: NumericDiffer[BigDecimal] = NumericDiffer.make[BigDecimal]
7373
implicit val bigIntDiffer: NumericDiffer[BigInt] = NumericDiffer.make[BigInt]
7474

75+
implicit def optionDiffer[T: Differ]: Differ[Option[T]] = derived[Option[T]]
76+
implicit def eitherDiffer[A: Differ, B: Differ]: Differ[Either[A, B]] = derived[Either[A, B]]
77+
7578
implicit def mapDiffer[M[_, _], K, V](
7679
implicit keyDiffer: ValueDiffer[K],
7780
valueDiffer: Differ[V],

modules/coretest/src/test/scala/difflicious/DifferSpec.scala

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,71 @@ class DifferSpec extends ScalaCheckSuite {
121121
)
122122
}
123123

124+
test("Option: fail if one is Some and one is None") {
125+
assertConsoleDiffOutput(
126+
Differ[Option[CC]],
127+
Some(CC(2, "2", 3.0)),
128+
None,
129+
s"""${R}Some$X != ${G}None$X
130+
|${R}=== Obtained ===
131+
|Some(
132+
| value: CC(
133+
| i: 2,
134+
| s: "2",
135+
| dd: 3.0,
136+
| ),
137+
|)$X
138+
|${G}=== Expected ===
139+
|None(
140+
|)$X""".stripMargin,
141+
)
142+
}
143+
144+
test("Option: isOk == true if two values are equal") {
145+
assertOkIfValuesEqualProp(Differ[Option[CC]])
146+
}
147+
148+
test("Option: isOk == false if two values are NOT equal") {
149+
assertNotOkIfNotEqualProp(Differ[Option[CC]])
150+
}
151+
152+
test("Option: isOk always true if differ is marked ignored") {
153+
assertIsOkIfIgnoredProp(Differ[Option[CC]])
154+
}
155+
156+
test("Either: fail if one is Some and one is None") {
157+
assertConsoleDiffOutput(
158+
Differ[Either[String, CC]],
159+
Right(CC(2, "2", 3.0)),
160+
Left("nope"),
161+
s"""${R}Right$X != ${G}Left$X
162+
|${R}=== Obtained ===
163+
|Right(
164+
| value: CC(
165+
| i: 2,
166+
| s: "2",
167+
| dd: 3.0,
168+
| ),
169+
|)$X
170+
|${G}=== Expected ===
171+
|Left(
172+
| value: "nope",
173+
|)$X""".stripMargin,
174+
)
175+
}
176+
177+
test("Either: isOk == true if two values are equal") {
178+
assertOkIfValuesEqualProp(Differ[Either[String, CC]])
179+
}
180+
181+
test("Either: isOk == false if two values are NOT equal") {
182+
assertNotOkIfNotEqualProp(Differ[Either[String, CC]])
183+
}
184+
185+
test("Either: isOk always true if differ is marked ignored") {
186+
assertIsOkIfIgnoredProp(Differ[Either[String, CC]])
187+
}
188+
124189
test("Map: isOk == true if two values are equal") {
125190
assertOkIfValuesEqualProp(Differ.mapDiffer[Map, MapKey, CC])
126191
}

0 commit comments

Comments
 (0)