-
Notifications
You must be signed in to change notification settings - Fork 14
/
AutoDerivedSuite.scala
67 lines (54 loc) · 3.02 KB
/
AutoDerivedSuite.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package io.circe.magnolia
import io.circe.magnolia.derivation.decoder.auto._
import io.circe.magnolia.derivation.encoder.auto._
import io.circe.testing.CodecTests
import io.circe.tests.CirceSuite
import io.circe.tests.examples._
import io.circe.{Decoder, Encoder, Json}
import shapeless.tag
import shapeless.tag.@@
import shapeless.test.illTyped
class AutoDerivedSuite extends CirceSuite {
import AutoDerivedSuiteInputs._
// TODO: All these imports are temporary workaround for https://github.com/propensive/magnolia/issues/89
import Encoder._
import Decoder._
private implicit val encodeStringTag: Encoder[String @@ Tag] = Encoder[String].narrow
private implicit val decodeStringTag: Decoder[String @@ Tag] = Decoder[String].map(tag[Tag](_))
checkLaws("Codec[Tuple1[Int]]", CodecTests[Tuple1[Int]].unserializableCodec)
checkLaws("Codec[(Int, Int, Foo)]", CodecTests[(Int, Int, Foo)].unserializableCodec)
checkLaws("Codec[Qux[Int]]", CodecTests[Qux[Int]].unserializableCodec)
checkLaws("Codec[Seq[Foo]]", CodecTests[Seq[Foo]].unserializableCodec)
checkLaws("Codec[Baz]", CodecTests[Baz].unserializableCodec)
checkLaws("Codec[Foo]", CodecTests[Foo].unserializableCodec)
checkLaws("Codec[OuterCaseClassExample]", CodecTests[OuterCaseClassExample].unserializableCodec)
checkLaws("Codec[RecursiveAdtExample]", CodecTests[RecursiveAdtExample].unserializableCodec)
checkLaws("Codec[RecursiveWithOptionExample]", CodecTests[RecursiveWithOptionExample].unserializableCodec)
checkLaws("Codec[RecursiveWithListExample]", CodecTests[RecursiveWithListExample].unserializableCodec)
checkLaws("Codec[AnyValInside]", CodecTests[AnyValInside].unserializableCodec)
"A generically derived codec" should "not interfere with base instances" in forAll { (is: List[Int]) =>
val json = Encoder[List[Int]].apply(is)
assert(json === Json.fromValues(is.map(Json.fromInt)) && json.as[List[Int]] === Right(is))
}
it should "not be derived for Object" in {
illTyped("Decoder[Object]")
illTyped("Encoder[Object]")
}
it should "not be derived for AnyRef" in {
illTyped("Decoder[AnyRef]")
illTyped("Encoder[AnyRef]")
}
"Generic decoders" should "not interfere with defined decoders" in forAll { (xs: List[String]) =>
val json = Json.obj("SubtypeWithExplicitInstance" -> Json.fromValues(xs.map(Json.fromString)))
val ch = Decoder[Sealed].apply(json.hcursor)
val res = ch === Right(SubtypeWithExplicitInstance(xs): Sealed)
assert(res)
}
"Generic encoders" should "not interfere with defined encoders" in forAll { (xs: List[String]) =>
val json = Json.obj("SubtypeWithExplicitInstance" -> Json.fromValues(xs.map(Json.fromString)))
assert(Encoder[Sealed].apply(SubtypeWithExplicitInstance(xs): Sealed) === json)
}
// TODO: tagged types don't work ATM, might be related to https://github.com/propensive/magnolia/issues/89
// checkLaws("Codec[WithTaggedMembers]", CodecTests[WithTaggedMembers].unserializableCodec)
checkLaws("Codec[Seq[WithSeqOfTagged]]", CodecTests[Seq[WithSeqOfTagged]].unserializableCodec)
}