ElementProxy.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. using ElementProxy = ArduinoJson::detail::ElementProxy<JsonDocument&>;
  9. TEST_CASE("ElementProxy::add()") {
  10. SpyingAllocator spy;
  11. JsonDocument doc(&spy);
  12. doc.add<JsonVariant>();
  13. const ElementProxy& ep = doc[0];
  14. SECTION("integer") {
  15. ep.add(42);
  16. REQUIRE(doc.as<std::string>() == "[[42]]");
  17. REQUIRE(spy.log() == AllocatorLog{
  18. Allocate(sizeofPool()),
  19. });
  20. }
  21. SECTION("string literal") {
  22. ep.add("world");
  23. REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
  24. REQUIRE(spy.log() == AllocatorLog{
  25. Allocate(sizeofPool()),
  26. });
  27. }
  28. SECTION("const char*") {
  29. const char* s = "world";
  30. ep.add(s);
  31. REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
  32. REQUIRE(spy.log() == AllocatorLog{
  33. Allocate(sizeofPool()),
  34. Allocate(sizeofString("world")),
  35. });
  36. }
  37. SECTION("char[]") {
  38. char s[] = "world";
  39. ep.add(s);
  40. strcpy(s, "!!!!!");
  41. REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
  42. REQUIRE(spy.log() == AllocatorLog{
  43. Allocate(sizeofPool()),
  44. Allocate(sizeofString("world")),
  45. });
  46. }
  47. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  48. SECTION("VLA") {
  49. size_t i = 8;
  50. char vla[i];
  51. strcpy(vla, "world");
  52. ep.add(vla);
  53. REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
  54. REQUIRE(spy.log() == AllocatorLog{
  55. Allocate(sizeofPool()),
  56. Allocate(sizeofString("world")),
  57. });
  58. }
  59. #endif
  60. }
  61. TEST_CASE("ElementProxy::clear()") {
  62. JsonDocument doc;
  63. doc.add<JsonVariant>();
  64. const ElementProxy& ep = doc[0];
  65. SECTION("size goes back to zero") {
  66. ep.add(42);
  67. ep.clear();
  68. REQUIRE(ep.size() == 0);
  69. }
  70. SECTION("isNull() return true") {
  71. ep.add("hello");
  72. ep.clear();
  73. REQUIRE(ep.isNull() == true);
  74. }
  75. }
  76. TEST_CASE("ElementProxy::operator==()") {
  77. JsonDocument doc;
  78. SECTION("1 vs 1") {
  79. doc.add(1);
  80. doc.add(1);
  81. REQUIRE(doc[0] <= doc[1]);
  82. REQUIRE(doc[0] == doc[1]);
  83. REQUIRE(doc[0] >= doc[1]);
  84. REQUIRE_FALSE(doc[0] != doc[1]);
  85. REQUIRE_FALSE(doc[0] < doc[1]);
  86. REQUIRE_FALSE(doc[0] > doc[1]);
  87. }
  88. SECTION("1 vs 2") {
  89. doc.add(1);
  90. doc.add(2);
  91. REQUIRE(doc[0] != doc[1]);
  92. REQUIRE(doc[0] < doc[1]);
  93. REQUIRE(doc[0] <= doc[1]);
  94. REQUIRE_FALSE(doc[0] == doc[1]);
  95. REQUIRE_FALSE(doc[0] > doc[1]);
  96. REQUIRE_FALSE(doc[0] >= doc[1]);
  97. }
  98. SECTION("'abc' vs 'bcd'") {
  99. doc.add("abc");
  100. doc.add("bcd");
  101. REQUIRE(doc[0] != doc[1]);
  102. REQUIRE(doc[0] < doc[1]);
  103. REQUIRE(doc[0] <= doc[1]);
  104. REQUIRE_FALSE(doc[0] == doc[1]);
  105. REQUIRE_FALSE(doc[0] > doc[1]);
  106. REQUIRE_FALSE(doc[0] >= doc[1]);
  107. }
  108. }
  109. TEST_CASE("ElementProxy::remove()") {
  110. JsonDocument doc;
  111. doc.add<JsonVariant>();
  112. const ElementProxy& ep = doc[0];
  113. SECTION("remove(int)") {
  114. ep.add(1);
  115. ep.add(2);
  116. ep.add(3);
  117. ep.remove(1);
  118. REQUIRE(ep.as<std::string>() == "[1,3]");
  119. }
  120. SECTION("remove(const char *)") {
  121. ep["a"] = 1;
  122. ep["b"] = 2;
  123. ep.remove("a");
  124. REQUIRE(ep.as<std::string>() == "{\"b\":2}");
  125. }
  126. SECTION("remove(std::string)") {
  127. ep["a"] = 1;
  128. ep["b"] = 2;
  129. ep.remove("b"_s);
  130. REQUIRE(ep.as<std::string>() == "{\"a\":1}");
  131. }
  132. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  133. SECTION("remove(vla)") {
  134. ep["a"] = 1;
  135. ep["b"] = 2;
  136. size_t i = 4;
  137. char vla[i];
  138. strcpy(vla, "b");
  139. ep.remove(vla);
  140. REQUIRE(ep.as<std::string>() == "{\"a\":1}");
  141. }
  142. #endif
  143. }
  144. TEST_CASE("ElementProxy::set()") {
  145. JsonDocument doc;
  146. const ElementProxy& ep = doc[0];
  147. SECTION("set(int)") {
  148. ep.set(42);
  149. REQUIRE(doc.as<std::string>() == "[42]");
  150. }
  151. SECTION("set(const char*)") {
  152. ep.set("world");
  153. REQUIRE(doc.as<std::string>() == "[\"world\"]");
  154. }
  155. SECTION("set(char[])") {
  156. char s[] = "world";
  157. ep.set(s);
  158. strcpy(s, "!!!!!");
  159. REQUIRE(doc.as<std::string>() == "[\"world\"]");
  160. }
  161. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  162. SECTION("set(VLA)") {
  163. size_t i = 8;
  164. char vla[i];
  165. strcpy(vla, "world");
  166. ep.set(vla);
  167. REQUIRE(doc.as<std::string>() == "[\"world\"]");
  168. }
  169. #endif
  170. }
  171. TEST_CASE("ElementProxy::size()") {
  172. JsonDocument doc;
  173. doc.add<JsonVariant>();
  174. const ElementProxy& ep = doc[0];
  175. SECTION("returns 0") {
  176. REQUIRE(ep.size() == 0);
  177. }
  178. SECTION("as an array, returns 2") {
  179. ep.add(1);
  180. ep.add(2);
  181. REQUIRE(ep.size() == 2);
  182. }
  183. SECTION("as an object, returns 2") {
  184. ep["a"] = 1;
  185. ep["b"] = 2;
  186. REQUIRE(ep.size() == 2);
  187. }
  188. }
  189. TEST_CASE("ElementProxy::operator[]") {
  190. JsonDocument doc;
  191. const ElementProxy& ep = doc[1];
  192. SECTION("set member") {
  193. ep["world"] = 42;
  194. REQUIRE(doc.as<std::string>() == "[null,{\"world\":42}]");
  195. }
  196. SECTION("set element") {
  197. ep[2] = 42;
  198. REQUIRE(doc.as<std::string>() == "[null,[null,null,42]]");
  199. }
  200. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  201. SECTION("set VLA") {
  202. size_t i = 8;
  203. char vla[i];
  204. strcpy(vla, "world");
  205. ep[0] = vla;
  206. REQUIRE(doc.as<std::string>() == "[null,[\"world\"]]");
  207. }
  208. #endif
  209. }
  210. TEST_CASE("ElementProxy cast to JsonVariantConst") {
  211. JsonDocument doc;
  212. doc[0] = "world";
  213. const ElementProxy& ep = doc[0];
  214. JsonVariantConst var = ep;
  215. CHECK(var.as<std::string>() == "world");
  216. }
  217. TEST_CASE("ElementProxy cast to JsonVariant") {
  218. JsonDocument doc;
  219. doc[0] = "world";
  220. const ElementProxy& ep = doc[0];
  221. JsonVariant var = ep;
  222. CHECK(var.as<std::string>() == "world");
  223. var.set("toto");
  224. CHECK(doc.as<std::string>() == "[\"toto\"]");
  225. }