2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2018-01-05 09:20:01 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2018
|
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("JsonObject::operator[]") {
|
2015-09-28 22:14:50 +02:00
|
|
|
DynamicJsonBuffer _jsonBuffer;
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonObject& _object = _jsonBuffer.createObject();
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("int") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_object["hello"] = 123;
|
|
|
|
|
|
|
|
REQUIRE(123 == _object["hello"].as<int>());
|
|
|
|
REQUIRE(true == _object["hello"].is<int>());
|
2017-05-20 09:06:53 +02:00
|
|
|
REQUIRE(false == _object["hello"].is<bool>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("volatile int") { // issue #415
|
2017-04-18 18:22:24 +02:00
|
|
|
volatile int i = 123;
|
|
|
|
_object["hello"] = i;
|
|
|
|
|
|
|
|
REQUIRE(123 == _object["hello"].as<int>());
|
|
|
|
REQUIRE(true == _object["hello"].is<int>());
|
2017-05-20 09:06:53 +02:00
|
|
|
REQUIRE(false == _object["hello"].is<bool>());
|
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
|
|
|
_object["hello"] = 123.45;
|
|
|
|
|
|
|
|
REQUIRE(true == _object["hello"].is<double>());
|
|
|
|
REQUIRE(false == _object["hello"].is<long>());
|
|
|
|
REQUIRE(123.45 == _object["hello"].as<double>());
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("bool") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_object["hello"] = true;
|
|
|
|
|
|
|
|
REQUIRE(true == _object["hello"].is<bool>());
|
|
|
|
REQUIRE(false == _object["hello"].is<long>());
|
|
|
|
REQUIRE(true == _object["hello"].as<bool>());
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("const char*") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_object["hello"] = "h3110";
|
|
|
|
|
|
|
|
REQUIRE(true == _object["hello"].is<const char*>());
|
|
|
|
REQUIRE(false == _object["hello"].is<long>());
|
|
|
|
REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
|
|
|
|
REQUIRE(std::string("h3110") ==
|
|
|
|
_object["hello"].as<char*>()); // <- short hand
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("array") {
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonArray& arr = _jsonBuffer.createArray();
|
|
|
|
|
|
|
|
_object["hello"] = arr;
|
|
|
|
|
|
|
|
REQUIRE(&arr == &_object["hello"].as<JsonArray&>());
|
|
|
|
REQUIRE(&arr == &_object["hello"].as<JsonArray>()); // <- short hand
|
|
|
|
REQUIRE(&arr == &_object["hello"].as<const JsonArray&>());
|
|
|
|
REQUIRE(&arr == &_object["hello"].as<const JsonArray>()); // <- short hand
|
|
|
|
REQUIRE(true == _object["hello"].is<JsonArray&>());
|
|
|
|
REQUIRE(true == _object["hello"].is<JsonArray>());
|
|
|
|
REQUIRE(true == _object["hello"].is<const JsonArray&>());
|
|
|
|
REQUIRE(true == _object["hello"].is<const JsonArray>());
|
|
|
|
REQUIRE(false == _object["hello"].is<JsonObject&>());
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("object") {
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonObject& obj = _jsonBuffer.createObject();
|
|
|
|
|
|
|
|
_object["hello"] = obj;
|
|
|
|
|
|
|
|
REQUIRE(&obj == &_object["hello"].as<JsonObject&>());
|
|
|
|
REQUIRE(&obj == &_object["hello"].as<JsonObject>()); // <- short hand
|
|
|
|
REQUIRE(&obj == &_object["hello"].as<const JsonObject&>());
|
|
|
|
REQUIRE(&obj == &_object["hello"].as<const JsonObject>()); // <- short hand
|
|
|
|
REQUIRE(true == _object["hello"].is<JsonObject&>());
|
|
|
|
REQUIRE(true == _object["hello"].is<JsonObject>());
|
|
|
|
REQUIRE(true == _object["hello"].is<const JsonObject&>());
|
|
|
|
REQUIRE(true == _object["hello"].is<const JsonObject>());
|
|
|
|
REQUIRE(false == _object["hello"].is<JsonArray&>());
|
|
|
|
}
|
|
|
|
|
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(42);
|
|
|
|
|
|
|
|
_object["a"] = arr[0];
|
|
|
|
|
|
|
|
REQUIRE(42 == _object["a"]);
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("object subscript") {
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonObject& obj = _jsonBuffer.createObject();
|
|
|
|
obj.set("x", 42);
|
|
|
|
|
|
|
|
_object["a"] = obj["x"];
|
|
|
|
|
|
|
|
REQUIRE(42 == _object["a"]);
|
|
|
|
}
|
|
|
|
|
2018-01-14 13:46:28 +01:00
|
|
|
SECTION("char key[]") { // issue #423
|
2017-04-18 18:22:24 +02:00
|
|
|
char key[] = "hello";
|
|
|
|
_object[key] = 42;
|
|
|
|
REQUIRE(42 == _object[key]);
|
|
|
|
}
|
2018-01-14 13:46:28 +01:00
|
|
|
|
|
|
|
SECTION("should not duplicate const char*") {
|
|
|
|
_object["hello"] = "world";
|
|
|
|
const size_t expectedSize = JSON_OBJECT_SIZE(1);
|
|
|
|
REQUIRE(expectedSize == _jsonBuffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate char* value") {
|
|
|
|
_object["hello"] = const_cast<char*>("world");
|
|
|
|
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
|
|
|
|
REQUIRE(expectedSize == _jsonBuffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate char* key") {
|
|
|
|
_object[const_cast<char*>("hello")] = "world";
|
|
|
|
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
|
|
|
|
REQUIRE(expectedSize == _jsonBuffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate char* key&value") {
|
|
|
|
_object[const_cast<char*>("hello")] = const_cast<char*>("world");
|
|
|
|
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
|
|
|
|
REQUIRE(expectedSize <= _jsonBuffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate std::string value") {
|
|
|
|
_object["hello"] = std::string("world");
|
|
|
|
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
|
|
|
|
REQUIRE(expectedSize == _jsonBuffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate std::string key") {
|
|
|
|
_object[std::string("hello")] = "world";
|
|
|
|
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
|
|
|
|
REQUIRE(expectedSize == _jsonBuffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate std::string key&value") {
|
|
|
|
_object[std::string("hello")] = std::string("world");
|
|
|
|
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
|
|
|
|
REQUIRE(expectedSize <= _jsonBuffer.size());
|
|
|
|
}
|
2017-01-22 11:10:45 +01:00
|
|
|
}
|