unsigned_char.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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[]") {
  10. SECTION("deserializeJson()") {
  11. unsigned char input[] = "{\"a\":42}";
  12. StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
  13. DeserializationError err = deserializeJson(doc, input);
  14. REQUIRE(err == DeserializationError::Ok);
  15. }
  16. SECTION("deserializeMsgPack()") {
  17. unsigned char input[] = "\xDE\x00\x01\xA5Hello\xA5world";
  18. StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
  19. DeserializationError err = deserializeMsgPack(doc, input);
  20. REQUIRE(err == DeserializationError::Ok);
  21. }
  22. SECTION("JsonVariant") {
  23. DynamicJsonDocument doc(4096);
  24. SECTION("set") {
  25. unsigned char value[] = "42";
  26. JsonVariant variant = doc.to<JsonVariant>();
  27. variant.set(value);
  28. REQUIRE(42 == variant.as<int>());
  29. }
  30. #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
  31. SECTION("operator[]") {
  32. unsigned char key[] = "hello";
  33. deserializeJson(doc, "{\"hello\":\"world\"}");
  34. JsonVariant variant = doc.as<JsonVariant>();
  35. REQUIRE(std::string("world") == variant[key]);
  36. }
  37. #endif
  38. #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
  39. SECTION("operator[] const") {
  40. unsigned char key[] = "hello";
  41. deserializeJson(doc, "{\"hello\":\"world\"}");
  42. const JsonVariant variant = doc.as<JsonVariant>();
  43. REQUIRE(std::string("world") == variant[key]);
  44. }
  45. #endif
  46. SECTION("operator==") {
  47. unsigned char comparand[] = "hello";
  48. JsonVariant variant = doc.to<JsonVariant>();
  49. variant.set("hello");
  50. REQUIRE(comparand == variant);
  51. REQUIRE(variant == comparand);
  52. REQUIRE_FALSE(comparand != variant);
  53. REQUIRE_FALSE(variant != comparand);
  54. }
  55. SECTION("operator!=") {
  56. unsigned char comparand[] = "hello";
  57. JsonVariant variant = doc.to<JsonVariant>();
  58. variant.set("world");
  59. REQUIRE(comparand != variant);
  60. REQUIRE(variant != comparand);
  61. REQUIRE_FALSE(comparand == variant);
  62. REQUIRE_FALSE(variant == comparand);
  63. }
  64. }
  65. SECTION("JsonObject") {
  66. #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
  67. SECTION("operator[]") {
  68. unsigned char key[] = "hello";
  69. DynamicJsonDocument doc(4096);
  70. JsonObject obj = doc.to<JsonObject>();
  71. obj[key] = "world";
  72. REQUIRE(std::string("world") == obj["hello"]);
  73. }
  74. SECTION("JsonObject::operator[] const") {
  75. unsigned char key[] = "hello";
  76. DynamicJsonDocument doc(4096);
  77. deserializeJson(doc, "{\"hello\":\"world\"}");
  78. JsonObject obj = doc.as<JsonObject>();
  79. REQUIRE(std::string("world") == obj[key]);
  80. }
  81. #endif
  82. SECTION("containsKey()") {
  83. unsigned char key[] = "hello";
  84. DynamicJsonDocument doc(4096);
  85. deserializeJson(doc, "{\"hello\":\"world\"}");
  86. JsonObject obj = doc.as<JsonObject>();
  87. REQUIRE(true == obj.containsKey(key));
  88. }
  89. SECTION("remove()") {
  90. unsigned char key[] = "hello";
  91. DynamicJsonDocument doc(4096);
  92. deserializeJson(doc, "{\"hello\":\"world\"}");
  93. JsonObject obj = doc.as<JsonObject>();
  94. obj.remove(key);
  95. REQUIRE(0 == obj.size());
  96. }
  97. SECTION("createNestedArray()") {
  98. unsigned char key[] = "hello";
  99. DynamicJsonDocument doc(4096);
  100. JsonObject obj = doc.to<JsonObject>();
  101. obj.createNestedArray(key);
  102. }
  103. SECTION("createNestedObject()") {
  104. unsigned char key[] = "hello";
  105. DynamicJsonDocument doc(4096);
  106. JsonObject obj = doc.to<JsonObject>();
  107. obj.createNestedObject(key);
  108. }
  109. }
  110. SECTION("MemberProxy") {
  111. SECTION("operator=") { // issue #416
  112. unsigned char value[] = "world";
  113. DynamicJsonDocument doc(4096);
  114. JsonObject obj = doc.to<JsonObject>();
  115. obj["hello"] = value;
  116. REQUIRE(std::string("world") == obj["hello"]);
  117. }
  118. SECTION("set()") {
  119. unsigned char value[] = "world";
  120. DynamicJsonDocument doc(4096);
  121. JsonObject obj = doc.to<JsonObject>();
  122. obj["hello"].set(value);
  123. REQUIRE(std::string("world") == obj["hello"]);
  124. }
  125. }
  126. SECTION("JsonArray") {
  127. SECTION("add()") {
  128. unsigned char value[] = "world";
  129. DynamicJsonDocument doc(4096);
  130. JsonArray arr = doc.to<JsonArray>();
  131. arr.add(value);
  132. REQUIRE(std::string("world") == arr[0]);
  133. }
  134. }
  135. SECTION("ElementProxy") {
  136. SECTION("set()") {
  137. unsigned char value[] = "world";
  138. DynamicJsonDocument doc(4096);
  139. JsonArray arr = doc.to<JsonArray>();
  140. arr.add("hello");
  141. arr[0].set(value);
  142. REQUIRE(std::string("world") == arr[0]);
  143. }
  144. SECTION("operator=") {
  145. unsigned char value[] = "world";
  146. DynamicJsonDocument doc(4096);
  147. JsonArray arr = doc.to<JsonArray>();
  148. arr.add("hello");
  149. arr[0] = value;
  150. REQUIRE(std::string("world") == arr[0]);
  151. }
  152. }
  153. }