Added JsonArrayConst, JsonObjectConst, and JsonVariantConst

This commit is contained in:
Benoit Blanchon
2018-10-12 12:00:27 +02:00
parent d1003ff6c9
commit b0560cbd99
47 changed files with 1909 additions and 1063 deletions

View File

@ -7,6 +7,7 @@ add_executable(JsonObjectTests
copy.cpp
createNestedArray.cpp
createNestedObject.cpp
equals.cpp
get.cpp
invalid.cpp
is.cpp

View File

@ -10,12 +10,15 @@ TEST_CASE("JsonObject::containsKey()") {
JsonObject obj = doc.to<JsonObject>();
obj.set("hello", 42);
SECTION("returns false if key not present") {
SECTION("returns true only if key is present") {
REQUIRE(false == obj.containsKey("world"));
REQUIRE(true == obj.containsKey("hello"));
}
SECTION("returns true if key present") {
REQUIRE(true == obj.containsKey("hello"));
SECTION("works with JsonObjectConst") {
JsonObjectConst cobj = obj;
REQUIRE(false == cobj.containsKey("world"));
REQUIRE(true == cobj.containsKey("hello"));
}
SECTION("returns false after remove()") {

View File

@ -56,4 +56,13 @@ TEST_CASE("JsonObject::copyFrom()") {
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
}
SECTION("should work with JsonObjectConst") {
obj1["hello"] = "world";
obj2.copyFrom(static_cast<JsonObjectConst>(obj1));
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
}
}

View File

@ -0,0 +1,35 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::operator==()") {
DynamicJsonDocument doc1;
JsonObject obj1 = doc1.to<JsonObject>();
JsonObjectConst obj1c = obj1;
DynamicJsonDocument doc2;
JsonObject obj2 = doc2.to<JsonObject>();
JsonObjectConst obj2c = obj2;
SECTION("should return false when objs differ") {
obj1["hello"] = "coucou";
obj2["world"] = 1;
REQUIRE_FALSE(obj1 == obj2);
REQUIRE_FALSE(obj1c == obj2c);
}
SECTION("should return false when objs differ") {
obj1["hello"] = "world";
obj1["anwser"] = 42;
// insert in different order
obj2["anwser"] = 42;
obj2["hello"] = "world";
REQUIRE(obj1 == obj2);
REQUIRE(obj1c == obj2c);
}
}

View File

@ -27,4 +27,11 @@ TEST_CASE("JsonObject::get()") {
REQUIRE(std::string("world") == obj.get<char*>(vla));
}
#endif
SECTION("works on JsonObjectConst") {
obj.set("hello", "world");
const char* value =
static_cast<JsonObjectConst>(obj).get<const char*>("hello");
REQUIRE_THAT(value, Equals("world"));
}
}

View File

@ -26,23 +26,35 @@ TEST_CASE("JsonObject::begin()/end()") {
REQUIRE(obj.end() == it);
}
// SECTION("ConstIterator") {
// const JsonObject const_object = obj;
// JsonObject::iterator it = const_object.begin();
// REQUIRE(const_object.end() != it);
// REQUIRE_THAT(it->key(), Equals("ab"));
// REQUIRE(12 == it->value());
// ++it;
// REQUIRE(const_object.end() != it);
// REQUIRE_THAT(it->key(), Equals("cd"));
// REQUIRE(34 == it->value());
// ++it;
// REQUIRE(const_object.end() == it);
// }
SECTION("Dereferencing end() is safe") {
REQUIRE(obj.end()->key().isNull());
REQUIRE(obj.end()->value().isNull());
}
}
TEST_CASE("JsonObjectConst::begin()/end()") {
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
JsonObject obj = doc.to<JsonObject>();
obj["ab"] = 12;
obj["cd"] = 34;
JsonObjectConst cobj = obj;
SECTION("NonConstIterator") {
JsonObjectConst::iterator it = cobj.begin();
REQUIRE(cobj.end() != it);
REQUIRE(it->key() == "ab");
REQUIRE(12 == it->value());
++it;
REQUIRE(cobj.end() != it);
REQUIRE(it->key() == "cd");
REQUIRE(34 == it->value());
++it;
REQUIRE(cobj.end() == it);
}
SECTION("Dereferencing end() is safe") {
REQUIRE(cobj.end()->key().isNull());
REQUIRE(cobj.end()->value().isNull());
}
}

View File

@ -58,13 +58,7 @@ TEST_CASE("JsonObject::operator[]") {
obj["hello"] = arr;
REQUIRE(arr == obj["hello"].as<JsonArray>());
REQUIRE(arr == obj["hello"].as<JsonArray>()); // <- short hand
// REQUIRE(arr == obj["hello"].as<const JsonArray>());
// REQUIRE(arr == obj["hello"].as<const JsonArray>()); // <- short hand
REQUIRE(true == obj["hello"].is<JsonArray>());
REQUIRE(true == obj["hello"].is<JsonArray>());
REQUIRE(true == obj["hello"].is<const JsonArray>());
REQUIRE(true == obj["hello"].is<const JsonArray>());
REQUIRE(false == obj["hello"].is<JsonObject>());
}
@ -75,9 +69,7 @@ TEST_CASE("JsonObject::operator[]") {
obj["hello"] = obj2;
REQUIRE(obj2 == obj["hello"].as<JsonObject>());
REQUIRE(obj2 == obj["hello"].as<const JsonObject>());
REQUIRE(true == obj["hello"].is<JsonObject>());
REQUIRE(true == obj["hello"].is<const JsonObject>());
REQUIRE(false == obj["hello"].is<JsonArray>());
}
@ -219,4 +211,12 @@ TEST_CASE("JsonObject::operator[]") {
REQUIRE(std::string("world") == obj[vla]);
}
#endif
SECTION("chain") {
obj.createNestedObject("hello")["world"] = 123;
REQUIRE(123 == obj["hello"]["world"].as<int>());
REQUIRE(true == obj["hello"]["world"].is<int>());
REQUIRE(false == obj["hello"]["world"].is<bool>());
}
}