first commit

This commit is contained in:
Jérôme Delacotte
2025-03-06 11:15:32 +01:00
commit 7b30d6e298
5276 changed files with 2108927 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
# ArduinoJson - https://arduinojson.org
# Copyright © 2014-2024, Benoit BLANCHON
# MIT License
add_executable(JsonDocumentTests
add.cpp
assignment.cpp
cast.cpp
compare.cpp
constructor.cpp
containsKey.cpp
ElementProxy.cpp
isNull.cpp
issue1120.cpp
MemberProxy.cpp
nesting.cpp
overflowed.cpp
remove.cpp
shrinkToFit.cpp
size.cpp
subscript.cpp
swap.cpp
)
add_test(JsonDocument JsonDocumentTests)
set_tests_properties(JsonDocument
PROPERTIES
LABELS "Catch"
)

View File

@@ -0,0 +1,232 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
typedef ArduinoJson::detail::ElementProxy<JsonDocument&> ElementProxy;
TEST_CASE("ElementProxy::add()") {
JsonDocument doc;
doc.add<JsonVariant>();
ElementProxy ep = doc[0];
SECTION("add(int)") {
ep.add(42);
REQUIRE(doc.as<std::string>() == "[[42]]");
}
SECTION("add(const char*)") {
ep.add("world");
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
}
SECTION("set(char[])") {
char s[] = "world";
ep.add(s);
strcpy(s, "!!!!!");
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
}
}
TEST_CASE("ElementProxy::clear()") {
JsonDocument doc;
doc.add<JsonVariant>();
ElementProxy ep = doc[0];
SECTION("size goes back to zero") {
ep.add(42);
ep.clear();
REQUIRE(ep.size() == 0);
}
SECTION("isNull() return true") {
ep.add("hello");
ep.clear();
REQUIRE(ep.isNull() == true);
}
}
TEST_CASE("ElementProxy::operator==()") {
JsonDocument doc;
SECTION("1 vs 1") {
doc.add(1);
doc.add(1);
REQUIRE(doc[0] <= doc[1]);
REQUIRE(doc[0] == doc[1]);
REQUIRE(doc[0] >= doc[1]);
REQUIRE_FALSE(doc[0] != doc[1]);
REQUIRE_FALSE(doc[0] < doc[1]);
REQUIRE_FALSE(doc[0] > doc[1]);
}
SECTION("1 vs 2") {
doc.add(1);
doc.add(2);
REQUIRE(doc[0] != doc[1]);
REQUIRE(doc[0] < doc[1]);
REQUIRE(doc[0] <= doc[1]);
REQUIRE_FALSE(doc[0] == doc[1]);
REQUIRE_FALSE(doc[0] > doc[1]);
REQUIRE_FALSE(doc[0] >= doc[1]);
}
SECTION("'abc' vs 'bcd'") {
doc.add("abc");
doc.add("bcd");
REQUIRE(doc[0] != doc[1]);
REQUIRE(doc[0] < doc[1]);
REQUIRE(doc[0] <= doc[1]);
REQUIRE_FALSE(doc[0] == doc[1]);
REQUIRE_FALSE(doc[0] > doc[1]);
REQUIRE_FALSE(doc[0] >= doc[1]);
}
}
TEST_CASE("ElementProxy::remove()") {
JsonDocument doc;
doc.add<JsonVariant>();
ElementProxy ep = doc[0];
SECTION("remove(int)") {
ep.add(1);
ep.add(2);
ep.add(3);
ep.remove(1);
REQUIRE(ep.as<std::string>() == "[1,3]");
}
SECTION("remove(const char *)") {
ep["a"] = 1;
ep["b"] = 2;
ep.remove("a");
REQUIRE(ep.as<std::string>() == "{\"b\":2}");
}
SECTION("remove(std::string)") {
ep["a"] = 1;
ep["b"] = 2;
ep.remove(std::string("b"));
REQUIRE(ep.as<std::string>() == "{\"a\":1}");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("remove(vla)") {
ep["a"] = 1;
ep["b"] = 2;
size_t i = 4;
char vla[i];
strcpy(vla, "b");
ep.remove(vla);
REQUIRE(ep.as<std::string>() == "{\"a\":1}");
}
#endif
}
TEST_CASE("ElementProxy::set()") {
JsonDocument doc;
ElementProxy ep = doc[0];
SECTION("set(int)") {
ep.set(42);
REQUIRE(doc.as<std::string>() == "[42]");
}
SECTION("set(const char*)") {
ep.set("world");
REQUIRE(doc.as<std::string>() == "[\"world\"]");
}
SECTION("set(char[])") {
char s[] = "world";
ep.set(s);
strcpy(s, "!!!!!");
REQUIRE(doc.as<std::string>() == "[\"world\"]");
}
}
TEST_CASE("ElementProxy::size()") {
JsonDocument doc;
doc.add<JsonVariant>();
ElementProxy ep = doc[0];
SECTION("returns 0") {
REQUIRE(ep.size() == 0);
}
SECTION("as an array, returns 2") {
ep.add(1);
ep.add(2);
REQUIRE(ep.size() == 2);
}
SECTION("as an object, returns 2") {
ep["a"] = 1;
ep["b"] = 2;
REQUIRE(ep.size() == 2);
}
}
TEST_CASE("ElementProxy::operator[]") {
JsonDocument doc;
ElementProxy ep = doc[1];
SECTION("set member") {
ep["world"] = 42;
REQUIRE(doc.as<std::string>() == "[null,{\"world\":42}]");
}
SECTION("set element") {
ep[2] = 42;
REQUIRE(doc.as<std::string>() == "[null,[null,null,42]]");
}
}
TEST_CASE("ElementProxy cast to JsonVariantConst") {
JsonDocument doc;
doc[0] = "world";
const ElementProxy ep = doc[0];
JsonVariantConst var = ep;
CHECK(var.as<std::string>() == "world");
}
TEST_CASE("ElementProxy cast to JsonVariant") {
JsonDocument doc;
doc[0] = "world";
ElementProxy ep = doc[0];
JsonVariant var = ep;
CHECK(var.as<std::string>() == "world");
var.set("toto");
CHECK(doc.as<std::string>() == "[\"toto\"]");
}

View File

@@ -0,0 +1,363 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
#define ARDUINOJSON_ENABLE_PROGMEM 1
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
typedef ArduinoJson::detail::MemberProxy<JsonDocument&, const char*>
MemberProxy;
TEST_CASE("MemberProxy::add()") {
JsonDocument doc;
MemberProxy mp = doc["hello"];
SECTION("add(int)") {
mp.add(42);
REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
}
SECTION("add(const char*)") {
mp.add("world");
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
}
}
TEST_CASE("MemberProxy::clear()") {
JsonDocument doc;
MemberProxy mp = doc["hello"];
SECTION("size goes back to zero") {
mp.add(42);
mp.clear();
REQUIRE(mp.size() == 0);
}
SECTION("isNull() return true") {
mp.add("hello");
mp.clear();
REQUIRE(mp.isNull() == true);
}
}
TEST_CASE("MemberProxy::operator==()") {
JsonDocument doc;
SECTION("1 vs 1") {
doc["a"] = 1;
doc["b"] = 1;
REQUIRE(doc["a"] <= doc["b"]);
REQUIRE(doc["a"] == doc["b"]);
REQUIRE(doc["a"] >= doc["b"]);
REQUIRE_FALSE(doc["a"] != doc["b"]);
REQUIRE_FALSE(doc["a"] < doc["b"]);
REQUIRE_FALSE(doc["a"] > doc["b"]);
}
SECTION("1 vs 2") {
doc["a"] = 1;
doc["b"] = 2;
REQUIRE(doc["a"] != doc["b"]);
REQUIRE(doc["a"] < doc["b"]);
REQUIRE(doc["a"] <= doc["b"]);
REQUIRE_FALSE(doc["a"] == doc["b"]);
REQUIRE_FALSE(doc["a"] > doc["b"]);
REQUIRE_FALSE(doc["a"] >= doc["b"]);
}
SECTION("'abc' vs 'bcd'") {
doc["a"] = "abc";
doc["b"] = "bcd";
REQUIRE(doc["a"] != doc["b"]);
REQUIRE(doc["a"] < doc["b"]);
REQUIRE(doc["a"] <= doc["b"]);
REQUIRE_FALSE(doc["a"] == doc["b"]);
REQUIRE_FALSE(doc["a"] > doc["b"]);
REQUIRE_FALSE(doc["a"] >= doc["b"]);
}
}
TEST_CASE("MemberProxy::containsKey()") {
JsonDocument doc;
MemberProxy mp = doc["hello"];
SECTION("containsKey(const char*)") {
mp["key"] = "value";
REQUIRE(mp.containsKey("key") == true);
REQUIRE(mp.containsKey("key") == true);
}
SECTION("containsKey(std::string)") {
mp["key"] = "value";
REQUIRE(mp.containsKey(std::string("key")) == true);
REQUIRE(mp.containsKey(std::string("key")) == true);
}
}
TEST_CASE("MemberProxy::operator|()") {
JsonDocument doc;
SECTION("const char*") {
doc["a"] = "hello";
REQUIRE((doc["a"] | "world") == std::string("hello"));
REQUIRE((doc["b"] | "world") == std::string("world"));
}
SECTION("Issue #1411") {
doc["sensor"] = "gps";
const char* test = "test"; // <- the literal must be captured in a variable
// to trigger the bug
const char* sensor = doc["sensor"] | test; // "gps"
REQUIRE(sensor == std::string("gps"));
}
SECTION("Issue #1415") {
JsonObject object = doc.to<JsonObject>();
object["hello"] = "world";
JsonDocument emptyDoc;
JsonObject anotherObject = object["hello"] | emptyDoc.to<JsonObject>();
REQUIRE(anotherObject.isNull() == false);
REQUIRE(anotherObject.size() == 0);
}
}
TEST_CASE("MemberProxy::remove()") {
JsonDocument doc;
MemberProxy mp = doc["hello"];
SECTION("remove(int)") {
mp.add(1);
mp.add(2);
mp.add(3);
mp.remove(1);
REQUIRE(mp.as<std::string>() == "[1,3]");
}
SECTION("remove(const char *)") {
mp["a"] = 1;
mp["b"] = 2;
mp.remove("a");
REQUIRE(mp.as<std::string>() == "{\"b\":2}");
}
SECTION("remove(std::string)") {
mp["a"] = 1;
mp["b"] = 2;
mp.remove(std::string("b"));
REQUIRE(mp.as<std::string>() == "{\"a\":1}");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("remove(vla)") {
mp["a"] = 1;
mp["b"] = 2;
size_t i = 4;
char vla[i];
strcpy(vla, "b");
mp.remove(vla);
REQUIRE(mp.as<std::string>() == "{\"a\":1}");
}
#endif
}
TEST_CASE("MemberProxy::set()") {
JsonDocument doc;
MemberProxy mp = doc["hello"];
SECTION("set(int)") {
mp.set(42);
REQUIRE(doc.as<std::string>() == "{\"hello\":42}");
}
SECTION("set(const char*)") {
mp.set("world");
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
}
SECTION("set(char[])") { // issue #1191
char s[] = "world";
mp.set(s);
strcpy(s, "!!!!!");
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
}
}
TEST_CASE("MemberProxy::size()") {
JsonDocument doc;
MemberProxy mp = doc["hello"];
SECTION("returns 0") {
REQUIRE(mp.size() == 0);
}
SECTION("as an array, return 2") {
mp.add(1);
mp.add(2);
REQUIRE(mp.size() == 2);
}
SECTION("as an object, return 2") {
mp["a"] = 1;
mp["b"] = 2;
REQUIRE(mp.size() == 2);
}
}
TEST_CASE("MemberProxy::operator[]") {
JsonDocument doc;
MemberProxy mp = doc["hello"];
SECTION("set member") {
mp["world"] = 42;
REQUIRE(doc.as<std::string>() == "{\"hello\":{\"world\":42}}");
}
SECTION("set element") {
mp[2] = 42;
REQUIRE(doc.as<std::string>() == "{\"hello\":[null,null,42]}");
}
}
TEST_CASE("MemberProxy cast to JsonVariantConst") {
JsonDocument doc;
doc["hello"] = "world";
const MemberProxy mp = doc["hello"];
JsonVariantConst var = mp;
CHECK(var.as<std::string>() == "world");
}
TEST_CASE("MemberProxy cast to JsonVariant") {
JsonDocument doc;
doc["hello"] = "world";
MemberProxy mp = doc["hello"];
JsonVariant var = mp;
CHECK(var.as<std::string>() == "world");
var.set("toto");
CHECK(doc.as<std::string>() == "{\"hello\":\"toto\"}");
}
TEST_CASE("Deduplicate keys") {
SpyingAllocator spy;
JsonDocument doc(&spy);
SECTION("std::string") {
doc[0][std::string("example")] = 1;
doc[1][std::string("example")] = 2;
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
CHECK(key1 == key2);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("example")),
});
}
SECTION("char*") {
char key[] = "example";
doc[0][key] = 1;
doc[1][key] = 2;
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
CHECK(key1 == key2);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("example")),
});
}
SECTION("Arduino String") {
doc[0][String("example")] = 1;
doc[1][String("example")] = 2;
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
CHECK(key1 == key2);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("example")),
});
}
SECTION("Flash string") {
doc[0][F("example")] = 1;
doc[1][F("example")] = 2;
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
CHECK(key1 == key2);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("example")),
});
}
}
TEST_CASE("MemberProxy under memory constraints") {
KillswitchAllocator killswitch;
SpyingAllocator spy(&killswitch);
JsonDocument doc(&spy);
SECTION("key allocation fails") {
killswitch.on();
doc[std::string("hello")] = "world";
REQUIRE(doc.is<JsonObject>());
REQUIRE(doc.size() == 0);
REQUIRE(doc.overflowed() == true);
REQUIRE(spy.log() == AllocatorLog{
AllocateFail(sizeofString("hello")),
});
}
}

