Initial commit of Arduino libraries
This commit is contained in:
25
ArduinoJson/extras/tests/JsonObject/CMakeLists.txt
Normal file
25
ArduinoJson/extras/tests/JsonObject/CMakeLists.txt
Normal file
@@ -0,0 +1,25 @@
|
||||
# ArduinoJson - https://arduinojson.org
|
||||
# Copyright © 2014-2025, Benoit BLANCHON
|
||||
# MIT License
|
||||
|
||||
add_executable(JsonObjectTests
|
||||
clear.cpp
|
||||
compare.cpp
|
||||
equals.cpp
|
||||
isNull.cpp
|
||||
iterator.cpp
|
||||
nesting.cpp
|
||||
remove.cpp
|
||||
set.cpp
|
||||
size.cpp
|
||||
std_string.cpp
|
||||
subscript.cpp
|
||||
unbound.cpp
|
||||
)
|
||||
|
||||
add_test(JsonObject JsonObjectTests)
|
||||
|
||||
set_tests_properties(JsonObject
|
||||
PROPERTIES
|
||||
LABELS "Catch"
|
||||
)
|
||||
25
ArduinoJson/extras/tests/JsonObject/clear.cpp
Normal file
25
ArduinoJson/extras/tests/JsonObject/clear.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonObject::clear()") {
|
||||
SECTION("No-op on null JsonObject") {
|
||||
JsonObject obj;
|
||||
obj.clear();
|
||||
REQUIRE(obj.isNull() == true);
|
||||
REQUIRE(obj.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Removes all elements") {
|
||||
JsonDocument doc;
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"] = 1;
|
||||
obj["world"] = 2;
|
||||
obj.clear();
|
||||
REQUIRE(obj.size() == 0);
|
||||
REQUIRE(obj.isNull() == false);
|
||||
}
|
||||
}
|
||||
512
ArduinoJson/extras/tests/JsonObject/compare.cpp
Normal file
512
ArduinoJson/extras/tests/JsonObject/compare.cpp
Normal file
@@ -0,0 +1,512 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("Compare JsonObject with JsonObject") {
|
||||
JsonDocument doc;
|
||||
|
||||
SECTION("Compare with unbound") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello";
|
||||
JsonObject unbound;
|
||||
|
||||
CHECK(object != unbound);
|
||||
CHECK_FALSE(object == unbound);
|
||||
CHECK_FALSE(object <= unbound);
|
||||
CHECK_FALSE(object >= unbound);
|
||||
CHECK_FALSE(object > unbound);
|
||||
CHECK_FALSE(object < unbound);
|
||||
|
||||
CHECK(unbound != object);
|
||||
CHECK_FALSE(unbound == object);
|
||||
CHECK_FALSE(unbound <= object);
|
||||
CHECK_FALSE(unbound >= object);
|
||||
CHECK_FALSE(unbound > object);
|
||||
CHECK_FALSE(unbound < object);
|
||||
}
|
||||
|
||||
SECTION("Compare with self") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello";
|
||||
|
||||
CHECK(object == object);
|
||||
CHECK(object <= object);
|
||||
CHECK(object >= object);
|
||||
CHECK_FALSE(object != object);
|
||||
CHECK_FALSE(object > object);
|
||||
CHECK_FALSE(object < object);
|
||||
}
|
||||
|
||||
SECTION("Compare with identical object") {
|
||||
JsonObject object1 = doc.add<JsonObject>();
|
||||
object1["a"] = 1;
|
||||
object1["b"] = "hello";
|
||||
object1["c"][0] = false;
|
||||
|
||||
JsonObject object2 = doc.add<JsonObject>();
|
||||
object2["a"] = 1;
|
||||
object2["b"] = "hello";
|
||||
object2["c"][0] = false;
|
||||
|
||||
CHECK(object1 == object2);
|
||||
CHECK(object1 <= object2);
|
||||
CHECK(object1 >= object2);
|
||||
CHECK_FALSE(object1 != object2);
|
||||
CHECK_FALSE(object1 > object2);
|
||||
CHECK_FALSE(object1 < object2);
|
||||
}
|
||||
|
||||
SECTION("Compare with different object") {
|
||||
JsonObject object1 = doc.add<JsonObject>();
|
||||
object1["a"] = 1;
|
||||
object1["b"] = "hello1";
|
||||
object1["c"][0] = false;
|
||||
|
||||
JsonObject object2 = doc.add<JsonObject>();
|
||||
object2["a"] = 1;
|
||||
object2["b"] = "hello2";
|
||||
object2["c"][0] = false;
|
||||
|
||||
CHECK(object1 != object2);
|
||||
CHECK_FALSE(object1 == object2);
|
||||
CHECK_FALSE(object1 > object2);
|
||||
CHECK_FALSE(object1 < object2);
|
||||
CHECK_FALSE(object1 <= object2);
|
||||
CHECK_FALSE(object1 >= object2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonObject with JsonVariant") {
|
||||
JsonDocument doc;
|
||||
|
||||
SECTION("Compare with self") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello";
|
||||
|
||||
JsonVariant variant = object;
|
||||
|
||||
CHECK(object == variant);
|
||||
CHECK(object <= variant);
|
||||
CHECK(object >= variant);
|
||||
CHECK_FALSE(object != variant);
|
||||
CHECK_FALSE(object > variant);
|
||||
CHECK_FALSE(object < variant);
|
||||
|
||||
CHECK(variant == object);
|
||||
CHECK(variant <= object);
|
||||
CHECK(variant >= object);
|
||||
CHECK_FALSE(variant != object);
|
||||
CHECK_FALSE(variant > object);
|
||||
CHECK_FALSE(variant < object);
|
||||
}
|
||||
|
||||
SECTION("Compare with identical object") {
|
||||
JsonObject object = doc.add<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello";
|
||||
object["c"][0] = false;
|
||||
|
||||
JsonVariant variant = doc.add<JsonObject>();
|
||||
variant["a"] = 1;
|
||||
variant["b"] = "hello";
|
||||
variant["c"][0] = false;
|
||||
|
||||
CHECK(object == variant);
|
||||
CHECK(object <= variant);
|
||||
CHECK(object >= variant);
|
||||
CHECK_FALSE(object != variant);
|
||||
CHECK_FALSE(object > variant);
|
||||
CHECK_FALSE(object < variant);
|
||||
|
||||
CHECK(variant == object);
|
||||
CHECK(variant <= object);
|
||||
CHECK(variant >= object);
|
||||
CHECK_FALSE(variant != object);
|
||||
CHECK_FALSE(variant > object);
|
||||
CHECK_FALSE(variant < object);
|
||||
}
|
||||
|
||||
SECTION("Compare with different object") {
|
||||
JsonObject object = doc.add<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello1";
|
||||
object["c"][0] = false;
|
||||
|
||||
JsonVariant variant = doc.add<JsonObject>();
|
||||
variant["a"] = 1;
|
||||
variant["b"] = "hello2";
|
||||
variant["c"][0] = false;
|
||||
|
||||
CHECK(object != variant);
|
||||
CHECK_FALSE(object == variant);
|
||||
CHECK_FALSE(object > variant);
|
||||
CHECK_FALSE(object < variant);
|
||||
CHECK_FALSE(object <= variant);
|
||||
CHECK_FALSE(object >= variant);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonObject with JsonVariantConst") {
|
||||
JsonDocument doc;
|
||||
|
||||
SECTION("Compare with unbound") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello";
|
||||
JsonVariantConst unbound;
|
||||
|
||||
CHECK(object != unbound);
|
||||
CHECK_FALSE(object == unbound);
|
||||
CHECK_FALSE(object <= unbound);
|
||||
CHECK_FALSE(object >= unbound);
|
||||
CHECK_FALSE(object > unbound);
|
||||
CHECK_FALSE(object < unbound);
|
||||
|
||||
CHECK(unbound != object);
|
||||
CHECK_FALSE(unbound == object);
|
||||
CHECK_FALSE(unbound <= object);
|
||||
CHECK_FALSE(unbound >= object);
|
||||
CHECK_FALSE(unbound > object);
|
||||
CHECK_FALSE(unbound < object);
|
||||
}
|
||||
|
||||
SECTION("Compare with self") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello";
|
||||
|
||||
JsonVariantConst variant = object;
|
||||
|
||||
CHECK(object == variant);
|
||||
CHECK(object <= variant);
|
||||
CHECK(object >= variant);
|
||||
CHECK_FALSE(object != variant);
|
||||
CHECK_FALSE(object > variant);
|
||||
CHECK_FALSE(object < variant);
|
||||
|
||||
CHECK(variant == object);
|
||||
CHECK(variant <= object);
|
||||
CHECK(variant >= object);
|
||||
CHECK_FALSE(variant != object);
|
||||
CHECK_FALSE(variant > object);
|
||||
CHECK_FALSE(variant < object);
|
||||
}
|
||||
|
||||
SECTION("Compare with identical object") {
|
||||
JsonObject object = doc.add<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello";
|
||||
object["c"][0] = false;
|
||||
|
||||
JsonObject object2 = doc.add<JsonObject>();
|
||||
object2["a"] = 1;
|
||||
object2["b"] = "hello";
|
||||
object2["c"][0] = false;
|
||||
JsonVariantConst variant = object2;
|
||||
|
||||
CHECK(object == variant);
|
||||
CHECK(object <= variant);
|
||||
CHECK(object >= variant);
|
||||
CHECK_FALSE(object != variant);
|
||||
CHECK_FALSE(object > variant);
|
||||
CHECK_FALSE(object < variant);
|
||||
|
||||
CHECK(variant == object);
|
||||
CHECK(variant <= object);
|
||||
CHECK(variant >= object);
|
||||
CHECK_FALSE(variant != object);
|
||||
CHECK_FALSE(variant > object);
|
||||
CHECK_FALSE(variant < object);
|
||||
}
|
||||
|
||||
SECTION("Compare with different object") {
|
||||
JsonObject object = doc.add<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello1";
|
||||
object["c"][0] = false;
|
||||
|
||||
JsonObject object2 = doc.add<JsonObject>();
|
||||
object2["a"] = 1;
|
||||
object2["b"] = "hello2";
|
||||
object2["c"][0] = false;
|
||||
JsonVariantConst variant = object2;
|
||||
|
||||
CHECK(object != variant);
|
||||
CHECK_FALSE(object == variant);
|
||||
CHECK_FALSE(object > variant);
|
||||
CHECK_FALSE(object < variant);
|
||||
CHECK_FALSE(object <= variant);
|
||||
CHECK_FALSE(object >= variant);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonObject with JsonObjectConst") {
|
||||
JsonDocument doc;
|
||||
|
||||
SECTION("Compare with unbound") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello";
|
||||
JsonObjectConst unbound;
|
||||
|
||||
CHECK(object != unbound);
|
||||
CHECK_FALSE(object == unbound);
|
||||
CHECK_FALSE(object <= unbound);
|
||||
CHECK_FALSE(object >= unbound);
|
||||
CHECK_FALSE(object > unbound);
|
||||
CHECK_FALSE(object < unbound);
|
||||
|
||||
CHECK(unbound != object);
|
||||
CHECK_FALSE(unbound == object);
|
||||
CHECK_FALSE(unbound <= object);
|
||||
CHECK_FALSE(unbound >= object);
|
||||
CHECK_FALSE(unbound > object);
|
||||
CHECK_FALSE(unbound < object);
|
||||
}
|
||||
|
||||
SECTION("Compare with self") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello";
|
||||
JsonObjectConst cobject = object;
|
||||
|
||||
CHECK(object == cobject);
|
||||
CHECK(object <= cobject);
|
||||
CHECK(object >= cobject);
|
||||
CHECK_FALSE(object != cobject);
|
||||
CHECK_FALSE(object > cobject);
|
||||
CHECK_FALSE(object < cobject);
|
||||
|
||||
CHECK(cobject == object);
|
||||
CHECK(cobject <= object);
|
||||
CHECK(cobject >= object);
|
||||
CHECK_FALSE(cobject != object);
|
||||
CHECK_FALSE(cobject > object);
|
||||
CHECK_FALSE(cobject < object);
|
||||
}
|
||||
|
||||
SECTION("Compare with identical object") {
|
||||
JsonObject object1 = doc.add<JsonObject>();
|
||||
object1["a"] = 1;
|
||||
object1["b"] = "hello";
|
||||
object1["c"][0] = false;
|
||||
|
||||
JsonObject object2 = doc.add<JsonObject>();
|
||||
object2["a"] = 1;
|
||||
object2["b"] = "hello";
|
||||
object2["c"][0] = false;
|
||||
JsonObjectConst carray2 = object2;
|
||||
|
||||
CHECK(object1 == carray2);
|
||||
CHECK(object1 <= carray2);
|
||||
CHECK(object1 >= carray2);
|
||||
CHECK_FALSE(object1 != carray2);
|
||||
CHECK_FALSE(object1 > carray2);
|
||||
CHECK_FALSE(object1 < carray2);
|
||||
|
||||
CHECK(carray2 == object1);
|
||||
CHECK(carray2 <= object1);
|
||||
CHECK(carray2 >= object1);
|
||||
CHECK_FALSE(carray2 != object1);
|
||||
CHECK_FALSE(carray2 > object1);
|
||||
CHECK_FALSE(carray2 < object1);
|
||||
}
|
||||
|
||||
SECTION("Compare with different object") {
|
||||
JsonObject object1 = doc.add<JsonObject>();
|
||||
object1["a"] = 1;
|
||||
object1["b"] = "hello1";
|
||||
object1["c"][0] = false;
|
||||
|
||||
JsonObject object2 = doc.add<JsonObject>();
|
||||
object2["a"] = 1;
|
||||
object2["b"] = "hello2";
|
||||
object2["c"][0] = false;
|
||||
JsonObjectConst carray2 = object2;
|
||||
|
||||
CHECK(object1 != carray2);
|
||||
CHECK_FALSE(object1 == carray2);
|
||||
CHECK_FALSE(object1 > carray2);
|
||||
CHECK_FALSE(object1 < carray2);
|
||||
CHECK_FALSE(object1 <= carray2);
|
||||
CHECK_FALSE(object1 >= carray2);
|
||||
|
||||
CHECK(carray2 != object1);
|
||||
CHECK_FALSE(carray2 == object1);
|
||||
CHECK_FALSE(carray2 > object1);
|
||||
CHECK_FALSE(carray2 < object1);
|
||||
CHECK_FALSE(carray2 <= object1);
|
||||
CHECK_FALSE(carray2 >= object1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonObjectConst with JsonObjectConst") {
|
||||
JsonDocument doc;
|
||||
|
||||
SECTION("Compare with unbound") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello";
|
||||
|
||||
JsonObjectConst cobject = object;
|
||||
JsonObjectConst unbound;
|
||||
|
||||
CHECK(cobject != unbound);
|
||||
CHECK_FALSE(cobject == unbound);
|
||||
CHECK_FALSE(cobject <= unbound);
|
||||
CHECK_FALSE(cobject >= unbound);
|
||||
CHECK_FALSE(cobject > unbound);
|
||||
CHECK_FALSE(cobject < unbound);
|
||||
|
||||
CHECK(unbound != cobject);
|
||||
CHECK_FALSE(unbound == cobject);
|
||||
CHECK_FALSE(unbound <= cobject);
|
||||
CHECK_FALSE(unbound >= cobject);
|
||||
CHECK_FALSE(unbound > cobject);
|
||||
CHECK_FALSE(unbound < cobject);
|
||||
}
|
||||
|
||||
SECTION("Compare with self") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello";
|
||||
JsonObjectConst cobject = object;
|
||||
|
||||
CHECK(cobject == cobject);
|
||||
CHECK(cobject <= cobject);
|
||||
CHECK(cobject >= cobject);
|
||||
CHECK_FALSE(cobject != cobject);
|
||||
CHECK_FALSE(cobject > cobject);
|
||||
CHECK_FALSE(cobject < cobject);
|
||||
}
|
||||
|
||||
SECTION("Compare with identical object") {
|
||||
JsonObject object1 = doc.add<JsonObject>();
|
||||
object1["a"] = 1;
|
||||
object1["b"] = "hello";
|
||||
object1["c"][0] = false;
|
||||
JsonObjectConst carray1 = object1;
|
||||
|
||||
JsonObject object2 = doc.add<JsonObject>();
|
||||
object2["a"] = 1;
|
||||
object2["b"] = "hello";
|
||||
object2["c"][0] = false;
|
||||
JsonObjectConst carray2 = object2;
|
||||
|
||||
CHECK(carray1 == carray2);
|
||||
CHECK(carray1 <= carray2);
|
||||
CHECK(carray1 >= carray2);
|
||||
CHECK_FALSE(carray1 != carray2);
|
||||
CHECK_FALSE(carray1 > carray2);
|
||||
CHECK_FALSE(carray1 < carray2);
|
||||
}
|
||||
|
||||
SECTION("Compare with different object") {
|
||||
JsonObject object1 = doc.add<JsonObject>();
|
||||
object1["a"] = 1;
|
||||
object1["b"] = "hello1";
|
||||
object1["c"][0] = false;
|
||||
JsonObjectConst carray1 = object1;
|
||||
|
||||
JsonObject object2 = doc.add<JsonObject>();
|
||||
object2["a"] = 1;
|
||||
object2["b"] = "hello2";
|
||||
object2["c"][0] = false;
|
||||
JsonObjectConst carray2 = object2;
|
||||
|
||||
CHECK(carray1 != carray2);
|
||||
CHECK_FALSE(carray1 == carray2);
|
||||
CHECK_FALSE(carray1 > carray2);
|
||||
CHECK_FALSE(carray1 < carray2);
|
||||
CHECK_FALSE(carray1 <= carray2);
|
||||
CHECK_FALSE(carray1 >= carray2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonObjectConst with JsonVariant") {
|
||||
JsonDocument doc;
|
||||
|
||||
SECTION("Compare with self") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["a"] = 1;
|
||||
object["b"] = "hello";
|
||||
JsonObjectConst cobject = object;
|
||||
JsonVariant variant = object;
|
||||
|
||||
CHECK(cobject == variant);
|
||||
CHECK(cobject <= variant);
|
||||
CHECK(cobject >= variant);
|
||||
CHECK_FALSE(cobject != variant);
|
||||
CHECK_FALSE(cobject > variant);
|
||||
CHECK_FALSE(cobject < variant);
|
||||
|
||||
CHECK(variant == cobject);
|
||||
CHECK(variant <= cobject);
|
||||
CHECK(variant >= cobject);
|
||||
CHECK_FALSE(variant != cobject);
|
||||
CHECK_FALSE(variant > cobject);
|
||||
CHECK_FALSE(variant < cobject);
|
||||
}
|
||||
|
||||
SECTION("Compare with identical object") {
|
||||
JsonObject object1 = doc.add<JsonObject>();
|
||||
object1["a"] = 1;
|
||||
object1["b"] = "hello";
|
||||
object1["c"][0] = false;
|
||||
JsonObjectConst carray1 = object1;
|
||||
|
||||
JsonObject object2 = doc.add<JsonObject>();
|
||||
object2["a"] = 1;
|
||||
object2["b"] = "hello";
|
||||
object2["c"][0] = false;
|
||||
JsonVariant variant2 = object2;
|
||||
|
||||
CHECK(carray1 == variant2);
|
||||
CHECK(carray1 <= variant2);
|
||||
CHECK(carray1 >= variant2);
|
||||
CHECK_FALSE(carray1 != variant2);
|
||||
CHECK_FALSE(carray1 > variant2);
|
||||
CHECK_FALSE(carray1 < variant2);
|
||||
|
||||
CHECK(variant2 == carray1);
|
||||
CHECK(variant2 <= carray1);
|
||||
CHECK(variant2 >= carray1);
|
||||
CHECK_FALSE(variant2 != carray1);
|
||||
CHECK_FALSE(variant2 > carray1);
|
||||
CHECK_FALSE(variant2 < carray1);
|
||||
}
|
||||
|
||||
SECTION("Compare with different object") {
|
||||
JsonObject object1 = doc.add<JsonObject>();
|
||||
object1["a"] = 1;
|
||||
object1["b"] = "hello1";
|
||||
object1["c"][0] = false;
|
||||
JsonObjectConst carray1 = object1;
|
||||
|
||||
JsonObject object2 = doc.add<JsonObject>();
|
||||
object2["a"] = 1;
|
||||
object2["b"] = "hello2";
|
||||
object2["c"][0] = false;
|
||||
JsonVariant variant2 = object2;
|
||||
|
||||
CHECK(carray1 != variant2);
|
||||
CHECK_FALSE(carray1 == variant2);
|
||||
CHECK_FALSE(carray1 > variant2);
|
||||
CHECK_FALSE(carray1 < variant2);
|
||||
CHECK_FALSE(carray1 <= variant2);
|
||||
CHECK_FALSE(carray1 >= variant2);
|
||||
|
||||
CHECK(variant2 != carray1);
|
||||
CHECK_FALSE(variant2 == carray1);
|
||||
CHECK_FALSE(variant2 > carray1);
|
||||
CHECK_FALSE(variant2 < carray1);
|
||||
CHECK_FALSE(variant2 <= carray1);
|
||||
CHECK_FALSE(variant2 >= carray1);
|
||||
}
|
||||
}
|
||||
59
ArduinoJson/extras/tests/JsonObject/equals.cpp
Normal file
59
ArduinoJson/extras/tests/JsonObject/equals.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonObject::operator==()") {
|
||||
JsonDocument doc1;
|
||||
JsonObject obj1 = doc1.to<JsonObject>();
|
||||
|
||||
JsonDocument doc2;
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
|
||||
SECTION("should return false when objs differ") {
|
||||
obj1["hello"] = "coucou";
|
||||
obj2["world"] = 1;
|
||||
|
||||
REQUIRE_FALSE(obj1 == obj2);
|
||||
}
|
||||
|
||||
SECTION("should return false when LHS has more elements") {
|
||||
obj1["hello"] = "coucou";
|
||||
obj1["world"] = 666;
|
||||
obj2["hello"] = "coucou";
|
||||
|
||||
REQUIRE_FALSE(obj1 == obj2);
|
||||
}
|
||||
|
||||
SECTION("should return false when RKS has more elements") {
|
||||
obj1["hello"] = "coucou";
|
||||
obj2["hello"] = "coucou";
|
||||
obj2["world"] = 666;
|
||||
|
||||
REQUIRE_FALSE(obj1 == obj2);
|
||||
}
|
||||
|
||||
SECTION("should return true when objs equal") {
|
||||
obj1["hello"] = "world";
|
||||
obj1["anwser"] = 42;
|
||||
// insert in different order
|
||||
obj2["anwser"] = 42;
|
||||
obj2["hello"] = "world";
|
||||
|
||||
REQUIRE(obj1 == obj2);
|
||||
}
|
||||
|
||||
SECTION("should return false when RHS is null") {
|
||||
JsonObject null;
|
||||
|
||||
REQUIRE_FALSE(obj1 == null);
|
||||
}
|
||||
|
||||
SECTION("should return false when LHS is null") {
|
||||
JsonObject null;
|
||||
|
||||
REQUIRE_FALSE(null == obj2);
|
||||
}
|
||||
}
|
||||
32
ArduinoJson/extras/tests/JsonObject/isNull.cpp
Normal file
32
ArduinoJson/extras/tests/JsonObject/isNull.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonObject::isNull()") {
|
||||
SECTION("returns true") {
|
||||
JsonObject obj;
|
||||
REQUIRE(obj.isNull() == true);
|
||||
}
|
||||
|
||||
SECTION("returns false") {
|
||||
JsonDocument doc;
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
REQUIRE(obj.isNull() == false);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonObject::operator bool()") {
|
||||
SECTION("returns false") {
|
||||
JsonObject obj;
|
||||
REQUIRE(static_cast<bool>(obj) == false);
|
||||
}
|
||||
|
||||
SECTION("returns true") {
|
||||
JsonDocument doc;
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
REQUIRE(static_cast<bool>(obj) == true);
|
||||
}
|
||||
}
|
||||
36
ArduinoJson/extras/tests/JsonObject/iterator.cpp
Normal file
36
ArduinoJson/extras/tests/JsonObject/iterator.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonObject::begin()/end()") {
|
||||
JsonDocument doc;
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["ab"] = 12;
|
||||
obj["cd"] = 34;
|
||||
|
||||
SECTION("NonConstIterator") {
|
||||
JsonObject::iterator it = obj.begin();
|
||||
REQUIRE(obj.end() != it);
|
||||
REQUIRE(it->key() == "ab");
|
||||
REQUIRE(12 == it->value());
|
||||
++it;
|
||||
REQUIRE(obj.end() != it);
|
||||
REQUIRE(it->key() == "cd");
|
||||
REQUIRE(34 == it->value());
|
||||
++it;
|
||||
REQUIRE(obj.end() == it);
|
||||
}
|
||||
|
||||
SECTION("Dereferencing end() is safe") {
|
||||
REQUIRE(obj.end()->key().isNull());
|
||||
REQUIRE(obj.end()->value().isNull());
|
||||
}
|
||||
|
||||
SECTION("null JsonObject") {
|
||||
JsonObject null;
|
||||
REQUIRE(null.begin() == null.end());
|
||||
}
|
||||
}
|
||||
35
ArduinoJson/extras/tests/JsonObject/nesting.cpp
Normal file
35
ArduinoJson/extras/tests/JsonObject/nesting.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonObject::nesting()") {
|
||||
JsonDocument doc;
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("return 0 if uninitialized") {
|
||||
JsonObject unitialized;
|
||||
REQUIRE(unitialized.nesting() == 0);
|
||||
}
|
||||
|
||||
SECTION("returns 1 for empty object") {
|
||||
REQUIRE(obj.nesting() == 1);
|
||||
}
|
||||
|
||||
SECTION("returns 1 for flat object") {
|
||||
obj["hello"] = "world";
|
||||
REQUIRE(obj.nesting() == 1);
|
||||
}
|
||||
|
||||
SECTION("returns 2 with nested array") {
|
||||
obj["nested"].to<JsonArray>();
|
||||
REQUIRE(obj.nesting() == 2);
|
||||
}
|
||||
|
||||
SECTION("returns 2 with nested object") {
|
||||
obj["nested"].to<JsonObject>();
|
||||
REQUIRE(obj.nesting() == 2);
|
||||
}
|
||||
}
|
||||
89
ArduinoJson/extras/tests/JsonObject/remove.cpp
Normal file
89
ArduinoJson/extras/tests/JsonObject/remove.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("JsonObject::remove()") {
|
||||
JsonDocument doc;
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["a"] = 0;
|
||||
obj["b"] = 1;
|
||||
obj["c"] = 2;
|
||||
std::string result;
|
||||
|
||||
SECTION("remove(key)") {
|
||||
SECTION("Remove first") {
|
||||
obj.remove("a");
|
||||
serializeJson(obj, result);
|
||||
REQUIRE("{\"b\":1,\"c\":2}" == result);
|
||||
}
|
||||
|
||||
SECTION("Remove middle") {
|
||||
obj.remove("b");
|
||||
serializeJson(obj, result);
|
||||
REQUIRE("{\"a\":0,\"c\":2}" == result);
|
||||
}
|
||||
|
||||
SECTION("Remove last") {
|
||||
obj.remove("c");
|
||||
serializeJson(obj, result);
|
||||
REQUIRE("{\"a\":0,\"b\":1}" == result);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("remove(iterator)") {
|
||||
JsonObject::iterator it = obj.begin();
|
||||
|
||||
SECTION("Remove first") {
|
||||
obj.remove(it);
|
||||
serializeJson(obj, result);
|
||||
REQUIRE("{\"b\":1,\"c\":2}" == result);
|
||||
}
|
||||
|
||||
SECTION("Remove middle") {
|
||||
++it;
|
||||
obj.remove(it);
|
||||
serializeJson(obj, result);
|
||||
REQUIRE("{\"a\":0,\"c\":2}" == result);
|
||||
}
|
||||
|
||||
SECTION("Remove last") {
|
||||
++it;
|
||||
++it;
|
||||
obj.remove(it);
|
||||
serializeJson(obj, result);
|
||||
REQUIRE("{\"a\":0,\"b\":1}" == result);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("key is a vla") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "b");
|
||||
obj.remove(vla);
|
||||
|
||||
serializeJson(obj, result);
|
||||
REQUIRE("{\"a\":0,\"c\":2}" == result);
|
||||
}
|
||||
#endif
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
SECTION("remove(JsonVariant)") {
|
||||
obj["key"] = "b";
|
||||
obj.remove(obj["key"]);
|
||||
REQUIRE("{\"a\":0,\"c\":2,\"key\":\"b\"}" == doc.as<std::string>());
|
||||
}
|
||||
}
|
||||
142
ArduinoJson/extras/tests/JsonObject/set.cpp
Normal file
142
ArduinoJson/extras/tests/JsonObject/set.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonObject::set()") {
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc1(&spy);
|
||||
JsonDocument doc2(&spy);
|
||||
|
||||
JsonObject obj1 = doc1.to<JsonObject>();
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
|
||||
SECTION("doesn't copy static string in key or value") {
|
||||
obj1["hello"] = "world";
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == "world"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("copy local string value") {
|
||||
obj1["hello"] = "world"_s;
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == "world"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("copy local key") {
|
||||
obj1["hello"_s] = "world";
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == "world"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("copy string from deserializeJson()") {
|
||||
deserializeJson(doc1, "{'hello':'world'}");
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == "world"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("copy string from deserializeMsgPack()") {
|
||||
deserializeMsgPack(doc1, "\x81\xA5hello\xA5world");
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == "world"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should work with JsonObjectConst") {
|
||||
obj1["hello"] = "world";
|
||||
|
||||
obj2.set(static_cast<JsonObjectConst>(obj1));
|
||||
|
||||
REQUIRE(obj2["hello"] == "world"_s);
|
||||
}
|
||||
|
||||
SECTION("copy fails in the middle of an object") {
|
||||
TimebombAllocator timebomb(2);
|
||||
JsonDocument doc3(&timebomb);
|
||||
JsonObject obj3 = doc3.to<JsonObject>();
|
||||
|
||||
obj1["alpha"_s] = 1;
|
||||
obj1["beta"_s] = 2;
|
||||
|
||||
bool success = obj3.set(obj1);
|
||||
|
||||
REQUIRE(success == false);
|
||||
REQUIRE(doc3.as<std::string>() == "{\"alpha\":1}");
|
||||
}
|
||||
|
||||
SECTION("copy fails in the middle of an array") {
|
||||
TimebombAllocator timebomb(1);
|
||||
JsonDocument doc3(&timebomb);
|
||||
JsonObject obj3 = doc3.to<JsonObject>();
|
||||
|
||||
obj1["hello"][0] = "world"_s;
|
||||
|
||||
bool success = obj3.set(obj1);
|
||||
|
||||
REQUIRE(success == false);
|
||||
REQUIRE(doc3.as<std::string>() == "{\"hello\":[]}");
|
||||
}
|
||||
|
||||
SECTION("destination is null") {
|
||||
JsonObject null;
|
||||
obj1["hello"] = "world";
|
||||
|
||||
bool success = null.set(obj1);
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("source is null") {
|
||||
JsonObject null;
|
||||
obj1["hello"] = "world";
|
||||
|
||||
bool success = obj1.set(null);
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
}
|
||||
39
ArduinoJson/extras/tests/JsonObject/size.cpp
Normal file
39
ArduinoJson/extras/tests/JsonObject/size.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("JsonObject::size()") {
|
||||
JsonDocument doc;
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("initial size is zero") {
|
||||
REQUIRE(0 == obj.size());
|
||||
}
|
||||
|
||||
SECTION("increases when values are added") {
|
||||
obj["hello"] = 42;
|
||||
REQUIRE(1 == obj.size());
|
||||
}
|
||||
|
||||
SECTION("decreases when values are removed") {
|
||||
obj["hello"] = 42;
|
||||
obj.remove("hello");
|
||||
REQUIRE(0 == obj.size());
|
||||
}
|
||||
|
||||
SECTION("doesn't increase when the same key is added twice") {
|
||||
obj["hello"] = 1;
|
||||
obj["hello"] = 2;
|
||||
REQUIRE(1 == obj.size());
|
||||
}
|
||||
|
||||
SECTION("doesn't decrease when another key is removed") {
|
||||
obj["hello"] = 1;
|
||||
obj.remove("world");
|
||||
REQUIRE(1 == obj.size());
|
||||
}
|
||||
}
|
||||
61
ArduinoJson/extras/tests/JsonObject/std_string.cpp
Normal file
61
ArduinoJson/extras/tests/JsonObject/std_string.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
static void eraseString(std::string& str) {
|
||||
char* p = const_cast<char*>(str.c_str());
|
||||
while (*p)
|
||||
*p++ = '*';
|
||||
}
|
||||
|
||||
TEST_CASE("std::string") {
|
||||
JsonDocument doc;
|
||||
|
||||
SECTION("operator[]") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
|
||||
deserializeJson(doc, json);
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE("value"_s == obj["key"_s]);
|
||||
}
|
||||
|
||||
SECTION("operator[] const") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
|
||||
deserializeJson(doc, json);
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE("value"_s == obj["key"_s]);
|
||||
}
|
||||
|
||||
SECTION("remove()") {
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["key"] = "value";
|
||||
|
||||
obj.remove("key"_s);
|
||||
|
||||
REQUIRE(0 == obj.size());
|
||||
}
|
||||
|
||||
SECTION("operator[], set key") {
|
||||
std::string key("hello");
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj[key] = "world";
|
||||
eraseString(key);
|
||||
REQUIRE("world"_s == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("operator[], set value") {
|
||||
std::string value("world");
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"] = value;
|
||||
eraseString(value);
|
||||
REQUIRE("world"_s == obj["hello"]);
|
||||
}
|
||||
}
|
||||
267
ArduinoJson/extras/tests/JsonObject/subscript.cpp
Normal file
267
ArduinoJson/extras/tests/JsonObject/subscript.cpp
Normal file
@@ -0,0 +1,267 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonObject::operator[]") {
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("int") {
|
||||
obj["hello"] = 123;
|
||||
|
||||
REQUIRE(123 == obj["hello"].as<int>());
|
||||
REQUIRE(true == obj["hello"].is<int>());
|
||||
REQUIRE(false == obj["hello"].is<bool>());
|
||||
}
|
||||
|
||||
SECTION("volatile int") { // issue #415
|
||||
volatile int i = 123;
|
||||
obj["hello"] = i;
|
||||
|
||||
REQUIRE(123 == obj["hello"].as<int>());
|
||||
REQUIRE(true == obj["hello"].is<int>());
|
||||
REQUIRE(false == obj["hello"].is<bool>());
|
||||
}
|
||||
|
||||
SECTION("double") {
|
||||
obj["hello"] = 123.45;
|
||||
|
||||
REQUIRE(true == obj["hello"].is<double>());
|
||||
REQUIRE(false == obj["hello"].is<long>());
|
||||
REQUIRE(123.45 == obj["hello"].as<double>());
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
obj["hello"] = true;
|
||||
|
||||
REQUIRE(true == obj["hello"].is<bool>());
|
||||
REQUIRE(false == obj["hello"].is<long>());
|
||||
REQUIRE(true == obj["hello"].as<bool>());
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
obj["hello"] = "h3110";
|
||||
|
||||
REQUIRE(true == obj["hello"].is<const char*>());
|
||||
REQUIRE(false == obj["hello"].is<long>());
|
||||
REQUIRE("h3110"_s == obj["hello"].as<const char*>());
|
||||
}
|
||||
|
||||
SECTION("array") {
|
||||
JsonDocument doc2;
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
|
||||
obj["hello"] = arr;
|
||||
|
||||
REQUIRE(arr == obj["hello"].as<JsonArray>());
|
||||
REQUIRE(true == obj["hello"].is<JsonArray>());
|
||||
REQUIRE(false == obj["hello"].is<JsonObject>());
|
||||
}
|
||||
|
||||
SECTION("object") {
|
||||
JsonDocument doc2;
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
|
||||
obj["hello"] = obj2;
|
||||
|
||||
REQUIRE(obj2 == obj["hello"].as<JsonObject>());
|
||||
REQUIRE(true == obj["hello"].is<JsonObject>());
|
||||
REQUIRE(false == obj["hello"].is<JsonArray>());
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
JsonDocument doc2;
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
arr.add(42);
|
||||
|
||||
obj["a"] = arr[0];
|
||||
|
||||
REQUIRE(42 == obj["a"]);
|
||||
}
|
||||
|
||||
SECTION("object subscript") {
|
||||
JsonDocument doc2;
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
obj2["x"] = 42;
|
||||
|
||||
obj["a"] = obj2["x"];
|
||||
|
||||
REQUIRE(42 == obj["a"]);
|
||||
}
|
||||
|
||||
SECTION("char key[]") { // issue #423
|
||||
char key[] = "hello";
|
||||
obj[key] = 42;
|
||||
REQUIRE(42 == obj[key]);
|
||||
}
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
obj["hello"] = "world";
|
||||
REQUIRE(spy.log() == AllocatorLog{Allocate(sizeofPool())});
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* value") {
|
||||
obj["hello"] = const_cast<char*>("world");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* key") {
|
||||
obj[const_cast<char*>("hello")] = "world";
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* key&value") {
|
||||
obj[const_cast<char*>("hello")] = const_cast<char*>("world");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string value") {
|
||||
obj["hello"] = "world"_s;
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string key") {
|
||||
obj["hello"_s] = "world";
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string key&value") {
|
||||
obj["hello"_s] = "world"_s;
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate a non-static JsonString key") {
|
||||
obj[JsonString("hello", false)] = "world";
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should not duplicate a static JsonString key") {
|
||||
obj[JsonString("hello", true)] = "world";
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should ignore null key") {
|
||||
// object must have a value to make a call to strcmp()
|
||||
obj["dummy"] = 42;
|
||||
|
||||
const char* null = 0;
|
||||
obj[null] = 666;
|
||||
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj[null] == null);
|
||||
}
|
||||
|
||||
SECTION("obj[key].to<JsonArray>()") {
|
||||
JsonArray arr = obj["hello"].to<JsonArray>();
|
||||
|
||||
REQUIRE(arr.isNull() == false);
|
||||
}
|
||||
|
||||
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
|
||||
!defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
|
||||
SECTION("obj[VLA] = str") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
obj[vla] = "world";
|
||||
|
||||
REQUIRE("world"_s == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("obj[str] = VLA") { // issue #416
|
||||
size_t i = 32;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
obj["hello"] = vla;
|
||||
|
||||
REQUIRE("world"_s == obj["hello"].as<const char*>());
|
||||
}
|
||||
|
||||
SECTION("obj.set(VLA, str)") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
obj[vla] = "world";
|
||||
|
||||
REQUIRE("world"_s == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("obj.set(str, VLA)") {
|
||||
size_t i = 32;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
obj["hello"].set(vla);
|
||||
|
||||
REQUIRE("world"_s == obj["hello"].as<const char*>());
|
||||
}
|
||||
|
||||
SECTION("obj[VLA]") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
|
||||
obj = doc.as<JsonObject>();
|
||||
REQUIRE("world"_s == obj[vla]);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("chain") {
|
||||
obj["hello"]["world"] = 123;
|
||||
|
||||
REQUIRE(123 == obj["hello"]["world"].as<int>());
|
||||
REQUIRE(true == obj["hello"]["world"].is<int>());
|
||||
REQUIRE(false == obj["hello"]["world"].is<bool>());
|
||||
}
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
obj["hello"] = "world";
|
||||
obj["a\0b"_s] = "ABC";
|
||||
|
||||
doc["key1"] = "hello";
|
||||
doc["key2"] = "a\0b"_s;
|
||||
doc["key3"] = "foo";
|
||||
|
||||
REQUIRE(obj[obj["key1"]] == "world");
|
||||
REQUIRE(obj[obj["key2"]] == "ABC");
|
||||
REQUIRE(obj[obj["key3"]] == nullptr);
|
||||
REQUIRE(obj[obj["key4"]] == nullptr);
|
||||
}
|
||||
}
|
||||
27
ArduinoJson/extras/tests/JsonObject/unbound.cpp
Normal file
27
ArduinoJson/extras/tests/JsonObject/unbound.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("Unbound JsonObject") {
|
||||
JsonObject obj;
|
||||
|
||||
SECTION("retrieve member") {
|
||||
REQUIRE(obj["key"].isNull());
|
||||
}
|
||||
|
||||
SECTION("add member") {
|
||||
obj["hello"] = "world";
|
||||
REQUIRE(0 == obj.size());
|
||||
}
|
||||
|
||||
SECTION("serialize") {
|
||||
char buffer[32];
|
||||
serializeJson(obj, buffer, sizeof(buffer));
|
||||
REQUIRE_THAT(buffer, Equals("null"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user