2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2017-01-06 21:07:34 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2017
|
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
|
|
|
TEST_CASE("JsonArray::add()") {
|
2015-09-28 22:14:50 +02:00
|
|
|
DynamicJsonBuffer _jsonBuffer;
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonArray& _array = _jsonBuffer.createArray();
|
|
|
|
|
|
|
|
SECTION("SizeIncreased_WhenValuesAreAdded") {
|
|
|
|
_array.add("hello");
|
|
|
|
REQUIRE(1U == _array.size());
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("int") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_array.add(123);
|
|
|
|
REQUIRE(123 == _array[0].as<int>());
|
|
|
|
REQUIRE(_array[0].is<int>());
|
2017-05-20 09:06:53 +02:00
|
|
|
REQUIRE(_array[0].is<double>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("double") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_array.add(123.45);
|
|
|
|
REQUIRE(123.45 == _array[0].as<double>());
|
|
|
|
REQUIRE(_array[0].is<double>());
|
2017-05-20 09:06:53 +02:00
|
|
|
REQUIRE_FALSE(_array[0].is<bool>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("bool") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_array.add(true);
|
|
|
|
REQUIRE(true == _array[0].as<bool>());
|
|
|
|
REQUIRE(_array[0].is<bool>());
|
|
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("const char*") {
|
2017-04-18 18:22:24 +02:00
|
|
|
const char* str = "hello";
|
|
|
|
_array.add(str);
|
|
|
|
REQUIRE(str == _array[0].as<const char*>());
|
|
|
|
REQUIRE(_array[0].is<const char*>());
|
|
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("nested array") {
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonArray& arr = _jsonBuffer.createArray();
|
|
|
|
|
|
|
|
_array.add(arr);
|
|
|
|
|
|
|
|
REQUIRE(&arr == &_array[0].as<JsonArray&>());
|
|
|
|
REQUIRE(_array[0].is<JsonArray&>());
|
|
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("nested object") {
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonObject& obj = _jsonBuffer.createObject();
|
|
|
|
|
|
|
|
_array.add(obj);
|
|
|
|
|
|
|
|
REQUIRE(&obj == &_array[0].as<JsonObject&>());
|
|
|
|
REQUIRE(_array[0].is<JsonObject&>());
|
|
|
|
REQUIRE_FALSE(_array[0].is<int>());
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("array subscript") {
|
2017-04-18 18:22:24 +02:00
|
|
|
const char* str = "hello";
|
|
|
|
JsonArray& arr = _jsonBuffer.createArray();
|
|
|
|
arr.add(str);
|
|
|
|
|
|
|
|
_array.add(arr[0]);
|
|
|
|
|
|
|
|
REQUIRE(str == _array[0]);
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("object subscript") {
|
2017-04-18 18:22:24 +02:00
|
|
|
const char* str = "hello";
|
|
|
|
JsonObject& obj = _jsonBuffer.createObject();
|
|
|
|
obj["x"] = str;
|
|
|
|
|
|
|
|
_array.add(obj["x"]);
|
|
|
|
|
|
|
|
REQUIRE(str == _array[0]);
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
}
|