View File

@@ -0,0 +1,104 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
#define ARDUINOJSON_ENABLE_PROGMEM 1
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
TEST_CASE("JsonDocument::add(T)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
SECTION("integer") {
doc.add(42);
REQUIRE(doc.as<std::string>() == "[42]");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("const char*") {
doc.add("hello");
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("std::string") {
doc.add(std::string("example"));
doc.add(std::string("example"));
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("example")),
});
}
SECTION("char*") {
char value[] = "example";
doc.add(value);
doc.add(value);
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("example")),
});
}
SECTION("Arduino String") {
doc.add(String("example"));
doc.add(String("example"));
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("example")),
});
}
SECTION("Flash string") {
doc.add(F("example"));
doc.add(F("example"));
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("example")),
});
}
}
TEST_CASE("JsonDocument::add<T>()") {
JsonDocument doc;
SECTION("JsonArray") {
JsonArray array = doc.add<JsonArray>();
array.add(1);
array.add(2);
REQUIRE(doc.as<std::string>() == "[[1,2]]");
}
SECTION("JsonObject") {
JsonObject object = doc.add<JsonObject>();
object["hello"] = "world";
REQUIRE(doc.as<std::string>() == "[{\"hello\":\"world\"}]");
}
SECTION("JsonVariant") {
JsonVariant variant = doc.add<JsonVariant>();
variant.set(42);
REQUIRE(doc.as<std::string>() == "[42]");
}
}

