MemberProxy.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
  5. #define ARDUINOJSON_ENABLE_PROGMEM 1
  6. #include <ArduinoJson.h>
  7. #include <catch.hpp>
  8. #include "Allocators.hpp"
  9. #include "Literals.hpp"
  10. using ArduinoJson::detail::sizeofArray;
  11. using ArduinoJson::detail::sizeofObject;
  12. TEST_CASE("MemberProxy::add()") {
  13. SpyingAllocator spy;
  14. JsonDocument doc(&spy);
  15. const auto& mp = doc["hello"];
  16. SECTION("integer") {
  17. mp.add(42);
  18. REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
  19. REQUIRE(spy.log() == AllocatorLog{
  20. Allocate(sizeofPool()),
  21. Allocate(sizeofStaticStringPool()),
  22. });
  23. }
  24. SECTION("string literal") {
  25. mp.add("world");
  26. REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
  27. REQUIRE(spy.log() == AllocatorLog{
  28. Allocate(sizeofPool()),
  29. Allocate(sizeofStaticStringPool()),
  30. });
  31. }
  32. SECTION("const char*") {
  33. const char* temp = "world";
  34. mp.add(temp);
  35. REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
  36. REQUIRE(spy.log() == AllocatorLog{
  37. Allocate(sizeofPool()),
  38. Allocate(sizeofStaticStringPool()),
  39. Allocate(sizeofString("world")),
  40. });
  41. }
  42. SECTION("char[]") {
  43. char temp[] = "world";
  44. mp.add(temp);
  45. REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
  46. REQUIRE(spy.log() == AllocatorLog{
  47. Allocate(sizeofPool()),
  48. Allocate(sizeofStaticStringPool()),
  49. Allocate(sizeofString("world")),
  50. });
  51. }
  52. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  53. SECTION("VLA") {
  54. size_t i = 16;
  55. char vla[i];
  56. strcpy(vla, "world");
  57. mp.add(vla);
  58. REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
  59. REQUIRE(spy.log() == AllocatorLog{
  60. Allocate(sizeofPool()),
  61. Allocate(sizeofStaticStringPool()),
  62. Allocate(sizeofString("world")),
  63. });
  64. }
  65. #endif
  66. }
  67. TEST_CASE("MemberProxy::clear()") {
  68. JsonDocument doc;
  69. const auto& mp = doc["hello"];
  70. SECTION("size goes back to zero") {
  71. mp.add(42);
  72. mp.clear();
  73. REQUIRE(mp.size() == 0);
  74. }
  75. SECTION("isNull() return true") {
  76. mp.add("hello");
  77. mp.clear();
  78. REQUIRE(mp.isNull() == true);
  79. }
  80. }
  81. TEST_CASE("MemberProxy::operator==()") {
  82. JsonDocument doc;
  83. SECTION("1 vs 1") {
  84. doc["a"] = 1;
  85. doc["b"] = 1;
  86. REQUIRE(doc["a"] <= doc["b"]);
  87. REQUIRE(doc["a"] == doc["b"]);
  88. REQUIRE(doc["a"] >= doc["b"]);
  89. REQUIRE_FALSE(doc["a"] != doc["b"]);
  90. REQUIRE_FALSE(doc["a"] < doc["b"]);
  91. REQUIRE_FALSE(doc["a"] > doc["b"]);
  92. }
  93. SECTION("1 vs 2") {
  94. doc["a"] = 1;
  95. doc["b"] = 2;
  96. REQUIRE(doc["a"] != doc["b"]);
  97. REQUIRE(doc["a"] < doc["b"]);
  98. REQUIRE(doc["a"] <= doc["b"]);
  99. REQUIRE_FALSE(doc["a"] == doc["b"]);
  100. REQUIRE_FALSE(doc["a"] > doc["b"]);
  101. REQUIRE_FALSE(doc["a"] >= doc["b"]);
  102. }
  103. SECTION("'abc' vs 'bcd'") {
  104. doc["a"] = "abc";
  105. doc["b"] = "bcd";
  106. REQUIRE(doc["a"] != doc["b"]);
  107. REQUIRE(doc["a"] < doc["b"]);
  108. REQUIRE(doc["a"] <= doc["b"]);
  109. REQUIRE_FALSE(doc["a"] == doc["b"]);
  110. REQUIRE_FALSE(doc["a"] > doc["b"]);
  111. REQUIRE_FALSE(doc["a"] >= doc["b"]);
  112. }
  113. }
  114. TEST_CASE("MemberProxy::operator|()") {
  115. JsonDocument doc;
  116. SECTION("const char*") {
  117. doc["a"] = "hello";
  118. REQUIRE((doc["a"] | "world") == "hello"_s);
  119. REQUIRE((doc["b"] | "world") == "world"_s);
  120. }
  121. SECTION("Issue #1411") {
  122. doc["sensor"] = "gps";
  123. const char* test = "test"; // <- the literal must be captured in a variable
  124. // to trigger the bug
  125. const char* sensor = doc["sensor"] | test; // "gps"
  126. REQUIRE(sensor == "gps"_s);
  127. }
  128. SECTION("Issue #1415") {
  129. JsonObject object = doc.to<JsonObject>();
  130. object["hello"] = "world";
  131. JsonDocument emptyDoc;
  132. JsonObject anotherObject = object["hello"] | emptyDoc.to<JsonObject>();
  133. REQUIRE(anotherObject.isNull() == false);
  134. REQUIRE(anotherObject.size() == 0);
  135. }
  136. }
  137. TEST_CASE("MemberProxy::remove()") {
  138. JsonDocument doc;
  139. const auto& mp = doc["hello"];
  140. SECTION("remove(int)") {
  141. mp.add(1);
  142. mp.add(2);
  143. mp.add(3);
  144. mp.remove(1);
  145. REQUIRE(mp.as<std::string>() == "[1,3]");
  146. }
  147. SECTION("remove(const char *)") {
  148. mp["a"] = 1;
  149. mp["b"] = 2;
  150. mp.remove("a");
  151. REQUIRE(mp.as<std::string>() == "{\"b\":2}");
  152. }
  153. SECTION("remove(std::string)") {
  154. mp["a"] = 1;
  155. mp["b"] = 2;
  156. mp.remove("b"_s);
  157. REQUIRE(mp.as<std::string>() == "{\"a\":1}");
  158. }
  159. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  160. SECTION("remove(vla)") {
  161. mp["a"] = 1;
  162. mp["b"] = 2;
  163. size_t i = 4;
  164. char vla[i];
  165. strcpy(vla, "b");
  166. mp.remove(vla);
  167. REQUIRE(mp.as<std::string>() == "{\"a\":1}");
  168. }
  169. #endif
  170. }
  171. TEST_CASE("MemberProxy::set()") {
  172. JsonDocument doc;
  173. const auto& mp = doc["hello"];
  174. SECTION("set(int)") {
  175. mp.set(42);
  176. REQUIRE(doc.as<std::string>() == "{\"hello\":42}");
  177. }
  178. SECTION("set(const char*)") {
  179. mp.set("world");
  180. REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
  181. }
  182. SECTION("set(char[])") { // issue #1191
  183. char s[] = "world";
  184. mp.set(s);
  185. strcpy(s, "!!!!!");
  186. REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
  187. }
  188. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  189. SECTION("set(vla)") {
  190. size_t i = 8;
  191. char vla[i];
  192. strcpy(vla, "world");
  193. mp.set(vla);
  194. REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
  195. }
  196. #endif
  197. }
  198. TEST_CASE("MemberProxy::size()") {
  199. JsonDocument doc;
  200. const auto& mp = doc["hello"];
  201. SECTION("returns 0") {
  202. REQUIRE(mp.size() == 0);
  203. }
  204. SECTION("as an array, return 2") {
  205. mp.add(1);
  206. mp.add(2);
  207. REQUIRE(mp.size() == 2);
  208. }
  209. SECTION("as an object, return 2") {
  210. mp["a"] = 1;
  211. mp["b"] = 2;
  212. REQUIRE(mp.size() == 2);
  213. }
  214. }
  215. TEST_CASE("MemberProxy::operator[]") {
  216. JsonDocument doc;
  217. const auto& mp = doc["hello"];
  218. SECTION("set member") {
  219. mp["world"] = 42;
  220. REQUIRE(doc.as<std::string>() == "{\"hello\":{\"world\":42}}");
  221. }
  222. SECTION("set element") {
  223. mp[2] = 42;
  224. REQUIRE(doc.as<std::string>() == "{\"hello\":[null,null,42]}");
  225. }
  226. }
  227. TEST_CASE("MemberProxy cast to JsonVariantConst") {
  228. JsonDocument doc;
  229. doc["hello"] = "world";
  230. const auto& mp = doc["hello"];
  231. JsonVariantConst var = mp;
  232. CHECK(var.as<std::string>() == "world");
  233. }
  234. TEST_CASE("MemberProxy cast to JsonVariant") {
  235. JsonDocument doc;
  236. doc["hello"] = "world";
  237. const auto& mp = doc["hello"];
  238. JsonVariant var = mp;
  239. CHECK(var.as<std::string>() == "world");
  240. var.set("toto");
  241. CHECK(doc.as<std::string>() == "{\"hello\":\"toto\"}");
  242. }
  243. TEST_CASE("Deduplicate keys") {
  244. SpyingAllocator spy;
  245. JsonDocument doc(&spy);
  246. SECTION("std::string") {
  247. doc[0]["example"_s] = 1;
  248. doc[1]["example"_s] = 2;
  249. const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
  250. const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
  251. CHECK(key1 == key2);
  252. REQUIRE(spy.log() == AllocatorLog{
  253. Allocate(sizeofPool()),
  254. Allocate(sizeofString("example")),
  255. });
  256. }
  257. SECTION("char*") {
  258. char key[] = "example";
  259. doc[0][key] = 1;
  260. doc[1][key] = 2;
  261. const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
  262. const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
  263. CHECK(key1 == key2);
  264. REQUIRE(spy.log() == AllocatorLog{
  265. Allocate(sizeofPool()),
  266. Allocate(sizeofString("example")),
  267. });
  268. }
  269. SECTION("Arduino String") {
  270. doc[0][String("example")] = 1;
  271. doc[1][String("example")] = 2;
  272. const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
  273. const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
  274. CHECK(key1 == key2);
  275. REQUIRE(spy.log() == AllocatorLog{
  276. Allocate(sizeofPool()),
  277. Allocate(sizeofString("example")),
  278. });
  279. }
  280. SECTION("Flash string") {
  281. doc[0][F("example")] = 1;
  282. doc[1][F("example")] = 2;
  283. const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
  284. const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
  285. CHECK(key1 == key2);
  286. REQUIRE(spy.log() == AllocatorLog{
  287. Allocate(sizeofPool()),
  288. Allocate(sizeofString("example")),
  289. });
  290. }
  291. }
  292. TEST_CASE("MemberProxy under memory constraints") {
  293. TimebombAllocator timebomb(1);
  294. SpyingAllocator spy(&timebomb);
  295. JsonDocument doc(&spy);
  296. SECTION("key slot allocation fails") {
  297. timebomb.setCountdown(0);
  298. doc["hello"_s] = "world";
  299. REQUIRE(doc.is<JsonObject>());
  300. REQUIRE(doc.size() == 0);
  301. REQUIRE(doc.overflowed() == true);
  302. REQUIRE(spy.log() == AllocatorLog{
  303. AllocateFail(sizeofPool()),
  304. });
  305. }
  306. SECTION("value slot allocation fails") {
  307. timebomb.setCountdown(2);
  308. // fill the pool entirely, but leave one slot for the key
  309. doc["foo"][ARDUINOJSON_POOL_CAPACITY - 4] = 1;
  310. REQUIRE(doc.overflowed() == false);
  311. doc["hello"_s] = "world";
  312. REQUIRE(doc.is<JsonObject>());
  313. REQUIRE(doc.size() == 1);
  314. REQUIRE(doc.overflowed() == true);
  315. REQUIRE(spy.log() == AllocatorLog{
  316. Allocate(sizeofPool()),
  317. Allocate(sizeofStaticStringPool()),
  318. AllocateFail(sizeofPool()),
  319. });
  320. }
  321. SECTION("key string allocation fails") {
  322. timebomb.setCountdown(1);
  323. doc["hello"_s] = "world";
  324. REQUIRE(doc.is<JsonObject>());
  325. REQUIRE(doc.size() == 0);
  326. REQUIRE(doc.overflowed() == true);
  327. REQUIRE(spy.log() == AllocatorLog{
  328. Allocate(sizeofPool()),
  329. AllocateFail(sizeofString("hello")),
  330. });
  331. }
  332. }