JSON.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. function(assert_strequal_error actual expected error)
  7. if(error)
  8. message(SEND_ERROR "Unexpected error: ${error}")
  9. endif()
  10. assert_strequal("${actual}" "${expected}")
  11. endfunction()
  12. function(assert_json_equal error actual expected)
  13. if(error)
  14. message(SEND_ERROR "Unexpected error: ${error}")
  15. endif()
  16. string(JSON eql EQUAL "${actual}" "${expected}")
  17. if(NOT eql)
  18. message(SEND_ERROR "Expected equality got\n ${actual}\n expected\n${expected}")
  19. endif()
  20. endfunction()
  21. # test EQUAL
  22. string(JSON result EQUAL
  23. [=[ {"foo":"bar"} ]=]
  24. [=[
  25. {
  26. "foo": "bar"
  27. }
  28. ]=])
  29. if(NOT result)
  30. message(SEND_ERROR "Expected ON got ${result}")
  31. endif()
  32. string(JSON result EQUAL
  33. [=[ {"foo":"bar"} ]=]
  34. [=[
  35. {
  36. "foo1": "bar"
  37. }
  38. ]=])
  39. if(result)
  40. message(SEND_ERROR "Expected OFF got ${result}")
  41. endif()
  42. set(json1 [=[
  43. {
  44. "foo" : "bar",
  45. "array" : [5, "val", {"some": "other"}, null],
  46. "types" : {
  47. "null" : null,
  48. "number" : 5,
  49. "string" : "foo",
  50. "boolean" : false,
  51. "array" : [1,2,3],
  52. "object" : {}
  53. },
  54. "values" : {
  55. "null" : null,
  56. "number" : 5,
  57. "string" : "foo",
  58. "false" : false,
  59. "true" : true
  60. },
  61. "special" : {
  62. "foo;bar" : "value1",
  63. ";" : "value2",
  64. "semicolon" : ";",
  65. "list" : ["one", "two;three", "four"],
  66. "quote" : "\"",
  67. "\"" : "quote",
  68. "backslash" : "\\",
  69. "\\" : "backslash",
  70. "slash" : "\/",
  71. "\/" : "slash",
  72. "newline" : "\n",
  73. "\n" : "newline",
  74. "return" : "\r",
  75. "\r" : "return",
  76. "tab" : "\t",
  77. "\t" : "tab",
  78. "backspace" : "\b",
  79. "\b" : "backspace",
  80. "formfeed" : "\f",
  81. "\f" : "formfeed"
  82. }
  83. }
  84. ]=])
  85. string(JSON result GET "${json1}" foo)
  86. assert_strequal("${result}" bar)
  87. string(JSON result GET "${json1}" array 0)
  88. assert_strequal("${result}" 5)
  89. string(JSON result GET "${json1}" array 1)
  90. assert_strequal("${result}" val)
  91. string(JSON result GET "${json1}" array 2 some)
  92. assert_strequal("${result}" other)
  93. string(JSON result GET "${json1}" values null)
  94. assert_strequal("${result}" "")
  95. string(JSON result GET "${json1}" values number)
  96. assert_strequal("${result}" 5)
  97. string(JSON result GET "${json1}" values string)
  98. assert_strequal("${result}" "foo")
  99. string(JSON result GET "${json1}" values true)
  100. assert_strequal("${result}" "ON")
  101. if(NOT result)
  102. message(SEND_ERROR "Output did not match expected: TRUE actual: ${result}")
  103. endif()
  104. string(JSON result GET "${json1}" values false)
  105. assert_strequal("${result}" "OFF")
  106. if(result)
  107. message(SEND_ERROR "Output did not match expected: FALSE actual: ${result}")
  108. endif()
  109. string(JSON result ERROR_VARIABLE error GET "${json1}" foo)
  110. assert_strequal_error("${result}" "bar" "${error}")
  111. string(JSON result ERROR_VARIABLE error GET "${json1}" notThere)
  112. assert_strequal("${result}" "notThere-NOTFOUND")
  113. assert_strequal("${error}" "member 'notThere' not found")
  114. string(JSON result ERROR_VARIABLE error GET "${json1}" 0)
  115. assert_strequal("${result}" "0-NOTFOUND")
  116. assert_strequal("${error}" "member '0' not found")
  117. string(JSON result ERROR_VARIABLE error GET "${json1}" array 10)
  118. assert_strequal("${result}" "array-10-NOTFOUND")
  119. assert_strequal("${error}" "expected an index less then 4 got '10'")
  120. string(JSON result ERROR_VARIABLE error GET "${json1}" array 2 some notThere)
  121. assert_strequal("${result}" "array-2-some-notThere-NOTFOUND")
  122. assert_strequal("${error}" "invalid path 'array 2 some notThere', need element of OBJECT or ARRAY type to lookup 'notThere' got STRING")
  123. # special chars
  124. string(JSON result ERROR_VARIABLE error GET "${json1}" special "foo;bar")
  125. assert_strequal_error("${result}" "value1" "${error}")
  126. string(JSON result ERROR_VARIABLE error GET "${json1}" special ";")
  127. assert_strequal_error("${result}" "value2" "${error}")
  128. string(JSON result ERROR_VARIABLE error GET "${json1}" special semicolon)
  129. assert_strequal_error("${result}" ";" "${error}")
  130. string(JSON result ERROR_VARIABLE error GET "${json1}" special list 1)
  131. assert_strequal_error("${result}" "two;three" "${error}")
  132. string(JSON result ERROR_VARIABLE error GET "${json1}")
  133. assert_json_equal("${error}" "${result}" "${json1}")
  134. string(JSON result ERROR_VARIABLE error GET "${json1}" array)
  135. assert_json_equal("${error}" "${result}" [=[ [5, "val", {"some": "other"}, null] ]=])
  136. string(JSON result ERROR_VARIABLE error GET "${json1}" special quote)
  137. assert_strequal_error("${result}" "\"" "${error}")
  138. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\"")
  139. assert_strequal_error("${result}" "quote" "${error}")
  140. string(JSON result ERROR_VARIABLE error GET "${json1}" special backslash)
  141. assert_strequal_error("${result}" "\\" "${error}")
  142. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\\")
  143. assert_strequal_error("${result}" "backslash" "${error}")
  144. string(JSON result ERROR_VARIABLE error GET "${json1}" special slash)
  145. assert_strequal_error("${result}" "/" "${error}")
  146. string(JSON result ERROR_VARIABLE error GET "${json1}" special "/")
  147. assert_strequal_error("${result}" "slash" "${error}")
  148. string(JSON result ERROR_VARIABLE error GET "${json1}" special newline)
  149. assert_strequal_error("${result}" "\n" "${error}")
  150. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\n")
  151. assert_strequal_error("${result}" "newline" "${error}")
  152. string(JSON result ERROR_VARIABLE error GET "${json1}" special return)
  153. assert_strequal_error("${result}" "\r" "${error}")
  154. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\r")
  155. assert_strequal_error("${result}" "return" "${error}")
  156. string(JSON result ERROR_VARIABLE error GET "${json1}" special tab)
  157. assert_strequal_error("${result}" "\t" "${error}")
  158. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\t")
  159. assert_strequal_error("${result}" "tab" "${error}")
  160. file(READ ${CMAKE_CURRENT_LIST_DIR}/json/unicode.json unicode)
  161. string(JSON char ERROR_VARIABLE error GET "${unicode}" backspace)
  162. string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
  163. assert_strequal_error("${result}" "backspace" "${error}")
  164. file(READ ${CMAKE_CURRENT_LIST_DIR}/json/unicode.json unicode)
  165. string(JSON char ERROR_VARIABLE error GET "${unicode}" backspace)
  166. string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
  167. assert_strequal_error("${result}" "backspace" "${error}")
  168. string(JSON char ERROR_VARIABLE error GET "${unicode}" formfeed)
  169. string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
  170. assert_strequal_error("${result}" "formfeed" "${error}")
  171. string(JSON char ERROR_VARIABLE error GET "${unicode}" datalinkescape)
  172. string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
  173. assert_strequal_error("${result}" "datalinkescape" "${error}")
  174. # Test TYPE
  175. string(JSON result TYPE "${json1}" types null)
  176. assert_strequal("${result}" NULL)
  177. string(JSON result TYPE "${json1}" types number)
  178. assert_strequal("${result}" NUMBER)
  179. string(JSON result TYPE "${json1}" types string)
  180. assert_strequal("${result}" STRING)
  181. string(JSON result TYPE "${json1}" types boolean)
  182. assert_strequal("${result}" BOOLEAN)
  183. string(JSON result TYPE "${json1}" types array)
  184. assert_strequal("${result}" ARRAY)
  185. string(JSON result TYPE "${json1}" types object)
  186. assert_strequal("${result}" OBJECT)
  187. # Test LENGTH
  188. string(JSON result ERROR_VARIABLE error LENGTH "${json1}")
  189. assert_strequal("${result}" 5)
  190. if(error)
  191. message(SEND_ERROR "Unexpected error: ${error}")
  192. endif()
  193. string(JSON result ERROR_VARIABLE error LENGTH "${json1}" array)
  194. assert_strequal("${result}" 4)
  195. if(error)
  196. message(SEND_ERROR "Unexpected error: ${error}")
  197. endif()
  198. string(JSON result ERROR_VARIABLE error LENGTH "${json1}" foo)
  199. assert_strequal("${result}" "foo-NOTFOUND")
  200. assert_strequal("${error}" "LENGTH needs to be called with an element of type ARRAY or OBJECT, got STRING")
  201. # Test MEMBER
  202. string(JSON result ERROR_VARIABLE error MEMBER "${json1}" values 2)
  203. assert_strequal("${result}" "number")
  204. if(error)
  205. message(SEND_ERROR "Unexpected error: ${error}")
  206. endif()
  207. string(JSON result ERROR_VARIABLE error MEMBER "${json1}" values 100)
  208. assert_strequal("${result}" "values-100-NOTFOUND")
  209. assert_strequal("${error}" "expected an index less then 5 got '100'")
  210. # Test length loops
  211. string(JSON arrayLength ERROR_VARIABLE error LENGTH "${json1}" types array)
  212. if(error)
  213. message(SEND_ERROR "Unexpected error: ${error}")
  214. endif()
  215. set(values "")
  216. math(EXPR arrayLength "${arrayLength}-1")
  217. foreach(index RANGE ${arrayLength})
  218. string(JSON value ERROR_VARIABLE error GET "${json1}" types array ${index})
  219. if(error)
  220. message(SEND_ERROR "Unexpected error: ${error}")
  221. endif()
  222. list(APPEND values "${value}")
  223. endforeach()
  224. assert_strequal("${values}" "1;2;3")
  225. string(JSON valuesLength ERROR_VARIABLE error LENGTH "${json1}" values)
  226. if(error)
  227. message(SEND_ERROR "Unexpected error: ${error}")
  228. endif()
  229. set(values "")
  230. set(members "")
  231. math(EXPR valuesLength "${valuesLength}-1")
  232. foreach(index RANGE ${valuesLength})
  233. string(JSON member ERROR_VARIABLE error MEMBER "${json1}" values ${index})
  234. if(error)
  235. message(SEND_ERROR "Unexpected error: ${error}")
  236. endif()
  237. string(JSON value ERROR_VARIABLE error GET "${json1}" values ${member})
  238. if(error)
  239. message(SEND_ERROR "Unexpected error: ${error}")
  240. endif()
  241. list(APPEND members "${member}")
  242. list(APPEND values "${value}")
  243. endforeach()
  244. assert_strequal("${members}" "false;null;number;string;true")
  245. assert_strequal("${values}" "OFF;;5;foo;ON")
  246. # Test REMOVE
  247. set(json2 [=[{
  248. "foo" : "bar",
  249. "array" : [5, "val", {"some": "other"}, null]
  250. }]=])
  251. string(JSON result ERROR_VARIABLE error REMOVE ${json2} foo)
  252. assert_json_equal("${error}" "${result}"
  253. [=[{
  254. "array" : [5, "val", {"some": "other"}, null]
  255. }]=])
  256. string(JSON result ERROR_VARIABLE error REMOVE ${json2} array 1)
  257. assert_json_equal("${error}" "${result}"
  258. [=[{
  259. "foo" : "bar",
  260. "array" : [5, {"some": "other"}, null]
  261. }]=])
  262. string(JSON result ERROR_VARIABLE error REMOVE ${json2} array 100)
  263. assert_strequal("${result}" "array-100-NOTFOUND")
  264. assert_strequal("${error}" "expected an index less then 4 got '100'")
  265. # Test SET
  266. string(JSON result ERROR_VARIABLE error SET ${json2} new 5)
  267. assert_json_equal("${error}" "${result}"
  268. [=[{
  269. "foo" : "bar",
  270. "array" : [5, "val", {"some": "other"}, null],
  271. "new" : 5
  272. }]=])
  273. string(JSON result ERROR_VARIABLE error SET ${json2} new [=[ {"obj" : false} ]=])
  274. assert_json_equal("${error}" "${result}"
  275. [=[{
  276. "foo" : "bar",
  277. "array" : [5, "val", {"some": "other"}, null],
  278. "new" : {"obj" : false}
  279. }]=])
  280. string(JSON result ERROR_VARIABLE error SET ${json2} array 0 6)
  281. assert_json_equal("${error}" "${result}"
  282. [=[{
  283. "foo" : "bar",
  284. "array" : [6, "val", {"some": "other"}, null]
  285. }]=])
  286. string(JSON result ERROR_VARIABLE error SET ${json2} array 5 [["append"]])
  287. assert_json_equal("${error}" "${result}"
  288. [=[{
  289. "foo" : "bar",
  290. "array" : [5, "val", {"some": "other"}, null, "append"]
  291. }]=])
  292. string(JSON result ERROR_VARIABLE error SET ${json2} array 100 [["append"]])
  293. assert_json_equal("${error}" "${result}"
  294. [=[{
  295. "foo" : "bar",
  296. "array" : [5, "val", {"some": "other"}, null, "append"]
  297. }]=])