View File

@@ -0,0 +1,134 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
TEST_CASE("JsonDocument assignment") {
SpyingAllocator spyingAllocator;
SECTION("Copy assignment same capacity") {
JsonDocument doc1(&spyingAllocator);
deserializeJson(doc1, "{\"hello\":\"world\"}");
JsonDocument doc2(&spyingAllocator);
spyingAllocator.clearLog();
doc2 = doc1;
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("Copy assignment reallocates when capacity is smaller") {
JsonDocument doc1(&spyingAllocator);
deserializeJson(doc1, "[{\"hello\":\"world\"}]");
JsonDocument doc2(&spyingAllocator);
spyingAllocator.clearLog();
doc2 = doc1;
REQUIRE(doc2.as<std::string>() == "[{\"hello\":\"world\"}]");
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
});
}
SECTION("Copy assignment reallocates when capacity is larger") {
JsonDocument doc1(&spyingAllocator);
deserializeJson(doc1, "{\"hello\":\"world\"}");
JsonDocument doc2(&spyingAllocator);
spyingAllocator.clearLog();
doc2 = doc1;
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("Move assign") {
{
JsonDocument doc1(&spyingAllocator);
doc1[std::string("hello")] = std::string("world");
JsonDocument doc2(&spyingAllocator);
doc2 = std::move(doc1);
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(doc1.as<std::string>() == "null");
}
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
Deallocate(sizeofString("hello")),
Deallocate(sizeofString("world")),
Deallocate(sizeofPool()),
});
}
SECTION("Assign from JsonObject") {
JsonDocument doc1;
JsonObject obj = doc1.to<JsonObject>();
obj["hello"] = "world";
JsonDocument doc2;
doc2 = obj;
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
}
SECTION("Assign from JsonArray") {
JsonDocument doc1;
JsonArray arr = doc1.to<JsonArray>();
arr.add("hello");
JsonDocument doc2;
doc2 = arr;
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
}
SECTION("Assign from JsonVariant") {
JsonDocument doc1;
deserializeJson(doc1, "42");
JsonDocument doc2;
doc2 = doc1.as<JsonVariant>();
REQUIRE(doc2.as<std::string>() == "42");
}
SECTION("Assign from MemberProxy") {
JsonDocument doc1;
doc1["value"] = 42;
JsonDocument doc2;
doc2 = doc1["value"];
REQUIRE(doc2.as<std::string>() == "42");
}
SECTION("Assign from ElementProxy") {
JsonDocument doc1;
doc1[0] = 42;
JsonDocument doc2;
doc2 = doc1[0];
REQUIRE(doc2.as<std::string>() == "42");
}
}

