aboutsummaryrefslogtreecommitdiff
path: root/shared/src/test/scala/OptionFieldTests.scala
blob: ac925d7f6507409d7e23c70e4329af86d89956eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package spray.json

import org.scalatest._

class OptionFieldTests
    extends FlatSpec
    with FormatTests {

  sealed trait Super
  case class Opt(x: Option[Int]) extends Super

  object HideNull extends DerivedJsonProtocol {
    override def printNull = false
    implicit val optFmt = jsonFormat[Opt]
  }

  object ShowNull extends DerivedJsonProtocol {
    override def printNull = true
    implicit val optFmt = jsonFormat[Opt]
  }


  "Option fields with some value" should behave like checkRoundtrip(
    Opt(Some(2)),
    """{"x":2}"""
  )(HideNull.optFmt)

  "Option fields with some value (show null)" should behave like checkRoundtrip(
    Opt(Some(2)),
    """{"x":2}"""
  )(ShowNull.optFmt)

  "Option fields with null value" should behave like checkRoundtrip(
    Opt(None),
    """{}"""
  )(HideNull.optFmt)

  "Option fields with null value (show null)" should behave like checkRoundtrip(
    Opt(None),
    """{"x":null}"""
  )(ShowNull.optFmt)

  "Option fields with undefined value" should "deserialize" in {
    import HideNull._
    assert("{}".parseJson.convertTo[Opt] == Opt(None))
  }

  "Option fields with undefined value (show null)" should "deserialize" in {
    import ShowNull._
    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}}"""
  )

}