diff options
author | Jakob Odersky <jakob@odersky.com> | 2018-03-08 16:13:03 -0800 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2018-03-08 16:21:49 -0800 |
commit | 5ef502abc058358ec3a329c774bb42b9a7bd106f (patch) | |
tree | ee5c20fa79307e1a8397a86f31fbe54cbfe9fe1a /src/test/scala/CoproductTypeFormatTests.scala | |
parent | eb1ad3c956c828d421b7650dd3b01d5129a41a3d (diff) | |
download | spray-json-derivation-59a39a04a1c813e27c5c738185c1a5e0edf3f913.tar.gz spray-json-derivation-59a39a04a1c813e27c5c738185c1a5e0edf3f913.tar.bz2 spray-json-derivation-59a39a04a1c813e27c5c738185c1a5e0edf3f913.zip |
Don't ake derived formats implicit by defaultv0.3.0
Diffstat (limited to 'src/test/scala/CoproductTypeFormatTests.scala')
-rw-r--r-- | src/test/scala/CoproductTypeFormatTests.scala | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/test/scala/CoproductTypeFormatTests.scala b/src/test/scala/CoproductTypeFormatTests.scala new file mode 100644 index 0000000..ec9b3f5 --- /dev/null +++ b/src/test/scala/CoproductTypeFormatTests.scala @@ -0,0 +1,81 @@ +package xyz.driver.json + +import spray.json._ + +import org.scalatest._ + +class CoproductTypeFormatTests + extends FlatSpec + with FormatTests + with DefaultJsonProtocol + with DerivedFormats { + + sealed trait Expr + case class Zero() extends Expr + case class Value(x: Int) extends Expr + case class Plus(lhs: Expr, rhs: Expr) extends Expr + case object One extends Expr + + implicit val exprFormat: RootJsonFormat[Expr] = jsonFormat[Expr] + + "No-parameter case class child" should behave like checkRoundtrip[Expr]( + Zero(), + """{"type":"Zero"}""" + ) + + "Simple parameter case class child" should behave like checkRoundtrip[Expr]( + Value(42), + """{"type":"Value","x":42}""" + ) + + "Nested parameter case class child" should behave like checkRoundtrip[Expr]( + Plus(Value(42), One), + """{"type":"Plus","lhs":{"type":"Value","x":42},"rhs":{"type":"One"}}""" + ) + + "Case object child" should behave like checkRoundtrip[Expr]( + One, + """{"type": "One"}""" + ) + + @gadt("kind") + sealed abstract class Keyword(`type`: String) + case class If(`type`: String) extends Keyword(`type`) + + implicit val keywordFormat: RootJsonFormat[Keyword] = jsonFormat[Keyword] + + "GADT with type field alias" should behave like checkRoundtrip[Keyword]( + If("class"), + """{"kind":"If","type":"class"}""" + ) + + @gadt("""_`crazy type!`"""") + sealed abstract trait Crazy + case class CrazyType() extends Crazy + + implicit val crazyFormat: RootJsonFormat[Crazy] = jsonFormat[Crazy] + + "GADT with special characters in type field" should behave like checkRoundtrip[ + Crazy]( + CrazyType(), + """{"_`crazy type!`\"": "CrazyType"}""" + ) + + sealed trait Enum + case object A extends Enum + case object B extends Enum + + implicit val enumFormat: RootJsonFormat[Enum] = jsonFormat[Enum] + + "Enum" should behave like checkRoundtrip[List[Enum]]( + A :: B :: Nil, + """[{"type":"A"}, {"type":"B"}]""" + ) + + "Serializing as sealed trait and deserializing as child" should "work" in { + implicit val plusFormat: RootJsonFormat[Plus] = jsonFormat[Plus] + val expr: Expr = Plus(Value(42), Plus(Zero(), One)) + assert(expr.toJson.convertTo[Plus] == expr) + } + +} |