// ArduinoJson - https://arduinojson.org // Copyright © 2014-2024, Benoit BLANCHON // MIT License #include #include #include namespace my { using ArduinoJson::detail::isinf; } // namespace my enum MY_ENUM { ONE = 1, TWO = 2 }; TEST_CASE("JsonVariant::as()") { static const char* null = 0; JsonDocument doc; JsonVariant variant = doc.to(); SECTION("not set") { REQUIRE(false == variant.as()); REQUIRE(0 == variant.as()); REQUIRE(0.0f == variant.as()); REQUIRE(0 == variant.as()); REQUIRE("null" == variant.as()); REQUIRE(variant.as().isNull()); } SECTION("set(4.2)") { variant.set(4.2); REQUIRE(variant.as()); REQUIRE(0 == variant.as()); REQUIRE(variant.as() == "4.2"); REQUIRE(variant.as() == 4L); REQUIRE(variant.as() == 4U); REQUIRE(variant.as().isNull()); } SECTION("set(0.0)") { variant.set(0.0); REQUIRE(variant.as() == false); REQUIRE(variant.as() == 0L); REQUIRE(variant.as().isNull()); } SECTION("set(false)") { variant.set(false); REQUIRE(false == variant.as()); REQUIRE(variant.as() == 0.0); REQUIRE(variant.as() == 0L); REQUIRE(variant.as() == "false"); REQUIRE(variant.as().isNull()); } SECTION("set(true)") { variant.set(true); REQUIRE(variant.as()); REQUIRE(variant.as() == 1.0); REQUIRE(variant.as() == 1L); REQUIRE(variant.as() == "true"); REQUIRE(variant.as().isNull()); } SECTION("set(42)") { variant.set(42); REQUIRE(variant.as() == true); REQUIRE(variant.as() == 42.0); REQUIRE(variant.as() == 42); REQUIRE(variant.as() == 42U); // issue #1601 REQUIRE(variant.as() == "42"); REQUIRE(variant.as().isNull()); } SECTION("set(42L)") { variant.set(42L); REQUIRE(variant.as() == true); REQUIRE(variant.as() == 42.0); REQUIRE(variant.as() == "42"); REQUIRE(variant.as().isNull()); } SECTION("set(-42L)") { variant.set(-42L); REQUIRE(variant.as() == -42.0); REQUIRE(variant.as() == "-42"); REQUIRE(variant.as().isNull()); } SECTION("set(42UL)") { variant.set(42UL); REQUIRE(variant.as() == true); REQUIRE(variant.as() == 42.0); REQUIRE(variant.as() == "42"); REQUIRE(variant.as().isNull()); } SECTION("set(0L)") { variant.set(0L); REQUIRE(variant.as() == false); REQUIRE(variant.as() == 0.0); REQUIRE(variant.as() == "0"); REQUIRE(variant.as().isNull()); } SECTION("set(0UL)") { variant.set(0UL); REQUIRE(variant.as() == false); REQUIRE(variant.as() == 0.0); REQUIRE(variant.as() == "0"); REQUIRE(variant.as().isNull()); } SECTION("set(null)") { variant.set(null); REQUIRE(variant.as() == false); REQUIRE(variant.as() == 0.0); REQUIRE(variant.as() == 0L); REQUIRE(variant.as() == "null"); REQUIRE(variant.as().isNull()); } SECTION("set(\"42\")") { variant.set("42"); REQUIRE(variant.as() == 42L); REQUIRE(variant.as() == "42"); REQUIRE(variant.as().isLinked() == true); } SECTION("set(\"hello\")") { variant.set("hello"); REQUIRE(variant.as() == true); REQUIRE(variant.as() == 0L); REQUIRE(variant.as() == std::string("hello")); REQUIRE(variant.as() == std::string("hello")); REQUIRE(variant.as() == std::string("hello")); REQUIRE(variant.as() == "hello"); } SECTION("set(std::string(\"4.2\"))") { variant.set(std::string("4.2")); REQUIRE(variant.as() == true); REQUIRE(variant.as() == 4L); REQUIRE(variant.as() == 4.2); REQUIRE(variant.as() == std::string("4.2")); REQUIRE(variant.as() == std::string("4.2")); REQUIRE(variant.as() == "4.2"); REQUIRE(variant.as().isLinked() == false); } SECTION("set(\"true\")") { variant.set("true"); REQUIRE(variant.as() == true); REQUIRE(variant.as() == 0); REQUIRE(variant.as() == "true"); } SECTION("set(-1e300)") { variant.set(-1e300); REQUIRE(variant.as() == true); REQUIRE(variant.as() == -1e300); REQUIRE(variant.as() < 0); REQUIRE(my::isinf(variant.as())); REQUIRE(variant.as().isNull()); } SECTION("set(1e300)") { variant.set(1e300); REQUIRE(variant.as() == true); REQUIRE(variant.as() == 1e300); REQUIRE(variant.as() > 0); REQUIRE(my::isinf(variant.as())); REQUIRE(variant.as().isNull()); } SECTION("set(1e-300)") { variant.set(1e-300); REQUIRE(variant.as() == true); REQUIRE(variant.as() == 1e-300); REQUIRE(variant.as() == 0); REQUIRE(variant.as().isNull()); } SECTION("to()") { JsonObject obj = variant.to(); obj["key"] = "value"; SECTION("as()") { REQUIRE(variant.as() == true); } SECTION("as()") { REQUIRE(variant.as() == std::string("{\"key\":\"value\"}")); } SECTION("ObjectAsJsonObject") { JsonObject o = variant.as(); REQUIRE(o.size() == 1); REQUIRE(o["key"] == std::string("value")); } } SECTION("to()") { JsonArray arr = variant.to(); arr.add(4); arr.add(2); SECTION("as()") { REQUIRE(variant.as() == true); } SECTION("as()") { REQUIRE(variant.as() == std::string("[4,2]")); } SECTION("as()") { JsonArray a = variant.as(); REQUIRE(a.size() == 2); REQUIRE(a[0] == 4); REQUIRE(a[1] == 2); } } #if ARDUINOJSON_USE_LONG_LONG SECTION("Smallest int64 negative") { variant.set("-9223372036854775808"); REQUIRE(variant.as() == -9223372036854775807 - 1); } SECTION("Biggerst int64 positive") { variant.set("9223372036854775807"); REQUIRE(variant.as() == 9223372036854775807); } #endif SECTION("as()") { variant.set(1); REQUIRE(variant.as() == ONE); } }