-
-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathScala3JsonAutomatedSpec.scala
More file actions
53 lines (44 loc) · 1.34 KB
/
Scala3JsonAutomatedSpec.scala
File metadata and controls
53 lines (44 loc) · 1.34 KB
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
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/
package scalaguide.json
import org.specs2.mutable.Specification
import play.api.libs.json._
object Scala3JsonAutomatedSpec {
//#reads-model
case class Resident(
name: String,
age: Int,
specialism: Option[String]
) derives Reads
//#reads-model
val residentJson = Json.parse(
"""{
"name" : "Fiver",
"age" : 4
}"""
)
val sampleResident = Resident("Fiver", 4, None)
//#reads-trait
sealed trait Role derives Reads
case object Admin extends Role derives Reads
case class Contributor(organization: String) extends Role derives Reads
//#reads-trait
val contributorJson = Json.obj("_type" -> "scalaguide.json.Scala3JsonAutomatedSpec.Contributor", "organization" -> "Foo")
val sampleContributor = Contributor("Foo")
}
class Scala3JsonAutomatedSpec extends Specification {
import Scala3JsonAutomatedSpec._
"Scala 3 JSON automated" should {
"for case class" >> {
"derive a Reads" in {
residentJson.as[Resident].must_===(sampleResident)
}
}
"for trait" >> {
"derive a Reads if every subclass derives Reads" in {
contributorJson.as[Role].must_===(sampleContributor)
}
}
}
}