// ArduinoJson - https://arduinojson.org // Copyright © 2014-2024, Benoit BLANCHON // MIT License #include #include #include TEST_CASE("JsonVariant::add(T)") { JsonDocument doc; JsonVariant var = doc.to(); SECTION("add integer to new variant") { var.add(42); REQUIRE(var.as() == "[42]"); } SECTION("add const char* to new variant") { var.add("hello"); REQUIRE(var.as() == "[\"hello\"]"); } SECTION("add std::string to new variant") { var.add(std::string("hello")); REQUIRE(var.as() == "[\"hello\"]"); } SECTION("add integer to integer") { var.set(123); var.add(456); // no-op REQUIRE(var.as() == "123"); } SECTION("add integer to object") { var["val"] = 123; var.add(456); // no-op REQUIRE(var.as() == "{\"val\":123}"); } } TEST_CASE("JsonVariant::add()") { JsonDocument doc; JsonVariant var = doc.to(); SECTION("JsonArray") { JsonArray array = var.add(); array.add(1); array.add(2); REQUIRE(doc.as() == "[[1,2]]"); } SECTION("JsonObject") { JsonObject object = var.add(); object["hello"] = "world"; REQUIRE(doc.as() == "[{\"hello\":\"world\"}]"); } SECTION("JsonVariant") { JsonVariant variant = var.add(); variant.set(42); REQUIRE(doc.as() == "[42]"); } }