MemberProxy.cpp 9.8 KB

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