subscript.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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["abcd"_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["abcd"] == "ABCD");
  23. REQUIRE(cdoc["abcd"] == "ABCD");
  24. }
  25. SECTION("std::string") {
  26. REQUIRE(doc["abc"_s] == "ABC");
  27. REQUIRE(cdoc["abc"_s] == "ABC");
  28. REQUIRE(doc["abcd"_s] == "ABCD");
  29. REQUIRE(cdoc["abcd"_s] == "ABCD");
  30. }
  31. SECTION("JsonVariant") {
  32. doc["key1"] = "abc";
  33. doc["key2"] = "abcd"_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. Allocate(sizeofString("hello")),
  92. });
  93. }
  94. SECTION("const char*") {
  95. const char* key = "hello";
  96. doc[key] = 0;
  97. REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
  98. REQUIRE(spy.log() == AllocatorLog{
  99. Allocate(sizeofPool()),
  100. Allocate(sizeofString("hello")),
  101. });
  102. }
  103. SECTION("char[]") {
  104. char key[] = "hello";
  105. doc[key] = 0;
  106. REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
  107. REQUIRE(spy.log() == AllocatorLog{
  108. Allocate(sizeofPool()),
  109. Allocate(sizeofString("hello")),
  110. });
  111. }
  112. SECTION("std::string") {
  113. doc["hello"_s] = 0;
  114. REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
  115. REQUIRE(spy.log() == AllocatorLog{
  116. Allocate(sizeofPool()),
  117. Allocate(sizeofString("hello")),
  118. });
  119. }
  120. #if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
  121. !defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
  122. SECTION("VLA") {
  123. size_t i = 16;
  124. char vla[i];
  125. strcpy(vla, "hello");
  126. doc[vla] = 0;
  127. REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
  128. REQUIRE(spy.log() == AllocatorLog{
  129. Allocate(sizeofPool()),
  130. Allocate(sizeofString("hello")),
  131. });
  132. }
  133. #endif
  134. }