From e0cd5b6405222b2008fb831d5261f863fc800440 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 10 Sep 2021 08:40:02 +0200 Subject: [PATCH] Increased test coverage --- extras/tests/Cpp11/nullptr.cpp | 8 ++ extras/tests/JsonArray/remove.cpp | 94 ++++++++++++---------- extras/tests/JsonObject/remove.cpp | 11 ++- extras/tests/JsonVariant/add.cpp | 22 ++++- extras/tests/JsonVariant/compare.cpp | 36 +++++++++ extras/tests/JsonVariant/copy.cpp | 9 +++ extras/tests/JsonVariant/subscript.cpp | 9 +++ extras/tests/Misc/StringWriter.cpp | 6 ++ src/ArduinoJson/Variant/VariantContent.hpp | 1 - 9 files changed, 147 insertions(+), 49 deletions(-) diff --git a/extras/tests/Cpp11/nullptr.cpp b/extras/tests/Cpp11/nullptr.cpp index cb3ba0ff..813b9cc2 100644 --- a/extras/tests/Cpp11/nullptr.cpp +++ b/extras/tests/Cpp11/nullptr.cpp @@ -29,6 +29,14 @@ TEST_CASE("nullptr") { REQUIRE(variant.isNull()); } + SECTION("JsonVariant.set(nullptr) with unbound reference") { + JsonVariant unboundReference; + + unboundReference.set(nullptr); + + REQUIRE(variant.isNull()); + } + SECTION("JsonVariant.is()") { variant.set(42); REQUIRE(variant.is() == false); diff --git a/extras/tests/JsonArray/remove.cpp b/extras/tests/JsonArray/remove.cpp index ee03f69f..2cf1dc04 100644 --- a/extras/tests/JsonArray/remove.cpp +++ b/extras/tests/JsonArray/remove.cpp @@ -7,73 +7,83 @@ TEST_CASE("JsonArray::remove()") { DynamicJsonDocument doc(4096); - JsonArray _array = doc.to(); - _array.add(1); - _array.add(2); - _array.add(3); + JsonArray array = doc.to(); + array.add(1); + array.add(2); + array.add(3); - SECTION("RemoveFirstByIndex") { - _array.remove(0); + SECTION("remove first by index") { + array.remove(0); - REQUIRE(2 == _array.size()); - REQUIRE(_array[0] == 2); - REQUIRE(_array[1] == 3); + REQUIRE(2 == array.size()); + REQUIRE(array[0] == 2); + REQUIRE(array[1] == 3); } - SECTION("RemoveMiddleByIndex") { - _array.remove(1); + SECTION("remove middle by index") { + array.remove(1); - REQUIRE(2 == _array.size()); - REQUIRE(_array[0] == 1); - REQUIRE(_array[1] == 3); + REQUIRE(2 == array.size()); + REQUIRE(array[0] == 1); + REQUIRE(array[1] == 3); } - SECTION("RemoveLastByIndex") { - _array.remove(2); + SECTION("remove last by index") { + array.remove(2); - REQUIRE(2 == _array.size()); - REQUIRE(_array[0] == 1); - REQUIRE(_array[1] == 2); + REQUIRE(2 == array.size()); + REQUIRE(array[0] == 1); + REQUIRE(array[1] == 2); } - SECTION("RemoveFirstByIterator") { - JsonArray::iterator it = _array.begin(); - _array.remove(it); + SECTION("remove first by iterator") { + JsonArray::iterator it = array.begin(); + array.remove(it); - REQUIRE(2 == _array.size()); - REQUIRE(_array[0] == 2); - REQUIRE(_array[1] == 3); + REQUIRE(2 == array.size()); + REQUIRE(array[0] == 2); + REQUIRE(array[1] == 3); } - SECTION("RemoveMiddleByIterator") { - JsonArray::iterator it = _array.begin(); + SECTION("remove middle by iterator") { + JsonArray::iterator it = array.begin(); ++it; - _array.remove(it); + array.remove(it); - REQUIRE(2 == _array.size()); - REQUIRE(_array[0] == 1); - REQUIRE(_array[1] == 3); + REQUIRE(2 == array.size()); + REQUIRE(array[0] == 1); + REQUIRE(array[1] == 3); } - SECTION("RemoveLastByIterator") { - JsonArray::iterator it = _array.begin(); + SECTION("remove last bty iterator") { + JsonArray::iterator it = array.begin(); ++it; ++it; - _array.remove(it); + array.remove(it); - REQUIRE(2 == _array.size()); - REQUIRE(_array[0] == 1); - REQUIRE(_array[1] == 2); + REQUIRE(2 == array.size()); + REQUIRE(array[0] == 1); + REQUIRE(array[1] == 2); } SECTION("In a loop") { - for (JsonArray::iterator it = _array.begin(); it != _array.end(); ++it) { + for (JsonArray::iterator it = array.begin(); it != array.end(); ++it) { if (*it == 2) - _array.remove(it); + array.remove(it); } - REQUIRE(2 == _array.size()); - REQUIRE(_array[0] == 1); - REQUIRE(_array[1] == 3); + REQUIRE(2 == array.size()); + REQUIRE(array[0] == 1); + REQUIRE(array[1] == 3); + } + + SECTION("remove by index on unbound reference") { + JsonArray unboundArray; + unboundArray.remove(20); + } + + SECTION("remove by iterator on unbound reference") { + JsonArray unboundArray; + unboundArray.remove(unboundArray.begin()); } } diff --git a/extras/tests/JsonObject/remove.cpp b/extras/tests/JsonObject/remove.cpp index f4227391..14d1fd5d 100644 --- a/extras/tests/JsonObject/remove.cpp +++ b/extras/tests/JsonObject/remove.cpp @@ -70,8 +70,13 @@ TEST_CASE("JsonObject::remove()") { } #endif - SECTION("should work on null object") { - JsonObject null; - null.remove("key"); + SECTION("remove by key on unbound reference") { + JsonObject unboundObject; + unboundObject.remove("key"); + } + + SECTION("remove by iterator on unbound reference") { + JsonObject unboundObject; + unboundObject.remove(unboundObject.begin()); } } diff --git a/extras/tests/JsonVariant/add.cpp b/extras/tests/JsonVariant/add.cpp index b0b3baab..cfdfe948 100644 --- a/extras/tests/JsonVariant/add.cpp +++ b/extras/tests/JsonVariant/add.cpp @@ -10,21 +10,37 @@ TEST_CASE("JsonVariant::add()") { DynamicJsonDocument doc(4096); JsonVariant var = doc.to(); - SECTION("integer") { + SECTION("add integer to new variant") { var.add(42); REQUIRE(var.as() == "[42]"); } - SECTION("const char*") { + SECTION("add const char* to new variant") { var.add("hello"); REQUIRE(var.as() == "[\"hello\"]"); } - SECTION("std::string") { + 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}"); + } } diff --git a/extras/tests/JsonVariant/compare.cpp b/extras/tests/JsonVariant/compare.cpp index 4cd8b50e..68e2e2f9 100644 --- a/extras/tests/JsonVariant/compare.cpp +++ b/extras/tests/JsonVariant/compare.cpp @@ -158,6 +158,18 @@ TEST_CASE("Compare JsonVariant with JsonVariant") { CHECK_FALSE(a > b); } + SECTION("42 vs 42U") { + a.set(42); + b.set(42U); + + CHECK(a == b); + CHECK(a <= b); + CHECK(a >= b); + CHECK_FALSE(a != b); + CHECK_FALSE(a < b); + CHECK_FALSE(a > b); + } + SECTION("42 vs 42.0") { a.set(42); b.set(42.0); @@ -230,6 +242,30 @@ TEST_CASE("Compare JsonVariant with JsonVariant") { CHECK_FALSE(a == b); } + SECTION("42U vs 42U") { + a.set(42U); + b.set(42U); + + CHECK(a == b); + CHECK(a <= b); + CHECK(a >= b); + CHECK_FALSE(a != b); + CHECK_FALSE(a < b); + CHECK_FALSE(a > b); + } + + SECTION("42U vs 42") { + a.set(42U); + b.set(42); + + CHECK(a == b); + CHECK(a <= b); + CHECK(a >= b); + CHECK_FALSE(a != b); + CHECK_FALSE(a < b); + CHECK_FALSE(a > b); + } + SECTION("[1] vs [1]") { a.add(1); b.add(1); diff --git a/extras/tests/JsonVariant/copy.cpp b/extras/tests/JsonVariant/copy.cpp index ac97f84a..75c1aeee 100644 --- a/extras/tests/JsonVariant/copy.cpp +++ b/extras/tests/JsonVariant/copy.cpp @@ -83,4 +83,13 @@ TEST_CASE("JsonVariant::set(JsonVariant)") { REQUIRE(doc1.memoryUsage() == JSON_STRING_SIZE(7)); REQUIRE(doc2.memoryUsage() == JSON_STRING_SIZE(7)); } + + SECTION("destination is unbound") { + JsonVariant unboundVariant; + + unboundVariant.set(var1); + + REQUIRE(unboundVariant.isUndefined()); + REQUIRE(unboundVariant.isNull()); + } } diff --git a/extras/tests/JsonVariant/subscript.cpp b/extras/tests/JsonVariant/subscript.cpp index 283d04bf..91b6b78c 100644 --- a/extras/tests/JsonVariant/subscript.cpp +++ b/extras/tests/JsonVariant/subscript.cpp @@ -58,6 +58,15 @@ TEST_CASE("JsonVariant::operator[]") { REQUIRE(1 == var[0].size()); REQUIRE(std::string("world") == var[0]["hello"]); } + + SECTION("variant[0] when variant contains an integer") { + var.set(123); + + var[0] = 345; // no-op + + REQUIRE(var.is()); + REQUIRE(var.as() == 123); + } } SECTION("The JsonVariant is a JsonObject") { diff --git a/extras/tests/Misc/StringWriter.cpp b/extras/tests/Misc/StringWriter.cpp index c2454ef6..0df5a5b7 100644 --- a/extras/tests/Misc/StringWriter.cpp +++ b/extras/tests/Misc/StringWriter.cpp @@ -15,6 +15,11 @@ static size_t print(StringWriter& writer, const char* s) { return writer.write(reinterpret_cast(s), strlen(s)); } +template +static size_t print(StringWriter& writer, char c) { + return writer.write(static_cast(c)); +} + template void common_tests(StringWriter& writer, const String& output) { SECTION("InitialState") { @@ -47,6 +52,7 @@ TEST_CASE("StaticStringWriter") { SECTION("OverCapacity") { REQUIRE(20 == print(writer, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")); REQUIRE(0 == print(writer, "ABC")); + REQUIRE(0 == print(writer, 'D')); REQUIRE("ABCDEFGHIJKLMNOPQRST" == std::string(output, 20)); } } diff --git a/src/ArduinoJson/Variant/VariantContent.hpp b/src/ArduinoJson/Variant/VariantContent.hpp index 47bf09cc..29526113 100644 --- a/src/ArduinoJson/Variant/VariantContent.hpp +++ b/src/ArduinoJson/Variant/VariantContent.hpp @@ -12,7 +12,6 @@ namespace ARDUINOJSON_NAMESPACE { -// enum { VALUE_MASK = 0x7F,