isnull.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonVariant::isNull()") {
  7. DynamicJsonDocument doc(4096);
  8. JsonVariant variant = doc.to<JsonVariant>();
  9. SECTION("returns true when Undefined") {
  10. REQUIRE(variant.isNull() == true);
  11. }
  12. SECTION("returns false when Integer") {
  13. variant.set(42);
  14. REQUIRE(variant.isNull() == false);
  15. }
  16. SECTION("returns false when EmptyArray") {
  17. DynamicJsonDocument doc2(4096);
  18. JsonArray array = doc2.to<JsonArray>();
  19. variant.set(array);
  20. REQUIRE(variant.isNull() == false);
  21. }
  22. SECTION("returns false when EmptyObject") {
  23. DynamicJsonDocument doc2(4096);
  24. JsonObject obj = doc2.to<JsonObject>();
  25. variant.set(obj);
  26. REQUIRE(variant.isNull() == false);
  27. }
  28. SECTION("returns true after set(JsonArray())") {
  29. variant.set(JsonArray());
  30. REQUIRE(variant.isNull() == true);
  31. }
  32. SECTION("returns true after set(JsonObject())") {
  33. variant.set(JsonObject());
  34. REQUIRE(variant.isNull() == true);
  35. }
  36. SECTION("returns false after set('hello')") {
  37. variant.set("hello");
  38. REQUIRE(variant.isNull() == false);
  39. }
  40. SECTION("returns true after set((char*)0)") {
  41. variant.set(static_cast<char*>(0));
  42. REQUIRE(variant.isNull() == true);
  43. }
  44. SECTION("returns true after set((const char*)0)") {
  45. variant.set(static_cast<const char*>(0));
  46. REQUIRE(variant.isNull() == true);
  47. }
  48. SECTION("returns true after set(serialized((char*)0))") {
  49. variant.set(serialized(static_cast<char*>(0)));
  50. REQUIRE(variant.isNull() == true);
  51. }
  52. SECTION("returns true after set(serialized((const char*)0))") {
  53. variant.set(serialized(static_cast<const char*>(0)));
  54. REQUIRE(variant.isNull() == true);
  55. }
  56. SECTION("returns true for a shallow null copy") {
  57. StaticJsonDocument<128> doc2;
  58. variant.shallowCopy(doc2);
  59. CHECK(variant.isNull() == true);
  60. }
  61. SECTION("returns false for a shallow array copy") {
  62. StaticJsonDocument<128> doc2;
  63. doc2[0] = 42;
  64. variant.shallowCopy(doc2);
  65. CHECK(variant.isNull() == false);
  66. }
  67. SECTION("works with JsonVariantConst") {
  68. variant.set(42);
  69. JsonVariantConst cvar = variant;
  70. REQUIRE(cvar.isNull() == false);
  71. }
  72. }