2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2019-02-15 13:34:37 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2019
|
2015-09-28 22:14:50 +02:00
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
using namespace Catch::Matchers;
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
TEST_CASE("JsonArray::set()") {
|
2015-09-28 22:14:50 +02:00
|
|
|
DynamicJsonBuffer _jsonBuffer;
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonArray& _array = _jsonBuffer.createArray();
|
|
|
|
_array.add(0);
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("int") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_array.set(0, 123);
|
|
|
|
REQUIRE(123 == _array[0].as<int>());
|
|
|
|
REQUIRE(_array[0].is<int>());
|
2017-05-20 09:06:53 +02:00
|
|
|
REQUIRE_FALSE(_array[0].is<bool>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("double") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_array.set(0, 123.45);
|
|
|
|
REQUIRE(123.45 == _array[0].as<double>());
|
|
|
|
REQUIRE(_array[0].is<double>());
|
|
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("bool") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_array.set(0, true);
|
|
|
|
REQUIRE(true == _array[0].as<bool>());
|
|
|
|
REQUIRE(_array[0].is<bool>());
|
|
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("const char*") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_array.set(0, "hello");
|
|
|
|
REQUIRE_THAT(_array[0].as<const char*>(), Equals("hello"));
|
|
|
|
REQUIRE(_array[0].is<const char*>());
|
|
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("nested array") {
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonArray& arr = _jsonBuffer.createArray();
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
_array.set(0, arr);
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(&arr == &_array[0].as<JsonArray&>());
|
|
|
|
REQUIRE(_array[0].is<JsonArray&>());
|
|
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("nested object") {
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonObject& obj = _jsonBuffer.createObject();
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
_array.set(0, obj);
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(&obj == &_array[0].as<JsonObject&>());
|
|
|
|
REQUIRE(_array[0].is<JsonObject&>());
|
|
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("array subscript") {
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonArray& arr = _jsonBuffer.createArray();
|
|
|
|
arr.add("hello");
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
_array.set(0, arr[0]);
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE_THAT(_array[0].as<char*>(), Equals("hello"));
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("object subscript") {
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonObject& obj = _jsonBuffer.createObject();
|
|
|
|
obj["x"] = "hello";
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
_array.set(0, obj["x"]);
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE_THAT(_array[0].as<char*>(), Equals("hello"));
|
|
|
|
}
|
2018-01-14 13:46:28 +01:00
|
|
|
|
|
|
|
SECTION("should not duplicate const char*") {
|
|
|
|
_array.set(0, "world");
|
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
|
|
|
REQUIRE(expectedSize == _jsonBuffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate char*") {
|
|
|
|
_array.set(0, const_cast<char*>("world"));
|
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
|
|
|
REQUIRE(expectedSize == _jsonBuffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate std::string") {
|
|
|
|
_array.set(0, std::string("world"));
|
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
|
|
|
REQUIRE(expectedSize == _jsonBuffer.size());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
}
|