View File

@@ -0,0 +1,18 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
TEST_CASE("Implicit cast to JsonVariant") {
JsonDocument doc;
doc["hello"] = "world";
JsonVariant var = doc;
CHECK(var.as<std::string>() == "{\"hello\":\"world\"}");
}

View File

@@ -0,0 +1,29 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonDocument::operator==(const JsonDocument&)") {
JsonDocument doc1;
JsonDocument doc2;
SECTION("Empty") {
REQUIRE(doc1 == doc2);
REQUIRE_FALSE(doc1 != doc2);
}
SECTION("With same object") {
doc1["hello"] = "world";
doc2["hello"] = "world";
REQUIRE(doc1 == doc2);
REQUIRE_FALSE(doc1 != doc2);
}
SECTION("With different object") {
doc1["hello"] = "world";
doc2["world"] = "hello";
REQUIRE_FALSE(doc1 == doc2);
REQUIRE(doc1 != doc2);
}
}

View File

@@ -0,0 +1,120 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
using ArduinoJson::detail::addPadding;
TEST_CASE("JsonDocument constructor") {
SpyingAllocator spyingAllocator;
SECTION("JsonDocument(size_t)") {
{ JsonDocument doc(&spyingAllocator); }
REQUIRE(spyingAllocator.log() == AllocatorLog{});
}
SECTION("JsonDocument(const JsonDocument&)") {
{
JsonDocument doc1(&spyingAllocator);
doc1.set(std::string("The size of this string is 32!!"));
JsonDocument doc2(doc1);
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
}
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofStringBuffer()),
Allocate(sizeofStringBuffer()),
Deallocate(sizeofStringBuffer()),
Deallocate(sizeofStringBuffer()),
});
}
SECTION("JsonDocument(JsonDocument&&)") {
{
JsonDocument doc1(&spyingAllocator);
doc1.set(std::string("The size of this string is 32!!"));
JsonDocument doc2(std::move(doc1));
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
REQUIRE(doc1.as<std::string>() == "null");
}
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofStringBuffer()),
Deallocate(sizeofStringBuffer()),
});
}
SECTION("JsonDocument(JsonObject, Allocator*)") {
JsonDocument doc1;
JsonObject obj = doc1.to<JsonObject>();
obj["hello"] = "world";
JsonDocument doc2(obj, &spyingAllocator);
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("JsonDocument(JsonObject)") {
JsonDocument doc1;
JsonObject obj = doc1.to<JsonObject>();
obj["hello"] = "world";
JsonDocument doc2(obj);
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
}
SECTION("JsonDocument(JsonArray, Allocator*)") {
JsonDocument doc1;
JsonArray arr = doc1.to<JsonArray>();
arr.add("hello");
JsonDocument doc2(arr, &spyingAllocator);
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("JsonDocument(JsonArray)") {
JsonDocument doc1;
JsonArray arr = doc1.to<JsonArray>();
arr.add("hello");
JsonDocument doc2(arr);
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
}
SECTION("JsonDocument(JsonVariant, Allocator*)") {
JsonDocument doc1;
deserializeJson(doc1, "\"hello\"");
JsonDocument doc2(doc1.as<JsonVariant>(), &spyingAllocator);
REQUIRE(doc2.as<std::string>() == "hello");
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello")),
});
}
SECTION("JsonDocument(JsonVariant)") {
JsonDocument doc1;
deserializeJson(doc1, "\"hello\"");
JsonDocument doc2(doc1.as<JsonVariant>());
REQUIRE(doc2.as<std::string>() == "hello");
}
}

