set.cpp 3.7 KB

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