// ArduinoJson - arduinojson.org // Copyright Benoit Blanchon 2014-2019 // MIT License #include #include template void checkIsArray(TVariant var) { REQUIRE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); } void testArray(JsonArray value) { DynamicJsonDocument doc(4096); JsonVariant var = doc.to(); var.set(value); checkIsArray(var); JsonVariantConst cvar = var; checkIsArray(cvar); } template void checkIsBool(TVariant var) { REQUIRE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); } void testBool(bool value) { DynamicJsonDocument doc(4096); JsonVariant var = doc.to(); var.set(value); checkIsBool(var); checkIsBool(JsonVariantConst(var)); } template void checkIsFloat(TVariant var) { REQUIRE(var.template is()); REQUIRE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); } void testFloat(double value) { DynamicJsonDocument doc(4096); JsonVariant var = doc.to(); var.set(value); checkIsFloat(var); checkIsFloat(JsonVariantConst(var)); } template void checkIsInteger(TVariant var) { REQUIRE(var.template is()); REQUIRE(var.template is()); REQUIRE(var.template is()); REQUIRE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); } template void testInteger(T value) { DynamicJsonDocument doc(4096); JsonVariant var = doc.to(); var.set(value); checkIsInteger(var); checkIsInteger(JsonVariantConst(var)); } template void checkIsString(TVariant var) { REQUIRE(var.template is()); REQUIRE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); REQUIRE_FALSE(var.template is()); } void testString(const char *value) { DynamicJsonDocument doc(4096); JsonVariant var = doc.to(); var.set(value); checkIsString(var); checkIsString(JsonVariantConst(var)); } TEST_CASE("JsonVariant::is()") { SECTION("JsonArray") { DynamicJsonDocument doc(4096); JsonArray array = doc.to(); testArray(array); } SECTION("bool") { testBool(true); testBool(false); } SECTION("double") { testFloat(4.2); } SECTION("int") { testInteger(42); } SECTION("long") { testInteger(42L); } SECTION("string") { testString("42"); } SECTION("null") { DynamicJsonDocument doc(4096); deserializeJson(doc, "[null]"); JsonVariant v = doc[0]; REQUIRE(v.is() == false); REQUIRE(v.is() == false); REQUIRE(v.is() == false); REQUIRE(v.is() == false); REQUIRE(v.is() == false); } }