MemberProxy.cpp 7.9 KB

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