MemberProxy.cpp 10 KB

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