diff options
author | Jakob Odersky <jakob@odersky.com> | 2018-02-14 12:17:15 -0800 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2018-02-14 12:17:15 -0800 |
commit | eada4c48b3954241e130f5d9b5e7feebe8c1e3f2 (patch) | |
tree | d017b30fb264b0560cbf7df3540f26e705da8ec0 /src/test/scala/ProductTypeFormats.scala | |
parent | af6845cc08ae223ad67ebad61559d8264fd3346b (diff) | |
download | spray-json-derivation-eada4c48b3954241e130f5d9b5e7feebe8c1e3f2.tar.gz spray-json-derivation-eada4c48b3954241e130f5d9b5e7feebe8c1e3f2.tar.bz2 spray-json-derivation-eada4c48b3954241e130f5d9b5e7feebe8c1e3f2.zip |
Prepare for releasing
Diffstat (limited to 'src/test/scala/ProductTypeFormats.scala')
-rw-r--r-- | src/test/scala/ProductTypeFormats.scala | 54 |
1 files changed, 54 insertions, 0 deletions
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}""" + ) + +} |