Update ArduinoJSON

This commit is contained in:
iranl
2025-01-06 22:27:44 +01:00
parent 565a887c95
commit a379b30c11
82 changed files with 1098 additions and 1500 deletions

View File

@@ -184,7 +184,7 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(variant.as<long>() == 42L);
REQUIRE(variant.as<JsonString>() == "42");
REQUIRE(variant.as<JsonString>().isLinked() == true);
REQUIRE(variant.as<JsonString>().isStatic() == true);
}
SECTION("set(\"hello\")") {
@@ -207,7 +207,7 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(variant.as<const char*>() == "4.2"_s);
REQUIRE(variant.as<std::string>() == "4.2"_s);
REQUIRE(variant.as<JsonString>() == "4.2");
REQUIRE(variant.as<JsonString>().isLinked() == false);
REQUIRE(variant.as<JsonString>().isStatic() == false);
}
SECTION("set(\"true\")") {

View File

@@ -1,36 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <stdint.h>
#include <catch.hpp>
#include "Literals.hpp"
TEST_CASE("JsonVariant::containsKey()") {
JsonDocument doc;
JsonVariant var = doc.to<JsonVariant>();
SECTION("containsKey(const char*)") {
var["hello"] = "world";
REQUIRE(var.containsKey("hello") == true);
REQUIRE(var.containsKey("world") == false);
}
SECTION("containsKey(std::string)") {
var["hello"] = "world";
REQUIRE(var.containsKey("hello"_s) == true);
REQUIRE(var.containsKey("world"_s) == false);
}
SECTION("containsKey(JsonVariant)") {
var["hello"] = "world";
var["key"] = "hello";
REQUIRE(var.containsKey(doc["key"]) == true);
REQUIRE(var.containsKey(doc["foo"]) == false);
}
}

View File

@@ -17,6 +17,15 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
JsonDocument doc(&spy);
JsonVariant variant = doc.to<JsonVariant>();
SECTION("string literal") {
bool result = variant.set("hello\0world");
REQUIRE(result == true);
CHECK(variant ==
"hello"_s); // linked string cannot contain '\0' at the moment
CHECK(spy.log() == AllocatorLog{});
}
SECTION("const char*") {
char str[16];
@@ -25,8 +34,10 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
strcpy(str, "world");
REQUIRE(result == true);
REQUIRE(variant == "world"); // stores by pointer
REQUIRE(spy.log() == AllocatorLog{});
REQUIRE(variant == "hello"); // stores by copy
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
});
}
SECTION("(const char*)0") {
@@ -34,6 +45,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant.isNull());
REQUIRE(variant.as<const char*>() == nullptr);
REQUIRE(spy.log() == AllocatorLog{});
}
@@ -105,16 +117,14 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
#endif
SECTION("std::string") {
std::string str;
str = "hello";
std::string str = "hello\0world"_s;
bool result = variant.set(str);
str.replace(0, 5, "world");
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
REQUIRE(variant == "hello\0world"_s); // stores by copy
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofString("hello?world")),
});
}
@@ -122,7 +132,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
char str[16];
strcpy(str, "hello");
bool result = variant.set(JsonString(str, JsonString::Linked));
bool result = variant.set(JsonString(str, true));
strcpy(str, "world");
REQUIRE(result == true);
@@ -134,7 +144,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
char str[16];
strcpy(str, "hello");
bool result = variant.set(JsonString(str, JsonString::Copied));
bool result = variant.set(JsonString(str));
strcpy(str, "world");
REQUIRE(result == true);

View File

@@ -7,14 +7,8 @@
#include <catch.hpp>
#include <limits>
template <typename T>
void checkValue(T expected) {
JsonDocument doc;
JsonVariant variant = doc.to<JsonVariant>();
variant.set(expected);
REQUIRE(expected == variant.as<T>());
}
#include "Allocators.hpp"
#include "Literals.hpp"
template <typename T>
void checkReference(T& expected) {
@@ -39,27 +33,29 @@ void checkNumericType() {
}
TEST_CASE("JsonVariant set()/get()") {
SpyingAllocator spy;
JsonDocument doc(&spy);
JsonVariant variant = doc.to<JsonVariant>();
#if ARDUINOJSON_USE_LONG_LONG
SECTION("SizeOfJsonInteger") {
REQUIRE(8 == sizeof(JsonInteger));
}
#endif
SECTION("Null") {
checkValue<const char*>(NULL);
}
SECTION("const char*") {
checkValue<const char*>("hello");
}
SECTION("std::string") {
checkValue<std::string>("hello");
}
// /!\ Most test were moved to `JsonVariant/set.cpp`
// TODO: move the remaining tests too
SECTION("False") {
checkValue<bool>(false);
variant.set(false);
REQUIRE(variant.as<bool>() == false);
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("True") {
checkValue<bool>(true);
variant.set(true);
REQUIRE(variant.as<bool>() == true);
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("Double") {
@@ -129,10 +125,12 @@ TEST_CASE("JsonVariant set()/get()") {
#endif
SECTION("CanStoreObject") {
JsonDocument doc;
JsonObject object = doc.to<JsonObject>();
JsonDocument doc2;
JsonObject object = doc2.to<JsonObject>();
checkValue<JsonObject>(object);
variant.set(object);
REQUIRE(variant.is<JsonObject>());
REQUIRE(variant.as<JsonObject>() == object);
}
}