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")
7 function(assert_strequal_error actual expected error)
9 message(SEND_ERROR "Unexpected error: ${error}")
11 assert_strequal("${actual}" "${expected}")
14 function(assert_json_equal error actual expected)
16 message(SEND_ERROR "Unexpected error: ${error}")
18 string(JSON eql EQUAL "${actual}" "${expected}")
20 message(SEND_ERROR "Expected equality got\n ${actual}\n expected\n${expected}")
25 string(JSON result EQUAL
33 message(SEND_ERROR "Expected ON got ${result}")
36 string(JSON result EQUAL
44 message(SEND_ERROR "Expected OFF got ${result}")
52 "array" : [5, "val", {"some": "other"}, null],
72 "list" : ["one", "two;three", "four"],
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)
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")
111 message(SEND_ERROR "Output did not match expected: TRUE actual: ${result}")
113 string(JSON result GET "${json1}" values false)
114 assert_strequal("${result}" "OFF")
116 message(SEND_ERROR "Output did not match expected: FALSE actual: ${result}")
119 string(JSON result ERROR_VARIABLE error GET "${json1}" foo)
120 assert_strequal_error("${result}" "bar" "${error}")
122 string(JSON result ERROR_VARIABLE error GET "${json1}" notThere)
123 assert_strequal("${result}" "notThere-NOTFOUND")
124 assert_strequal("${error}" "member 'notThere' not found")
126 string(JSON result ERROR_VARIABLE error GET "${json1}" 0)
127 assert_strequal("${result}" "0-NOTFOUND")
128 assert_strequal("${error}" "member '0' not found")
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 then 4 got '10'")
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")
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}")
146 string(JSON result ERROR_VARIABLE error GET "${json1}" special list 1)
147 assert_strequal_error("${result}" "two;three" "${error}")
149 string(JSON result ERROR_VARIABLE error GET "${json1}")
150 assert_json_equal("${error}" "${result}" "${json1}")
152 string(JSON result ERROR_VARIABLE error GET "${json1}" array)
153 assert_json_equal("${error}" "${result}" [=[ [5, "val", {"some": "other"}, null] ]=])
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}")
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}")
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}")
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}")
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}")
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}")
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}")
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}")
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}")
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}")
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)
218 string(JSON result ERROR_VARIABLE error LENGTH "${json1}")
219 assert_strequal("${result}" 5)
221 message(SEND_ERROR "Unexpected error: ${error}")
224 string(JSON result ERROR_VARIABLE error LENGTH "${json1}" array)
225 assert_strequal("${result}" 4)
227 message(SEND_ERROR "Unexpected error: ${error}")
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")
235 string(JSON result ERROR_VARIABLE error MEMBER "${json1}" values 2)
236 assert_strequal("${result}" "number")
238 message(SEND_ERROR "Unexpected error: ${error}")
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 then 5 got '100'")
246 string(JSON arrayLength ERROR_VARIABLE error LENGTH "${json1}" types array)
248 message(SEND_ERROR "Unexpected error: ${error}")
251 math(EXPR arrayLength "${arrayLength}-1")
252 foreach(index RANGE ${arrayLength})
253 string(JSON value ERROR_VARIABLE error GET "${json1}" types array ${index})
255 message(SEND_ERROR "Unexpected error: ${error}")
257 list(APPEND values "${value}")
259 assert_strequal("${values}" "1;2;3")
261 string(JSON valuesLength ERROR_VARIABLE error LENGTH "${json1}" values)
263 message(SEND_ERROR "Unexpected error: ${error}")
267 math(EXPR valuesLength "${valuesLength}-1")
268 foreach(index RANGE ${valuesLength})
269 string(JSON member ERROR_VARIABLE error MEMBER "${json1}" values ${index})
271 message(SEND_ERROR "Unexpected error: ${error}")
273 string(JSON value ERROR_VARIABLE error GET "${json1}" values ${member})
275 message(SEND_ERROR "Unexpected error: ${error}")
278 list(APPEND members "${member}")
279 list(APPEND values "${value}")
281 assert_strequal("${members}" "false;null;number;string;true")
282 assert_strequal("${values}" "OFF;;5;foo;ON")
287 "array" : [5, "val", {"some": "other"}, null]
289 string(JSON result ERROR_VARIABLE error REMOVE ${json2} foo)
290 assert_json_equal("${error}" "${result}"
292 "array" : [5, "val", {"some": "other"}, null]
295 string(JSON result ERROR_VARIABLE error REMOVE ${json2} array 1)
296 assert_json_equal("${error}" "${result}"
299 "array" : [5, {"some": "other"}, null]
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 then 4 got '100'")
307 string(JSON result ERROR_VARIABLE error SET ${json2} new 5)
308 assert_json_equal("${error}" "${result}"
311 "array" : [5, "val", {"some": "other"}, null],
315 string(JSON result ERROR_VARIABLE error SET ${json2} new [=[ {"obj" : false} ]=])
316 assert_json_equal("${error}" "${result}"
319 "array" : [5, "val", {"some": "other"}, null],
320 "new" : {"obj" : false}
323 string(JSON result ERROR_VARIABLE error SET ${json2} array 0 6)
324 assert_json_equal("${error}" "${result}"
327 "array" : [6, "val", {"some": "other"}, null]
330 string(JSON result ERROR_VARIABLE error SET ${json2} array 5 [["append"]])
331 assert_json_equal("${error}" "${result}"
334 "array" : [5, "val", {"some": "other"}, null, "append"]
337 string(JSON result ERROR_VARIABLE error SET ${json2} array 100 [["append"]])
338 assert_json_equal("${error}" "${result}"
341 "array" : [5, "val", {"some": "other"}, null, "append"]