Update libs
This commit is contained in:
@@ -5,15 +5,18 @@
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_subdirectory(catch)
|
||||
link_libraries(ArduinoJson)
|
||||
|
||||
link_libraries(ArduinoJson catch)
|
||||
# Failing builds should only link with ArduinoJson, not catch
|
||||
add_subdirectory(FailingBuilds)
|
||||
|
||||
add_subdirectory(catch)
|
||||
link_libraries(catch)
|
||||
|
||||
include_directories(Helpers)
|
||||
add_subdirectory(Cpp17)
|
||||
add_subdirectory(Cpp20)
|
||||
add_subdirectory(Deprecated)
|
||||
add_subdirectory(FailingBuilds)
|
||||
add_subdirectory(IntegrationTests)
|
||||
add_subdirectory(JsonArray)
|
||||
add_subdirectory(JsonArrayConst)
|
||||
|
||||
@@ -44,13 +44,25 @@ TEST_CASE("JsonDocument::containsKey()") {
|
||||
REQUIRE(doc.containsKey("hello") == false);
|
||||
}
|
||||
|
||||
SECTION("support JsonVariant") {
|
||||
SECTION("supports JsonVariant") {
|
||||
doc["hello"] = "world";
|
||||
doc["key"] = "hello";
|
||||
|
||||
REQUIRE(doc.containsKey(doc["key"]) == true);
|
||||
REQUIRE(doc.containsKey(doc["foo"]) == false);
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("supports VLAs") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
doc["hello"] = "world";
|
||||
|
||||
REQUIRE(doc.containsKey(vla) == true);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::containsKey()") {
|
||||
@@ -70,6 +82,18 @@ TEST_CASE("MemberProxy::containsKey()") {
|
||||
REQUIRE(mp.containsKey("key"_s) == true);
|
||||
REQUIRE(mp.containsKey("key"_s) == true);
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("supports VLAs") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
mp["hello"] = "world";
|
||||
|
||||
REQUIRE(mp.containsKey(vla) == true);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("JsonObject::containsKey()") {
|
||||
@@ -175,6 +199,18 @@ TEST_CASE("JsonVariant::containsKey()") {
|
||||
REQUIRE(var.containsKey(doc["key"]) == true);
|
||||
REQUIRE(var.containsKey(doc["foo"]) == false);
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("supports VLAs") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
var["hello"] = "world";
|
||||
|
||||
REQUIRE(var.containsKey(vla) == true);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("JsonVariantConst::containsKey()") {
|
||||
|
||||
@@ -2,7 +2,11 @@
|
||||
# Copyright © 2014-2024, Benoit BLANCHON
|
||||
# MIT License
|
||||
|
||||
macro(build_should_fail target)
|
||||
macro(add_failing_build source_file)
|
||||
get_filename_component(target ${source_file} NAME_WE)
|
||||
|
||||
add_executable(${target} ${source_file})
|
||||
|
||||
set_target_properties(${target}
|
||||
PROPERTIES
|
||||
EXCLUDE_FROM_ALL TRUE
|
||||
@@ -16,21 +20,13 @@ macro(build_should_fail target)
|
||||
set_tests_properties(${target}
|
||||
PROPERTIES
|
||||
WILL_FAIL TRUE
|
||||
LABELS "WillFail;Catch"
|
||||
LABELS "WillFail"
|
||||
)
|
||||
endmacro()
|
||||
|
||||
add_executable(Issue978 Issue978.cpp)
|
||||
build_should_fail(Issue978)
|
||||
|
||||
add_executable(read_long_long read_long_long.cpp)
|
||||
build_should_fail(read_long_long)
|
||||
|
||||
add_executable(write_long_long write_long_long.cpp)
|
||||
build_should_fail(write_long_long)
|
||||
|
||||
add_executable(variant_as_char variant_as_char.cpp)
|
||||
build_should_fail(variant_as_char)
|
||||
|
||||
add_executable(assign_char assign_char.cpp)
|
||||
build_should_fail(assign_char)
|
||||
add_failing_build(Issue978.cpp)
|
||||
add_failing_build(read_long_long.cpp)
|
||||
add_failing_build(write_long_long.cpp)
|
||||
add_failing_build(variant_as_char.cpp)
|
||||
add_failing_build(assign_char.cpp)
|
||||
add_failing_build(deserialize_object.cpp)
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2024, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
// See issue #2135
|
||||
|
||||
int main() {
|
||||
JsonObject obj;
|
||||
deserializeJson(obj, "");
|
||||
}
|
||||
@@ -16,6 +16,7 @@ add_executable(JsonDocumentTests
|
||||
nesting.cpp
|
||||
overflowed.cpp
|
||||
remove.cpp
|
||||
set.cpp
|
||||
shrinkToFit.cpp
|
||||
size.cpp
|
||||
subscript.cpp
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
typedef ArduinoJson::detail::ElementProxy<JsonDocument&> ElementProxy;
|
||||
using ElementProxy = ArduinoJson::detail::ElementProxy<JsonDocument&>;
|
||||
|
||||
TEST_CASE("ElementProxy::add()") {
|
||||
JsonDocument doc;
|
||||
@@ -26,13 +26,25 @@ TEST_CASE("ElementProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
|
||||
}
|
||||
|
||||
SECTION("set(char[])") {
|
||||
SECTION("add(char[])") {
|
||||
char s[] = "world";
|
||||
ep.add(s);
|
||||
strcpy(s, "!!!!!");
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("set(vla)") {
|
||||
size_t i = 8;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
ep.add(vla);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy::clear()") {
|
||||
@@ -166,6 +178,18 @@ TEST_CASE("ElementProxy::set()") {
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[\"world\"]");
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("set(VLA)") {
|
||||
size_t i = 8;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
ep.set(vla);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[\"world\"]");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy::size()") {
|
||||
@@ -205,6 +229,18 @@ TEST_CASE("ElementProxy::operator[]") {
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[null,[null,null,42]]");
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("set VLA") {
|
||||
size_t i = 8;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
ep[0] = vla;
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[null,[\"world\"]]");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy cast to JsonVariantConst") {
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
|
||||
typedef ArduinoJson::detail::MemberProxy<JsonDocument&, const char*>
|
||||
MemberProxy;
|
||||
using MemberProxy =
|
||||
ArduinoJson::detail::MemberProxy<JsonDocument&, const char*>;
|
||||
|
||||
TEST_CASE("MemberProxy::add()") {
|
||||
JsonDocument doc;
|
||||
@@ -32,6 +32,18 @@ TEST_CASE("MemberProxy::add()") {
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("add(vla)") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
mp.add(vla);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::clear()") {
|
||||
@@ -195,6 +207,18 @@ TEST_CASE("MemberProxy::set()") {
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("set(vla)") {
|
||||
size_t i = 8;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
mp.set(vla);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::size()") {
|
||||
|
||||
@@ -79,6 +79,24 @@ TEST_CASE("JsonDocument::add(T)") {
|
||||
Allocate(sizeofString("example")),
|
||||
});
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("VLA") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "example");
|
||||
|
||||
doc.add(vla);
|
||||
doc.add(vla);
|
||||
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
REQUIRE("example"_s == doc[0]);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("example")),
|
||||
});
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument::add<T>()") {
|
||||
|
||||
79
lib/ArduinoJson/extras/tests/JsonDocument/set.cpp
Normal file
79
lib/ArduinoJson/extras/tests/JsonDocument/set.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
|
||||
#define ARDUINOJSON_ENABLE_PROGMEM 1
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonDocument::set()") {
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("integer") {
|
||||
doc.set(42);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "42");
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
doc.set("example");
|
||||
|
||||
REQUIRE(doc.as<const char*>() == "example"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
doc.set("example"_s);
|
||||
|
||||
REQUIRE(doc.as<const char*>() == "example"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofString("example")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("char*") {
|
||||
char value[] = "example";
|
||||
doc.set(value);
|
||||
|
||||
REQUIRE(doc.as<const char*>() == "example"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofString("example")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("Arduino String") {
|
||||
doc.set(String("example"));
|
||||
|
||||
REQUIRE(doc.as<const char*>() == "example"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofString("example")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("Flash string") {
|
||||
doc.set(F("example"));
|
||||
|
||||
REQUIRE(doc.as<const char*>() == "example"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofString("example")),
|
||||
});
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("VLA") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "example");
|
||||
|
||||
doc.set(vla);
|
||||
|
||||
REQUIRE(doc.as<const char*>() == "example"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofString("example")),
|
||||
});
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -12,28 +12,55 @@ TEST_CASE("JsonDocument::operator[]") {
|
||||
const JsonDocument& cdoc = doc;
|
||||
|
||||
SECTION("object") {
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
doc["abc"_s] = "ABC";
|
||||
doc["abc\0d"_s] = "ABCD";
|
||||
|
||||
SECTION("const char*") {
|
||||
REQUIRE(doc["hello"] == "world");
|
||||
REQUIRE(cdoc["hello"] == "world");
|
||||
REQUIRE(doc["abc"] == "ABC");
|
||||
REQUIRE(cdoc["abc"] == "ABC");
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
REQUIRE(doc["hello"_s] == "world");
|
||||
REQUIRE(cdoc["hello"_s] == "world");
|
||||
REQUIRE(doc["abc"_s] == "ABC");
|
||||
REQUIRE(cdoc["abc"_s] == "ABC");
|
||||
REQUIRE(doc["abc\0d"_s] == "ABCD");
|
||||
REQUIRE(cdoc["abc\0d"_s] == "ABCD");
|
||||
}
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
doc["key"] = "hello";
|
||||
REQUIRE(doc[doc["key"]] == "world");
|
||||
REQUIRE(cdoc[cdoc["key"]] == "world");
|
||||
doc["key1"] = "abc";
|
||||
doc["key2"] = "abc\0d"_s;
|
||||
doc["key3"] = "foo";
|
||||
|
||||
CHECK(doc[doc["key1"]] == "ABC");
|
||||
CHECK(doc[doc["key2"]] == "ABCD");
|
||||
CHECK(doc[doc["key3"]] == nullptr);
|
||||
CHECK(doc[doc["key4"]] == nullptr);
|
||||
|
||||
CHECK(cdoc[cdoc["key1"]] == "ABC");
|
||||
CHECK(cdoc[cdoc["key2"]] == "ABCD");
|
||||
CHECK(cdoc[cdoc["key3"]] == nullptr);
|
||||
CHECK(cdoc[cdoc["key4"]] == nullptr);
|
||||
}
|
||||
|
||||
SECTION("supports operator|") {
|
||||
REQUIRE((doc["hello"] | "nope") == "world"_s);
|
||||
REQUIRE((doc["world"] | "nope") == "nope"_s);
|
||||
REQUIRE((doc["abc"] | "nope") == "ABC"_s);
|
||||
REQUIRE((doc["def"] | "nope") == "nope"_s);
|
||||
}
|
||||
|
||||
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
|
||||
!defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
|
||||
SECTION("supports VLAs") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
doc[vla] = "world";
|
||||
|
||||
REQUIRE(doc[vla] == "world");
|
||||
REQUIRE(cdoc[vla] == "world");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("array") {
|
||||
|
||||
@@ -253,9 +253,15 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
obj["hello"] = "world";
|
||||
doc["key"] = "hello";
|
||||
obj["a\0b"_s] = "ABC";
|
||||
|
||||
REQUIRE(obj[obj["key"]] == "world");
|
||||
REQUIRE(obj[obj["foo"]] == nullptr);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
TEST_CASE("JsonObjectConst::operator[]") {
|
||||
JsonDocument doc;
|
||||
doc["hello"] = "world";
|
||||
doc["a\0b"_s] = "ABC";
|
||||
JsonObjectConst obj = doc.as<JsonObjectConst>();
|
||||
|
||||
SECTION("supports const char*") {
|
||||
@@ -19,6 +20,7 @@ TEST_CASE("JsonObjectConst::operator[]") {
|
||||
|
||||
SECTION("supports std::string") {
|
||||
REQUIRE(obj["hello"_s] == "world"); // issue #2019
|
||||
REQUIRE(obj["a\0b"_s] == "ABC");
|
||||
}
|
||||
|
||||
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
|
||||
@@ -28,13 +30,17 @@ TEST_CASE("JsonObjectConst::operator[]") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
REQUIRE("world"_s == obj[vla]);
|
||||
REQUIRE(obj[vla] == "world"_s);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("supports JsonVariant") {
|
||||
doc["key"] = "hello";
|
||||
REQUIRE(obj[obj["key"]] == "world");
|
||||
REQUIRE(obj[obj["foo"]] == nullptr);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,18 @@ TEST_CASE("JsonVariant::add(T)") {
|
||||
|
||||
REQUIRE(var.as<std::string>() == "{\"val\":123}");
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("supports VLAs") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
var.add(vla);
|
||||
|
||||
REQUIRE(var.as<std::string>() == "[\"hello\"]");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("JsonVariant::add<T>()") {
|
||||
|
||||
@@ -83,6 +83,23 @@ TEST_CASE("JsonVariant::remove(std::string)") {
|
||||
REQUIRE(var.as<std::string>() == "{\"a\":1}");
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
TEST_CASE("JsonVariant::remove(VLA)") {
|
||||
JsonDocument doc;
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
|
||||
var["a"] = 1;
|
||||
var["b"] = 2;
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "b");
|
||||
|
||||
var.remove("b"_s);
|
||||
|
||||
REQUIRE(var.as<std::string>() == "{\"a\":1}");
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("JsonVariant::remove(JsonVariant) from object") {
|
||||
JsonDocument doc;
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
|
||||
@@ -99,7 +99,7 @@ TEST_CASE("vector<int>") {
|
||||
}
|
||||
|
||||
TEST_CASE("array<int, 2>") {
|
||||
typedef std::array<int, 2> array_type;
|
||||
using array_type = std::array<int, 2>;
|
||||
|
||||
SECTION("toJson") {
|
||||
array_type v;
|
||||
|
||||
@@ -116,12 +116,19 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("use JsonVariant as key") {
|
||||
object["a"] = "a";
|
||||
object["b"] = "b";
|
||||
object["c"] = "b";
|
||||
object["a"] = "A";
|
||||
object["ab"] = "AB";
|
||||
object["ab\0c"_s] = "ABC";
|
||||
object["key1"] = "a";
|
||||
object["key2"] = "ab";
|
||||
object["key3"] = "ab\0c"_s;
|
||||
object["key4"] = "foo";
|
||||
|
||||
REQUIRE(var[var["c"]] == "b");
|
||||
REQUIRE(var[var["d"]].isNull());
|
||||
REQUIRE(var[var["key1"]] == "A");
|
||||
REQUIRE(var[var["key2"]] == "AB");
|
||||
REQUIRE(var[var["key3"]] == "ABC");
|
||||
REQUIRE(var[var["key4"]].isNull());
|
||||
REQUIRE(var[var["key5"]].isNull());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,20 +50,22 @@ TEST_CASE("JsonVariantConst::operator[]") {
|
||||
|
||||
SECTION("object") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["a"] = "A";
|
||||
object["b"] = "B";
|
||||
object["ab"_s] = "AB";
|
||||
object["abc"_s] = "ABC";
|
||||
object["abc\0d"_s] = "ABCD";
|
||||
|
||||
SECTION("supports const char*") {
|
||||
REQUIRE("A"_s == var["a"]);
|
||||
REQUIRE("B"_s == var["b"]);
|
||||
REQUIRE(var["c"].isNull());
|
||||
REQUIRE(var["ab"] == "AB"_s);
|
||||
REQUIRE(var["abc"] == "ABC"_s);
|
||||
REQUIRE(var["def"].isNull());
|
||||
REQUIRE(var[0].isNull());
|
||||
}
|
||||
|
||||
SECTION("supports std::string") {
|
||||
REQUIRE("A"_s == var["a"_s]);
|
||||
REQUIRE("B"_s == var["b"_s]);
|
||||
REQUIRE(var["c"_s].isNull());
|
||||
REQUIRE(var["ab"_s] == "AB"_s);
|
||||
REQUIRE(var["abc"_s] == "ABC"_s);
|
||||
REQUIRE(var["abc\0d"_s] == "ABCD"_s);
|
||||
REQUIRE(var["def"_s].isNull());
|
||||
}
|
||||
|
||||
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
|
||||
@@ -71,16 +73,23 @@ TEST_CASE("JsonVariantConst::operator[]") {
|
||||
SECTION("supports VLA") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "a");
|
||||
strcpy(vla, "abc");
|
||||
|
||||
REQUIRE("A"_s == var[vla]);
|
||||
REQUIRE(var[vla] == "ABC"_s);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("supports JsonVariant") {
|
||||
object["c"] = "b";
|
||||
REQUIRE(var[var["c"]] == "B");
|
||||
REQUIRE(var[var["d"]].isNull());
|
||||
object["key1"] = "ab";
|
||||
object["key2"] = "abc";
|
||||
object["key3"] = "abc\0d"_s;
|
||||
object["key4"] = "foo";
|
||||
|
||||
REQUIRE(var[var["key1"]] == "AB"_s);
|
||||
REQUIRE(var[var["key2"]] == "ABC"_s);
|
||||
REQUIRE(var[var["key3"]] == "ABCD"_s);
|
||||
REQUIRE(var[var["key4"]].isNull());
|
||||
REQUIRE(var[var["key5"]].isNull());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ add_executable(MiscTests
|
||||
arithmeticCompare.cpp
|
||||
conflicts.cpp
|
||||
issue1967.cpp
|
||||
issue2129.cpp
|
||||
JsonString.cpp
|
||||
NoArduinoHeader.cpp
|
||||
printable.cpp
|
||||
|
||||
@@ -12,77 +12,101 @@
|
||||
#include "custom_string.hpp"
|
||||
#include "weird_strcmp.hpp"
|
||||
|
||||
using ArduinoJson::JsonString;
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("ZeroTerminatedRamString") {
|
||||
SECTION("null") {
|
||||
ZeroTerminatedRamString s = adaptString(static_cast<const char*>(0));
|
||||
TEST_CASE("adaptString()") {
|
||||
SECTION("null const char*") {
|
||||
auto s = adaptString(static_cast<const char*>(0));
|
||||
|
||||
CHECK(s.isNull() == true);
|
||||
CHECK(s.size() == 0);
|
||||
CHECK(s.isLinked() == true);
|
||||
}
|
||||
|
||||
SECTION("non-null") {
|
||||
ZeroTerminatedRamString s = adaptString("bravo");
|
||||
SECTION("non-null const char*") {
|
||||
auto s = adaptString("bravo");
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isLinked() == true);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("SizedRamString") {
|
||||
SECTION("null") {
|
||||
SizedRamString s = adaptString(static_cast<const char*>(0), 10);
|
||||
SECTION("null const char* + size") {
|
||||
auto s = adaptString(static_cast<const char*>(0), 10);
|
||||
|
||||
CHECK(s.isNull() == true);
|
||||
CHECK(s.isLinked() == false);
|
||||
}
|
||||
|
||||
SECTION("non-null") {
|
||||
SizedRamString s = adaptString("bravo", 5);
|
||||
SECTION("non-null const char* + size") {
|
||||
auto s = adaptString("bravo", 5);
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isLinked() == false);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("FlashString") {
|
||||
SECTION("null") {
|
||||
FlashString s = adaptString(static_cast<const __FlashStringHelper*>(0));
|
||||
SECTION("null Flash string") {
|
||||
auto s = adaptString(static_cast<const __FlashStringHelper*>(0));
|
||||
|
||||
CHECK(s.isNull() == true);
|
||||
CHECK(s.size() == 0);
|
||||
CHECK(s.isLinked() == false);
|
||||
}
|
||||
|
||||
SECTION("non-null") {
|
||||
FlashString s = adaptString(F("bravo"));
|
||||
SECTION("non-null Flash string") {
|
||||
auto s = adaptString(F("bravo"));
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isLinked() == false);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("std::string") {
|
||||
std::string orig("bravo");
|
||||
SizedRamString s = adaptString(orig);
|
||||
SECTION("std::string") {
|
||||
std::string orig("bravo");
|
||||
auto s = adaptString(orig);
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
}
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isLinked() == false);
|
||||
}
|
||||
|
||||
TEST_CASE("Arduino String") {
|
||||
::String orig("bravo");
|
||||
SizedRamString s = adaptString(orig);
|
||||
SECTION("Arduino String") {
|
||||
::String orig("bravo");
|
||||
auto s = adaptString(orig);
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
}
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isLinked() == false);
|
||||
}
|
||||
|
||||
TEST_CASE("custom_string") {
|
||||
custom_string orig("bravo");
|
||||
SizedRamString s = adaptString(orig);
|
||||
SECTION("custom_string") {
|
||||
custom_string orig("bravo");
|
||||
auto s = adaptString(orig);
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isLinked() == false);
|
||||
}
|
||||
|
||||
SECTION("JsonString linked") {
|
||||
JsonString orig("hello", JsonString::Ownership::Linked);
|
||||
auto s = adaptString(orig);
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isLinked() == true);
|
||||
}
|
||||
|
||||
SECTION("JsonString copied") {
|
||||
JsonString orig("hello", JsonString::Ownership::Copied);
|
||||
auto s = adaptString(orig);
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isLinked() == false);
|
||||
}
|
||||
}
|
||||
|
||||
struct EmptyStruct {};
|
||||
@@ -97,6 +121,7 @@ TEST_CASE("IsString<T>") {
|
||||
CHECK(IsString<::String>::value == true);
|
||||
CHECK(IsString<::StringSumHelper>::value == true);
|
||||
CHECK(IsString<const EmptyStruct*>::value == false);
|
||||
CHECK(IsString<JsonString>::value == true);
|
||||
}
|
||||
|
||||
TEST_CASE("stringCompare") {
|
||||
|
||||
@@ -8,4 +8,4 @@
|
||||
|
||||
struct custom_char_traits : std::char_traits<char> {};
|
||||
|
||||
typedef std::basic_string<char, custom_char_traits> custom_string;
|
||||
using custom_string = std::basic_string<char, custom_char_traits>;
|
||||
|
||||
55
lib/ArduinoJson/extras/tests/Misc/issue2129.cpp
Normal file
55
lib/ArduinoJson/extras/tests/Misc/issue2129.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2024, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
template <typename T>
|
||||
class Nullable {
|
||||
public:
|
||||
Nullable() : value_{} {}
|
||||
Nullable(T value) : value_{value} {}
|
||||
|
||||
operator T() const {
|
||||
return value_;
|
||||
}
|
||||
|
||||
operator T&() {
|
||||
return value_;
|
||||
}
|
||||
|
||||
bool is_valid() const {
|
||||
return value_ != invalid_value_;
|
||||
}
|
||||
|
||||
T value() const {
|
||||
return value_;
|
||||
}
|
||||
|
||||
private:
|
||||
T value_;
|
||||
static T invalid_value_;
|
||||
};
|
||||
|
||||
template <>
|
||||
float Nullable<float>::invalid_value_ = std::numeric_limits<float>::lowest();
|
||||
|
||||
template <typename T>
|
||||
void convertToJson(const Nullable<T>& src, JsonVariant dst) {
|
||||
if (src.is_valid()) {
|
||||
dst.set(src.value());
|
||||
} else {
|
||||
dst.clear();
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Issue #2129") {
|
||||
Nullable<float> nullable_value = Nullable<float>{123.4f};
|
||||
|
||||
JsonDocument doc;
|
||||
|
||||
doc["value"] = nullable_value;
|
||||
|
||||
REQUIRE(doc["value"].as<float>() == Approx(123.4f));
|
||||
}
|
||||
Reference in New Issue
Block a user