TestComments.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #include "TestSuite.h"
  2. #include "../Source/JSONNode.h"
  3. #ifdef JSON_COMMENTS
  4. void TestSuite::TestComments(void){
  5. UnitTest::SetPrefix("TestComments.cpp - Comments");
  6. #ifdef JSON_READ_PRIORITY
  7. #ifdef JSON_LIBRARY
  8. JSONNODE * one = json_new(JSON_NULL);
  9. json_set_i(one, 15);
  10. JSONNODE * two = json_new(JSON_NULL);
  11. json_set_i(two, 15);
  12. assertTrue(json_equal(one, two));
  13. json_set_comment(one, JSON_TEXT("Number"));
  14. assertTrue(json_equal(one, two));
  15. json_delete(one);
  16. json_delete(two);
  17. JSONNODE * test = json_parse(JSON_TEXT("#one line comment\n{\"hello\":\"world\"}"));
  18. assertEquals(json_type(test), JSON_NODE);
  19. assertEquals(json_size(test), 1);
  20. json_char * res = json_as_string(json_at(test, 0));
  21. assertCStringSame(res, JSON_TEXT("world"));
  22. json_free(res);
  23. res = json_name(json_at(test, 0));
  24. assertCStringSame(res, JSON_TEXT("hello"));
  25. json_free(res);
  26. res = json_get_comment(test);
  27. assertCStringSame(res, JSON_TEXT("one line comment"));
  28. json_free(res);
  29. json_delete(test);
  30. test = json_parse(JSON_TEXT("//one line comment\n{\"hello\":\"world\"}"));
  31. assertEquals(json_type(test), JSON_NODE);
  32. assertEquals(json_size(test), 1);
  33. res = json_as_string(json_at(test, 0));
  34. assertCStringSame(res, JSON_TEXT("world"));
  35. json_free(res);
  36. res = json_name(json_at(test, 0));
  37. assertCStringSame(res, JSON_TEXT("hello"));
  38. json_free(res);
  39. res = json_get_comment(test);
  40. assertCStringSame(res, JSON_TEXT("one line comment"));
  41. json_free(res);
  42. json_delete(test);
  43. test = json_parse(JSON_TEXT("/*one line comment*/{\"hello\":\"world\"}"));
  44. assertEquals(json_type(test), JSON_NODE);
  45. assertEquals(json_size(test), 1);
  46. res = json_as_string(json_at(test, 0));
  47. assertCStringSame(res, JSON_TEXT("world"));
  48. json_free(res);
  49. res = json_name(json_at(test, 0));
  50. assertCStringSame(res, JSON_TEXT("hello"));
  51. json_free(res);
  52. res = json_get_comment(test);
  53. assertCStringSame(res, JSON_TEXT("one line comment"));
  54. json_free(res);
  55. json_delete(test);
  56. test = json_parse(JSON_TEXT("#one line comment\n#another\n{\"hello\":\"world\"}"));
  57. assertEquals(json_type(test), JSON_NODE);
  58. assertEquals(json_size(test), 1);
  59. res = json_as_string(json_at(test, 0));
  60. assertCStringSame(res, JSON_TEXT("world"));
  61. json_free(res);
  62. res = json_name(json_at(test, 0));
  63. assertCStringSame(res, JSON_TEXT("hello"));
  64. json_free(res);
  65. res = json_get_comment(test);
  66. assertCStringSame(res, JSON_TEXT("one line comment\nanother"));
  67. json_free(res);
  68. json_delete(test);
  69. test = json_parse(JSON_TEXT("//one line comment\n//another\n{\"hello\":\"world\"}"));
  70. assertEquals(json_type(test), JSON_NODE);
  71. assertEquals(json_size(test), 1);
  72. res = json_as_string(json_at(test, 0));
  73. assertCStringSame(res, JSON_TEXT("world"));
  74. json_free(res);
  75. res = json_name(json_at(test, 0));
  76. assertCStringSame(res, JSON_TEXT("hello"));
  77. json_free(res);
  78. res = json_get_comment(test);
  79. assertCStringSame(res, JSON_TEXT("one line comment\nanother"));
  80. json_free(res);
  81. json_delete(test);
  82. test = json_parse(JSON_TEXT("/*one line comment*//*another*/{\"hello\":\"world\"}"));
  83. assertEquals(json_type(test), JSON_NODE);
  84. assertEquals(json_size(test), 1);
  85. res = json_as_string(json_at(test, 0));
  86. assertCStringSame(res, JSON_TEXT("world"));
  87. json_free(res);
  88. res = json_name(json_at(test, 0));
  89. assertCStringSame(res, JSON_TEXT("hello"));
  90. json_free(res);
  91. res = json_get_comment(test);
  92. assertCStringSame(res, JSON_TEXT("one line comment\nanother"));
  93. json_free(res);
  94. json_delete(test);
  95. test = json_parse(JSON_TEXT("#one line comment\n{#comment\n\"hello\":\"world\"}"));
  96. assertEquals(json_type(test), JSON_NODE);
  97. assertEquals(json_size(test), 1);
  98. res = json_get_comment(test);
  99. assertCStringSame(res, JSON_TEXT("one line comment"));
  100. json_free(res);
  101. res = json_as_string(json_at(test, 0));
  102. assertCStringSame(res, JSON_TEXT("world"));
  103. json_free(res);
  104. res = json_name(json_at(test, 0));
  105. assertCStringSame(res, JSON_TEXT("hello"));
  106. json_free(res);
  107. res = json_get_comment(json_at(test, 0));
  108. assertCStringSame(res, JSON_TEXT("comment"));
  109. json_free(res);
  110. json_delete(test);
  111. test = json_parse(JSON_TEXT("//one line comment\n{//comment\n\"hello\":\"world\"}"));
  112. assertEquals(json_type(test), JSON_NODE);
  113. assertEquals(json_size(test), 1);
  114. res = json_get_comment(test);
  115. assertCStringSame(res, JSON_TEXT("one line comment"));
  116. json_free(res);
  117. res = json_as_string(json_at(test, 0));
  118. assertCStringSame(res, JSON_TEXT("world"));
  119. json_free(res);
  120. res = json_name(json_at(test, 0));
  121. assertCStringSame(res, JSON_TEXT("hello"));
  122. json_free(res);
  123. res = json_get_comment(json_at(test, 0));
  124. assertCStringSame(res, JSON_TEXT("comment"));
  125. json_free(res);
  126. json_delete(test);
  127. test = json_parse(JSON_TEXT("/*one line comment*/{/*comment*/\"hello\":\"world\"}"));
  128. assertEquals(json_type(test), JSON_NODE);
  129. assertEquals(json_size(test), 1);
  130. res = json_get_comment(test);
  131. assertCStringSame(res, JSON_TEXT("one line comment"));
  132. json_free(res);
  133. res = json_as_string(json_at(test, 0));
  134. assertCStringSame(res, JSON_TEXT("world"));
  135. json_free(res);
  136. res = json_name(json_at(test, 0));
  137. assertCStringSame(res, JSON_TEXT("hello"));
  138. json_free(res);
  139. res = json_get_comment(json_at(test, 0));
  140. assertCStringSame(res, JSON_TEXT("comment"));
  141. json_free(res);
  142. json_delete(test);
  143. test = json_parse(JSON_TEXT("#one line comment\n#another\n{#comment\n#comment2\n\"hello\":\"world\"}"));
  144. assertEquals(json_type(test), JSON_NODE);
  145. assertEquals(json_size(test), 1);
  146. res = json_get_comment(test);
  147. assertCStringSame(res, JSON_TEXT("one line comment\nanother"));
  148. json_free(res);
  149. res = json_as_string(json_at(test, 0));
  150. assertCStringSame(res, JSON_TEXT("world"));
  151. json_free(res);
  152. res = json_name(json_at(test, 0));
  153. assertCStringSame(res, JSON_TEXT("hello"));
  154. json_free(res);
  155. res = json_get_comment(json_at(test, 0));
  156. assertCStringSame(res, JSON_TEXT("comment\ncomment2"));
  157. json_free(res);
  158. json_delete(test);
  159. test = json_parse(JSON_TEXT("//one line comment\n//another\n{//comment\n//comment2\n\"hello\":\"world\"}"));
  160. assertEquals(json_type(test), JSON_NODE);
  161. assertEquals(json_size(test), 1);
  162. res = json_get_comment(test);
  163. assertCStringSame(res, JSON_TEXT("one line comment\nanother"));
  164. json_free(res);
  165. res = json_as_string(json_at(test, 0));
  166. assertCStringSame(res, JSON_TEXT("world"));
  167. json_free(res);
  168. res = json_name(json_at(test, 0));
  169. assertCStringSame(res, JSON_TEXT("hello"));
  170. json_free(res);
  171. res = json_get_comment(json_at(test, 0));
  172. assertCStringSame(res, JSON_TEXT("comment\ncomment2"));
  173. json_free(res);
  174. json_delete(test);
  175. test = json_parse(JSON_TEXT("/*one line comment*//*another*/{/*comment*//*comment2*/\"hello\":\"world\"}"));
  176. assertEquals(json_type(test), JSON_NODE);
  177. assertEquals(json_size(test), 1);
  178. res = json_get_comment(test);
  179. assertCStringSame(res, JSON_TEXT("one line comment\nanother"));
  180. json_free(res);
  181. res = json_as_string(json_at(test, 0));
  182. assertCStringSame(res, JSON_TEXT("world"));
  183. json_free(res);
  184. res = json_name(json_at(test, 0));
  185. assertCStringSame(res, JSON_TEXT("hello"));
  186. json_free(res);
  187. res = json_get_comment(json_at(test, 0));
  188. assertCStringSame(res, JSON_TEXT("comment\ncomment2"));
  189. json_free(res);
  190. json_delete(test);
  191. test = json_parse(JSON_TEXT("/*one line comment*//*another*/{/*comment*//*comment2*/\"hello\":\"world\", #comment\n\"hi\" : \"mars\"}"));
  192. assertEquals(json_type(test), JSON_NODE);
  193. assertEquals(json_size(test), 2);
  194. res = json_get_comment(test);
  195. assertCStringSame(res, JSON_TEXT("one line comment\nanother"));
  196. json_free(res);
  197. res = json_as_string(json_at(test, 0));
  198. assertCStringSame(res, JSON_TEXT("world"));
  199. json_free(res);
  200. res = json_name(json_at(test, 0));
  201. assertCStringSame(res, JSON_TEXT("hello"));
  202. json_free(res);
  203. res = json_get_comment(json_at(test, 0));
  204. assertCStringSame(res, JSON_TEXT("comment\ncomment2"));
  205. json_free(res);
  206. res = json_as_string(json_at(test, 1));
  207. assertCStringSame(res, JSON_TEXT("mars"));
  208. json_free(res);
  209. res = json_name(json_at(test, 1));
  210. assertCStringSame(res, JSON_TEXT("hi"));
  211. json_free(res);
  212. res = json_get_comment(json_at(test, 1));
  213. assertCStringSame(res, JSON_TEXT("comment"));
  214. json_free(res);
  215. json_delete(test);
  216. test = json_parse(JSON_TEXT("/*one line comment*//*another*/{/*comment*//*comment2*/\"hello\":\"world\", #comment\n\"hi\" : \"mars\", //comment 2\n\"and\" : \"pluto\"}"));
  217. assertEquals(json_type(test), JSON_NODE);
  218. assertEquals(json_size(test), 3);
  219. res = json_get_comment(test);
  220. assertCStringSame(res, JSON_TEXT("one line comment\nanother"));
  221. json_free(res);
  222. res = json_as_string(json_at(test, 0));
  223. assertCStringSame(res, JSON_TEXT("world"));
  224. json_free(res);
  225. res = json_name(json_at(test, 0));
  226. assertCStringSame(res, JSON_TEXT("hello"));
  227. json_free(res);
  228. res = json_get_comment(json_at(test, 0));
  229. assertCStringSame(res, JSON_TEXT("comment\ncomment2"));
  230. json_free(res);
  231. res = json_as_string(json_at(test, 1));
  232. assertCStringSame(res, JSON_TEXT("mars"));
  233. json_free(res);
  234. res = json_name(json_at(test, 1));
  235. assertCStringSame(res, JSON_TEXT("hi"));
  236. json_free(res);
  237. res = json_get_comment(json_at(test, 1));
  238. assertCStringSame(res, JSON_TEXT("comment"));
  239. json_free(res);
  240. res = json_as_string(json_at(test, 2));
  241. assertCStringSame(res, JSON_TEXT("pluto"));
  242. json_free(res);
  243. res = json_name(json_at(test, 2));
  244. assertCStringSame(res, JSON_TEXT("and"));
  245. json_free(res);
  246. res = json_get_comment(json_at(test, 2));
  247. assertCStringSame(res, JSON_TEXT("comment 2"));
  248. json_free(res);
  249. json_delete(test);
  250. test = json_parse(JSON_TEXT("#array\n [#one\n\"hello\", //two\n\"world\", /*three*/\"mars\"]\r\n"));
  251. assertEquals(json_type(test), JSON_ARRAY);
  252. assertEquals(json_size(test), 3);
  253. res = json_get_comment(test);
  254. assertCStringSame(res, JSON_TEXT("array"));
  255. json_free(res);
  256. res = json_as_string(json_at(test, 0));
  257. assertCStringSame(res, JSON_TEXT("hello"));
  258. json_free(res);
  259. res = json_get_comment(json_at(test, 0));
  260. assertCStringSame(res, JSON_TEXT("one"));
  261. json_free(res);
  262. res = json_as_string(json_at(test, 1));
  263. assertCStringSame(res, JSON_TEXT("world"));
  264. json_free(res);
  265. res = json_get_comment(json_at(test, 1));
  266. assertCStringSame(res, JSON_TEXT("two"));
  267. json_free(res);
  268. res = json_as_string(json_at(test, 2));
  269. assertCStringSame(res, JSON_TEXT("mars"));
  270. json_free(res);
  271. res = json_get_comment(json_at(test, 2));
  272. assertCStringSame(res, JSON_TEXT("three"));
  273. json_free(res);
  274. json_delete(test);
  275. #else
  276. JSONNode one;
  277. one = 15;
  278. JSONNode two;
  279. two = 15;
  280. assertEquals(one, two);
  281. one.set_comment(JSON_TEXT("Number"));
  282. assertEquals(one, two);
  283. JSONNode test = libjson::parse(JSON_TEXT("#one line comment\n{\"hello\":\"world\"}"));
  284. assertEquals(test.type(), JSON_NODE);
  285. assertEquals(test.size(), 1);
  286. assertEquals(test[0], JSON_TEXT("world"));
  287. assertEquals(test[0].name(), JSON_TEXT("hello"));
  288. assertEquals(test.get_comment(), JSON_TEXT("one line comment"));
  289. test = libjson::parse(JSON_TEXT("//one line comment\n{\"hello\":\"world\"}"));
  290. assertEquals(test.type(), JSON_NODE);
  291. assertEquals(test.size(), 1);
  292. assertEquals(test[0], JSON_TEXT("world"));
  293. assertEquals(test[0].name(), JSON_TEXT("hello"));
  294. assertEquals(test.get_comment(), JSON_TEXT("one line comment"));
  295. test = libjson::parse(JSON_TEXT("/*one line comment*/{\"hello\":\"world\"}"));
  296. assertEquals(test.type(), JSON_NODE);
  297. assertEquals(test.size(), 1);
  298. assertEquals(test[0], JSON_TEXT("world"));
  299. assertEquals(test[0].name(), JSON_TEXT("hello"));
  300. assertEquals(test.get_comment(), JSON_TEXT("one line comment"));
  301. test = libjson::parse(JSON_TEXT("#one line comment\n#another\n{\"hello\":\"world\"}"));
  302. assertEquals(test.type(), JSON_NODE);
  303. assertEquals(test.size(), 1);
  304. assertEquals(test[0], JSON_TEXT("world"));
  305. assertEquals(test[0].name(), JSON_TEXT("hello"));
  306. assertEquals(test.get_comment(), JSON_TEXT("one line comment\nanother"));
  307. test = libjson::parse(JSON_TEXT("//one line comment\n//another\n{\"hello\":\"world\"}"));
  308. assertEquals(test.type(), JSON_NODE);
  309. assertEquals(test.size(), 1);
  310. assertEquals(test[0], JSON_TEXT("world"));
  311. assertEquals(test[0].name(), JSON_TEXT("hello"));
  312. assertEquals(test.get_comment(), JSON_TEXT("one line comment\nanother"));
  313. test = libjson::parse(JSON_TEXT("/*one line comment*//*another*/{\"hello\":\"world\"}"));
  314. assertEquals(test.type(), JSON_NODE);
  315. assertEquals(test.size(), 1);
  316. assertEquals(test[0], JSON_TEXT("world"));
  317. assertEquals(test[0].name(), JSON_TEXT("hello"));
  318. assertEquals(test.get_comment(), JSON_TEXT("one line comment\nanother"));
  319. test = libjson::parse(JSON_TEXT("#one line comment\n{#comment\n\"hello\":\"world\"}"));
  320. assertEquals(test.type(), JSON_NODE);
  321. assertEquals(test.size(), 1);
  322. assertEquals(test.get_comment(), JSON_TEXT("one line comment"));
  323. assertEquals(test[0], JSON_TEXT("world"));
  324. assertEquals(test[0].name(), JSON_TEXT("hello"));
  325. assertEquals(test[0].get_comment(), JSON_TEXT("comment"));
  326. test = libjson::parse(JSON_TEXT("//one line comment\n{//comment\n\"hello\":\"world\"}"));
  327. assertEquals(test.type(), JSON_NODE);
  328. assertEquals(test.size(), 1);
  329. assertEquals(test.get_comment(), JSON_TEXT("one line comment"));
  330. assertEquals(test[0], JSON_TEXT("world"));
  331. assertEquals(test[0].name(), JSON_TEXT("hello"));
  332. assertEquals(test[0].get_comment(), JSON_TEXT("comment"));
  333. test = libjson::parse(JSON_TEXT("/*one line comment*/{/*comment*/\"hello\":\"world\"}"));
  334. assertEquals(test.type(), JSON_NODE);
  335. assertEquals(test.size(), 1);
  336. assertEquals(test.get_comment(), JSON_TEXT("one line comment"));
  337. assertEquals(test[0], JSON_TEXT("world"));
  338. assertEquals(test[0].name(), JSON_TEXT("hello"));
  339. assertEquals(test[0].get_comment(), JSON_TEXT("comment"));
  340. test = libjson::parse(JSON_TEXT("#one line comment\n#another\n{#comment\n#comment2\n\"hello\":\"world\"}"));
  341. assertEquals(test.type(), JSON_NODE);
  342. assertEquals(test.size(), 1);
  343. assertEquals(test[0].name(), JSON_TEXT("hello"));
  344. assertEquals(test[0], JSON_TEXT("world"));
  345. assertEquals(test.get_comment(), JSON_TEXT("one line comment\nanother"));
  346. assertEquals(test[0].get_comment(), JSON_TEXT("comment\ncomment2"));
  347. test = libjson::parse(JSON_TEXT("//one line comment\n//another\n{//comment\n//comment2\n\"hello\":\"world\"}"));
  348. assertEquals(test.type(), JSON_NODE);
  349. assertEquals(test.size(), 1);
  350. assertEquals(test[0].name(), JSON_TEXT("hello"));
  351. assertEquals(test[0], JSON_TEXT("world"));
  352. assertEquals(test.get_comment(), JSON_TEXT("one line comment\nanother"));
  353. assertEquals(test[0].get_comment(), JSON_TEXT("comment\ncomment2"));
  354. test = libjson::parse(JSON_TEXT("/*one line comment*//*another*/{/*comment*//*comment2*/\"hello\":\"world\"}"));
  355. assertEquals(test.type(), JSON_NODE);
  356. assertEquals(test.size(), 1);
  357. assertEquals(test[0].name(), JSON_TEXT("hello"));
  358. assertEquals(test[0], JSON_TEXT("world"));
  359. assertEquals(test.get_comment(), JSON_TEXT("one line comment\nanother"));
  360. assertEquals(test[0].get_comment(), JSON_TEXT("comment\ncomment2"));
  361. test = libjson::parse(JSON_TEXT("/*one line comment*//*another*/{/*comment*//*comment2*/\"hello\":\"world\", #comment\n\"hi\" : \"mars\"}"));
  362. assertEquals(test.type(), JSON_NODE);
  363. assertEquals(test.size(), 2);
  364. assertEquals(test[0].name(), JSON_TEXT("hello"));
  365. assertEquals(test[1].name(), JSON_TEXT("hi"));
  366. assertEquals(test[0], JSON_TEXT("world"));
  367. assertEquals(test[1], JSON_TEXT("mars"));
  368. assertEquals(test.get_comment(), JSON_TEXT("one line comment\nanother"));
  369. assertEquals(test[0].get_comment(), JSON_TEXT("comment\ncomment2"));
  370. assertEquals(test[1].get_comment(), JSON_TEXT("comment"));
  371. test = libjson::parse(JSON_TEXT("/*one line comment*//*another*/{/*comment*//*comment2*/\"hello\":\"world\", #comment\n\"hi\" : \"mars\", //comment 2\n\"and\" : \"pluto\"}"));
  372. assertEquals(test.type(), JSON_NODE);
  373. assertEquals(test.size(), 3);
  374. assertEquals(test[0].name(), JSON_TEXT("hello"));
  375. assertEquals(test[1].name(), JSON_TEXT("hi"));
  376. assertEquals(test[2].name(), JSON_TEXT("and"));
  377. assertEquals(test[0], JSON_TEXT("world"));
  378. assertEquals(test[1], JSON_TEXT("mars"));
  379. assertEquals(test[2], JSON_TEXT("pluto"));
  380. assertEquals(test.get_comment(), JSON_TEXT("one line comment\nanother"));
  381. assertEquals(test[0].get_comment(), JSON_TEXT("comment\ncomment2"));
  382. assertEquals(test[1].get_comment(), JSON_TEXT("comment"));
  383. assertEquals(test[2].get_comment(), JSON_TEXT("comment 2"));
  384. test = libjson::parse(JSON_TEXT("#array\n [#one\n\"hello\", //two\n\"world\", /*three*/\"mars\"]\r\n"));
  385. assertEquals(test.type(), JSON_ARRAY);
  386. assertEquals(test.get_comment(), JSON_TEXT("array"));
  387. assertEquals(test.size(), 3);
  388. assertEquals(test[0], JSON_TEXT("hello"));
  389. assertEquals(test[0].get_comment(), JSON_TEXT("one"));
  390. assertEquals(test[1], JSON_TEXT("world"));
  391. assertEquals(test[1].get_comment(), JSON_TEXT("two"));
  392. assertEquals(test[2], JSON_TEXT("mars"));
  393. assertEquals(test[2].get_comment(), JSON_TEXT("three"));
  394. #endif
  395. #endif
  396. }
  397. #endif