2017-01-06 21:07:34 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2017
|
2015-09-28 22:14:50 +02:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
2017-03-25 22:05:06 +01:00
|
|
|
// https://bblanchon.github.io/ArduinoJson/
|
2016-01-07 22:35:12 +01:00
|
|
|
// If you like this project, please add a star!
|
2015-09-28 22:14:50 +02:00
|
|
|
|
|
|
|
#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-04-18 18:22:24 +02:00
|
|
|
SECTION("SizeIsUnchanged") {
|
|
|
|
_array.set(0, "hello");
|
|
|
|
REQUIRE(1U == _array.size());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("StoreInteger") {
|
|
|
|
_array.set(0, 123);
|
|
|
|
REQUIRE(123 == _array[0].as<int>());
|
|
|
|
REQUIRE(_array[0].is<int>());
|
|
|
|
REQUIRE_FALSE(_array[0].is<double>());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("StoreDouble") {
|
|
|
|
_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-04-18 18:22:24 +02:00
|
|
|
SECTION("StoreBoolean") {
|
|
|
|
_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-04-18 18:22:24 +02:00
|
|
|
SECTION("StoreString") {
|
|
|
|
_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-04-18 18:22:24 +02:00
|
|
|
SECTION("StoreNestedArray") {
|
|
|
|
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-04-18 18:22:24 +02:00
|
|
|
SECTION("StoreNestedObject") {
|
|
|
|
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-04-18 18:22:24 +02:00
|
|
|
SECTION("StoreArraySubscript") {
|
|
|
|
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-04-18 18:22:24 +02:00
|
|
|
SECTION("StoreObjectSubscript") {
|
|
|
|
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"));
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
}
|