unsigned_char.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2023
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #if defined(__clang__)
  7. #define CONFLICTS_WITH_BUILTIN_OPERATOR
  8. #endif
  9. TEST_CASE("unsigned char string") {
  10. SECTION("JsonBuffer::parseArray") {
  11. unsigned char json[] = "[42]";
  12. StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
  13. JsonArray& arr = jsonBuffer.parseArray(json);
  14. REQUIRE(true == arr.success());
  15. }
  16. SECTION("JsonBuffer::parseObject") {
  17. unsigned char json[] = "{\"a\":42}";
  18. StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
  19. JsonObject& obj = jsonBuffer.parseObject(json);
  20. REQUIRE(true == obj.success());
  21. }
  22. SECTION("JsonVariant constructor") {
  23. unsigned char value[] = "42";
  24. JsonVariant variant(value);
  25. REQUIRE(42 == variant.as<int>());
  26. }
  27. SECTION("JsonVariant assignment operator") {
  28. unsigned char value[] = "42";
  29. JsonVariant variant(666);
  30. variant = value;
  31. REQUIRE(42 == variant.as<int>());
  32. }
  33. #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
  34. SECTION("JsonVariant::operator[]") {
  35. unsigned char key[] = "hello";
  36. DynamicJsonBuffer jsonBuffer;
  37. JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
  38. REQUIRE(std::string("world") == variant[key]);
  39. }
  40. #endif
  41. #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
  42. SECTION("JsonVariant::operator[] const") {
  43. unsigned char key[] = "hello";
  44. DynamicJsonBuffer jsonBuffer;
  45. const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
  46. REQUIRE(std::string("world") == variant[key]);
  47. }
  48. #endif
  49. SECTION("JsonVariant::operator==") {
  50. unsigned char comparand[] = "hello";
  51. DynamicJsonBuffer jsonBuffer;
  52. const JsonVariant variant = "hello";
  53. REQUIRE(comparand == variant);
  54. REQUIRE(variant == comparand);
  55. REQUIRE_FALSE(comparand != variant);
  56. REQUIRE_FALSE(variant != comparand);
  57. }
  58. SECTION("JsonVariant::operator!=") {
  59. unsigned char comparand[] = "hello";
  60. DynamicJsonBuffer jsonBuffer;
  61. const JsonVariant variant = "world";
  62. REQUIRE(comparand != variant);
  63. REQUIRE(variant != comparand);
  64. REQUIRE_FALSE(comparand == variant);
  65. REQUIRE_FALSE(variant == comparand);
  66. }
  67. #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
  68. SECTION("JsonObject::operator[]") {
  69. unsigned char key[] = "hello";
  70. DynamicJsonBuffer jsonBuffer;
  71. JsonObject& obj = jsonBuffer.createObject();
  72. obj[key] = "world";
  73. REQUIRE(std::string("world") == obj["hello"]);
  74. }
  75. #endif
  76. SECTION("JsonObjectSubscript::operator=") { // issue #416
  77. unsigned char value[] = "world";
  78. DynamicJsonBuffer jsonBuffer;
  79. JsonObject& obj = jsonBuffer.createObject();
  80. obj["hello"] = value;
  81. REQUIRE(std::string("world") == obj["hello"]);
  82. }
  83. SECTION("JsonObjectSubscript::set()") {
  84. unsigned char value[] = "world";
  85. DynamicJsonBuffer jsonBuffer;
  86. JsonObject& obj = jsonBuffer.createObject();
  87. obj["hello"].set(value);
  88. REQUIRE(std::string("world") == obj["hello"]);
  89. }
  90. #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
  91. SECTION("JsonObject::operator[] const") {
  92. unsigned char key[] = "hello";
  93. DynamicJsonBuffer jsonBuffer;
  94. const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
  95. REQUIRE(std::string("world") == obj[key]);
  96. }
  97. #endif
  98. SECTION("JsonObject::get()") {
  99. unsigned char key[] = "hello";
  100. DynamicJsonBuffer jsonBuffer;
  101. JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
  102. REQUIRE(std::string("world") == obj.get<char*>(key));
  103. }
  104. SECTION("JsonObject::set() key") {
  105. unsigned char key[] = "hello";
  106. DynamicJsonBuffer jsonBuffer;
  107. JsonObject& obj = jsonBuffer.createObject();
  108. obj.set(key, "world");
  109. REQUIRE(std::string("world") == obj["hello"]);
  110. }
  111. SECTION("JsonObject::set() value") {
  112. unsigned char value[] = "world";
  113. DynamicJsonBuffer jsonBuffer;
  114. JsonObject& obj = jsonBuffer.createObject();
  115. obj.set("hello", value);
  116. REQUIRE(std::string("world") == obj["hello"]);
  117. }
  118. SECTION("JsonObject::set key&value") {
  119. unsigned char key[] = "world";
  120. DynamicJsonBuffer jsonBuffer;
  121. JsonObject& obj = jsonBuffer.createObject();
  122. obj.set(key, key);
  123. REQUIRE(std::string("world") == obj["world"]);
  124. }
  125. SECTION("JsonObject::containsKey()") {
  126. unsigned char key[] = "hello";
  127. DynamicJsonBuffer jsonBuffer;
  128. const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
  129. REQUIRE(true == obj.containsKey(key));
  130. }
  131. SECTION("JsonObject::remove()") {
  132. unsigned char key[] = "hello";
  133. DynamicJsonBuffer jsonBuffer;
  134. JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
  135. obj.remove(key);
  136. REQUIRE(0 == obj.size());
  137. }
  138. SECTION("JsonObject::is()") {
  139. unsigned char key[] = "hello";
  140. DynamicJsonBuffer jsonBuffer;
  141. JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}");
  142. REQUIRE(true == obj.is<int>(key));
  143. }
  144. SECTION("JsonObject::createNestedArray()") {
  145. unsigned char key[] = "hello";
  146. DynamicJsonBuffer jsonBuffer;
  147. JsonObject& obj = jsonBuffer.createObject();
  148. obj.createNestedArray(key);
  149. }
  150. SECTION("JsonObject::createNestedObject()") {
  151. unsigned char key[] = "hello";
  152. DynamicJsonBuffer jsonBuffer;
  153. JsonObject& obj = jsonBuffer.createObject();
  154. obj.createNestedObject(key);
  155. }
  156. SECTION("JsonArray::add()") {
  157. unsigned char value[] = "world";
  158. DynamicJsonBuffer jsonBuffer;
  159. JsonArray& arr = jsonBuffer.createArray();
  160. arr.add(value);
  161. REQUIRE(std::string("world") == arr[0]);
  162. }
  163. SECTION("JsonArray::set()") {
  164. unsigned char value[] = "world";
  165. DynamicJsonBuffer jsonBuffer;
  166. JsonArray& arr = jsonBuffer.createArray();
  167. arr.add("hello");
  168. arr.set(0, value);
  169. REQUIRE(std::string("world") == arr[0]);
  170. }
  171. SECTION("JsonArraySubscript::set()") {
  172. unsigned char value[] = "world";
  173. DynamicJsonBuffer jsonBuffer;
  174. JsonArray& arr = jsonBuffer.createArray();
  175. arr.add("hello");
  176. arr[0].set(value);
  177. REQUIRE(std::string("world") == arr[0]);
  178. }
  179. SECTION("JsonArraySubscript::operator=") {
  180. unsigned char value[] = "world";
  181. DynamicJsonBuffer jsonBuffer;
  182. JsonArray& arr = jsonBuffer.createArray();
  183. arr.add("hello");
  184. arr[0] = value;
  185. REQUIRE(std::string("world") == arr[0]);
  186. }
  187. }