View File

@@ -0,0 +1,44 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonDocument::containsKey()") {
JsonDocument doc;
SECTION("returns true on object") {
doc["hello"] = "world";
REQUIRE(doc.containsKey("hello") == true);
}
SECTION("returns true when value is null") {
doc["hello"] = static_cast<const char*>(0);
REQUIRE(doc.containsKey("hello") == true);
}
SECTION("returns true when key is a std::string") {
doc["hello"] = "world";
REQUIRE(doc.containsKey(std::string("hello")) == true);
}
SECTION("returns false on object") {
doc["world"] = "hello";
REQUIRE(doc.containsKey("hello") == false);
}
SECTION("returns false on array") {
doc.add("hello");
REQUIRE(doc.containsKey("hello") == false);
}
SECTION("returns false on null") {
REQUIRE(doc.containsKey("hello") == false);
}
}

View File

@@ -0,0 +1,39 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonDocument::isNull()") {
JsonDocument doc;
SECTION("returns true if uninitialized") {
REQUIRE(doc.isNull() == true);
}
SECTION("returns false after to<JsonObject>()") {
doc.to<JsonObject>();
REQUIRE(doc.isNull() == false);
}
SECTION("returns false after to<JsonArray>()") {
doc.to<JsonArray>();
REQUIRE(doc.isNull() == false);
}
SECTION("returns true after to<JsonVariant>()") {
REQUIRE(doc.isNull() == true);
}
SECTION("returns false after set()") {
doc.to<JsonVariant>().set(42);
REQUIRE(doc.isNull() == false);
}
SECTION("returns true after clear()") {
doc.to<JsonObject>();
doc.clear();
REQUIRE(doc.isNull() == true);
}
}

