set.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonVariant and strings") {
  7. DynamicJsonDocument doc(4096);
  8. JsonVariant variant = doc.to<JsonVariant>();
  9. SECTION("stores const char* by reference") {
  10. char str[16];
  11. strcpy(str, "hello");
  12. variant.set(static_cast<const char *>(str));
  13. strcpy(str, "world");
  14. REQUIRE(variant == "world");
  15. }
  16. SECTION("stores char* by copy") {
  17. char str[16];
  18. strcpy(str, "hello");
  19. variant.set(str);
  20. strcpy(str, "world");
  21. REQUIRE(variant == "hello");
  22. }
  23. SECTION("stores unsigned char* by copy") {
  24. char str[16];
  25. strcpy(str, "hello");
  26. variant.set(reinterpret_cast<unsigned char *>(str));
  27. strcpy(str, "world");
  28. REQUIRE(variant == "hello");
  29. }
  30. SECTION("stores signed char* by copy") {
  31. char str[16];
  32. strcpy(str, "hello");
  33. variant.set(reinterpret_cast<signed char *>(str));
  34. strcpy(str, "world");
  35. REQUIRE(variant == "hello");
  36. }
  37. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  38. SECTION("stores VLA by copy") {
  39. int n = 16;
  40. char str[n];
  41. strcpy(str, "hello");
  42. variant.set(str);
  43. strcpy(str, "world");
  44. REQUIRE(variant == "hello");
  45. }
  46. #endif
  47. SECTION("stores std::string by copy") {
  48. std::string str;
  49. str = "hello";
  50. variant.set(str);
  51. str.replace(0, 5, "world");
  52. REQUIRE(variant == "hello");
  53. }
  54. SECTION("stores static JsonString by reference") {
  55. char str[16];
  56. strcpy(str, "hello");
  57. variant.set(JsonString(str, true));
  58. strcpy(str, "world");
  59. REQUIRE(variant == "hello");
  60. }
  61. SECTION("stores non-static JsonString by copy") {
  62. char str[16];
  63. strcpy(str, "hello");
  64. variant.set(JsonString(str, false));
  65. strcpy(str, "world");
  66. REQUIRE(variant == "hello");
  67. }
  68. }
  69. TEST_CASE("JsonVariant with not enough memory") {
  70. StaticJsonDocument<1> doc;
  71. JsonVariant v = doc.to<JsonVariant>();
  72. SECTION("std::string") {
  73. v.set(std::string("hello world!!"));
  74. REQUIRE(v.isNull());
  75. }
  76. SECTION("Serialized<std::string>") {
  77. v.set(serialized(std::string("hello world!!")));
  78. REQUIRE(v.isNull());
  79. }
  80. }
  81. TEST_CASE("JsonVariant::set(DynamicJsonDocument)") {
  82. DynamicJsonDocument doc1(1024);
  83. doc1["hello"] = "world";
  84. DynamicJsonDocument doc2(1024);
  85. JsonVariant v = doc2.to<JsonVariant>();
  86. // Should copy the doc
  87. v.set(doc1);
  88. doc1.clear();
  89. std::string json;
  90. serializeJson(doc2, json);
  91. REQUIRE(json == "{\"hello\":\"world\"}");
  92. }