aboutsummaryrefslogtreecommitdiff
path: root/shared/src/test/scala
diff options
context:
space:
mode:
Diffstat (limited to 'shared/src/test/scala')
-rw-r--r--shared/src/test/scala/FieldNameTests.scala1
-rw-r--r--shared/src/test/scala/OptionFieldTests.scala24
-rw-r--r--shared/src/test/scala/ProductTypeFormatTests.scala20
3 files changed, 34 insertions, 11 deletions
diff --git a/shared/src/test/scala/FieldNameTests.scala b/shared/src/test/scala/FieldNameTests.scala
index ef4064d..cda5837 100644
--- a/shared/src/test/scala/FieldNameTests.scala
+++ b/shared/src/test/scala/FieldNameTests.scala
@@ -9,6 +9,7 @@ class FieldNameTests extends FlatSpec with FormatTests {
case class C(abA: String)
trait All extends DefaultJsonProtocol with DerivedFormats {
+ implicit val aFormat = jsonFormat[A]
implicit val bFormat = jsonFormat[B]
implicit val cFormat = jsonFormat[C]
}
diff --git a/shared/src/test/scala/OptionFieldTests.scala b/shared/src/test/scala/OptionFieldTests.scala
index 8cabf25..ac925d7 100644
--- a/shared/src/test/scala/OptionFieldTests.scala
+++ b/shared/src/test/scala/OptionFieldTests.scala
@@ -6,7 +6,8 @@ class OptionFieldTests
extends FlatSpec
with FormatTests {
- case class Opt(x: Option[Int])
+ sealed trait Super
+ case class Opt(x: Option[Int]) extends Super
object HideNull extends DerivedJsonProtocol {
override def printNull = false
@@ -49,5 +50,26 @@ class OptionFieldTests
assert("{}".parseJson.convertTo[Opt] == Opt(None))
}
+ {
+ import ShowNull._
+ implicit val superFmt = jsonFormat[Super]
+ "Option fields of ADTs" should behave like checkRoundtrip(
+ Opt(Some(2)): Super,
+ """{ "@type": "Opt", "x":2}"""
+ )
+ }
+
+ sealed trait Enum
+ case class Value(x: Int) extends Enum
+ case class Wrapper(enum: Option[Enum])
+
+ import ShowNull._
+ implicit val enumFormat: RootJsonFormat[Enum] = jsonFormat[Enum]
+ implicit val superFmt: RootJsonFormat[Wrapper] = jsonFormat[Wrapper]
+ "Option fields of inner ADTs" should behave like checkRoundtrip(
+ Wrapper(Some(Value(1))),
+ """{"enum":{"@type":"Value", "x": 1}}"""
+ )
+
}
diff --git a/shared/src/test/scala/ProductTypeFormatTests.scala b/shared/src/test/scala/ProductTypeFormatTests.scala
index d7721ba..7964743 100644
--- a/shared/src/test/scala/ProductTypeFormatTests.scala
+++ b/shared/src/test/scala/ProductTypeFormatTests.scala
@@ -12,8 +12,8 @@ class ProductTypeFormatTests
case class C(b: B)
case object D
case class E(d: D.type)
- case class F(x: Int)
- case class G(f: F)
+ case class FF(x: Int)
+ case class G(f: FF)
implicit val aFormat: RootJsonFormat[A] = jsonFormat[A]
implicit val bFormat: RootJsonFormat[B] = jsonFormat[B]
@@ -44,28 +44,28 @@ class ProductTypeFormatTests
)
// 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("y" -> f.x.toJson)
- override def read(js: JsValue): F =
- F(js.asJsObject.fields("y").convertTo[Int])
+ implicit val fFormat: RootJsonFormat[FF] = new RootJsonFormat[FF] {
+ override def write(f: FF): JsValue = JsObject("y" -> f.x.toJson)
+ override def read(js: JsValue): FF =
+ FF(js.asJsObject.fields("y").convertTo[Int])
}
"Overriding with a custom format" should behave like checkRoundtrip(
- F(2),
+ FF(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)),
+ G(FF(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 hFormat = jsonFormat[H]
implicit val iFormat: RootJsonFormat[I] = jsonFormat[I]
"Deriving a format that has no implicit child formats available" should behave like checkRoundtrip(
@@ -105,7 +105,7 @@ class ProductTypeFormatTests
implicit def vcg[T: JsonFormat] = jsonFormat[ProductTypeFormatTests.WrapperGeneric[T]]
"Value classes with generic parameters" should behave like checkRoundtrip(
- ProductTypeFormatTests.WrapperGeneric[F](F(42)),
+ ProductTypeFormatTests.WrapperGeneric[FF](FF(42)),
"""{"y": 42}"""
)