123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- // ArduinoJson - https://arduinojson.org
- // Copyright © 2014-2022, Benoit BLANCHON
- // MIT License
- #define ARDUINOJSON_ENABLE_COMMENTS 1
- #include <ArduinoJson.h>
- #include <catch.hpp>
- TEST_CASE("Comments in arrays") {
- DynamicJsonDocument doc(2048);
- SECTION("Block comments") {
- SECTION("Before opening bracket") {
- DeserializationError err =
- deserializeJson(doc, "/*COMMENT*/ [\"hello\"]");
- JsonArray arr = doc.as<JsonArray>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(1 == arr.size());
- REQUIRE(arr[0] == "hello");
- }
- SECTION("After opening bracket") {
- DeserializationError err =
- deserializeJson(doc, "[/*COMMENT*/ \"hello\"]");
- JsonArray arr = doc.as<JsonArray>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(1 == arr.size());
- REQUIRE(arr[0] == "hello");
- }
- SECTION("Before closing bracket") {
- DeserializationError err = deserializeJson(doc, "[\"hello\"/*COMMENT*/]");
- JsonArray arr = doc.as<JsonArray>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(1 == arr.size());
- REQUIRE(arr[0] == "hello");
- }
- SECTION("After closing bracket") {
- DeserializationError err = deserializeJson(doc, "[\"hello\"]/*COMMENT*/");
- JsonArray arr = doc.as<JsonArray>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(1 == arr.size());
- REQUIRE(arr[0] == "hello");
- }
- SECTION("Before comma") {
- DeserializationError err =
- deserializeJson(doc, "[\"hello\"/*COMMENT*/,\"world\"]");
- JsonArray arr = doc.as<JsonArray>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(2 == arr.size());
- REQUIRE(arr[0] == "hello");
- REQUIRE(arr[1] == "world");
- }
- SECTION("After comma") {
- DeserializationError err =
- deserializeJson(doc, "[\"hello\",/*COMMENT*/ \"world\"]");
- JsonArray arr = doc.as<JsonArray>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(2 == arr.size());
- REQUIRE(arr[0] == "hello");
- REQUIRE(arr[1] == "world");
- }
- SECTION("/*/") {
- DeserializationError err = deserializeJson(doc, "[/*/\n]");
- REQUIRE(err == DeserializationError::IncompleteInput);
- }
- SECTION("Unfinished comment") {
- DeserializationError err = deserializeJson(doc, "[/*COMMENT]");
- REQUIRE(err == DeserializationError::IncompleteInput);
- }
- SECTION("Final slash missing") {
- DeserializationError err = deserializeJson(doc, "[/*COMMENT*]");
- REQUIRE(err == DeserializationError::IncompleteInput);
- }
- }
- SECTION("Trailing comments") {
- SECTION("Before opening bracket") {
- DeserializationError err =
- deserializeJson(doc, "//COMMENT\n\t[\"hello\"]");
- JsonArray arr = doc.as<JsonArray>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(1 == arr.size());
- REQUIRE(arr[0] == "hello");
- }
- SECTION("After opening bracket") {
- DeserializationError err = deserializeJson(doc, "[//COMMENT\n\"hello\"]");
- JsonArray arr = doc.as<JsonArray>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(1 == arr.size());
- REQUIRE(arr[0] == "hello");
- }
- SECTION("Before closing bracket") {
- DeserializationError err =
- deserializeJson(doc, "[\"hello\"//COMMENT\r\n]");
- JsonArray arr = doc.as<JsonArray>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(1 == arr.size());
- REQUIRE(arr[0] == "hello");
- }
- SECTION("After closing bracket") {
- DeserializationError err = deserializeJson(doc, "[\"hello\"]//COMMENT\n");
- JsonArray arr = doc.as<JsonArray>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(1 == arr.size());
- REQUIRE(arr[0] == "hello");
- }
- SECTION("Before comma") {
- DeserializationError err =
- deserializeJson(doc, "[\"hello\"//COMMENT\n,\"world\"]");
- JsonArray arr = doc.as<JsonArray>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(2 == arr.size());
- REQUIRE(arr[0] == "hello");
- REQUIRE(arr[1] == "world");
- }
- SECTION("After comma") {
- DeserializationError err =
- deserializeJson(doc, "[\"hello\",//COMMENT\n\"world\"]");
- JsonArray arr = doc.as<JsonArray>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(2 == arr.size());
- REQUIRE(arr[0] == "hello");
- REQUIRE(arr[1] == "world");
- }
- SECTION("Invalid comment") {
- DeserializationError err = deserializeJson(doc, "[/COMMENT\n]");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("End document with comment") {
- DeserializationError err = deserializeJson(doc, "[//COMMENT");
- REQUIRE(err == DeserializationError::IncompleteInput);
- }
- }
- }
- TEST_CASE("Comments in objects") {
- DynamicJsonDocument doc(2048);
- SECTION("Block comments") {
- SECTION("Before opening brace") {
- DeserializationError err =
- deserializeJson(doc, "/*COMMENT*/ {\"hello\":\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("After opening brace") {
- DeserializationError err =
- deserializeJson(doc, "{/*COMMENT*/\"hello\":\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before colon") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\"/*COMMENT*/:\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("After colon") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":/*COMMENT*/\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before closing brace") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":\"world\"/*COMMENT*/}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("After closing brace") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":\"world\"}/*COMMENT*/");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before comma") {
- DeserializationError err = deserializeJson(
- doc, "{\"hello\":\"world\"/*COMMENT*/,\"answer\":42}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- REQUIRE(obj["answer"] == 42);
- }
- SECTION("After comma") {
- DeserializationError err = deserializeJson(
- doc, "{\"hello\":\"world\",/*COMMENT*/\"answer\":42}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- REQUIRE(obj["answer"] == 42);
- }
- }
- SECTION("Trailing comments") {
- SECTION("Before opening brace") {
- DeserializationError err =
- deserializeJson(doc, "//COMMENT\n {\"hello\":\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("After opening brace") {
- DeserializationError err =
- deserializeJson(doc, "{//COMMENT\n\"hello\":\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before colon") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\"//COMMENT\n:\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("After colon") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\"://COMMENT\n\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before closing brace") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":\"world\"//COMMENT\n}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("After closing brace") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":\"world\"}//COMMENT\n");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before comma") {
- DeserializationError err = deserializeJson(
- doc, "{\"hello\":\"world\"//COMMENT\n,\"answer\":42}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- REQUIRE(obj["answer"] == 42);
- }
- SECTION("After comma") {
- DeserializationError err = deserializeJson(
- doc, "{\"hello\":\"world\",//COMMENT\n\"answer\":42}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- REQUIRE(obj["answer"] == 42);
- }
- }
- SECTION("Dangling slash") {
- SECTION("Before opening brace") {
- DeserializationError err = deserializeJson(doc, "/{\"hello\":\"world\"}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("After opening brace") {
- DeserializationError err = deserializeJson(doc, "{/\"hello\":\"world\"}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("Before colon") {
- DeserializationError err = deserializeJson(doc, "{\"hello\"/:\"world\"}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("After colon") {
- DeserializationError err = deserializeJson(doc, "{\"hello\":/\"world\"}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("Before closing brace") {
- DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"/}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("After closing brace") {
- DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"}/");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before comma") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":\"world\"/,\"answer\":42}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("After comma") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":\"world\",/\"answer\":42}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- }
- }
- TEST_CASE("Comments alone") {
- DynamicJsonDocument doc(2048);
- SECTION("Just a trailing comment with no line break") {
- DeserializationError err = deserializeJson(doc, "// comment");
- REQUIRE(err == DeserializationError::IncompleteInput);
- }
- SECTION("Just a trailing comment with no a break") {
- DeserializationError err = deserializeJson(doc, "// comment\n");
- REQUIRE(err == DeserializationError::EmptyInput);
- }
- SECTION("Just a block comment") {
- DeserializationError err = deserializeJson(doc, "/*comment*/");
- REQUIRE(err == DeserializationError::EmptyInput);
- }
- SECTION("Just a slash") {
- DeserializationError err = deserializeJson(doc, "/");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("Premature terminator") {
- DeserializationError err = deserializeJson(doc, "/* comment");
- REQUIRE(err == DeserializationError::IncompleteInput);
- }
- SECTION("Premature end on sized input") {
- DeserializationError err = deserializeJson(doc, "/* comment */", 10);
- REQUIRE(err == DeserializationError::IncompleteInput);
- }
- }
|