forked from robb/RBBJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
READMETests.swift
239 lines (224 loc) · 6.9 KB
/
READMETests.swift
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import XCTest
import RBBJSON
private let json = [
"store": [
"book": [
[
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
],
[
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
],
[
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
],
[
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
]
],
"bicycle": [
"color": "red",
"price": 19.95
]
],
"expensive": 10
] as RBBJSON
final class READMETests: XCTestCase {
func testQueriesAny() {
// JSONPath: $.store.book[*].author
RBBAssertEqual(json.store.book[any: .child].author, [
"Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
])
}
func testQueriesDescend() {
// JSONPath: $..author
RBBAssertEqual(json[any: .descendantOrSelf].author, [
"Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
])
}
func testQueriesAnyAtEnd() {
// JSONPath: $.store.*
RBBAssertEqual(json.store[any: .child], [
[
"color": "red",
"price": 19.95
],
[
[
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
],
[
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
],
[
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
],
[
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
]
]
])
}
func testQueriesDescend2() {
// JSONPath: $.store..price
RBBAssertEqual(json.store[any: .descendantOrSelf].price, [
19.95,
8.95,
12.99,
8.99,
22.99
])
}
func testQueriesSingleIndex() {
// JSONPath: $..book[2]
RBBAssertEqual(json[any: .descendantOrSelf].book[2], [
[
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
]
])
}
func testQueriesNegativeIndex() {
// JSONPath: $..book[-2]
RBBAssertEqual(json[any: .descendantOrSelf].book[-2], [
[
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
]
])
}
func testQueriesMultipleIndicesAtEnd() {
// JSONPath: $..book[0,1]
RBBAssertEqual(json[any: .descendantOrSelf].book[0, 1], [
[
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
],
[
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
]
])
}
func testFilter() {
// JSONPath: $..book[?(@.isbn)]
RBBAssertEqual(json[any: .descendantOrSelf].book[has: \.isbn], [
[
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
],
[
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
]
])
}
func testBooleanPredicate() {
// JSONPath: $.store.book[?(@.price < 10)]
RBBAssertEqual(json.store.book[matches: { $0.price <= 10 }], [
[
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
],
[
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
]
])
}
func testMultipleKeys() {
// JSONPath: $.store["book", "bicycle"]..["price", "author"]
//
// NOTE: This query doesn't seem to work in Gatling but does in Jayway
// with slightly different semantics: Turning on _Return null for
// missing leaf_ will produce the expected result although with a
// `null` value for the bicycle's author that we're omitting here.
RBBAssertEqual(json.store["book", "bicycle"][any: .descendantOrSelf]["price", "author"], [
[
"price": 19.95,
],
[
"price": 8.95,
"author": "Nigel Rees"
],
[
"price": 12.99,
"author": "Evelyn Waugh"
],
[
"price": 8.99,
"author": "Herman Melville"
],
[
"price": 22.99,
"author": "J. R. R. Tolkien"
],
])
}
func testExample() {
RBBAssertEqual(json.store.book[0].title.compactMap(String.init), [
"Sayings of the Century"
])
RBBAssertEqual(json.store.book[0, 1].title.compactMap(String.init), [
"Sayings of the Century",
"Sword of Honour"
])
RBBAssertEqual(json.store.book[0]["invalid Property"].compactMap(String.init), [])
RBBAssertEqual(json.store.book[0, 1]["invalid Property"].compactMap(String.init), [])
}
}