/
JsonMatcherSpec.scala
221 lines (204 loc) · 9.11 KB
/
JsonMatcherSpec.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package matchers
import org.specs2.mutable.Specification
import play.api.libs.json.{JsArray, JsObject, Json}
class JsonMatcherSpec extends Specification with JsonMatcher {
sequential
"equalToJson" >> {
"createKoMessageForEqualToJsonDetail" should {
val koMessage = createKoMessageForEqualToJsonDetail _
val jsonA = Json.parse(
"""
{
"string_field" : "string",
"number_field" : 100,
"boolean_field" : false,
"null_field" : null,
"array_field" : ["string", 100, false, null],
"object_field" : {"xxx_field" : "xxx", "yyy_field" : 100}
}
""").as[JsObject]
"Objectの順番が違っているだけならKOメッセージが作成されないこと" >> {
val jsonB = Json.parse(
"""
{
"number_field" : 100,
"object_field" : {"yyy_field" : 100, "xxx_field" : "xxx"},
"string_field" : "string",
"boolean_field" : false,
"null_field" : null,
"array_field" : ["string", 100, false, null]
}
""").as[JsObject]
koMessage(jsonA, jsonB, false) must beNone
koMessage(jsonB, jsonA, false) must beNone
// 念のため matcherでも true になることを試す
jsonA must equalToJson(jsonB)
}
"フィールド値が異なる場合はKOメッセージが作成されること" >> {
// 各フィールドの値をそれぞれ変えてテスト
koMessage(jsonA, jsonA ++ Json.obj("string_field" -> "string_x"), false) must beSome.which { m =>
m must contain("""Path : string_field""")
m must contain("""Actual : "string"""")
m must contain("""Expected : "string_x"""")
}
koMessage(jsonA, jsonA ++ Json.obj("number_field" -> 101), false) must beSome.which { m =>
m must contain("""Path : number_field""")
m must contain("""Actual : 100""")
m must contain("""Expected : 101""")
}
koMessage(jsonA, jsonA ++ Json.obj("boolean_field" -> true), false) must beSome.which { m =>
m must contain("""Path : boolean_field""")
m must contain("""Actual : false""")
m must contain("""Expected : true""")
}
koMessage(jsonA, jsonA ++ Json.obj("null_field" -> ""), false) must beSome.which { m =>
m must contain("""Path : null_field""")
m must contain("""Actual : null""")
m must contain("""Expected : """")
}
koMessage(jsonA, jsonA ++ Json.obj("array_field" -> Json.parse("""["string", 101, false, null]""")), false) must beSome.which { m =>
m must contain("""Path : array_field->array(1)""")
m must contain("""Actual : 100""")
m must contain("""Expected : 101""")
}
koMessage(jsonA, jsonA ++ Json.obj("object_field" -> Json.parse("""{"xxx_field" : "xxx", "yyy_field" : 101}""")), false) must beSome.which { m =>
m must contain("""Path : object_field->yyy_field""")
m must contain("""Actual : 100""")
m must contain("""Expected : 101""")
}
// 各フィールドの値ではなく、型自体が違う場合
koMessage(jsonA, jsonA ++ Json.obj("number_field" -> "100"), false) must beSome.which { m =>
m must contain("""Path : number_field""")
m must contain("""Actual : 100""")
m must contain("""Expected : "100"""")
}
// 念のため matcherでも false になることを試す
jsonA must not be equalToJson(jsonA ++ Json.obj("string_field" -> 100))
}
"フィールドの要素が異なる場合はKOメッセージが作成されること" >> {
// expected のフィールドが多い
koMessage(jsonA, jsonA ++ Json.obj("add_field" -> "add"), false) must beSome.which { m =>
m must contain("""Path : add_field""")
m must contain("""Actual : null""")
m must contain("""Expected : "add"""")
}
// expected のフィールドが少ない
koMessage(jsonA, jsonA - "array_field", false) must beSome.which { m =>
m must contain("""Path : array_field""")
m must contain("""Actual : ["string",100,false,null]""")
m must contain("""Expected : null""")
}
// expected のフィールドが異なる
koMessage(jsonA, jsonA ++ Json.obj("add_field" -> "add") - "array_field", false) must beSome.which { m =>
m must contain("""Path : add_field""")
m must contain("""Actual : null""")
m must contain("""Expected : "add"""")
}
// expected の全体が空
koMessage(jsonA, Json.parse("{}"), false) must beSome.which { m =>
m must contain("""Path : array_field""")
m must contain("""Actual : ["string",100,false,null]""")
m must contain("""Expected : null""")
}
// expected のobject_fieldフィールドの、フィールドが多い
koMessage(jsonA, jsonA ++ Json.obj("object_field" -> Json.parse("""{"xxx_field" : "xxx", "yyy_field" : 100, "zzz_field" : true}""")), false) must beSome.which { m =>
m must contain("""Path : object_field->zzz_field""")
m must contain("""Actual : null""")
m must contain("""Expected : true""")
}
// 念のため matcherでも false になることを試す
jsonA must not be equalToJson(jsonA ++ jsonA ++ Json.obj("object_field" -> Json.parse("""{"xxx_field" : "xxx", "yyy_field" : 100, "zzz_field" : true}""")))
}
"配列の並び順が異なる場合はKOメッセージが作成されること" >> {
// 配列フィールドの並び順が異なる
koMessage(jsonA, jsonA ++ Json.obj("array_field" -> Json.parse("""["string", 100, null, false]""")), false) must beSome.which { m =>
m must contain("""Path : array_field->array(2)""")
m must contain("""Actual : false""")
m must contain("""Expected : null""")
}
// Json全体が配列となっていて、その配列の並び順が異なる
val jsonArrayA = JsArray(Seq(
jsonA ++ Json.obj("string_field" -> "string 1"),
jsonA ++ Json.obj("string_field" -> "string 2"),
jsonA ++ Json.obj("string_field" -> "string 3"))
)
val jsonArrayB = JsArray(Seq(
jsonA ++ Json.obj("string_field" -> "string 1"),
jsonA ++ Json.obj("string_field" -> "string 3"),
jsonA ++ Json.obj("string_field" -> "string 2"))
)
koMessage(jsonArrayA, jsonArrayB, false) must beSome.which { m =>
m must contain("""Path : array(1)->string_field""")
m must contain("""Actual : "string 2"""")
m must contain("""Expected : "string 3"""")
}
// 念のため matcherでも false になることを試す
jsonArrayA must not be equalToJson(jsonArrayB)
}
"ネストした項目で値が異なってもKOメッセージが作成されること" >> {
// ネストした項目のstring_field2の値が異なる
val jsonNestA = Json.parse(
"""
[
{
"string_field": "string"
},
{
"string_field": "string",
"array_field": [
100,
{
"array_field2": [
{
"number_field": 10,
"string_field2": "HOGEHOGE"
},
false
]
}
],
"number_field": 10
},
{
"string_field": "string"
}
]
""")
val jsonNestB = Json.parse(
"""
[
{
"string_field": "string"
},
{
"string_field": "string",
"array_field": [
100,
{
"array_field2": [
{
"number_field": 10,
"string_field2": "FUGAFUGA"
},
false
]
}
],
"number_field": 10
},
{
"string_field": "string"
}
]
""")
koMessage(jsonNestA, jsonNestB, false) must beSome.which { m =>
m must contain("""Path : array(1)->array_field->array(1)->array_field2->array(0)->string_field2""")
m must contain("""Actual : "HOGEHOGE"""")
m must contain("""Expected : "FUGAFUGA"""")
}
// 念のため matcherでも false になることを試す
jsonNestA must not be equalToJson(jsonNestB)
}
}
}
}