/
PrinterTests.scala
51 lines (44 loc) · 1.38 KB
/
PrinterTests.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package io.circe.testing
import cats.instances.option._
import cats.kernel.Eq
import cats.kernel.laws.SerializableLaws
import cats.laws._
import cats.laws.discipline._
import io.circe.{ Decoder, Encoder, Parser, Printer }
import org.scalacheck.{ Arbitrary, Prop, Shrink }
import org.typelevel.discipline.Laws
trait PrinterLaws[A] {
def decode: Decoder[A]
def encode: Encoder[A]
def printerRoundTrip(printer: Printer, parser: Parser, a: A): IsEq[Option[A]] =
parser.decode(printer.pretty(encode(a)))(decode).right.toOption <-> Some(a)
}
object PrinterLaws {
def apply[A](implicit decodeA: Decoder[A], encodeA: Encoder[A]): PrinterLaws[A] =
new PrinterLaws[A] {
val decode: Decoder[A] = decodeA
val encode: Encoder[A] = encodeA
}
}
trait PrinterTests[A] extends Laws {
def laws: PrinterLaws[A]
def printer(printer: Printer, parser: Parser)(implicit
arbitraryA: Arbitrary[A],
shrinkA: Shrink[A],
eqA: Eq[A]
): RuleSet =
new DefaultRuleSet(
name = "printer",
parent = None,
"roundTrip" -> Prop.forAll { (a: A) =>
laws.printerRoundTrip(printer, parser, a)
},
"printer serializability" -> SerializableLaws.serializable(printer)
)
}
object PrinterTests {
def apply[A](implicit decodeA: Decoder[A], encodeA: Encoder[A]): PrinterTests[A] =
new PrinterTests[A] {
val laws: PrinterLaws[A] = PrinterLaws[A]
}
}