forked from kifi/json-annotation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonFormatAnnotationTest.scala
57 lines (43 loc) · 1.48 KB
/
JsonFormatAnnotationTest.scala
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 com.kifi.macros
import org.specs2.mutable.Specification
import play.api.libs.json._
@json case class City(name: String)
@json case class Person(name: String, age: Int)
@jsonstrict case class City2(name: String)
@jsonstrict case class Person2(name: String, age: Int)
class JsonFormatAnnotationTest extends Specification {
"@json annotation" should {
"create correct formatter for case class with 1 field" in {
val city = City("San Francisco")
val json = Json.toJson(city)
json === JsString("San Francisco")
Json.fromJson[City](json).asOpt must beSome(city)
}
"create correct formatter for case class with >= 2 fields" in {
val person = Person("Victor Hugo", 46)
val json = Json.toJson(person)
json === Json.obj(
"name" -> "Victor Hugo",
"age" -> 46
)
Json.fromJson[Person](json).asOpt must beSome(person)
}
}
"@jsonstrict annotation" should {
"create correct formatter for case class with 1 field" in {
val city = City2("San Francisco")
val json = Json.toJson(city)
json === Json.obj("name" -> "San Francisco")
Json.fromJson[City2](json).asOpt must beSome(city)
}
"create correct formatter for case class with >= 2 fields" in {
val person = Person2("Victor Hugo", 46)
val json = Json.toJson(person)
json === Json.obj(
"name" -> "Victor Hugo",
"age" -> 46
)
Json.fromJson[Person2](json).asOpt must beSome(person)
}
}
}