View File

@@ -0,0 +1,58 @@
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("Issue #1120") {
JsonDocument doc;
constexpr char str[] =
"{\"contents\":[{\"module\":\"Packet\"},{\"module\":\"Analog\"}]}";
deserializeJson(doc, str);
SECTION("MemberProxy<std::string>::isNull()") {
SECTION("returns false") {
auto value = doc[std::string("contents")];
CHECK(value.isNull() == false);
}
SECTION("returns true") {
auto value = doc[std::string("zontents")];
CHECK(value.isNull() == true);
}
}
SECTION("ElementProxy<MemberProxy<const char*> >::isNull()") {
SECTION("returns false") { // Issue #1120
auto value = doc["contents"][1];
CHECK(value.isNull() == false);
}
SECTION("returns true") {
auto value = doc["contents"][2];
CHECK(value.isNull() == true);
}
}
SECTION("MemberProxy<ElementProxy<MemberProxy>, const char*>::isNull()") {
SECTION("returns false") {
auto value = doc["contents"][1]["module"];
CHECK(value.isNull() == false);
}
SECTION("returns true") {
auto value = doc["contents"][1]["zodule"];
CHECK(value.isNull() == true);
}
}
SECTION("MemberProxy<ElementProxy<MemberProxy>, std::string>::isNull()") {
SECTION("returns false") {
auto value = doc["contents"][1][std::string("module")];
CHECK(value.isNull() == false);
}
SECTION("returns true") {
auto value = doc["contents"][1][std::string("zodule")];
CHECK(value.isNull() == true);
}
}
}

View File

@@ -0,0 +1,30 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonDocument::nesting()") {
JsonDocument doc;
SECTION("return 0 if uninitialized") {
REQUIRE(doc.nesting() == 0);
}
SECTION("returns 0 for string") {
JsonVariant var = doc.to<JsonVariant>();
var.set("hello");
REQUIRE(doc.nesting() == 0);
}
SECTION("returns 1 for empty object") {
doc.to<JsonObject>();
REQUIRE(doc.nesting() == 1);
}
SECTION("returns 1 for empty array") {
doc.to<JsonArray>();
REQUIRE(doc.nesting() == 1);
}
}

