// ArduinoJson - arduinojson.org // Copyright Benoit Blanchon 2014-2018 // MIT License #include #include #include TEST_CASE("JsonObject::set()") { DynamicJsonDocument doc; JsonObject obj = doc.to(); SECTION("int") { obj.set("hello", 123); REQUIRE(123 == obj["hello"].as()); REQUIRE(obj["hello"].is()); REQUIRE_FALSE(obj["hello"].is()); } SECTION("double") { obj.set("hello", 123.45); REQUIRE(123.45 == obj["hello"].as()); REQUIRE(obj["hello"].is()); REQUIRE_FALSE(obj["hello"].is()); } SECTION("bool") { obj.set("hello", true); REQUIRE(obj["hello"].as()); REQUIRE(obj["hello"].is()); REQUIRE_FALSE(obj["hello"].is()); } SECTION("const char*") { obj.set("hello", "h3110"); REQUIRE(std::string("h3110") == obj["hello"].as()); REQUIRE(obj["hello"].is()); REQUIRE_FALSE(obj["hello"].is()); } SECTION("nested array") { DynamicJsonDocument doc2; JsonArray arr = doc2.to(); obj.set("hello", arr); REQUIRE(arr == obj["hello"].as()); REQUIRE(obj["hello"].is()); REQUIRE_FALSE(obj["hello"].is()); } SECTION("nested object") { DynamicJsonDocument doc2; JsonObject obj2 = doc2.to(); obj.set("hello", obj2); REQUIRE(obj2 == obj["hello"].as()); REQUIRE(obj["hello"].is()); REQUIRE_FALSE(obj["hello"].is()); } SECTION("array subscript") { DynamicJsonDocument doc2; JsonArray arr = doc2.to(); arr.add(42); obj.set("a", arr[0]); REQUIRE(42 == obj["a"]); } SECTION("object subscript") { DynamicJsonDocument doc2; JsonObject obj2 = doc2.to(); obj2.set("x", 42); obj.set("a", obj2["x"]); REQUIRE(42 == obj["a"]); } SECTION("returns true when allocation succeeds") { StaticJsonDocument doc2; JsonObject obj2 = doc2.to(); REQUIRE(true == obj2.set(std::string("hello"), std::string("world"))); } SECTION("returns false when allocation fails") { StaticJsonDocument doc2; JsonObject obj2 = doc2.to(); REQUIRE(false == obj2.set(std::string("hello"), std::string("world"))); } SECTION("should not duplicate const char*") { obj.set("hello", "world"); const size_t expectedSize = JSON_OBJECT_SIZE(1); REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate char* value") { obj.set("hello", const_cast("world")); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6; REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate char* key") { obj.set(const_cast("hello"), "world"); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6; REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate char* key&value") { obj.set(const_cast("hello"), const_cast("world")); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12; REQUIRE(expectedSize <= doc.memoryUsage()); } SECTION("should duplicate std::string value") { obj.set("hello", std::string("world")); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6; REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate std::string key") { obj.set(std::string("hello"), "world"); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6; REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate std::string key&value") { obj.set(std::string("hello"), std::string("world")); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12; REQUIRE(expectedSize <= doc.memoryUsage()); } }