// Copyright Benoit Blanchon 2014-2017 // MIT License // // Arduino JSON library // https://bblanchon.github.io/ArduinoJson/ // If you like this project, please add a star! #include #include TEST_CASE("JsonObject::operator[]") { DynamicJsonBuffer _jsonBuffer; JsonObject& _object = _jsonBuffer.createObject(); SECTION("SizeIncreased_WhenValuesAreAdded") { _object["hello"] = 1; REQUIRE(1 == _object.size()); } SECTION("SizeUntouched_WhenSameValueIsAdded") { _object["hello"] = 1; _object["hello"] = 2; REQUIRE(1 == _object.size()); } SECTION("StoreInteger") { _object["hello"] = 123; REQUIRE(123 == _object["hello"].as()); REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); } SECTION("StoreVolatileInteger") { // issue #415 volatile int i = 123; _object["hello"] = i; REQUIRE(123 == _object["hello"].as()); REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); } SECTION("StoreDouble") { _object["hello"] = 123.45; REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); REQUIRE(123.45 == _object["hello"].as()); } SECTION("StoreDoubleWithDigits") { _object["hello"].set(123.45, 2); REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); REQUIRE(123.45 == _object["hello"].as()); } SECTION("StoreBoolean") { _object["hello"] = true; REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); REQUIRE(true == _object["hello"].as()); } SECTION("StoreString") { _object["hello"] = "h3110"; REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); REQUIRE(std::string("h3110") == _object["hello"].as()); REQUIRE(std::string("h3110") == _object["hello"].as()); // <- short hand } SECTION("StoreArray") { JsonArray& arr = _jsonBuffer.createArray(); _object["hello"] = arr; REQUIRE(&arr == &_object["hello"].as()); REQUIRE(&arr == &_object["hello"].as()); // <- short hand REQUIRE(&arr == &_object["hello"].as()); REQUIRE(&arr == &_object["hello"].as()); // <- short hand REQUIRE(true == _object["hello"].is()); REQUIRE(true == _object["hello"].is()); REQUIRE(true == _object["hello"].is()); REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); } SECTION("StoreObject") { JsonObject& obj = _jsonBuffer.createObject(); _object["hello"] = obj; REQUIRE(&obj == &_object["hello"].as()); REQUIRE(&obj == &_object["hello"].as()); // <- short hand REQUIRE(&obj == &_object["hello"].as()); REQUIRE(&obj == &_object["hello"].as()); // <- short hand REQUIRE(true == _object["hello"].is()); REQUIRE(true == _object["hello"].is()); REQUIRE(true == _object["hello"].is()); REQUIRE(true == _object["hello"].is()); REQUIRE(false == _object["hello"].is()); } SECTION("StoreArraySubscript") { JsonArray& arr = _jsonBuffer.createArray(); arr.add(42); _object["a"] = arr[0]; REQUIRE(42 == _object["a"]); } SECTION("StoreObjectSubscript") { JsonObject& obj = _jsonBuffer.createObject(); obj.set("x", 42); _object["a"] = obj["x"]; REQUIRE(42 == _object["a"]); } SECTION("KeyAsCharArray") { // issue #423 char key[] = "hello"; _object[key] = 42; REQUIRE(42 == _object[key]); } }