aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/CoproductTypeFormats.scala
blob: f9b71b844c1afedf14e2d77362da332db6e926fb (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
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,
    """"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"}"""
  )

  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"]"""
  )

}