cpp11.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <ArduinoJson.h>
  2. #include <catch.hpp>
  3. #if __cplusplus >= 201103L
  4. TEST_CASE("nullptr") {
  5. DynamicJsonDocument doc(4096);
  6. JsonVariant variant = doc.to<JsonVariant>();
  7. SECTION("JsonVariant == nullptr") {
  8. REQUIRE((variant == nullptr));
  9. REQUIRE_FALSE((variant != nullptr));
  10. }
  11. SECTION("JsonVariant != nullptr") {
  12. variant.set(42);
  13. REQUIRE_FALSE((variant == nullptr));
  14. REQUIRE((variant != nullptr));
  15. }
  16. SECTION("JsonVariant.set(nullptr)") {
  17. variant.set(42);
  18. variant.set(nullptr);
  19. REQUIRE(variant.isNull());
  20. }
  21. }
  22. TEST_CASE("Issue #1120") {
  23. StaticJsonDocument<500> doc;
  24. constexpr char str[] =
  25. "{\"contents\":[{\"module\":\"Packet\"},{\"module\":\"Analog\"}]}";
  26. deserializeJson(doc, str);
  27. SECTION("MemberProxy<std::string>::isNull()") {
  28. SECTION("returns false") {
  29. auto value = doc[std::string("contents")];
  30. CHECK(value.isNull() == false);
  31. }
  32. SECTION("returns true") {
  33. auto value = doc[std::string("zontents")];
  34. CHECK(value.isNull() == true);
  35. }
  36. }
  37. SECTION("ElementProxy<MemberProxy<const char*> >::isNull()") {
  38. SECTION("returns false") { // Issue #1120
  39. auto value = doc["contents"][1];
  40. CHECK(value.isNull() == false);
  41. }
  42. SECTION("returns true") {
  43. auto value = doc["contents"][2];
  44. CHECK(value.isNull() == true);
  45. }
  46. }
  47. SECTION("MemberProxy<ElementProxy<MemberProxy>, const char*>::isNull()") {
  48. SECTION("returns false") {
  49. auto value = doc["contents"][1]["module"];
  50. CHECK(value.isNull() == false);
  51. }
  52. SECTION("returns true") {
  53. auto value = doc["contents"][1]["zodule"];
  54. CHECK(value.isNull() == true);
  55. }
  56. }
  57. SECTION("MemberProxy<ElementProxy<MemberProxy>, std::string>::isNull()") {
  58. SECTION("returns false") {
  59. auto value = doc["contents"][1][std::string("module")];
  60. CHECK(value.isNull() == false);
  61. }
  62. SECTION("returns true") {
  63. auto value = doc["contents"][1][std::string("zodule")];
  64. CHECK(value.isNull() == true);
  65. }
  66. }
  67. }
  68. #endif