set.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2023
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <string>
  7. TEST_CASE("JsonObject::set()") {
  8. DynamicJsonBuffer jb;
  9. JsonObject& _object = jb.createObject();
  10. SECTION("int") {
  11. _object.set("hello", 123);
  12. REQUIRE(123 == _object["hello"].as<int>());
  13. REQUIRE(_object["hello"].is<int>());
  14. REQUIRE_FALSE(_object["hello"].is<bool>());
  15. }
  16. SECTION("double") {
  17. _object.set("hello", 123.45);
  18. REQUIRE(123.45 == _object["hello"].as<double>());
  19. REQUIRE(_object["hello"].is<double>());
  20. REQUIRE_FALSE(_object["hello"].is<bool>());
  21. }
  22. SECTION("bool") {
  23. _object.set("hello", true);
  24. REQUIRE(_object["hello"].as<bool>());
  25. REQUIRE(_object["hello"].is<bool>());
  26. REQUIRE_FALSE(_object["hello"].is<long>());
  27. }
  28. SECTION("const char*") {
  29. _object.set("hello", "h3110");
  30. REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
  31. REQUIRE(_object["hello"].is<const char*>());
  32. REQUIRE_FALSE(_object["hello"].is<long>());
  33. }
  34. SECTION("nested array") {
  35. JsonArray& arr = jb.createArray();
  36. _object.set("hello", arr);
  37. REQUIRE(&arr == &_object["hello"].as<JsonArray>());
  38. REQUIRE(_object["hello"].is<JsonArray&>());
  39. REQUIRE_FALSE(_object["hello"].is<JsonObject&>());
  40. }
  41. SECTION("nested object") {
  42. JsonObject& obj = jb.createObject();
  43. _object.set("hello", obj);
  44. REQUIRE(&obj == &_object["hello"].as<JsonObject>());
  45. REQUIRE(_object["hello"].is<JsonObject&>());
  46. REQUIRE_FALSE(_object["hello"].is<JsonArray&>());
  47. }
  48. SECTION("array subscript") {
  49. JsonArray& arr = jb.createArray();
  50. arr.add(42);
  51. _object.set("a", arr[0]);
  52. REQUIRE(42 == _object["a"]);
  53. }
  54. SECTION("object subscript") {
  55. JsonObject& obj = jb.createObject();
  56. obj.set("x", 42);
  57. _object.set("a", obj["x"]);
  58. REQUIRE(42 == _object["a"]);
  59. }
  60. SECTION("returns true when allocation succeeds") {
  61. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
  62. JsonObject& obj = jsonBuffer.createObject();
  63. REQUIRE(true == obj.set(std::string("hello"), std::string("world")));
  64. }
  65. SECTION("returns false when allocation fails") {
  66. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
  67. JsonObject& obj = jsonBuffer.createObject();
  68. REQUIRE(false == obj.set(std::string("hello"), std::string("world")));
  69. }
  70. SECTION("should not duplicate const char*") {
  71. _object.set("hello", "world");
  72. const size_t expectedSize = JSON_OBJECT_SIZE(1);
  73. REQUIRE(expectedSize == jb.size());
  74. }
  75. SECTION("should duplicate char* value") {
  76. _object.set("hello", const_cast<char*>("world"));
  77. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
  78. REQUIRE(expectedSize == jb.size());
  79. }
  80. SECTION("should duplicate char* key") {
  81. _object.set(const_cast<char*>("hello"), "world");
  82. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
  83. REQUIRE(expectedSize == jb.size());
  84. }
  85. SECTION("should duplicate char* key&value") {
  86. _object.set(const_cast<char*>("hello"), const_cast<char*>("world"));
  87. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
  88. REQUIRE(expectedSize <= jb.size());
  89. }
  90. SECTION("should duplicate std::string value") {
  91. _object.set("hello", std::string("world"));
  92. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
  93. REQUIRE(expectedSize == jb.size());
  94. }
  95. SECTION("should duplicate std::string key") {
  96. _object.set(std::string("hello"), "world");
  97. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
  98. REQUIRE(expectedSize == jb.size());
  99. }
  100. SECTION("should duplicate std::string key&value") {
  101. _object.set(std::string("hello"), std::string("world"));
  102. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
  103. REQUIRE(expectedSize <= jb.size());
  104. }
  105. }