misc.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. using namespace Catch::Matchers;
  7. TEST_CASE("deserializeJson(DynamicJsonDocument&)") {
  8. DynamicJsonDocument doc(4096);
  9. SECTION("Edge cases") {
  10. SECTION("null char*") {
  11. DeserializationError err = deserializeJson(doc, static_cast<char*>(0));
  12. REQUIRE(err != DeserializationError::Ok);
  13. }
  14. SECTION("null const char*") {
  15. DeserializationError err =
  16. deserializeJson(doc, static_cast<const char*>(0));
  17. REQUIRE(err != DeserializationError::Ok);
  18. }
  19. SECTION("Empty input") {
  20. DeserializationError err = deserializeJson(doc, "");
  21. REQUIRE(err == DeserializationError::IncompleteInput);
  22. }
  23. SECTION("issue #628") {
  24. DeserializationError err = deserializeJson(doc, "null");
  25. REQUIRE(err == DeserializationError::Ok);
  26. REQUIRE(doc.is<float>() == false);
  27. }
  28. SECTION("Garbage") {
  29. DeserializationError err = deserializeJson(doc, "%*$£¤");
  30. REQUIRE(err == DeserializationError::InvalidInput);
  31. }
  32. }
  33. SECTION("Booleans") {
  34. SECTION("True") {
  35. DeserializationError err = deserializeJson(doc, "true");
  36. REQUIRE(err == DeserializationError::Ok);
  37. REQUIRE(doc.is<bool>());
  38. REQUIRE(doc.as<bool>() == true);
  39. }
  40. SECTION("False") {
  41. DeserializationError err = deserializeJson(doc, "false");
  42. REQUIRE(err == DeserializationError::Ok);
  43. REQUIRE(doc.is<bool>());
  44. REQUIRE(doc.as<bool>() == false);
  45. }
  46. }
  47. SECTION("Comments") {
  48. SECTION("Just a trailing comment") {
  49. DeserializationError err = deserializeJson(doc, "// comment");
  50. REQUIRE(err == DeserializationError::IncompleteInput);
  51. }
  52. SECTION("Just a block comment") {
  53. DeserializationError err = deserializeJson(doc, "/*comment*/");
  54. REQUIRE(err == DeserializationError::IncompleteInput);
  55. }
  56. SECTION("Just a slash") {
  57. DeserializationError err = deserializeJson(doc, "/");
  58. REQUIRE(err == DeserializationError::InvalidInput);
  59. }
  60. }
  61. SECTION("Premature null-terminator") {
  62. SECTION("In escape sequence") {
  63. DeserializationError err = deserializeJson(doc, "\"\\");
  64. REQUIRE(err == DeserializationError::IncompleteInput);
  65. }
  66. SECTION("In block comment") {
  67. DeserializationError err = deserializeJson(doc, "/* comment");
  68. REQUIRE(err == DeserializationError::IncompleteInput);
  69. }
  70. SECTION("In double quoted string") {
  71. DeserializationError err = deserializeJson(doc, "\"hello");
  72. REQUIRE(err == DeserializationError::IncompleteInput);
  73. }
  74. SECTION("In single quoted string") {
  75. DeserializationError err = deserializeJson(doc, "'hello");
  76. REQUIRE(err == DeserializationError::IncompleteInput);
  77. }
  78. }
  79. SECTION("Premature end of input") {
  80. SECTION("In escape sequence") {
  81. DeserializationError err = deserializeJson(doc, "\"\\n\"", 2);
  82. REQUIRE(err == DeserializationError::IncompleteInput);
  83. }
  84. SECTION("In block comment") {
  85. DeserializationError err = deserializeJson(doc, "/* comment */", 10);
  86. REQUIRE(err == DeserializationError::IncompleteInput);
  87. }
  88. SECTION("In double quoted string") {
  89. DeserializationError err = deserializeJson(doc, "\"hello\"", 6);
  90. REQUIRE(err == DeserializationError::IncompleteInput);
  91. }
  92. SECTION("In single quoted string") {
  93. DeserializationError err = deserializeJson(doc, "'hello'", 6);
  94. REQUIRE(err == DeserializationError::IncompleteInput);
  95. }
  96. }
  97. SECTION("Should clear the JsonVariant") {
  98. deserializeJson(doc, "[1,2,3]");
  99. deserializeJson(doc, "{}");
  100. REQUIRE(doc.is<JsonObject>());
  101. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
  102. }
  103. }