Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Tests / RunCMake / string / JSON.cmake
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   "special" : {
69     "foo;bar" : "value1",
70     ";" : "value2",
71     "semicolon" : ";",
72     "list" : ["one", "two;three", "four"],
73     "quote" : "\"",
74     "\"" : "quote",
75     "backslash" : "\\",
76     "\\" : "backslash",
77     "slash" : "\/",
78     "\/" : "slash",
79     "newline" : "\n",
80     "\n" : "newline",
81     "return" : "\r",
82     "\r" : "return",
83     "tab" : "\t",
84     "\t" : "tab",
85     "backspace" : "\b",
86     "\b" : "backspace",
87     "formfeed" : "\f",
88     "\f" : "formfeed"
89    }
90 }
91 ]=])
92
93 string(JSON result GET "${json1}" foo)
94 assert_strequal("${result}" bar)
95 string(JSON result GET "${json1}" array 0)
96 assert_strequal("${result}" 5)
97 string(JSON result GET "${json1}" array 1)
98 assert_strequal("${result}" val)
99 string(JSON result GET "${json1}" array 2 some)
100 assert_strequal("${result}" other)
101
102 string(JSON result GET "${json1}" values null)
103 assert_strequal("${result}" "")
104 string(JSON result GET "${json1}" values number)
105 assert_strequal("${result}" 5)
106 string(JSON result GET "${json1}" values string)
107 assert_strequal("${result}" "foo")
108 string(JSON result GET "${json1}" values true)
109 assert_strequal("${result}" "ON")
110 if(NOT result)
111   message(SEND_ERROR "Output did not match expected: TRUE actual: ${result}")
112 endif()
113 string(JSON result GET "${json1}" values false)
114 assert_strequal("${result}" "OFF")
115 if(result)
116   message(SEND_ERROR "Output did not match expected: FALSE actual: ${result}")
117 endif()
118
119 string(JSON result ERROR_VARIABLE error GET "${json1}" foo)
120 assert_strequal_error("${result}" "bar" "${error}")
121
122 string(JSON result ERROR_VARIABLE error GET "${json1}" notThere)
123 assert_strequal("${result}" "notThere-NOTFOUND")
124 assert_strequal("${error}" "member 'notThere' not found")
125
126 string(JSON result ERROR_VARIABLE error GET "${json1}" 0)
127 assert_strequal("${result}" "0-NOTFOUND")
128 assert_strequal("${error}" "member '0' not found")
129
130 string(JSON result ERROR_VARIABLE error GET "${json1}" array 10)
131 assert_strequal("${result}" "array-10-NOTFOUND")
132 assert_strequal("${error}" "expected an index less than 4 got '10'")
133
134 string(JSON result ERROR_VARIABLE error GET "${json1}" array 2 some notThere)
135 assert_strequal("${result}" "array-2-some-notThere-NOTFOUND")
136 assert_strequal("${error}" "invalid path 'array 2 some notThere', need element of OBJECT or ARRAY type to lookup 'notThere' got STRING")
137
138 # special chars
139 string(JSON result ERROR_VARIABLE error GET "${json1}" special "foo;bar")
140 assert_strequal_error("${result}" "value1" "${error}")
141 string(JSON result ERROR_VARIABLE error GET "${json1}" special ";")
142 assert_strequal_error("${result}" "value2" "${error}")
143 string(JSON result ERROR_VARIABLE error GET "${json1}" special semicolon)
144 assert_strequal_error("${result}" ";" "${error}")
145
146 string(JSON result ERROR_VARIABLE error GET "${json1}" special list 1)
147 assert_strequal_error("${result}" "two;three" "${error}")
148
149 string(JSON result ERROR_VARIABLE error GET "${json1}")
150 assert_json_equal("${error}" "${result}" "${json1}")
151
152 string(JSON result ERROR_VARIABLE error GET "${json1}" array)
153 assert_json_equal("${error}" "${result}" [=[ [5, "val", {"some": "other"}, null] ]=])
154
155 string(JSON result ERROR_VARIABLE error GET "${json1}" special quote)
156 assert_strequal_error("${result}" "\"" "${error}")
157 string(JSON result ERROR_VARIABLE error GET "${json1}" special "\"")
158 assert_strequal_error("${result}" "quote" "${error}")
159
160 string(JSON result ERROR_VARIABLE error GET "${json1}" special backslash)
161 assert_strequal_error("${result}" "\\" "${error}")
162 string(JSON result ERROR_VARIABLE error GET "${json1}" special "\\")
163 assert_strequal_error("${result}" "backslash" "${error}")
164
165 string(JSON result ERROR_VARIABLE error GET "${json1}" special slash)
166 assert_strequal_error("${result}" "/" "${error}")
167 string(JSON result ERROR_VARIABLE error GET "${json1}" special "/")
168 assert_strequal_error("${result}" "slash" "${error}")
169
170 string(JSON result ERROR_VARIABLE error GET "${json1}" special newline)
171 assert_strequal_error("${result}" "\n" "${error}")
172 string(JSON result ERROR_VARIABLE error GET "${json1}" special "\n")
173 assert_strequal_error("${result}" "newline" "${error}")
174
175 string(JSON result ERROR_VARIABLE error GET "${json1}" special return)
176 assert_strequal_error("${result}" "\r" "${error}")
177 string(JSON result ERROR_VARIABLE error GET "${json1}" special "\r")
178 assert_strequal_error("${result}" "return" "${error}")
179
180 string(JSON result ERROR_VARIABLE error GET "${json1}" special tab)
181 assert_strequal_error("${result}" "\t" "${error}")
182 string(JSON result ERROR_VARIABLE error GET "${json1}" special "\t")
183 assert_strequal_error("${result}" "tab" "${error}")
184
185 file(READ ${CMAKE_CURRENT_LIST_DIR}/json/unicode.json unicode)
186 string(JSON char ERROR_VARIABLE error GET "${unicode}" backspace)
187 string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
188 assert_strequal_error("${result}" "backspace" "${error}")
189
190 file(READ ${CMAKE_CURRENT_LIST_DIR}/json/unicode.json unicode)
191 string(JSON char ERROR_VARIABLE error GET "${unicode}" backspace)
192 string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
193 assert_strequal_error("${result}" "backspace" "${error}")
194
195 string(JSON char ERROR_VARIABLE error GET "${unicode}" formfeed)
196 string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
197 assert_strequal_error("${result}" "formfeed" "${error}")
198
199 string(JSON char ERROR_VARIABLE error GET "${unicode}" datalinkescape)
200 string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
201 assert_strequal_error("${result}" "datalinkescape" "${error}")
202
203 # Test TYPE
204 string(JSON result TYPE "${json1}" types null)
205 assert_strequal("${result}" NULL)
206 string(JSON result TYPE "${json1}" types number)
207 assert_strequal("${result}" NUMBER)
208 string(JSON result TYPE "${json1}" types string)
209 assert_strequal("${result}" STRING)
210 string(JSON result TYPE "${json1}" types boolean)
211 assert_strequal("${result}" BOOLEAN)
212 string(JSON result TYPE "${json1}" types array)
213 assert_strequal("${result}" ARRAY)
214 string(JSON result TYPE "${json1}" types object)
215 assert_strequal("${result}" OBJECT)
216
217 # Test LENGTH
218 string(JSON result ERROR_VARIABLE error LENGTH "${json1}")
219 assert_strequal("${result}" 5)
220 if(error)
221   message(SEND_ERROR "Unexpected error: ${error}")
222 endif()
223
224 string(JSON result ERROR_VARIABLE error LENGTH "${json1}" array)
225 assert_strequal("${result}" 4)
226 if(error)
227   message(SEND_ERROR "Unexpected error: ${error}")
228 endif()
229
230 string(JSON result ERROR_VARIABLE error LENGTH "${json1}" foo)
231 assert_strequal("${result}" "foo-NOTFOUND")
232 assert_strequal("${error}" "LENGTH needs to be called with an element of type ARRAY or OBJECT, got STRING")
233
234 # Test MEMBER
235 string(JSON result ERROR_VARIABLE error MEMBER "${json1}" values 2)
236 assert_strequal("${result}" "number")
237 if(error)
238   message(SEND_ERROR "Unexpected error: ${error}")
239 endif()
240
241 string(JSON result ERROR_VARIABLE error MEMBER "${json1}" values 100)
242 assert_strequal("${result}" "values-100-NOTFOUND")
243 assert_strequal("${error}" "expected an index less than 5 got '100'")
244
245 # Test length loops
246 string(JSON arrayLength ERROR_VARIABLE error LENGTH "${json1}" types array)
247 if(error)
248   message(SEND_ERROR "Unexpected error: ${error}")
249 endif()
250 set(values "")
251 math(EXPR arrayLength "${arrayLength}-1")
252 foreach(index RANGE ${arrayLength})
253   string(JSON value ERROR_VARIABLE error GET "${json1}" types array ${index})
254   if(error)
255     message(SEND_ERROR "Unexpected error: ${error}")
256   endif()
257   list(APPEND values "${value}")
258 endforeach()
259 assert_strequal("${values}" "1;2;3")
260
261 string(JSON valuesLength ERROR_VARIABLE error LENGTH "${json1}" values)
262 if(error)
263   message(SEND_ERROR "Unexpected error: ${error}")
264 endif()
265 set(values "")
266 set(members "")
267 math(EXPR valuesLength "${valuesLength}-1")
268 foreach(index RANGE ${valuesLength})
269   string(JSON member ERROR_VARIABLE error MEMBER "${json1}" values ${index})
270   if(error)
271     message(SEND_ERROR "Unexpected error: ${error}")
272   endif()
273   string(JSON value ERROR_VARIABLE error GET "${json1}" values ${member})
274   if(error)
275     message(SEND_ERROR "Unexpected error: ${error}")
276   endif()
277
278   list(APPEND members "${member}")
279   list(APPEND values "${value}")
280 endforeach()
281 assert_strequal("${members}" "false;null;number;string;true")
282 assert_strequal("${values}" "OFF;;5;foo;ON")
283
284 # Test REMOVE
285 set(json2 [=[{
286   "foo" : "bar",
287   "array" : [5, "val", {"some": "other"}, null]
288 }]=])
289 string(JSON result ERROR_VARIABLE error REMOVE ${json2} foo)
290 assert_json_equal("${error}" "${result}"
291 [=[{
292   "array" : [5, "val", {"some": "other"}, null]
293 }]=])
294
295 string(JSON result ERROR_VARIABLE error REMOVE ${json2} array 1)
296 assert_json_equal("${error}" "${result}"
297 [=[{
298   "foo" : "bar",
299   "array" : [5, {"some": "other"}, null]
300 }]=])
301
302 string(JSON result ERROR_VARIABLE error REMOVE ${json2} array 100)
303 assert_strequal("${result}" "array-100-NOTFOUND")
304 assert_strequal("${error}" "expected an index less than 4 got '100'")
305
306 # Test SET
307 string(JSON result ERROR_VARIABLE error SET ${json2} new 5)
308 assert_json_equal("${error}" "${result}"
309 [=[{
310   "foo" : "bar",
311   "array" : [5, "val", {"some": "other"}, null],
312   "new" : 5
313 }]=])
314
315 string(JSON result ERROR_VARIABLE error SET ${json2} new [=[ {"obj" : false} ]=])
316 assert_json_equal("${error}" "${result}"
317 [=[{
318   "foo" : "bar",
319   "array" : [5, "val", {"some": "other"}, null],
320   "new" : {"obj" : false}
321 }]=])
322
323 string(JSON result ERROR_VARIABLE error SET ${json2} array 0 6)
324 assert_json_equal("${error}" "${result}"
325 [=[{
326   "foo" : "bar",
327   "array" : [6, "val", {"some": "other"}, null]
328 }]=])
329
330 string(JSON result ERROR_VARIABLE error SET ${json2} array 5 [["append"]])
331 assert_json_equal("${error}" "${result}"
332 [=[{
333   "foo" : "bar",
334   "array" : [5, "val", {"some": "other"}, null, "append"]
335 }]=])
336
337 string(JSON result ERROR_VARIABLE error SET ${json2} array 100 [["append"]])
338 assert_json_equal("${error}" "${result}"
339 [=[{
340   "foo" : "bar",
341   "array" : [5, "val", {"some": "other"}, null, "append"]
342 }]=])