mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-15 19:42:12 +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()
141 lines
3.5 KiB
C++
141 lines
3.5 KiB
C++
// ArduinoJson - arduinojson.org
|
|
// Copyright Benoit Blanchon 2014-2018
|
|
// MIT License
|
|
|
|
#define ARDUINOJSON_ENABLE_DEPRECATED 1
|
|
|
|
#include <ArduinoJson.h>
|
|
#include <catch.hpp>
|
|
|
|
#if defined(__clang__)
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
#elif defined(__GNUC__)
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
#elif defined(_MSC_VER)
|
|
#pragma warning(disable : 4996)
|
|
#endif
|
|
|
|
TEST_CASE("Deprecated functions") {
|
|
SECTION("JsonVariant::asArray()") {
|
|
DynamicJsonArray array;
|
|
JsonVariant variant = array;
|
|
REQUIRE(variant.asArray().success());
|
|
}
|
|
|
|
SECTION("JsonVariant::asObject()") {
|
|
DynamicJsonObject obj;
|
|
JsonVariant variant = obj;
|
|
REQUIRE(variant.asObject().success());
|
|
}
|
|
|
|
SECTION("JsonVariant::asString()") {
|
|
JsonVariant variant = "hello";
|
|
REQUIRE(std::string("hello") == variant.asString());
|
|
}
|
|
|
|
SECTION("JsonArray::removeAt()") {
|
|
DynamicJsonArray arr;
|
|
arr.removeAt(0);
|
|
}
|
|
|
|
SECTION("JsonVariant::JsonVariant(float, uint8_t)") {
|
|
JsonVariant variant(3.14f, 2);
|
|
REQUIRE(variant == 3.14f);
|
|
}
|
|
|
|
SECTION("JsonVariant::JsonVariant(double, uint8_t)") {
|
|
JsonVariant variant(3.14, 2);
|
|
REQUIRE(variant == 3.14);
|
|
}
|
|
|
|
SECTION("float_with_n_digits()") {
|
|
JsonVariant variant = float_with_n_digits(3.14f, 4);
|
|
REQUIRE(variant == 3.14f);
|
|
}
|
|
|
|
SECTION("double_with_n_digits()") {
|
|
JsonVariant variant = double_with_n_digits(3.14f, 4);
|
|
REQUIRE(variant == 3.14f);
|
|
}
|
|
|
|
SECTION("JsonArraySubscript::set(double, uint8_t)") {
|
|
DynamicJsonArray arr;
|
|
arr.add(666);
|
|
arr[0].set(123.45, 2);
|
|
REQUIRE(123.45 == arr[0].as<double>());
|
|
REQUIRE(true == arr[0].is<double>());
|
|
REQUIRE(false == arr[0].is<int>());
|
|
}
|
|
|
|
SECTION("JsonArray::add(double, uint8_t)") {
|
|
DynamicJsonArray arr;
|
|
arr.add(3.14159265358979323846, 4);
|
|
}
|
|
|
|
SECTION("JsonArray::add(float, uint8_t)") {
|
|
DynamicJsonArray arr;
|
|
arr.add(3.14159265358979323846f, 4);
|
|
}
|
|
|
|
SECTION("JsonObject::set(unsigned char[], double, uint8_t)") {
|
|
unsigned char key[] = "hello";
|
|
|
|
DynamicJsonObject obj;
|
|
obj.set(key, 3.14, 2);
|
|
|
|
REQUIRE(3.14 == obj["hello"]);
|
|
}
|
|
|
|
SECTION("JsonObject::set(const char*, double, uint8_t)") {
|
|
DynamicJsonObject obj;
|
|
obj.set("hello", 123.45, 2);
|
|
|
|
REQUIRE(123.45 == obj["hello"].as<double>());
|
|
REQUIRE(obj["hello"].is<double>());
|
|
REQUIRE_FALSE(obj["hello"].is<long>());
|
|
}
|
|
|
|
SECTION("JsonObjectSubscript::set(double, uint8_t)") {
|
|
DynamicJsonObject obj;
|
|
obj["hello"].set(123.45, 2);
|
|
|
|
REQUIRE(true == obj["hello"].is<double>());
|
|
REQUIRE(false == obj["hello"].is<long>());
|
|
REQUIRE(123.45 == obj["hello"].as<double>());
|
|
}
|
|
}
|
|
|
|
TEST_CASE("DynamicJsonBuffer::strdup()") {
|
|
DynamicJsonBuffer buffer;
|
|
|
|
SECTION("char*") {
|
|
char original[] = "hello";
|
|
const char* copy = buffer.strdup(original);
|
|
strcpy(original, "world");
|
|
REQUIRE(std::string("hello") == copy);
|
|
}
|
|
|
|
SECTION("unsigned char*") {
|
|
unsigned char value[] = "world";
|
|
|
|
DynamicJsonBuffer jsonBuffer;
|
|
const char* dup = jsonBuffer.strdup(value);
|
|
|
|
REQUIRE(static_cast<const void*>(value) != static_cast<const void*>(dup));
|
|
REQUIRE(std::string("world") == dup);
|
|
}
|
|
|
|
SECTION("std::string") {
|
|
std::string original("hello");
|
|
const char* copy = buffer.strdup(original);
|
|
original[0] = 'w';
|
|
REQUIRE(std::string("hello") == copy);
|
|
}
|
|
|
|
SECTION("NULL") {
|
|
const char* original = NULL;
|
|
const char* copy = buffer.strdup(original);
|
|
REQUIRE(0 == copy);
|
|
}
|
|
}
|