ElementProxy.cpp 5.9 KB

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