unsigned_char.cpp 6.6 KB

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