// ArduinoJson - arduinojson.org // Copyright Benoit Blanchon 2014-2018 // MIT License #include #include TEST_CASE("JsonObject::operator[]") { DynamicJsonObject _object; SECTION("int") { _object["hello"] = 123; REQUIRE(123 == _object["hello"].as()); REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); } SECTION("volatile int") { // issue #415 volatile int i = 123; _object["hello"] = i; REQUIRE(123 == _object["hello"].as()); REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); } SECTION("double") { _object["hello"] = 123.45; REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); REQUIRE(123.45 == _object["hello"].as()); } SECTION("bool") { _object["hello"] = true; REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); REQUIRE(true == _object["hello"].as()); } SECTION("const char*") { _object["hello"] = "h3110"; REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); REQUIRE(std::string("h3110") == _object["hello"].as()); REQUIRE(std::string("h3110") == _object["hello"].as()); // <- short hand } SECTION("array") { DynamicJsonArray arr; _object["hello"] = arr; REQUIRE(&arr == &_object["hello"].as()); REQUIRE(&arr == &_object["hello"].as()); // <- short hand REQUIRE(&arr == &_object["hello"].as()); REQUIRE(&arr == &_object["hello"].as()); // <- short hand REQUIRE(true == _object["hello"].is()); REQUIRE(true == _object["hello"].is()); REQUIRE(true == _object["hello"].is()); REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); } SECTION("object") { DynamicJsonObject obj; _object["hello"] = obj; REQUIRE(&obj == &_object["hello"].as()); REQUIRE(&obj == &_object["hello"].as()); // <- short hand REQUIRE(&obj == &_object["hello"].as()); REQUIRE(&obj == &_object["hello"].as()); // <- short hand REQUIRE(true == _object["hello"].is()); REQUIRE(true == _object["hello"].is()); REQUIRE(true == _object["hello"].is()); REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); } SECTION("array subscript") { DynamicJsonArray arr; arr.add(42); _object["a"] = arr[0]; REQUIRE(42 == _object["a"]); } SECTION("object subscript") { DynamicJsonObject obj; obj.set("x", 42); _object["a"] = obj["x"]; REQUIRE(42 == _object["a"]); } SECTION("char key[]") { // issue #423 char key[] = "hello"; _object[key] = 42; REQUIRE(42 == _object[key]); } SECTION("should not duplicate const char*") { _object["hello"] = "world"; const size_t expectedSize = JSON_OBJECT_SIZE(1); REQUIRE(expectedSize == _object.memoryUsage()); } SECTION("should duplicate char* value") { _object["hello"] = const_cast("world"); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6; REQUIRE(expectedSize == _object.memoryUsage()); } SECTION("should duplicate char* key") { _object[const_cast("hello")] = "world"; const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6; REQUIRE(expectedSize == _object.memoryUsage()); } SECTION("should duplicate char* key&value") { _object[const_cast("hello")] = const_cast("world"); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12; REQUIRE(expectedSize <= _object.memoryUsage()); } SECTION("should duplicate std::string value") { _object["hello"] = std::string("world"); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6; REQUIRE(expectedSize == _object.memoryUsage()); } SECTION("should duplicate std::string key") { _object[std::string("hello")] = "world"; const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6; REQUIRE(expectedSize == _object.memoryUsage()); } SECTION("should duplicate std::string key&value") { _object[std::string("hello")] = std::string("world"); const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12; REQUIRE(expectedSize <= _object.memoryUsage()); } }