MemberProxy.cpp 8.8 KB

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