misc.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  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::EmptyInput);
  22. }
  23. SECTION("Only spaces") {
  24. DeserializationError err = deserializeJson(doc, " \t\n\r");
  25. REQUIRE(err == DeserializationError::EmptyInput);
  26. }
  27. SECTION("issue #628") {
  28. DeserializationError err = deserializeJson(doc, "null");
  29. REQUIRE(err == DeserializationError::Ok);
  30. REQUIRE(doc.is<float>() == false);
  31. }
  32. SECTION("Garbage") {
  33. DeserializationError err = deserializeJson(doc, "%*$£¤");
  34. REQUIRE(err == DeserializationError::InvalidInput);
  35. }
  36. }
  37. SECTION("Booleans") {
  38. SECTION("True") {
  39. DeserializationError err = deserializeJson(doc, "true");
  40. REQUIRE(err == DeserializationError::Ok);
  41. REQUIRE(doc.is<bool>());
  42. REQUIRE(doc.as<bool>() == true);
  43. }
  44. SECTION("False") {
  45. DeserializationError err = deserializeJson(doc, "false");
  46. REQUIRE(err == DeserializationError::Ok);
  47. REQUIRE(doc.is<bool>());
  48. REQUIRE(doc.as<bool>() == false);
  49. }
  50. }
  51. SECTION("Premature null-terminator") {
  52. SECTION("In escape sequence") {
  53. DeserializationError err = deserializeJson(doc, "\"\\");
  54. REQUIRE(err == DeserializationError::IncompleteInput);
  55. }
  56. SECTION("In double quoted string") {
  57. DeserializationError err = deserializeJson(doc, "\"hello");
  58. REQUIRE(err == DeserializationError::IncompleteInput);
  59. }
  60. SECTION("In single quoted string") {
  61. DeserializationError err = deserializeJson(doc, "'hello");
  62. REQUIRE(err == DeserializationError::IncompleteInput);
  63. }
  64. }
  65. SECTION("Premature end of input") {
  66. SECTION("In escape sequence") {
  67. DeserializationError err = deserializeJson(doc, "\"\\n\"", 2);
  68. REQUIRE(err == DeserializationError::IncompleteInput);
  69. }
  70. SECTION("In double quoted string") {
  71. DeserializationError err = deserializeJson(doc, "\"hello\"", 6);
  72. REQUIRE(err == DeserializationError::IncompleteInput);
  73. }
  74. SECTION("In single quoted string") {
  75. DeserializationError err = deserializeJson(doc, "'hello'", 6);
  76. REQUIRE(err == DeserializationError::IncompleteInput);
  77. }
  78. }
  79. SECTION("Should clear the JsonVariant") {
  80. deserializeJson(doc, "[1,2,3]");
  81. deserializeJson(doc, "{}");
  82. REQUIRE(doc.is<JsonObject>());
  83. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
  84. }
  85. }