Debian Codegraph @

Tests/RunCMake/string/JSON.cmake

raw
1 function(assert_strequal actual expected)
2 if(NOT expected STREQUAL actual)
3 message(SEND_ERROR "Output:\n${actual}\nDid not match expected:\n${expected}\n")
4 endif()
5 endfunction()
6
7 function(assert_strequal_error actual expected error)
8 if(error)
9 message(SEND_ERROR "Unexpected error: ${error}")
10 endif()
11 assert_strequal("${actual}" "${expected}")
12 endfunction()
13
14 function(assert_json_equal error actual expected)
15 if(error)
16 message(SEND_ERROR "Unexpected error: ${error}")
17 endif()
18 string(JSON eql EQUAL "${actual}" "${expected}")
19 if(NOT eql)
20 message(SEND_ERROR "Expected equality got\n ${actual}\n expected\n${expected}")
21 endif()
22 endfunction()
23
24 # test EQUAL
25 string(JSON result EQUAL
26 [=[ {"foo":"bar"} ]=]
27 [=[
28 {
29 "foo": "bar"
30 }
31 ]=])
32 if(NOT result)
33 message(SEND_ERROR "Expected ON got ${result}")
34 endif()
35
36 string(JSON result EQUAL
37 [=[ {"foo":"bar"} ]=]
38 [=[
39 {
40 "foo1": "bar"
41 }
42 ]=])
43 if(result)
44 message(SEND_ERROR "Expected OFF got ${result}")
45 endif()
46
47
48
49 set(json1 [=[
50 {
51 "foo" : "bar",
52 "array" : [5, "val", {"some": "other"}, null],
53 "types" : {
54 "null" : null,
55 "number" : 5,
56 "string" : "foo",
57 "boolean" : false,
58 "array" : [1,2,3],
59 "object" : {}
60 },
61 "values" : {
62 "null" : null,
63 "number" : 5,
64 "string" : "foo",
65 "false" : false,
66 "true" : true
67 },
68 "object": {
69 "foo": "bar"
70 },
71 "special" : {
72 "foo;bar" : "value1",
73 ";" : "value2",
74 "semicolon" : ";",
75 "list" : ["one", "two;three", "four"],
76 "quote" : "\"",
77 "\"" : "quote",
78 "backslash" : "\\",
79 "\\" : "backslash",
80 "slash" : "\/",
81 "\/" : "slash",
82 "newline" : "\n",
83 "\n" : "newline",
84 "return" : "\r",
85 "\r" : "return",
86 "tab" : "\t",
87 "\t" : "tab",
88 "backspace" : "\b",
89 "\b" : "backspace",
90 "formfeed" : "\f",
91 "\f" : "formfeed"
92 }
93 }
94 ]=])
95
96 # test GET
97 string(JSON result GET "${json1}" foo)
98 assert_strequal("${result}" bar)
99 string(JSON result GET "${json1}" array 0)
100 assert_strequal("${result}" 5)
101 string(JSON result GET "${json1}" array 1)
102 assert_strequal("${result}" val)
103 string(JSON result GET "${json1}" array 2 some)
104 assert_strequal("${result}" other)
105
106 string(JSON result GET "${json1}" values null)
107 assert_strequal("${result}" "")
108 string(JSON result GET "${json1}" values number)
109 assert_strequal("${result}" 5)
110 string(JSON result GET "${json1}" values string)
111 assert_strequal("${result}" "foo")
112 string(JSON result GET "${json1}" values true)
113 assert_strequal("${result}" "ON")
114 if(NOT result)
115 message(SEND_ERROR "Output did not match expected: TRUE actual: ${result}")
116 endif()
117 string(JSON result GET "${json1}" values false)
118 assert_strequal("${result}" "OFF")
119 if(result)
120 message(SEND_ERROR "Output did not match expected: FALSE actual: ${result}")
121 endif()
122
123 string(JSON result GET "\"Hello\"")
124 assert_strequal("${result}" Hello)
125 string(JSON result GET "5")
126 assert_strequal("${result}" 5)
127 string(JSON result GET "null")
128 assert_strequal("${result}" "")
129 string(JSON result ERROR_VARIABLE error GET "{}")
130 assert_json_equal("${error}" "${result}" "{}")
131 string(JSON result ERROR_VARIABLE error GET "[]")
132 assert_json_equal("${error}" "${result}" "[]")
133
134 string(JSON result ERROR_VARIABLE error GET "${json1}" foo)
135 assert_strequal_error("${result}" "bar" "${error}")
136
137 string(JSON result ERROR_VARIABLE error GET "${json1}" notThere)
138 assert_strequal("${result}" "notThere-NOTFOUND")
139 assert_strequal("${error}" "member 'notThere' not found")
140
141 string(JSON result ERROR_VARIABLE error GET "${json1}" 0)
142 assert_strequal("${result}" "0-NOTFOUND")
143 assert_strequal("${error}" "member '0' not found")
144
145 string(JSON result ERROR_VARIABLE error GET "${json1}" array 10)
146 assert_strequal("${result}" "array-10-NOTFOUND")
147 assert_strequal("${error}" "expected an index less than 4 got '10'")
148
149 string(JSON result ERROR_VARIABLE error GET "${json1}" array 2 some notThere)
150 assert_strequal("${result}" "array-2-some-notThere-NOTFOUND")
151 assert_strequal("${error}" "invalid path 'array 2 some notThere', need element of OBJECT or ARRAY type to lookup 'notThere' got STRING")
152
153 # special chars
154 string(JSON result ERROR_VARIABLE error GET "${json1}" special "foo;bar")
155 assert_strequal_error("${result}" "value1" "${error}")
156 string(JSON result ERROR_VARIABLE error GET "${json1}" special ";")
157 assert_strequal_error("${result}" "value2" "${error}")
158 string(JSON result ERROR_VARIABLE error GET "${json1}" special semicolon)
159 assert_strequal_error("${result}" ";" "${error}")
160
161 string(JSON result ERROR_VARIABLE error GET "${json1}" special list 1)
162 assert_strequal_error("${result}" "two;three" "${error}")
163
164 string(JSON result ERROR_VARIABLE error GET "${json1}")
165 assert_json_equal("${error}" "${result}" "${json1}")
166
167 string(JSON result ERROR_VARIABLE error GET "${json1}" array)
168 assert_json_equal("${error}" "${result}" [=[ [5, "val", {"some": "other"}, null] ]=])
169
170 string(JSON result ERROR_VARIABLE error GET "${json1}" special quote)
171 assert_strequal_error("${result}" "\"" "${error}")
172 string(JSON result ERROR_VARIABLE error GET "${json1}" special "\"")
173 assert_strequal_error("${result}" "quote" "${error}")
174
175 string(JSON result ERROR_VARIABLE error GET "${json1}" special backslash)
176 assert_strequal_error("${result}" "\\" "${error}")
177 string(JSON result ERROR_VARIABLE error GET "${json1}" special "\\")
178 assert_strequal_error("${result}" "backslash" "${error}")
179
180 string(JSON result ERROR_VARIABLE error GET "${json1}" special slash)
181 assert_strequal_error("${result}" "/" "${error}")
182 string(JSON result ERROR_VARIABLE error GET "${json1}" special "/")
183 assert_strequal_error("${result}" "slash" "${error}")
184
185 string(JSON result ERROR_VARIABLE error GET "${json1}" special newline)
186 assert_strequal_error("${result}" "\n" "${error}")
187 string(JSON result ERROR_VARIABLE error GET "${json1}" special "\n")
188 assert_strequal_error("${result}" "newline" "${error}")
189
190 string(JSON result ERROR_VARIABLE error GET "${json1}" special return)
191 assert_strequal_error("${result}" "\r" "${error}")
192 string(JSON result ERROR_VARIABLE error GET "${json1}" special "\r")
193 assert_strequal_error("${result}" "return" "${error}")
194
195 string(JSON result ERROR_VARIABLE error GET "${json1}" special tab)
196 assert_strequal_error("${result}" "\t" "${error}")
197 string(JSON result ERROR_VARIABLE error GET "${json1}" special "\t")
198 assert_strequal_error("${result}" "tab" "${error}")
199
200 file(READ ${CMAKE_CURRENT_LIST_DIR}/json/unicode.json unicode)
201 string(JSON char ERROR_VARIABLE error GET "${unicode}" backspace)
202 string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
203 assert_strequal_error("${result}" "backspace" "${error}")
204
205 file(READ ${CMAKE_CURRENT_LIST_DIR}/json/unicode.json unicode)
206 string(JSON char ERROR_VARIABLE error GET "${unicode}" backspace)
207 string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
208 assert_strequal_error("${result}" "backspace" "${error}")
209
210 string(JSON char ERROR_VARIABLE error GET "${unicode}" formfeed)
211 string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
212 assert_strequal_error("${result}" "formfeed" "${error}")
213
214 string(JSON char ERROR_VARIABLE error GET "${unicode}" datalinkescape)
215 string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
216 assert_strequal_error("${result}" "datalinkescape" "${error}")
217
218 # Test GET_RAW
219 string(JSON result GET_RAW "${json1}" values null)
220 assert_strequal("${result}" null)
221 string(JSON result GET_RAW "${json1}" values number)
222 assert_strequal("${result}" 5)
223 string(JSON result GET_RAW "${json1}" values string)
224 assert_strequal("${result}" "\"foo\"")
225 string(JSON result GET_RAW "${json1}" values false)
226 assert_strequal("${result}" false)
227 string(JSON result GET_RAW "${json1}" values true)
228 assert_strequal("${result}" true)
229 string(JSON result ERROR_VARIABLE error GET_RAW "${json1}" array)
230 assert_json_equal("${error}" "${result}" [=[ [5, "val", {"some": "other"}, null] ]=])
231 string(JSON result ERROR_VARIABLE error GET_RAW "${json1}" object)
232 assert_json_equal("${error}" "${result}" [=[ { "foo": "bar" } ]=])
233
234 # Test TYPE
235 string(JSON result TYPE "${json1}" types null)
236 assert_strequal("${result}" NULL)
237 string(JSON result TYPE "${json1}" types number)
238 assert_strequal("${result}" NUMBER)
239 string(JSON result TYPE "${json1}" types string)
240 assert_strequal("${result}" STRING)
241 string(JSON result TYPE "${json1}" types boolean)
242 assert_strequal("${result}" BOOLEAN)
243 string(JSON result TYPE "${json1}" types array)
244 assert_strequal("${result}" ARRAY)
245 string(JSON result TYPE "${json1}" types object)
246 assert_strequal("${result}" OBJECT)
247 string(JSON result TYPE "null")
248 assert_strequal("${result}" NULL)
249 string(JSON result TYPE "5")
250 assert_strequal("${result}" NUMBER)
251 string(JSON result TYPE "\"Hello\"")
252 assert_strequal("${result}" STRING)
253
254 # Test LENGTH
255 string(JSON result ERROR_VARIABLE error LENGTH "${json1}")
256 assert_strequal("${result}" 6)
257 if(error)
258 message(SEND_ERROR "Unexpected error: ${error}")
259 endif()
260
261 string(JSON result ERROR_VARIABLE error LENGTH "${json1}" array)
262 assert_strequal("${result}" 4)
263 if(error)
264 message(SEND_ERROR "Unexpected error: ${error}")
265 endif()
266
267 string(JSON result ERROR_VARIABLE error LENGTH "${json1}" foo)
268 assert_strequal("${result}" "foo-NOTFOUND")
269 assert_strequal("${error}" "LENGTH needs to be called with an element of type ARRAY or OBJECT, got STRING")
270
271 # Test MEMBER
272 string(JSON result ERROR_VARIABLE error MEMBER "${json1}" values 2)
273 assert_strequal("${result}" "number")
274 if(error)
275 message(SEND_ERROR "Unexpected error: ${error}")
276 endif()
277
278 string(JSON result ERROR_VARIABLE error MEMBER "${json1}" values 100)
279 assert_strequal("${result}" "values-100-NOTFOUND")
280 assert_strequal("${error}" "expected an index less than 5 got '100'")
281
282 # Test length loops
283 string(JSON arrayLength ERROR_VARIABLE error LENGTH "${json1}" types array)
284 if(error)
285 message(SEND_ERROR "Unexpected error: ${error}")
286 endif()
287 set(values "")
288 math(EXPR arrayLength "${arrayLength}-1")
289 foreach(index RANGE ${arrayLength})
290 string(JSON value ERROR_VARIABLE error GET "${json1}" types array ${index})
291 if(error)
292 message(SEND_ERROR "Unexpected error: ${error}")
293 endif()
294 list(APPEND values "${value}")
295 endforeach()
296 assert_strequal("${values}" "1;2;3")
297
298 string(JSON valuesLength ERROR_VARIABLE error LENGTH "${json1}" values)
299 if(error)
300 message(SEND_ERROR "Unexpected error: ${error}")
301 endif()
302 set(values "")
303 set(members "")
304 math(EXPR valuesLength "${valuesLength}-1")
305 foreach(index RANGE ${valuesLength})
306 string(JSON member ERROR_VARIABLE error MEMBER "${json1}" values ${index})
307 if(error)
308 message(SEND_ERROR "Unexpected error: ${error}")
309 endif()
310 string(JSON value ERROR_VARIABLE error GET "${json1}" values ${member})
311 if(error)
312 message(SEND_ERROR "Unexpected error: ${error}")
313 endif()
314
315 list(APPEND members "${member}")
316 list(APPEND values "${value}")
317 endforeach()
318 assert_strequal("${members}" "false;null;number;string;true")
319 assert_strequal("${values}" "OFF;;5;foo;ON")
320
321 # Test REMOVE
322 set(json2 [=[{
323 "foo" : "bar",
324 "array" : [5, "val", {"some": "other"}, null]
325 }]=])
326 string(JSON result ERROR_VARIABLE error REMOVE ${json2} foo)
327 assert_json_equal("${error}" "${result}"
328 [=[{
329 "array" : [5, "val", {"some": "other"}, null]
330 }]=])
331
332 string(JSON result ERROR_VARIABLE error REMOVE ${json2} array 1)
333 assert_json_equal("${error}" "${result}"
334 [=[{
335 "foo" : "bar",
336 "array" : [5, {"some": "other"}, null]
337 }]=])
338
339 string(JSON result ERROR_VARIABLE error REMOVE ${json2} array 100)
340 assert_strequal("${result}" "array-100-NOTFOUND")
341 assert_strequal("${error}" "expected an index less than 4 got '100'")
342
343 # Test SET
344 string(JSON result ERROR_VARIABLE error SET ${json2} new 5)
345 assert_json_equal("${error}" "${result}"
346 [=[{
347 "foo" : "bar",
348 "array" : [5, "val", {"some": "other"}, null],
349 "new" : 5
350 }]=])
351
352 string(JSON result ERROR_VARIABLE error SET ${json2} new [=[ {"obj" : false} ]=])
353 assert_json_equal("${error}" "${result}"
354 [=[{
355 "foo" : "bar",
356 "array" : [5, "val", {"some": "other"}, null],
357 "new" : {"obj" : false}
358 }]=])
359
360 string(JSON result ERROR_VARIABLE error SET ${json2} array 0 6)
361 assert_json_equal("${error}" "${result}"
362 [=[{
363 "foo" : "bar",
364 "array" : [6, "val", {"some": "other"}, null]
365 }]=])
366
367 string(JSON result ERROR_VARIABLE error SET ${json2} array 5 [["append"]])
368 assert_json_equal("${error}" "${result}"
369 [=[{
370 "foo" : "bar",
371 "array" : [5, "val", {"some": "other"}, null, "append"]
372 }]=])
373
374 string(JSON result ERROR_VARIABLE error SET ${json2} array 100 [["append"]])
375 assert_json_equal("${error}" "${result}"
376 [=[{
377 "foo" : "bar",
378 "array" : [5, "val", {"some": "other"}, null, "append"]
379 }]=])
380
381 # Test STRING_ENCODE
382 string(JSON result STRING_ENCODE Hello)
383 assert_strequal("${result}" "\"Hello\"")
384 string(JSON result STRING_ENCODE "\"Hello\"")
385 assert_strequal("${result}" "\"\\\"Hello\\\"\"")
386 string(JSON result STRING_ENCODE null)
387 assert_strequal("${result}" "\"null\"")
388 string(JSON result STRING_ENCODE 0)
389 assert_strequal("${result}" "\"0\"")
390 string(JSON result STRING_ENCODE false)
391 assert_strequal("${result}" "\"false\"")
392 string(JSON result STRING_ENCODE {})
393 assert_strequal("${result}" "\"{}\"")
394 string(JSON result STRING_ENCODE [])
395 assert_strequal("${result}" "\"[]\"")