MemberProxy.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. typedef ARDUINOJSON_NAMESPACE::MemberProxy<JsonDocument&, const char*>
  7. MemberProxy;
  8. TEST_CASE("MemberProxy::add()") {
  9. DynamicJsonDocument doc(4096);
  10. MemberProxy mp = doc["hello"];
  11. SECTION("add(int)") {
  12. mp.add(42);
  13. REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
  14. }
  15. SECTION("add(const char*)") {
  16. mp.add("world");
  17. REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
  18. }
  19. }
  20. TEST_CASE("MemberProxy::clear()") {
  21. DynamicJsonDocument doc(4096);
  22. MemberProxy mp = doc["hello"];
  23. SECTION("size goes back to zero") {
  24. mp.add(42);
  25. mp.clear();
  26. REQUIRE(mp.size() == 0);
  27. }
  28. SECTION("isNull() return true") {
  29. mp.add("hello");
  30. mp.clear();
  31. REQUIRE(mp.isNull() == true);
  32. }
  33. }
  34. TEST_CASE("MemberProxy::operator==()") {
  35. DynamicJsonDocument doc(4096);
  36. SECTION("1 vs 1") {
  37. doc["a"] = 1;
  38. doc["b"] = 1;
  39. REQUIRE(doc["a"] <= doc["b"]);
  40. REQUIRE(doc["a"] == doc["b"]);
  41. REQUIRE(doc["a"] >= doc["b"]);
  42. REQUIRE_FALSE(doc["a"] != doc["b"]);
  43. REQUIRE_FALSE(doc["a"] < doc["b"]);
  44. REQUIRE_FALSE(doc["a"] > doc["b"]);
  45. }
  46. SECTION("1 vs 2") {
  47. doc["a"] = 1;
  48. doc["b"] = 2;
  49. REQUIRE(doc["a"] != doc["b"]);
  50. REQUIRE(doc["a"] < doc["b"]);
  51. REQUIRE(doc["a"] <= doc["b"]);
  52. REQUIRE_FALSE(doc["a"] == doc["b"]);
  53. REQUIRE_FALSE(doc["a"] > doc["b"]);
  54. REQUIRE_FALSE(doc["a"] >= doc["b"]);
  55. }
  56. SECTION("'abc' vs 'bcd'") {
  57. doc["a"] = "abc";
  58. doc["b"] = "bcd";
  59. REQUIRE(doc["a"] != doc["b"]);
  60. REQUIRE(doc["a"] < doc["b"]);
  61. REQUIRE(doc["a"] <= doc["b"]);
  62. REQUIRE_FALSE(doc["a"] == doc["b"]);
  63. REQUIRE_FALSE(doc["a"] > doc["b"]);
  64. REQUIRE_FALSE(doc["a"] >= doc["b"]);
  65. }
  66. }
  67. TEST_CASE("MemberProxy::containsKey()") {
  68. DynamicJsonDocument doc(4096);
  69. MemberProxy mp = doc["hello"];
  70. SECTION("containsKey(const char*)") {
  71. mp["key"] = "value";
  72. REQUIRE(mp.containsKey("key") == true);
  73. REQUIRE(mp.containsKey("key") == true);
  74. }
  75. SECTION("containsKey(std::string)") {
  76. mp["key"] = "value";
  77. REQUIRE(mp.containsKey(std::string("key")) == true);
  78. REQUIRE(mp.containsKey(std::string("key")) == true);
  79. }
  80. }
  81. TEST_CASE("MemberProxy::operator|()") {
  82. DynamicJsonDocument doc(4096);
  83. SECTION("const char*") {
  84. doc["a"] = "hello";
  85. REQUIRE((doc["a"] | "world") == std::string("hello"));
  86. REQUIRE((doc["b"] | "world") == std::string("world"));
  87. }
  88. SECTION("Issue #1411") {
  89. doc["sensor"] = "gps";
  90. const char* test = "test"; // <- the literal must be captured in a variable
  91. // to trigger the bug
  92. const char* sensor = doc["sensor"] | test; // "gps"
  93. REQUIRE(sensor == std::string("gps"));
  94. }
  95. SECTION("Issue #1415") {
  96. JsonObject object = doc.to<JsonObject>();
  97. object["hello"] = "world";
  98. StaticJsonDocument<0> emptyDoc;
  99. JsonObject anotherObject = object["hello"] | emptyDoc.to<JsonObject>();
  100. REQUIRE(anotherObject.isNull() == false);
  101. REQUIRE(anotherObject.size() == 0);
  102. }
  103. }
  104. TEST_CASE("MemberProxy::remove()") {
  105. DynamicJsonDocument doc(4096);
  106. MemberProxy mp = doc["hello"];
  107. SECTION("remove(int)") {
  108. mp.add(1);
  109. mp.add(2);
  110. mp.add(3);
  111. mp.remove(1);
  112. REQUIRE(mp.as<std::string>() == "[1,3]");
  113. }
  114. SECTION("remove(const char *)") {
  115. mp["a"] = 1;
  116. mp["b"] = 2;
  117. mp.remove("a");
  118. REQUIRE(mp.as<std::string>() == "{\"b\":2}");
  119. }
  120. SECTION("remove(std::string)") {
  121. mp["a"] = 1;
  122. mp["b"] = 2;
  123. mp.remove(std::string("b"));
  124. REQUIRE(mp.as<std::string>() == "{\"a\":1}");
  125. }
  126. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  127. SECTION("remove(vla)") {
  128. mp["a"] = 1;
  129. mp["b"] = 2;
  130. size_t i = 4;
  131. char vla[i];
  132. strcpy(vla, "b");
  133. mp.remove(vla);
  134. REQUIRE(mp.as<std::string>() == "{\"a\":1}");
  135. }
  136. #endif
  137. }
  138. TEST_CASE("MemberProxy::set()") {
  139. DynamicJsonDocument doc(4096);
  140. MemberProxy mp = doc["hello"];
  141. SECTION("set(int)") {
  142. mp.set(42);
  143. REQUIRE(doc.as<std::string>() == "{\"hello\":42}");
  144. }
  145. SECTION("set(const char*)") {
  146. mp.set("world");
  147. REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
  148. }
  149. SECTION("set(char[])") { // issue #1191
  150. char s[] = "world";
  151. mp.set(s);
  152. strcpy(s, "!!!!!");
  153. REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
  154. }
  155. }
  156. TEST_CASE("MemberProxy::size()") {
  157. DynamicJsonDocument doc(4096);
  158. MemberProxy mp = doc["hello"];
  159. SECTION("returns 0") {
  160. REQUIRE(mp.size() == 0);
  161. }
  162. SECTION("as an array, return 2") {
  163. mp.add(1);
  164. mp.add(2);
  165. REQUIRE(mp.size() == 2);
  166. }
  167. SECTION("as an object, return 2") {
  168. mp["a"] = 1;
  169. mp["b"] = 2;
  170. REQUIRE(mp.size() == 2);
  171. }
  172. }
  173. TEST_CASE("MemberProxy::memoryUsage()") {
  174. DynamicJsonDocument doc(4096);
  175. MemberProxy mp = doc["hello"];
  176. SECTION("returns 0 when null") {
  177. REQUIRE(mp.memoryUsage() == 0);
  178. }
  179. SECTION("return the size for a string") {
  180. mp.set(std::string("hello"));
  181. REQUIRE(mp.memoryUsage() == 6);
  182. }
  183. }
  184. TEST_CASE("MemberProxy::operator[]") {
  185. DynamicJsonDocument doc(4096);
  186. MemberProxy mp = doc["hello"];
  187. SECTION("set member") {
  188. mp["world"] = 42;
  189. REQUIRE(doc.as<std::string>() == "{\"hello\":{\"world\":42}}");
  190. }
  191. SECTION("set element") {
  192. mp[2] = 42;
  193. REQUIRE(doc.as<std::string>() == "{\"hello\":[null,null,42]}");
  194. }
  195. }
  196. TEST_CASE("MemberProxy cast to JsonVariantConst") {
  197. DynamicJsonDocument doc(4096);
  198. doc["hello"] = "world";
  199. const MemberProxy mp = doc["hello"];
  200. JsonVariantConst var = mp;
  201. CHECK(var.as<std::string>() == "world");
  202. }
  203. TEST_CASE("MemberProxy cast to JsonVariant") {
  204. DynamicJsonDocument doc(4096);
  205. doc["hello"] = "world";
  206. MemberProxy mp = doc["hello"];
  207. JsonVariant var = mp;
  208. CHECK(var.as<std::string>() == "world");
  209. var.set("toto");
  210. CHECK(doc.as<std::string>() == "{\"hello\":\"toto\"}");
  211. }
  212. TEST_CASE("MemberProxy::createNestedArray()") {
  213. StaticJsonDocument<1024> doc;
  214. JsonArray arr = doc["items"].createNestedArray();
  215. arr.add(42);
  216. CHECK(doc["items"][0][0] == 42);
  217. }
  218. TEST_CASE("MemberProxy::createNestedArray(key)") {
  219. StaticJsonDocument<1024> doc;
  220. JsonArray arr = doc["weather"].createNestedArray("temp");
  221. arr.add(42);
  222. CHECK(doc["weather"]["temp"][0] == 42);
  223. }
  224. TEST_CASE("MemberProxy::createNestedObject()") {
  225. StaticJsonDocument<1024> doc;
  226. JsonObject obj = doc["items"].createNestedObject();
  227. obj["value"] = 42;
  228. CHECK(doc["items"][0]["value"] == 42);
  229. }
  230. TEST_CASE("MemberProxy::createNestedObject(key)") {
  231. StaticJsonDocument<1024> doc;
  232. JsonObject obj = doc["status"].createNestedObject("weather");
  233. obj["temp"] = 42;
  234. CHECK(doc["status"]["weather"]["temp"] == 42);
  235. }
  236. TEST_CASE("MemberProxy::shallowCopy()") {
  237. StaticJsonDocument<1024> doc1, doc2;
  238. doc2["hello"] = "world";
  239. doc1["obj"].shallowCopy(doc2);
  240. CHECK(doc1.as<std::string>() == "{\"obj\":{\"hello\":\"world\"}}");
  241. }