ElementProxy.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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::ElementProxy<JsonDocument&> ElementProxy;
  7. TEST_CASE("ElementProxy::add()") {
  8. DynamicJsonDocument doc(4096);
  9. doc.add();
  10. ElementProxy ep = doc[0];
  11. SECTION("add(int)") {
  12. ep.add(42);
  13. REQUIRE(doc.as<std::string>() == "[[42]]");
  14. }
  15. SECTION("add(const char*)") {
  16. ep.add("world");
  17. REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
  18. }
  19. SECTION("set(char[])") {
  20. char s[] = "world";
  21. ep.add(s);
  22. strcpy(s, "!!!!!");
  23. REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
  24. }
  25. }
  26. TEST_CASE("ElementProxy::clear()") {
  27. DynamicJsonDocument doc(4096);
  28. doc.add();
  29. ElementProxy ep = doc[0];
  30. SECTION("size goes back to zero") {
  31. ep.add(42);
  32. ep.clear();
  33. REQUIRE(ep.size() == 0);
  34. }
  35. SECTION("isNull() return true") {
  36. ep.add("hello");
  37. ep.clear();
  38. REQUIRE(ep.isNull() == true);
  39. }
  40. }
  41. TEST_CASE("ElementProxy::operator==()") {
  42. DynamicJsonDocument doc(4096);
  43. SECTION("1 vs 1") {
  44. doc.add(1);
  45. doc.add(1);
  46. REQUIRE(doc[0] <= doc[1]);
  47. REQUIRE(doc[0] == doc[1]);
  48. REQUIRE(doc[0] >= doc[1]);
  49. REQUIRE_FALSE(doc[0] != doc[1]);
  50. REQUIRE_FALSE(doc[0] < doc[1]);
  51. REQUIRE_FALSE(doc[0] > doc[1]);
  52. }
  53. SECTION("1 vs 2") {
  54. doc.add(1);
  55. doc.add(2);
  56. REQUIRE(doc[0] != doc[1]);
  57. REQUIRE(doc[0] < doc[1]);
  58. REQUIRE(doc[0] <= doc[1]);
  59. REQUIRE_FALSE(doc[0] == doc[1]);
  60. REQUIRE_FALSE(doc[0] > doc[1]);
  61. REQUIRE_FALSE(doc[0] >= doc[1]);
  62. }
  63. SECTION("'abc' vs 'bcd'") {
  64. doc.add("abc");
  65. doc.add("bcd");
  66. REQUIRE(doc[0] != doc[1]);
  67. REQUIRE(doc[0] < doc[1]);
  68. REQUIRE(doc[0] <= doc[1]);
  69. REQUIRE_FALSE(doc[0] == doc[1]);
  70. REQUIRE_FALSE(doc[0] > doc[1]);
  71. REQUIRE_FALSE(doc[0] >= doc[1]);
  72. }
  73. }
  74. TEST_CASE("ElementProxy::remove()") {
  75. DynamicJsonDocument doc(4096);
  76. doc.add();
  77. ElementProxy ep = doc[0];
  78. SECTION("remove(int)") {
  79. ep.add(1);
  80. ep.add(2);
  81. ep.add(3);
  82. ep.remove(1);
  83. REQUIRE(ep.as<std::string>() == "[1,3]");
  84. }
  85. SECTION("remove(const char *)") {
  86. ep["a"] = 1;
  87. ep["b"] = 2;
  88. ep.remove("a");
  89. REQUIRE(ep.as<std::string>() == "{\"b\":2}");
  90. }
  91. SECTION("remove(std::string)") {
  92. ep["a"] = 1;
  93. ep["b"] = 2;
  94. ep.remove(std::string("b"));
  95. REQUIRE(ep.as<std::string>() == "{\"a\":1}");
  96. }
  97. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  98. SECTION("remove(vla)") {
  99. ep["a"] = 1;
  100. ep["b"] = 2;
  101. size_t i = 4;
  102. char vla[i];
  103. strcpy(vla, "b");
  104. ep.remove(vla);
  105. REQUIRE(ep.as<std::string>() == "{\"a\":1}");
  106. }
  107. #endif
  108. }
  109. TEST_CASE("ElementProxy::set()") {
  110. DynamicJsonDocument doc(4096);
  111. ElementProxy ep = doc[0];
  112. SECTION("set(int)") {
  113. ep.set(42);
  114. REQUIRE(doc.as<std::string>() == "[42]");
  115. }
  116. SECTION("set(const char*)") {
  117. ep.set("world");
  118. REQUIRE(doc.as<std::string>() == "[\"world\"]");
  119. }
  120. SECTION("set(char[])") {
  121. char s[] = "world";
  122. ep.set(s);
  123. strcpy(s, "!!!!!");
  124. REQUIRE(doc.as<std::string>() == "[\"world\"]");
  125. }
  126. }
  127. TEST_CASE("ElementProxy::size()") {
  128. DynamicJsonDocument doc(4096);
  129. doc.add();
  130. ElementProxy ep = doc[0];
  131. SECTION("returns 0") {
  132. REQUIRE(ep.size() == 0);
  133. }
  134. SECTION("as an array, returns 2") {
  135. ep.add(1);
  136. ep.add(2);
  137. REQUIRE(ep.size() == 2);
  138. }
  139. SECTION("as an object, returns 2") {
  140. ep["a"] = 1;
  141. ep["b"] = 2;
  142. REQUIRE(ep.size() == 2);
  143. }
  144. }
  145. TEST_CASE("ElementProxy::memoryUsage()") {
  146. DynamicJsonDocument doc(4096);
  147. doc.add();
  148. ElementProxy ep = doc[0];
  149. SECTION("returns 0 for null") {
  150. REQUIRE(ep.memoryUsage() == 0);
  151. }
  152. SECTION("returns size for string") {
  153. ep.set(std::string("hello"));
  154. REQUIRE(ep.memoryUsage() == 6);
  155. }
  156. }
  157. TEST_CASE("ElementProxy::operator[]") {
  158. DynamicJsonDocument doc(4096);
  159. ElementProxy ep = doc[1];
  160. SECTION("set member") {
  161. ep["world"] = 42;
  162. REQUIRE(doc.as<std::string>() == "[null,{\"world\":42}]");
  163. }
  164. SECTION("set element") {
  165. ep[2] = 42;
  166. REQUIRE(doc.as<std::string>() == "[null,[null,null,42]]");
  167. }
  168. }
  169. TEST_CASE("ElementProxy cast to JsonVariantConst") {
  170. DynamicJsonDocument doc(4096);
  171. doc[0] = "world";
  172. const ElementProxy ep = doc[0];
  173. JsonVariantConst var = ep;
  174. CHECK(var.as<std::string>() == "world");
  175. }
  176. TEST_CASE("ElementProxy cast to JsonVariant") {
  177. DynamicJsonDocument doc(4096);
  178. doc[0] = "world";
  179. ElementProxy ep = doc[0];
  180. JsonVariant var = ep;
  181. CHECK(var.as<std::string>() == "world");
  182. var.set("toto");
  183. CHECK(doc.as<std::string>() == "[\"toto\"]");
  184. }
  185. TEST_CASE("ElementProxy::shallowCopy()") {
  186. StaticJsonDocument<1024> doc1, doc2;
  187. doc2["hello"] = "world";
  188. doc1[0].shallowCopy(doc2);
  189. CHECK(doc1.as<std::string>() == "[{\"hello\":\"world\"}]");
  190. }