MemberProxy.cpp 8.7 KB

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