enable_comments_1.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #define ARDUINOJSON_ENABLE_COMMENTS 1
  5. #include <ArduinoJson.h>
  6. #include <catch.hpp>
  7. TEST_CASE("Comments in arrays") {
  8. DynamicJsonDocument doc(2048);
  9. SECTION("Block comments") {
  10. SECTION("Before opening bracket") {
  11. DeserializationError err =
  12. deserializeJson(doc, "/*COMMENT*/ [\"hello\"]");
  13. JsonArray arr = doc.as<JsonArray>();
  14. REQUIRE(err == DeserializationError::Ok);
  15. REQUIRE(1 == arr.size());
  16. REQUIRE(arr[0] == "hello");
  17. }
  18. SECTION("After opening bracket") {
  19. DeserializationError err =
  20. deserializeJson(doc, "[/*COMMENT*/ \"hello\"]");
  21. JsonArray arr = doc.as<JsonArray>();
  22. REQUIRE(err == DeserializationError::Ok);
  23. REQUIRE(1 == arr.size());
  24. REQUIRE(arr[0] == "hello");
  25. }
  26. SECTION("Before closing bracket") {
  27. DeserializationError err = deserializeJson(doc, "[\"hello\"/*COMMENT*/]");
  28. JsonArray arr = doc.as<JsonArray>();
  29. REQUIRE(err == DeserializationError::Ok);
  30. REQUIRE(1 == arr.size());
  31. REQUIRE(arr[0] == "hello");
  32. }
  33. SECTION("After closing bracket") {
  34. DeserializationError err = deserializeJson(doc, "[\"hello\"]/*COMMENT*/");
  35. JsonArray arr = doc.as<JsonArray>();
  36. REQUIRE(err == DeserializationError::Ok);
  37. REQUIRE(1 == arr.size());
  38. REQUIRE(arr[0] == "hello");
  39. }
  40. SECTION("Before comma") {
  41. DeserializationError err =
  42. deserializeJson(doc, "[\"hello\"/*COMMENT*/,\"world\"]");
  43. JsonArray arr = doc.as<JsonArray>();
  44. REQUIRE(err == DeserializationError::Ok);
  45. REQUIRE(2 == arr.size());
  46. REQUIRE(arr[0] == "hello");
  47. REQUIRE(arr[1] == "world");
  48. }
  49. SECTION("After comma") {
  50. DeserializationError err =
  51. deserializeJson(doc, "[\"hello\",/*COMMENT*/ \"world\"]");
  52. JsonArray arr = doc.as<JsonArray>();
  53. REQUIRE(err == DeserializationError::Ok);
  54. REQUIRE(2 == arr.size());
  55. REQUIRE(arr[0] == "hello");
  56. REQUIRE(arr[1] == "world");
  57. }
  58. SECTION("/*/") {
  59. DeserializationError err = deserializeJson(doc, "[/*/\n]");
  60. REQUIRE(err == DeserializationError::IncompleteInput);
  61. }
  62. SECTION("Unfinished comment") {
  63. DeserializationError err = deserializeJson(doc, "[/*COMMENT]");
  64. REQUIRE(err == DeserializationError::IncompleteInput);
  65. }
  66. SECTION("Final slash missing") {
  67. DeserializationError err = deserializeJson(doc, "[/*COMMENT*]");
  68. REQUIRE(err == DeserializationError::IncompleteInput);
  69. }
  70. }
  71. SECTION("Trailing comments") {
  72. SECTION("Before opening bracket") {
  73. DeserializationError err =
  74. deserializeJson(doc, "//COMMENT\n\t[\"hello\"]");
  75. JsonArray arr = doc.as<JsonArray>();
  76. REQUIRE(err == DeserializationError::Ok);
  77. REQUIRE(1 == arr.size());
  78. REQUIRE(arr[0] == "hello");
  79. }
  80. SECTION("After opening bracket") {
  81. DeserializationError err = deserializeJson(doc, "[//COMMENT\n\"hello\"]");
  82. JsonArray arr = doc.as<JsonArray>();
  83. REQUIRE(err == DeserializationError::Ok);
  84. REQUIRE(1 == arr.size());
  85. REQUIRE(arr[0] == "hello");
  86. }
  87. SECTION("Before closing bracket") {
  88. DeserializationError err =
  89. deserializeJson(doc, "[\"hello\"//COMMENT\r\n]");
  90. JsonArray arr = doc.as<JsonArray>();
  91. REQUIRE(err == DeserializationError::Ok);
  92. REQUIRE(1 == arr.size());
  93. REQUIRE(arr[0] == "hello");
  94. }
  95. SECTION("After closing bracket") {
  96. DeserializationError err = deserializeJson(doc, "[\"hello\"]//COMMENT\n");
  97. JsonArray arr = doc.as<JsonArray>();
  98. REQUIRE(err == DeserializationError::Ok);
  99. REQUIRE(1 == arr.size());
  100. REQUIRE(arr[0] == "hello");
  101. }
  102. SECTION("Before comma") {
  103. DeserializationError err =
  104. deserializeJson(doc, "[\"hello\"//COMMENT\n,\"world\"]");
  105. JsonArray arr = doc.as<JsonArray>();
  106. REQUIRE(err == DeserializationError::Ok);
  107. REQUIRE(2 == arr.size());
  108. REQUIRE(arr[0] == "hello");
  109. REQUIRE(arr[1] == "world");
  110. }
  111. SECTION("After comma") {
  112. DeserializationError err =
  113. deserializeJson(doc, "[\"hello\",//COMMENT\n\"world\"]");
  114. JsonArray arr = doc.as<JsonArray>();
  115. REQUIRE(err == DeserializationError::Ok);
  116. REQUIRE(2 == arr.size());
  117. REQUIRE(arr[0] == "hello");
  118. REQUIRE(arr[1] == "world");
  119. }
  120. SECTION("Invalid comment") {
  121. DeserializationError err = deserializeJson(doc, "[/COMMENT\n]");
  122. REQUIRE(err == DeserializationError::InvalidInput);
  123. }
  124. SECTION("End document with comment") {
  125. DeserializationError err = deserializeJson(doc, "[//COMMENT");
  126. REQUIRE(err == DeserializationError::IncompleteInput);
  127. }
  128. }
  129. }
  130. TEST_CASE("Comments in objects") {
  131. DynamicJsonDocument doc(2048);
  132. SECTION("Block comments") {
  133. SECTION("Before opening brace") {
  134. DeserializationError err =
  135. deserializeJson(doc, "/*COMMENT*/ {\"hello\":\"world\"}");
  136. JsonObject obj = doc.as<JsonObject>();
  137. REQUIRE(err == DeserializationError::Ok);
  138. REQUIRE(obj["hello"] == "world");
  139. }
  140. SECTION("After opening brace") {
  141. DeserializationError err =
  142. deserializeJson(doc, "{/*COMMENT*/\"hello\":\"world\"}");
  143. JsonObject obj = doc.as<JsonObject>();
  144. REQUIRE(err == DeserializationError::Ok);
  145. REQUIRE(obj["hello"] == "world");
  146. }
  147. SECTION("Before colon") {
  148. DeserializationError err =
  149. deserializeJson(doc, "{\"hello\"/*COMMENT*/:\"world\"}");
  150. JsonObject obj = doc.as<JsonObject>();
  151. REQUIRE(err == DeserializationError::Ok);
  152. REQUIRE(obj["hello"] == "world");
  153. }
  154. SECTION("After colon") {
  155. DeserializationError err =
  156. deserializeJson(doc, "{\"hello\":/*COMMENT*/\"world\"}");
  157. JsonObject obj = doc.as<JsonObject>();
  158. REQUIRE(err == DeserializationError::Ok);
  159. REQUIRE(obj["hello"] == "world");
  160. }
  161. SECTION("Before closing brace") {
  162. DeserializationError err =
  163. deserializeJson(doc, "{\"hello\":\"world\"/*COMMENT*/}");
  164. JsonObject obj = doc.as<JsonObject>();
  165. REQUIRE(err == DeserializationError::Ok);
  166. REQUIRE(obj["hello"] == "world");
  167. }
  168. SECTION("After closing brace") {
  169. DeserializationError err =
  170. deserializeJson(doc, "{\"hello\":\"world\"}/*COMMENT*/");
  171. JsonObject obj = doc.as<JsonObject>();
  172. REQUIRE(err == DeserializationError::Ok);
  173. REQUIRE(obj["hello"] == "world");
  174. }
  175. SECTION("Before comma") {
  176. DeserializationError err = deserializeJson(
  177. doc, "{\"hello\":\"world\"/*COMMENT*/,\"answer\":42}");
  178. JsonObject obj = doc.as<JsonObject>();
  179. REQUIRE(err == DeserializationError::Ok);
  180. REQUIRE(obj["hello"] == "world");
  181. REQUIRE(obj["answer"] == 42);
  182. }
  183. SECTION("After comma") {
  184. DeserializationError err = deserializeJson(
  185. doc, "{\"hello\":\"world\",/*COMMENT*/\"answer\":42}");
  186. JsonObject obj = doc.as<JsonObject>();
  187. REQUIRE(err == DeserializationError::Ok);
  188. REQUIRE(obj["hello"] == "world");
  189. REQUIRE(obj["answer"] == 42);
  190. }
  191. }
  192. SECTION("Trailing comments") {
  193. SECTION("Before opening brace") {
  194. DeserializationError err =
  195. deserializeJson(doc, "//COMMENT\n {\"hello\":\"world\"}");
  196. JsonObject obj = doc.as<JsonObject>();
  197. REQUIRE(err == DeserializationError::Ok);
  198. REQUIRE(obj["hello"] == "world");
  199. }
  200. SECTION("After opening brace") {
  201. DeserializationError err =
  202. deserializeJson(doc, "{//COMMENT\n\"hello\":\"world\"}");
  203. JsonObject obj = doc.as<JsonObject>();
  204. REQUIRE(err == DeserializationError::Ok);
  205. REQUIRE(obj["hello"] == "world");
  206. }
  207. SECTION("Before colon") {
  208. DeserializationError err =
  209. deserializeJson(doc, "{\"hello\"//COMMENT\n:\"world\"}");
  210. JsonObject obj = doc.as<JsonObject>();
  211. REQUIRE(err == DeserializationError::Ok);
  212. REQUIRE(obj["hello"] == "world");
  213. }
  214. SECTION("After colon") {
  215. DeserializationError err =
  216. deserializeJson(doc, "{\"hello\"://COMMENT\n\"world\"}");
  217. JsonObject obj = doc.as<JsonObject>();
  218. REQUIRE(err == DeserializationError::Ok);
  219. REQUIRE(obj["hello"] == "world");
  220. }
  221. SECTION("Before closing brace") {
  222. DeserializationError err =
  223. deserializeJson(doc, "{\"hello\":\"world\"//COMMENT\n}");
  224. JsonObject obj = doc.as<JsonObject>();
  225. REQUIRE(err == DeserializationError::Ok);
  226. REQUIRE(obj["hello"] == "world");
  227. }
  228. SECTION("After closing brace") {
  229. DeserializationError err =
  230. deserializeJson(doc, "{\"hello\":\"world\"}//COMMENT\n");
  231. JsonObject obj = doc.as<JsonObject>();
  232. REQUIRE(err == DeserializationError::Ok);
  233. REQUIRE(obj["hello"] == "world");
  234. }
  235. SECTION("Before comma") {
  236. DeserializationError err = deserializeJson(
  237. doc, "{\"hello\":\"world\"//COMMENT\n,\"answer\":42}");
  238. JsonObject obj = doc.as<JsonObject>();
  239. REQUIRE(err == DeserializationError::Ok);
  240. REQUIRE(obj["hello"] == "world");
  241. REQUIRE(obj["answer"] == 42);
  242. }
  243. SECTION("After comma") {
  244. DeserializationError err = deserializeJson(
  245. doc, "{\"hello\":\"world\",//COMMENT\n\"answer\":42}");
  246. JsonObject obj = doc.as<JsonObject>();
  247. REQUIRE(err == DeserializationError::Ok);
  248. REQUIRE(obj["hello"] == "world");
  249. REQUIRE(obj["answer"] == 42);
  250. }
  251. }
  252. SECTION("Dangling slash") {
  253. SECTION("Before opening brace") {
  254. DeserializationError err = deserializeJson(doc, "/{\"hello\":\"world\"}");
  255. REQUIRE(err == DeserializationError::InvalidInput);
  256. }
  257. SECTION("After opening brace") {
  258. DeserializationError err = deserializeJson(doc, "{/\"hello\":\"world\"}");
  259. REQUIRE(err == DeserializationError::InvalidInput);
  260. }
  261. SECTION("Before colon") {
  262. DeserializationError err = deserializeJson(doc, "{\"hello\"/:\"world\"}");
  263. REQUIRE(err == DeserializationError::InvalidInput);
  264. }
  265. SECTION("After colon") {
  266. DeserializationError err = deserializeJson(doc, "{\"hello\":/\"world\"}");
  267. REQUIRE(err == DeserializationError::InvalidInput);
  268. }
  269. SECTION("Before closing brace") {
  270. DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"/}");
  271. REQUIRE(err == DeserializationError::InvalidInput);
  272. }
  273. SECTION("After closing brace") {
  274. DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"}/");
  275. JsonObject obj = doc.as<JsonObject>();
  276. REQUIRE(err == DeserializationError::Ok);
  277. REQUIRE(obj["hello"] == "world");
  278. }
  279. SECTION("Before comma") {
  280. DeserializationError err =
  281. deserializeJson(doc, "{\"hello\":\"world\"/,\"answer\":42}");
  282. REQUIRE(err == DeserializationError::InvalidInput);
  283. }
  284. SECTION("After comma") {
  285. DeserializationError err =
  286. deserializeJson(doc, "{\"hello\":\"world\",/\"answer\":42}");
  287. REQUIRE(err == DeserializationError::InvalidInput);
  288. }
  289. }
  290. }
  291. TEST_CASE("Comments alone") {
  292. DynamicJsonDocument doc(2048);
  293. SECTION("Just a trailing comment with no line break") {
  294. DeserializationError err = deserializeJson(doc, "// comment");
  295. REQUIRE(err == DeserializationError::IncompleteInput);
  296. }
  297. SECTION("Just a trailing comment with no a break") {
  298. DeserializationError err = deserializeJson(doc, "// comment\n");
  299. REQUIRE(err == DeserializationError::EmptyInput);
  300. }
  301. SECTION("Just a block comment") {
  302. DeserializationError err = deserializeJson(doc, "/*comment*/");
  303. REQUIRE(err == DeserializationError::EmptyInput);
  304. }
  305. SECTION("Just a slash") {
  306. DeserializationError err = deserializeJson(doc, "/");
  307. REQUIRE(err == DeserializationError::InvalidInput);
  308. }
  309. SECTION("Premature terminator") {
  310. DeserializationError err = deserializeJson(doc, "/* comment");
  311. REQUIRE(err == DeserializationError::IncompleteInput);
  312. }
  313. SECTION("Premature end on sized input") {
  314. DeserializationError err = deserializeJson(doc, "/* comment */", 10);
  315. REQUIRE(err == DeserializationError::IncompleteInput);
  316. }
  317. }