cpp11.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. SECTION("JsonVariant.is<nullptr_t>()") {
  22. variant.set(42);
  23. REQUIRE(variant.is<std::nullptr_t>() == false);
  24. variant.clear();
  25. REQUIRE(variant.is<std::nullptr_t>() == true);
  26. }
  27. }
  28. TEST_CASE("Issue #1120") {
  29. StaticJsonDocument<500> doc;
  30. constexpr char str[] =
  31. "{\"contents\":[{\"module\":\"Packet\"},{\"module\":\"Analog\"}]}";
  32. deserializeJson(doc, str);
  33. SECTION("MemberProxy<std::string>::isNull()") {
  34. SECTION("returns false") {
  35. auto value = doc[std::string("contents")];
  36. CHECK(value.isNull() == false);
  37. }
  38. SECTION("returns true") {
  39. auto value = doc[std::string("zontents")];
  40. CHECK(value.isNull() == true);
  41. }
  42. }
  43. SECTION("ElementProxy<MemberProxy<const char*> >::isNull()") {
  44. SECTION("returns false") { // Issue #1120
  45. auto value = doc["contents"][1];
  46. CHECK(value.isNull() == false);
  47. }
  48. SECTION("returns true") {
  49. auto value = doc["contents"][2];
  50. CHECK(value.isNull() == true);
  51. }
  52. }
  53. SECTION("MemberProxy<ElementProxy<MemberProxy>, const char*>::isNull()") {
  54. SECTION("returns false") {
  55. auto value = doc["contents"][1]["module"];
  56. CHECK(value.isNull() == false);
  57. }
  58. SECTION("returns true") {
  59. auto value = doc["contents"][1]["zodule"];
  60. CHECK(value.isNull() == true);
  61. }
  62. }
  63. SECTION("MemberProxy<ElementProxy<MemberProxy>, std::string>::isNull()") {
  64. SECTION("returns false") {
  65. auto value = doc["contents"][1][std::string("module")];
  66. CHECK(value.isNull() == false);
  67. }
  68. SECTION("returns true") {
  69. auto value = doc["contents"][1][std::string("zodule")];
  70. CHECK(value.isNull() == true);
  71. }
  72. }
  73. }
  74. #endif