mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-04 22:26:39 +02:00
* Added DynamicJsonArray and StaticJsonArray * Added DynamicJsonObject and StaticJsonObject * Added DynamicJsonVariant and StaticJsonVariant * Added deserializeJson() * Removed JsonBuffer::parseArray(), parseObject() and parse() * Removed JsonBuffer::createArray() and createObject()
110 lines
2.6 KiB
C++
110 lines
2.6 KiB
C++
// ArduinoJson - arduinojson.org
|
|
// Copyright Benoit Blanchon 2014-2018
|
|
// MIT License
|
|
|
|
#include <ArduinoJson.h>
|
|
#include <catch.hpp>
|
|
|
|
TEST_CASE("JsonArray::add()") {
|
|
DynamicJsonArray _array;
|
|
|
|
SECTION("int") {
|
|
_array.add(123);
|
|
REQUIRE(123 == _array[0].as<int>());
|
|
REQUIRE(_array[0].is<int>());
|
|
REQUIRE(_array[0].is<double>());
|
|
}
|
|
|
|
SECTION("double") {
|
|
_array.add(123.45);
|
|
REQUIRE(123.45 == _array[0].as<double>());
|
|
REQUIRE(_array[0].is<double>());
|
|
REQUIRE_FALSE(_array[0].is<bool>());
|
|
}
|
|
|
|
SECTION("bool") {
|
|
_array.add(true);
|
|
REQUIRE(true == _array[0].as<bool>());
|
|
REQUIRE(_array[0].is<bool>());
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
}
|
|
|
|
SECTION("const char*") {
|
|
const char* str = "hello";
|
|
_array.add(str);
|
|
REQUIRE(str == _array[0].as<std::string>());
|
|
REQUIRE(_array[0].is<const char*>());
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
}
|
|
|
|
SECTION("nested array") {
|
|
DynamicJsonArray arr;
|
|
|
|
_array.add(arr);
|
|
|
|
REQUIRE(&arr == &_array[0].as<JsonArray&>());
|
|
REQUIRE(_array[0].is<JsonArray&>());
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
}
|
|
|
|
SECTION("nested object") {
|
|
DynamicJsonObject obj;
|
|
|
|
_array.add(obj);
|
|
|
|
REQUIRE(&obj == &_array[0].as<JsonObject&>());
|
|
REQUIRE(_array[0].is<JsonObject&>());
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
}
|
|
|
|
SECTION("array subscript") {
|
|
const char* str = "hello";
|
|
DynamicJsonArray arr;
|
|
arr.add(str);
|
|
|
|
_array.add(arr[0]);
|
|
|
|
REQUIRE(str == _array[0]);
|
|
}
|
|
|
|
SECTION("object subscript") {
|
|
const char* str = "hello";
|
|
DynamicJsonObject obj;
|
|
obj["x"] = str;
|
|
|
|
_array.add(obj["x"]);
|
|
|
|
REQUIRE(str == _array[0]);
|
|
}
|
|
|
|
SECTION("should not duplicate const char*") {
|
|
_array.add("world");
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
|
REQUIRE(expectedSize == _array.memoryUsage());
|
|
}
|
|
|
|
SECTION("should duplicate char*") {
|
|
_array.add(const_cast<char*>("world"));
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
|
REQUIRE(expectedSize == _array.memoryUsage());
|
|
}
|
|
|
|
SECTION("should duplicate std::string") {
|
|
_array.add(std::string("world"));
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
|
REQUIRE(expectedSize == _array.memoryUsage());
|
|
}
|
|
|
|
SECTION("should not duplicate RawJson(const char*)") {
|
|
_array.add(RawJson("{}"));
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
|
REQUIRE(expectedSize == _array.memoryUsage());
|
|
}
|
|
|
|
SECTION("should duplicate RawJson(char*)") {
|
|
_array.add(RawJson(const_cast<char*>("{}")));
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 3;
|
|
REQUIRE(expectedSize == _array.memoryUsage());
|
|
}
|
|
}
|