// ArduinoJson - arduinojson.org // Copyright Benoit Blanchon 2014-2017 // MIT License #include #include #include TEST_CASE("JsonArray::operator[]") { DynamicJsonBuffer _jsonBuffer; JsonArray& _array = _jsonBuffer.createArray(); _array.add(0); SECTION("SizeIsUnchanged") { _array[0] = "hello"; REQUIRE(1U == _array.size()); } SECTION("int") { _array[0] = 123; REQUIRE(123 == _array[0].as()); REQUIRE(true == _array[0].is()); REQUIRE(false == _array[0].is()); } #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64 SECTION("long long") { _array[0] = 9223372036854775807; REQUIRE(9223372036854775807 == _array[0].as()); REQUIRE(true == _array[0].is()); REQUIRE(false == _array[0].is()); } #endif SECTION("double") { _array[0] = 123.45; REQUIRE(123.45 == _array[0].as()); REQUIRE(true == _array[0].is()); REQUIRE(false == _array[0].is()); } SECTION("bool") { _array[0] = true; REQUIRE(true == _array[0].as()); REQUIRE(true == _array[0].is()); REQUIRE(false == _array[0].is()); } SECTION("const char*") { const char* str = "hello"; _array[0] = str; REQUIRE(str == _array[0].as()); REQUIRE(str == _array[0].as()); // <- short hand REQUIRE(true == _array[0].is()); REQUIRE(false == _array[0].is()); } SECTION("nested array") { JsonArray& arr = _jsonBuffer.createArray(); _array[0] = arr; REQUIRE(&arr == &_array[0].as()); REQUIRE(&arr == &_array[0].as()); // <- short hand REQUIRE(&arr == &_array[0].as()); REQUIRE(&arr == &_array[0].as()); // <- short hand REQUIRE(true == _array[0].is()); REQUIRE(false == _array[0].is()); } SECTION("nested object") { JsonObject& obj = _jsonBuffer.createObject(); _array[0] = obj; REQUIRE(&obj == &_array[0].as()); REQUIRE(&obj == &_array[0].as()); // <- short hand REQUIRE(&obj == &_array[0].as()); REQUIRE(&obj == &_array[0].as()); // <- short hand REQUIRE(true == _array[0].is()); REQUIRE(false == _array[0].is()); } SECTION("array subscript") { JsonArray& arr = _jsonBuffer.createArray(); const char* str = "hello"; arr.add(str); _array[0] = arr[0]; REQUIRE(str == _array[0]); } SECTION("object subscript") { JsonObject& obj = _jsonBuffer.createObject(); const char* str = "hello"; obj["x"] = str; _array[0] = obj["x"]; REQUIRE(str == _array[0]); } }