diff options
Diffstat (limited to 'src/test/scala')
-rw-r--r-- | src/test/scala/CoproductTypeFormatTests.scala (renamed from src/test/scala/CoproductTypeFormats.scala) | 13 | ||||
-rw-r--r-- | src/test/scala/ImplicitDerivedFormatTests.scala | 26 | ||||
-rw-r--r-- | src/test/scala/ProductTypeFormatTests.scala (renamed from src/test/scala/ProductTypeFormats.scala) | 33 |
3 files changed, 66 insertions, 6 deletions
diff --git a/src/test/scala/CoproductTypeFormats.scala b/src/test/scala/CoproductTypeFormatTests.scala index 6496e00..ec9b3f5 100644 --- a/src/test/scala/CoproductTypeFormats.scala +++ b/src/test/scala/CoproductTypeFormatTests.scala @@ -4,7 +4,7 @@ import spray.json._ import org.scalatest._ -class CoproductTypeFormats +class CoproductTypeFormatTests extends FlatSpec with FormatTests with DefaultJsonProtocol @@ -16,6 +16,8 @@ class CoproductTypeFormats 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"}""" @@ -40,6 +42,8 @@ class CoproductTypeFormats 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"}""" @@ -49,6 +53,8 @@ class CoproductTypeFormats 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(), @@ -59,12 +65,15 @@ class CoproductTypeFormats 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 an deserializing as child" should "work" in { + "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) } diff --git a/src/test/scala/ImplicitDerivedFormatTests.scala b/src/test/scala/ImplicitDerivedFormatTests.scala new file mode 100644 index 0000000..9723cb0 --- /dev/null +++ b/src/test/scala/ImplicitDerivedFormatTests.scala @@ -0,0 +1,26 @@ +package xyz.driver.json + +import spray.json._ + +import org.scalatest._ + +class ImplicitDerivedFormatTests + extends FlatSpec + with FormatTests + with ImplicitDerivedFormats + with DefaultJsonProtocol { + + case class B(x: Int, b: String, mp: Map[String, Int]) + case class C(b: B) + + "Simple parameter product" should behave like checkRoundtrip( + B(42, "Hello World", Map("a" -> 1, "b" -> -1024)), + """{ "x": 42, "b": "Hello World", "mp": { "a": 1, "b": -1024 } }""" + ) + + "Nested parameter product with custom child format" should behave like checkRoundtrip( + C(B(42, "Hello World", Map("a" -> 1, "b" -> -1024))), + """{"b" :{ "x": 42, "b": "Hello World", "mp": { "a": 1, "b": -1024 } } }""" + ) + +} diff --git a/src/test/scala/ProductTypeFormats.scala b/src/test/scala/ProductTypeFormatTests.scala index 02fb554..b7d8d27 100644 --- a/src/test/scala/ProductTypeFormats.scala +++ b/src/test/scala/ProductTypeFormatTests.scala @@ -4,7 +4,7 @@ import spray.json._ import org.scalatest._ -class ProductTypeFormats +class ProductTypeFormatTests extends FlatSpec with FormatTests with DerivedFormats @@ -16,6 +16,13 @@ class ProductTypeFormats case object D case class E(d: D.type) case class F(x: Int) + case class G(f: F) + + implicit val aFormat: RootJsonFormat[A] = jsonFormat[A] + implicit val bFormat: RootJsonFormat[B] = jsonFormat[B] + implicit val cFormat: RootJsonFormat[C] = jsonFormat[C] + implicit val dFormat: RootJsonFormat[D.type] = jsonFormat[D.type] + implicit val eFormat: RootJsonFormat[E] = jsonFormat[E] "No-parameter product" should behave like checkRoundtrip(A(), "{}") @@ -41,14 +48,32 @@ class ProductTypeFormats // custom format for F, that inverts the value of parameter x implicit val fFormat: RootJsonFormat[F] = new RootJsonFormat[F] { - override def write(f: F): JsValue = JsObject("x" -> JsNumber(-f.x)) + override def write(f: F): JsValue = JsObject("y" -> f.x.toJson) override def read(js: JsValue): F = - F(-js.asJsObject.fields("x").convertTo[Int]) + F(js.asJsObject.fields("y").convertTo[Int]) } "Overriding with a custom format" should behave like checkRoundtrip( F(2), - """{"x":-2}""" + """{"y":2}""" + ) + + implicit val gFormat: RootJsonFormat[G] = jsonFormat[G] + + "Derving a format with a custom child format" should behave like checkRoundtrip( + G(F(2)), + """{"f": {"y":2}}""" + ) + + case class H(x: Boolean) + case class I(h: H) + + // there is no format defined for H, Magnolia will generate one automatically + implicit val iFormat: RootJsonFormat[I] = jsonFormat[I] + + "Deriving a format that has no implicit child formats available" should behave like checkRoundtrip( + I(H(true)), + """{"h": {"x":true}}""" ) } |