subscript.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "Allocators.hpp"
  7. #include "Literals.hpp"
  8. TEST_CASE("JsonDocument::operator[]") {
  9. JsonDocument doc;
  10. const JsonDocument& cdoc = doc;
  11. SECTION("object") {
  12. doc["abc"_s] = "ABC";
  13. doc["abc\0d"_s] = "ABCD";
  14. SECTION("const char*") {
  15. const char* key = "abc";
  16. REQUIRE(doc[key] == "ABC");
  17. REQUIRE(cdoc[key] == "ABC");
  18. }
  19. SECTION("string literal") {
  20. REQUIRE(doc["abc"] == "ABC");
  21. REQUIRE(cdoc["abc"] == "ABC");
  22. REQUIRE(doc["abc\0d"] == "ABCD");
  23. REQUIRE(cdoc["abc\0d"] == "ABCD");
  24. }
  25. SECTION("std::string") {
  26. REQUIRE(doc["abc"_s] == "ABC");
  27. REQUIRE(cdoc["abc"_s] == "ABC");
  28. REQUIRE(doc["abc\0d"_s] == "ABCD");
  29. REQUIRE(cdoc["abc\0d"_s] == "ABCD");
  30. }
  31. SECTION("JsonVariant") {
  32. doc["key1"] = "abc";
  33. doc["key2"] = "abc\0d"_s;
  34. doc["key3"] = "foo";
  35. CHECK(doc[doc["key1"]] == "ABC");
  36. CHECK(doc[doc["key2"]] == "ABCD");
  37. CHECK(doc[doc["key3"]] == nullptr);
  38. CHECK(doc[doc["key4"]] == nullptr);
  39. CHECK(cdoc[cdoc["key1"]] == "ABC");
  40. CHECK(cdoc[cdoc["key2"]] == "ABCD");
  41. CHECK(cdoc[cdoc["key3"]] == nullptr);
  42. CHECK(cdoc[cdoc["key4"]] == nullptr);
  43. }
  44. SECTION("supports operator|") {
  45. REQUIRE((doc["abc"] | "nope") == "ABC"_s);
  46. REQUIRE((doc["def"] | "nope") == "nope"_s);
  47. }
  48. #if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
  49. !defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
  50. SECTION("supports VLAs") {
  51. size_t i = 16;
  52. char vla[i];
  53. strcpy(vla, "hello");
  54. doc[vla] = "world";
  55. REQUIRE(doc[vla] == "world");
  56. REQUIRE(cdoc[vla] == "world");
  57. }
  58. #endif
  59. }
  60. SECTION("array") {
  61. deserializeJson(doc, "[\"hello\",\"world\"]");
  62. SECTION("int") {
  63. REQUIRE(doc[1] == "world");
  64. REQUIRE(cdoc[1] == "world");
  65. }
  66. SECTION("JsonVariant") {
  67. doc[2] = 1;
  68. REQUIRE(doc[doc[2]] == "world");
  69. REQUIRE(cdoc[doc[2]] == "world");
  70. }
  71. }
  72. }
  73. TEST_CASE("JsonDocument automatically promotes to object") {
  74. JsonDocument doc;
  75. doc["one"]["two"]["three"] = 4;
  76. REQUIRE(doc["one"]["two"]["three"] == 4);
  77. }
  78. TEST_CASE("JsonDocument automatically promotes to array") {
  79. JsonDocument doc;
  80. doc[2] = 2;
  81. REQUIRE(doc.as<std::string>() == "[null,null,2]");
  82. }
  83. TEST_CASE("JsonDocument::operator[] key storage") {
  84. SpyingAllocator spy;
  85. JsonDocument doc(&spy);
  86. SECTION("string literal") {
  87. doc["hello"] = 0;
  88. REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
  89. REQUIRE(spy.log() == AllocatorLog{
  90. Allocate(sizeofPool()),
  91. });
  92. }
  93. SECTION("const char*") {
  94. const char* key = "hello";
  95. doc[key] = 0;
  96. REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
  97. REQUIRE(spy.log() == AllocatorLog{
  98. Allocate(sizeofPool()),
  99. Allocate(sizeofString("hello")),
  100. });
  101. }
  102. SECTION("char[]") {
  103. char key[] = "hello";
  104. doc[key] = 0;
  105. REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
  106. REQUIRE(spy.log() == AllocatorLog{
  107. Allocate(sizeofPool()),
  108. Allocate(sizeofString("hello")),
  109. });
  110. }
  111. SECTION("std::string") {
  112. doc["hello"_s] = 0;
  113. REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
  114. REQUIRE(spy.log() == AllocatorLog{
  115. Allocate(sizeofPool()),
  116. Allocate(sizeofString("hello")),
  117. });
  118. }
  119. #if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
  120. !defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
  121. SECTION("VLA") {
  122. size_t i = 16;
  123. char vla[i];
  124. strcpy(vla, "hello");
  125. doc[vla] = 0;
  126. REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
  127. REQUIRE(spy.log() == AllocatorLog{
  128. Allocate(sizeofPool()),
  129. Allocate(sizeofString("hello")),
  130. });
  131. }
  132. #endif
  133. }