From eada4c48b3954241e130f5d9b5e7feebe8c1e3f2 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Wed, 14 Feb 2018 12:17:15 -0800 Subject: Prepare for releasing --- src/test/scala/CoproductTypeFormats.scala | 58 +++++++++++++++++++++++++++++++ src/test/scala/FormatTests.scala | 20 +++++++++++ src/test/scala/ProductTypeFormats.scala | 54 ++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 src/test/scala/CoproductTypeFormats.scala create mode 100644 src/test/scala/FormatTests.scala create mode 100644 src/test/scala/ProductTypeFormats.scala (limited to 'src/test') diff --git a/src/test/scala/CoproductTypeFormats.scala b/src/test/scala/CoproductTypeFormats.scala new file mode 100644 index 0000000..f16e4c7 --- /dev/null +++ b/src/test/scala/CoproductTypeFormats.scala @@ -0,0 +1,58 @@ +package xyz.driver.json + +import spray.json._ + +import org.scalatest._ + +class CoproductTypeFormats + 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 + + "No-parameter case class child" should behave like checkCoherence[Expr]( + Zero(), + """{"type":"Zero"}""" + ) + + "Simple parameter case class child" should behave like checkCoherence[Expr]( + Value(42), + """{"type":"Value","x":42}""" + ) + + "Nested parameter case class child" should behave like checkCoherence[Expr]( + Plus(Value(42), Value(0)), + """{"type":"Plus","lhs":{"type":"Value","x":42},"rhs":{"type":"Value","x":0}}""" + ) + + // "Case object child" should behave like checkCoherence[Expr]( + // One, + // """{"type":"One"}""" + // ) + + @gadt("kind") + sealed abstract class Keyword(`type`: String) + case class If(`type`: String) extends Keyword(`type`) + + "GADT with type field alias" should behave like checkCoherence[Keyword]( + If("class"), + """{"kind":"If","type":"class"}""" + ) + + @enum + sealed trait Enum + case object A extends Enum + case object B extends Enum + + "Enum" should behave like checkCoherence[List[Enum]]( + A :: B :: Nil, + """["A", "B"]""" + ) + +} diff --git a/src/test/scala/FormatTests.scala b/src/test/scala/FormatTests.scala new file mode 100644 index 0000000..e29e49f --- /dev/null +++ b/src/test/scala/FormatTests.scala @@ -0,0 +1,20 @@ +package xyz.driver.json + +import spray.json._ +import org.scalatest._ + +trait FormatTests { self: FlatSpec => + + def checkCoherence[A: JsonFormat](a: A, expectedJson: String) = { + it should "serialize to the expected JSON value" in { + val expected: JsValue = expectedJson.parseJson + assert(a.toJson == expected) + } + + it should "serialize then deserialize back to itself" in { + val reread = a.toJson.convertTo[A] + assert(reread == a) + } + } + +} diff --git a/src/test/scala/ProductTypeFormats.scala b/src/test/scala/ProductTypeFormats.scala new file mode 100644 index 0000000..9755198 --- /dev/null +++ b/src/test/scala/ProductTypeFormats.scala @@ -0,0 +1,54 @@ +package xyz.driver.json + +import spray.json._ + +import org.scalatest._ + +class ProductTypeFormats + extends FlatSpec + with FormatTests + with DerivedFormats + with DefaultJsonProtocol { + + case class A() + case class B(x: Int, b: String, mp: Map[String, Int]) + case class C(b: B) + case object D + case class E(d: D.type) + case class F(x: Int) + + "No-parameter product" should behave like checkCoherence(A(), "{}") + + "Simple parameter product" should behave like checkCoherence( + B(42, "Hello World", Map("a" -> 1, "b" -> -1024)), + """{ "x": 42, "b": "Hello World", "mp": { "a": 1, "b": -1024 } }""" + ) + + "Nested parameter product" should behave like checkCoherence( + C(B(42, "Hello World", Map("a" -> 1, "b" -> -1024))), + """{"b" :{ "x": 42, "b": "Hello World", "mp": { "a": 1, "b": -1024 } } }""" + ) + + "Case object" should behave like checkCoherence( + D, + """"D"""" + ) + + "Case object as parameter" should behave like checkCoherence( + E(D), + """{"d":"D"}""" + ) + + // custom format for F, that inverts the value of parameter x + implicit val fFormat: JsonFormat[F] = new JsonFormat[F] { + override def write(f: F): JsValue = JsObject("x" -> JsNumber(-f.x)) + override def read(js: JsValue): F = + F(-js.asJsObject.fields("x").convertTo[Int]) + } + + "Overriding with a custom format" should behave like checkCoherence( + F(2), + """{"x":-2}""" + ) + +} -- cgit v1.2.3