123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- // ArduinoJson - https://arduinojson.org
- // Copyright © 2014-2022, Benoit BLANCHON
- // MIT License
- #include <ArduinoJson.h>
- #include <stdint.h>
- #include <catch.hpp>
- namespace my {
- using ARDUINOJSON_NAMESPACE::isinf;
- } // namespace my
- enum MY_ENUM { ONE = 1, TWO = 2 };
- TEST_CASE("JsonVariant::as()") {
- static const char* null = 0;
- DynamicJsonDocument doc(4096);
- JsonVariant variant = doc.to<JsonVariant>();
- SECTION("not set") {
- REQUIRE(false == variant.as<bool>());
- REQUIRE(0 == variant.as<int>());
- REQUIRE(0.0f == variant.as<float>());
- REQUIRE(0 == variant.as<const char*>());
- REQUIRE("null" == variant.as<std::string>());
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(4.2)") {
- variant.set(4.2);
- REQUIRE(variant.as<bool>());
- REQUIRE(0 == variant.as<const char*>());
- REQUIRE(variant.as<std::string>() == "4.2");
- REQUIRE(variant.as<long>() == 4L);
- REQUIRE(variant.as<unsigned>() == 4U);
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(0.0)") {
- variant.set(0.0);
- REQUIRE(variant.as<bool>() == false);
- REQUIRE(variant.as<long>() == 0L);
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(false)") {
- variant.set(false);
- REQUIRE(false == variant.as<bool>());
- REQUIRE(variant.as<double>() == 0.0);
- REQUIRE(variant.as<long>() == 0L);
- REQUIRE(variant.as<std::string>() == "false");
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(true)") {
- variant.set(true);
- REQUIRE(variant.as<bool>());
- REQUIRE(variant.as<double>() == 1.0);
- REQUIRE(variant.as<long>() == 1L);
- REQUIRE(variant.as<std::string>() == "true");
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(42)") {
- variant.set(42);
- REQUIRE(variant.as<bool>() == true);
- REQUIRE(variant.as<double>() == 42.0);
- REQUIRE(variant.as<int>() == 42);
- REQUIRE(variant.as<unsigned int>() == 42U); // issue #1601
- REQUIRE(variant.as<std::string>() == "42");
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(42L)") {
- variant.set(42L);
- REQUIRE(variant.as<bool>() == true);
- REQUIRE(variant.as<double>() == 42.0);
- REQUIRE(variant.as<std::string>() == "42");
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(-42L)") {
- variant.set(-42L);
- REQUIRE(variant.as<double>() == -42.0);
- REQUIRE(variant.as<std::string>() == "-42");
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(42UL)") {
- variant.set(42UL);
- REQUIRE(variant.as<bool>() == true);
- REQUIRE(variant.as<double>() == 42.0);
- REQUIRE(variant.as<std::string>() == "42");
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(0L)") {
- variant.set(0L);
- REQUIRE(variant.as<bool>() == false);
- REQUIRE(variant.as<double>() == 0.0);
- REQUIRE(variant.as<std::string>() == "0");
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(0UL)") {
- variant.set(0UL);
- REQUIRE(variant.as<bool>() == false);
- REQUIRE(variant.as<double>() == 0.0);
- REQUIRE(variant.as<std::string>() == "0");
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(null)") {
- variant.set(null);
- REQUIRE(variant.as<bool>() == false);
- REQUIRE(variant.as<double>() == 0.0);
- REQUIRE(variant.as<long>() == 0L);
- REQUIRE(variant.as<std::string>() == "null");
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(\"42\")") {
- variant.set("42");
- REQUIRE(variant.as<long>() == 42L);
- REQUIRE(variant.as<JsonString>() == "42");
- REQUIRE(variant.as<JsonString>().isLinked() == true);
- }
- SECTION("set(\"hello\")") {
- variant.set("hello");
- REQUIRE(variant.as<bool>() == true);
- REQUIRE(variant.as<long>() == 0L);
- REQUIRE(variant.as<const char*>() == std::string("hello"));
- REQUIRE(variant.as<const char*>() == std::string("hello"));
- REQUIRE(variant.as<std::string>() == std::string("hello"));
- REQUIRE(variant.as<JsonString>() == "hello");
- }
- SECTION("set(std::string(\"4.2\"))") {
- variant.set(std::string("4.2"));
- REQUIRE(variant.as<bool>() == true);
- REQUIRE(variant.as<long>() == 4L);
- REQUIRE(variant.as<double>() == 4.2);
- REQUIRE(variant.as<const char*>() == std::string("4.2"));
- REQUIRE(variant.as<std::string>() == std::string("4.2"));
- REQUIRE(variant.as<JsonString>() == "4.2");
- REQUIRE(variant.as<JsonString>().isLinked() == false);
- }
- SECTION("set(\"true\")") {
- variant.set("true");
- REQUIRE(variant.as<bool>() == true);
- REQUIRE(variant.as<int>() == 0);
- REQUIRE(variant.as<JsonString>() == "true");
- }
- SECTION("set(-1e300)") {
- variant.set(-1e300);
- REQUIRE(variant.as<bool>() == true);
- REQUIRE(variant.as<double>() == -1e300);
- REQUIRE(variant.as<float>() < 0);
- REQUIRE(my::isinf(variant.as<float>()));
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(1e300)") {
- variant.set(1e300);
- REQUIRE(variant.as<bool>() == true);
- REQUIRE(variant.as<double>() == 1e300);
- REQUIRE(variant.as<float>() > 0);
- REQUIRE(my::isinf(variant.as<float>()));
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("set(1e-300)") {
- variant.set(1e-300);
- REQUIRE(variant.as<bool>() == true);
- REQUIRE(variant.as<double>() == 1e-300);
- REQUIRE(variant.as<float>() == 0);
- REQUIRE(variant.as<JsonString>().isNull());
- }
- SECTION("to<JsonObject>()") {
- JsonObject obj = variant.to<JsonObject>();
- obj["key"] = "value";
- SECTION("as<bool>()") {
- REQUIRE(variant.as<bool>() == true);
- }
- SECTION("as<std::string>()") {
- REQUIRE(variant.as<std::string>() == std::string("{\"key\":\"value\"}"));
- }
- SECTION("ObjectAsJsonObject") {
- JsonObject o = variant.as<JsonObject>();
- REQUIRE(o.size() == 1);
- REQUIRE(o["key"] == std::string("value"));
- }
- }
- SECTION("to<JsonArray>()") {
- JsonArray arr = variant.to<JsonArray>();
- arr.add(4);
- arr.add(2);
- SECTION("as<bool>()") {
- REQUIRE(variant.as<bool>() == true);
- }
- SECTION("as<std::string>()") {
- REQUIRE(variant.as<std::string>() == std::string("[4,2]"));
- }
- SECTION("as<JsonArray>()") {
- JsonArray a = variant.as<JsonArray>();
- 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<long long>() == -9223372036854775807 - 1);
- }
- SECTION("Biggerst int64 positive") {
- variant.set("9223372036854775807");
- REQUIRE(variant.as<long long>() == 9223372036854775807);
- }
- #endif
- SECTION("should work on JsonVariantConst") {
- variant.set("hello");
- JsonVariantConst cvar = variant;
- REQUIRE(cvar.as<bool>() == true);
- REQUIRE(cvar.as<long>() == 0L);
- REQUIRE(cvar.as<const char*>() == std::string("hello"));
- REQUIRE(cvar.as<std::string>() == std::string("hello"));
- }
- SECTION("as<enum>()") {
- variant.set(1);
- REQUIRE(variant.as<MY_ENUM>() == ONE);
- }
- }
|