// ArduinoJson - arduinojson.org // Copyright Benoit Blanchon 2014-2018 // MIT License #include #include #include #include template void checkValue(T expected) { DynamicJsonDocument doc; JsonVariant variant = doc.to(); variant.set(expected); REQUIRE(expected == variant.as()); } template void checkReference(T &expected) { JsonVariant variant = expected; REQUIRE(expected == variant.as()); } template void checkNumericType() { DynamicJsonDocument docMin, docMax; JsonVariant variantMin = docMin.to(); JsonVariant variantMax = docMax.to(); T min = std::numeric_limits::min(); T max = std::numeric_limits::max(); variantMin.set(min); variantMax.set(max); REQUIRE(min == variantMin.as()); REQUIRE(max == variantMax.as()); } TEST_CASE("JsonVariant set()/get()") { #if ARDUINOJSON_USE_LONG_LONG SECTION("SizeOfJsonInteger") { REQUIRE(8 == sizeof(JsonInteger)); } #endif SECTION("Null") { checkValue(NULL); } SECTION("const char*") { checkValue("hello"); } SECTION("std::string") { checkValue("hello"); } SECTION("False") { checkValue(false); } SECTION("True") { checkValue(true); } SECTION("Double") { checkNumericType(); } SECTION("Float") { checkNumericType(); } SECTION("Char") { checkNumericType(); } SECTION("SChar") { checkNumericType(); } SECTION("SInt") { checkNumericType(); } SECTION("SLong") { checkNumericType(); } SECTION("SShort") { checkNumericType(); } SECTION("UChar") { checkNumericType(); } SECTION("UInt") { checkNumericType(); } SECTION("ULong") { checkNumericType(); } SECTION("UShort") { checkNumericType(); } #if ARDUINOJSON_USE_LONG_LONG SECTION("LongLong") { checkNumericType(); } SECTION("ULongLong") { checkNumericType(); } #endif SECTION("Int8") { checkNumericType(); } SECTION("Uint8") { checkNumericType(); } SECTION("Int16") { checkNumericType(); } SECTION("Uint16") { checkNumericType(); } SECTION("Int32") { checkNumericType(); } SECTION("Uint32") { checkNumericType(); } #if ARDUINOJSON_USE_LONG_LONG SECTION("Int64") { checkNumericType(); } SECTION("Uint64") { checkNumericType(); } #endif SECTION("CanStoreObject") { DynamicJsonDocument doc; JsonObject object = doc.to(); checkValue(object); } } TEST_CASE("JsonVariant and strings") { DynamicJsonDocument doc; JsonVariant variant = doc.to(); SECTION("stores const char* by reference") { char str[16]; strcpy(str, "hello"); variant.set(static_cast(str)); strcpy(str, "world"); REQUIRE(variant == "world"); } SECTION("stores char* by copy") { char str[16]; strcpy(str, "hello"); variant.set(str); strcpy(str, "world"); REQUIRE(variant == "hello"); } SECTION("stores unsigned char* by copy") { char str[16]; strcpy(str, "hello"); variant.set(reinterpret_cast(str)); strcpy(str, "world"); REQUIRE(variant == "hello"); } SECTION("stores signed char* by copy") { char str[16]; strcpy(str, "hello"); variant.set(reinterpret_cast(str)); strcpy(str, "world"); REQUIRE(variant == "hello"); } #ifdef HAS_VARIABLE_LENGTH_ARRAY SECTION("stores VLA by copy") { int n = 16; char str[n]; strcpy(str, "hello"); variant.set(str); strcpy(str, "world"); REQUIRE(variant == "hello"); } #endif SECTION("stores std::string by copy") { std::string str; str = "hello"; variant.set(str); str.replace(0, 5, "world"); REQUIRE(variant == "hello"); } } TEST_CASE("JsonVariant with not enough memory") { StaticJsonDocument<1> doc; JsonVariant v = doc.to(); SECTION("std::string") { v.set(std::string("hello")); REQUIRE(v.isNull()); } SECTION("Serialized") { v.set(serialized(std::string("hello"))); REQUIRE(v.isNull()); } }