View File

@@ -0,0 +1,95 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
TEST_CASE("JsonDocument::overflowed()") {
TimebombAllocator timebomb(10);
JsonDocument doc(&timebomb);
SECTION("returns false on a fresh object") {
timebomb.setCountdown(0);
CHECK(doc.overflowed() == false);
}
SECTION("returns true after a failed insertion") {
timebomb.setCountdown(0);
doc.add(0);
CHECK(doc.overflowed() == true);
}
SECTION("returns false after successful insertion") {
timebomb.setCountdown(2);
doc.add(0);
CHECK(doc.overflowed() == false);
}
SECTION("returns true after a failed string copy") {
timebomb.setCountdown(0);
doc.add(std::string("example"));
CHECK(doc.overflowed() == true);
}
SECTION("returns false after a successful string copy") {
timebomb.setCountdown(3);
doc.add(std::string("example"));
CHECK(doc.overflowed() == false);
}
SECTION("returns true after a failed member add") {
timebomb.setCountdown(0);
doc["example"] = true;
CHECK(doc.overflowed() == true);
}
SECTION("returns true after a failed deserialization") {
timebomb.setCountdown(0);
deserializeJson(doc, "[1, 2]");
CHECK(doc.overflowed() == true);
}
SECTION("returns false after a successful deserialization") {
timebomb.setCountdown(3);
deserializeJson(doc, "[\"example\"]");
CHECK(doc.overflowed() == false);
}
SECTION("returns false after clear()") {
timebomb.setCountdown(0);
doc.add(0);
doc.clear();
CHECK(doc.overflowed() == false);
}
SECTION("remains false after shrinkToFit()") {
timebomb.setCountdown(2);
doc.add(0);
timebomb.setCountdown(2);
doc.shrinkToFit();
CHECK(doc.overflowed() == false);
}
SECTION("remains true after shrinkToFit()") {
timebomb.setCountdown(0);
doc.add(0);
timebomb.setCountdown(2);
doc.shrinkToFit();
CHECK(doc.overflowed() == true);
}
SECTION("returns false when string length doesn't overflow") {
auto maxLength = ArduinoJson::detail::StringNode::maxLength;
CHECK(doc.set(std::string(maxLength, 'a')) == true);
CHECK(doc.overflowed() == false);
}
SECTION("returns true when string length overflows") {
auto maxLength = ArduinoJson::detail::StringNode::maxLength;
CHECK(doc.set(std::string(maxLength + 1, 'a')) == false);
CHECK(doc.overflowed() == true);
}
}

View File

@@ -0,0 +1,52 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonDocument::remove()") {
JsonDocument doc;
SECTION("remove(int)") {
doc.add(1);
doc.add(2);
doc.add(3);
doc.remove(1);
REQUIRE(doc.as<std::string>() == "[1,3]");
}
SECTION("remove(const char *)") {
doc["a"] = 1;
doc["b"] = 2;
doc.remove("a");
REQUIRE(doc.as<std::string>() == "{\"b\":2}");
}
SECTION("remove(std::string)") {
doc["a"] = 1;
doc["b"] = 2;
doc.remove(std::string("b"));
REQUIRE(doc.as<std::string>() == "{\"a\":1}");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("remove(vla)") {
doc["a"] = 1;
doc["b"] = 2;
size_t i = 4;
char vla[i];
strcpy(vla, "b");
doc.remove(vla);
REQUIRE(doc.as<std::string>() == "{\"a\":1}");
}
#endif
}

View File

