MemberProxy.cpp 8.4 KB

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