diff options
author | Jakob Odersky <jakob@odersky.com> | 2018-03-08 16:53:44 -0800 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2018-03-28 10:34:31 -0700 |
commit | c5fe23594f66225eb18bf4e67472cd80023eb448 (patch) | |
tree | 58fe8509b3990932134c18e09ed69ee83b779373 /shared/src/test/scala/CoproductTypeFormatTests.scala | |
parent | 2f3f9aa851080b6c79f3af14b3fdfeab56feffec (diff) | |
download | spray-json-derivation-c5fe23594f66225eb18bf4e67472cd80023eb448.tar.gz spray-json-derivation-c5fe23594f66225eb18bf4e67472cd80023eb448.tar.bz2 spray-json-derivation-c5fe23594f66225eb18bf4e67472cd80023eb448.zip |
Refactor build to use sbt-crossproject
Diffstat (limited to 'shared/src/test/scala/CoproductTypeFormatTests.scala')
-rw-r--r-- | shared/src/test/scala/CoproductTypeFormatTests.scala | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/shared/src/test/scala/CoproductTypeFormatTests.scala b/shared/src/test/scala/CoproductTypeFormatTests.scala new file mode 100644 index 0000000..de73967 --- /dev/null +++ b/shared/src/test/scala/CoproductTypeFormatTests.scala @@ -0,0 +1,79 @@ +package 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) + } + +} |