@@ -0,0 +1,183 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <stdlib.h> // malloc, free
#include <string>
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
class ArmoredAllocator : public Allocator {
public:
virtual ~ArmoredAllocator() {}
void* allocate(size_t size) override {
return malloc(size);
}
void deallocate(void* ptr) override {
free(ptr);
}
void* reallocate(void* ptr, size_t new_size) override {
// don't call realloc, instead alloc a new buffer and erase the old one
// this way we make sure we support relocation
void* new_ptr = malloc(new_size);
memset(new_ptr, '#', new_size); // erase
if (ptr) {
memcpy(new_ptr, ptr, std::min(new_size, new_size));
free(ptr);
}
return new_ptr;
}
};
TEST_CASE("JsonDocument::shrinkToFit()") {
ArmoredAllocator armoredAllocator;
SpyingAllocator spyingAllocator(&armoredAllocator);
JsonDocument doc(&spyingAllocator);
SECTION("null") {
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "null");
REQUIRE(spyingAllocator.log() == AllocatorLog{});
}
SECTION("empty object") {
deserializeJson(doc, "{}");
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{}");
REQUIRE(spyingAllocator.log() == AllocatorLog{});
}
SECTION("empty array") {
deserializeJson(doc, "[]");
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[]");
REQUIRE(spyingAllocator.log() == AllocatorLog{});
}
SECTION("linked string") {
doc.set("hello");
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "hello");
REQUIRE(spyingAllocator.log() == AllocatorLog{});
}
SECTION("owned string") {
doc.set(std::string("abcdefg"));
REQUIRE(doc.as<std::string>() == "abcdefg");
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "abcdefg");
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("abcdefg")),
});
}
SECTION("raw string") {
doc.set(serialized("[{},12]"));
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[{},12]");
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("[{},12]")),
});
}
SECTION("linked key") {
doc["key"] = 42;
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"key\":42}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofObject(1)),
});
}
SECTION("owned key") {
doc[std::string("abcdefg")] = 42;
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"abcdefg\":42}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofString("abcdefg")),
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofObject(1)),
});
}
SECTION("linked string in array") {
doc.add("hello");
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofArray(1)),
});
}
SECTION("owned string in array") {
doc.add(std::string("abcdefg"));
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[\"abcdefg\"]");
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("abcdefg")),
Reallocate(sizeofPool(), sizeofArray(1)),
});
}
SECTION("linked string in object") {
doc["key"] = "hello";
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"key\":\"hello\"}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofObject(1)),
});
}
SECTION("owned string in object") {
doc["key"] = std::string("abcdefg");
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"key\":\"abcdefg\"}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("abcdefg")),
Reallocate(sizeofPool(), sizeofPool(1)),
});
}
}

View File

@@ -0,0 +1,28 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonDocument::size()") {
JsonDocument doc;
SECTION("returns 0") {
REQUIRE(doc.size() == 0);
}
SECTION("as an array, return 2") {
doc.add(1);
doc.add(2);
REQUIRE(doc.size() == 2);
}
SECTION("as an object, return 2") {
doc["a"] = 1;
doc["b"] = 2;
REQUIRE(doc.size() == 2);
}
}

View File

@@ -0,0 +1,53 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonDocument::operator[]") {
JsonDocument doc;
const JsonDocument& cdoc = doc;
SECTION("object") {
deserializeJson(doc, "{\"hello\":\"world\"}");
SECTION("const char*") {
REQUIRE(doc["hello"] == "world");
REQUIRE(cdoc["hello"] == "world");
}
SECTION("std::string") {
REQUIRE(doc[std::string("hello")] == "world");
REQUIRE(cdoc[std::string("hello")] == "world");
}
SECTION("supports operator|") {
REQUIRE((doc["hello"] | "nope") == std::string("world"));
REQUIRE((doc["world"] | "nope") == std::string("nope"));
}
}
SECTION("array") {
deserializeJson(doc, "[\"hello\",\"world\"]");
REQUIRE(doc[1] == "world");
REQUIRE(cdoc[1] == "world");
}
}
TEST_CASE("JsonDocument automatically promotes to object") {
JsonDocument doc;
doc["one"]["two"]["three"] = 4;
REQUIRE(doc["one"]["two"]["three"] == 4);
}
TEST_CASE("JsonDocument automatically promotes to array") {
JsonDocument doc;
doc[2] = 2;
REQUIRE(doc.as<std::string>() == "[null,null,2]");
}

View File

@@ -0,0 +1,25 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
#include <utility>
using namespace std;
TEST_CASE("std::swap") {
SECTION("JsonDocument*") {
JsonDocument *p1, *p2;
swap(p1, p2); // issue #1678
}
SECTION("JsonDocument") {
JsonDocument doc1, doc2;
doc1.set("hello");
doc2.set("world");
swap(doc1, doc2);
CHECK(doc1.as<string>() == "world");
CHECK(doc2.as<string>() == "hello");
}
}