subscript.cpp 4.0 KB

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