set.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 };
  7. TEST_CASE("JsonVariant::set() when there is enough memory") {
  8. DynamicJsonDocument doc(4096);
  9. JsonVariant variant = doc.to<JsonVariant>();
  10. SECTION("const char*") {
  11. char str[16];
  12. strcpy(str, "hello");
  13. bool result = variant.set(static_cast<const char*>(str));
  14. strcpy(str, "world");
  15. REQUIRE(result == true);
  16. REQUIRE(variant == "world"); // stores by pointer
  17. }
  18. SECTION("(const char*)0") {
  19. bool result = variant.set(static_cast<const char*>(0));
  20. REQUIRE(result == true);
  21. REQUIRE(variant.isNull());
  22. }
  23. SECTION("char*") {
  24. char str[16];
  25. strcpy(str, "hello");
  26. bool result = variant.set(str);
  27. strcpy(str, "world");
  28. REQUIRE(result == true);
  29. REQUIRE(variant == "hello"); // stores by copy
  30. }
  31. SECTION("(char*)0") {
  32. bool result = variant.set(static_cast<char*>(0));
  33. REQUIRE(result == true);
  34. REQUIRE(variant.isNull());
  35. }
  36. SECTION("unsigned char*") {
  37. char str[16];
  38. strcpy(str, "hello");
  39. bool result = variant.set(reinterpret_cast<unsigned char*>(str));
  40. strcpy(str, "world");
  41. REQUIRE(result == true);
  42. REQUIRE(variant == "hello"); // stores by copy
  43. }
  44. SECTION("signed char*") {
  45. char str[16];
  46. strcpy(str, "hello");
  47. bool result = variant.set(reinterpret_cast<signed char*>(str));
  48. strcpy(str, "world");
  49. REQUIRE(result == true);
  50. REQUIRE(variant == "hello"); // stores by copy
  51. }
  52. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  53. SECTION("VLA") {
  54. size_t n = 16;
  55. char str[n];
  56. strcpy(str, "hello");
  57. bool result = variant.set(str);
  58. strcpy(str, "world");
  59. REQUIRE(result == true);
  60. REQUIRE(variant == "hello"); // stores by copy
  61. }
  62. #endif
  63. SECTION("std::string") {
  64. std::string str;
  65. str = "hello";
  66. bool result = variant.set(str);
  67. str.replace(0, 5, "world");
  68. REQUIRE(result == true);
  69. REQUIRE(variant == "hello"); // stores by copy
  70. }
  71. SECTION("static JsonString") {
  72. char str[16];
  73. strcpy(str, "hello");
  74. bool result = variant.set(JsonString(str, JsonString::Linked));
  75. strcpy(str, "world");
  76. REQUIRE(result == true);
  77. REQUIRE(variant == "world"); // stores by pointer
  78. }
  79. SECTION("non-static JsonString") {
  80. char str[16];
  81. strcpy(str, "hello");
  82. bool result = variant.set(JsonString(str, JsonString::Copied));
  83. strcpy(str, "world");
  84. REQUIRE(result == true);
  85. REQUIRE(variant == "hello"); // stores by copy
  86. }
  87. SECTION("enum") {
  88. ErrorCode code = ERROR_10;
  89. bool result = variant.set(code);
  90. REQUIRE(result == true);
  91. REQUIRE(variant.is<int>() == true);
  92. REQUIRE(variant.as<int>() == 10);
  93. }
  94. }
  95. TEST_CASE("JsonVariant::set() with not enough memory") {
  96. StaticJsonDocument<1> doc;
  97. JsonVariant v = doc.to<JsonVariant>();
  98. SECTION("std::string") {
  99. bool result = v.set(std::string("hello world!!"));
  100. REQUIRE(result == false);
  101. REQUIRE(v.isNull());
  102. }
  103. SECTION("Serialized<std::string>") {
  104. bool result = v.set(serialized(std::string("hello world!!")));
  105. REQUIRE(result == false);
  106. REQUIRE(v.isNull());
  107. }
  108. SECTION("char*") {
  109. char s[] = "hello world!!";
  110. bool result = v.set(s);
  111. REQUIRE(result == false);
  112. REQUIRE(v.isNull());
  113. }
  114. }
  115. TEST_CASE("JsonVariant::set(DynamicJsonDocument)") {
  116. DynamicJsonDocument doc1(1024);
  117. doc1["hello"] = "world";
  118. DynamicJsonDocument doc2(1024);
  119. JsonVariant v = doc2.to<JsonVariant>();
  120. // Should copy the doc
  121. v.set(doc1);
  122. doc1.clear();
  123. std::string json;
  124. serializeJson(doc2, json);
  125. REQUIRE(json == "{\"hello\":\"world\"}");
  126. }