misc.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  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("Premature null-terminator") {
  48. SECTION("In escape sequence") {
  49. DeserializationError err = deserializeJson(doc, "\"\\");
  50. REQUIRE(err == DeserializationError::IncompleteInput);
  51. }
  52. SECTION("In double quoted string") {
  53. DeserializationError err = deserializeJson(doc, "\"hello");
  54. REQUIRE(err == DeserializationError::IncompleteInput);
  55. }
  56. SECTION("In single quoted string") {
  57. DeserializationError err = deserializeJson(doc, "'hello");
  58. REQUIRE(err == DeserializationError::IncompleteInput);
  59. }
  60. }
  61. SECTION("Premature end of input") {
  62. SECTION("In escape sequence") {
  63. DeserializationError err = deserializeJson(doc, "\"\\n\"", 2);
  64. REQUIRE(err == DeserializationError::IncompleteInput);
  65. }
  66. SECTION("In double quoted string") {
  67. DeserializationError err = deserializeJson(doc, "\"hello\"", 6);
  68. REQUIRE(err == DeserializationError::IncompleteInput);
  69. }
  70. SECTION("In single quoted string") {
  71. DeserializationError err = deserializeJson(doc, "'hello'", 6);
  72. REQUIRE(err == DeserializationError::IncompleteInput);
  73. }
  74. }
  75. SECTION("Should clear the JsonVariant") {
  76. deserializeJson(doc, "[1,2,3]");
  77. deserializeJson(doc, "{}");
  78. REQUIRE(doc.is<JsonObject>());
  79. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
  80. }
  81. }