// ArduinoJson - arduinojson.org // Copyright Benoit Blanchon 2014-2018 // MIT License #include #include TEST_CASE("JsonArray::add()") { DynamicJsonDocument doc; JsonArray& _array = doc.to(); SECTION("int") { _array.add(123); REQUIRE(123 == _array[0].as()); REQUIRE(_array[0].is()); REQUIRE(_array[0].is()); } SECTION("double") { _array.add(123.45); REQUIRE(123.45 == _array[0].as()); REQUIRE(_array[0].is()); REQUIRE_FALSE(_array[0].is()); } SECTION("bool") { _array.add(true); REQUIRE(true == _array[0].as()); REQUIRE(_array[0].is()); REQUIRE_FALSE(_array[0].is()); } SECTION("const char*") { const char* str = "hello"; _array.add(str); REQUIRE(str == _array[0].as()); REQUIRE(_array[0].is()); REQUIRE_FALSE(_array[0].is()); } SECTION("nested array") { DynamicJsonDocument doc2; JsonArray& arr = doc2.to(); _array.add(arr); REQUIRE(&arr == &_array[0].as()); REQUIRE(_array[0].is()); REQUIRE_FALSE(_array[0].is()); } SECTION("nested object") { DynamicJsonDocument doc2; JsonObject& obj = doc2.to(); _array.add(obj); REQUIRE(&obj == &_array[0].as()); REQUIRE(_array[0].is()); REQUIRE_FALSE(_array[0].is()); } SECTION("array subscript") { const char* str = "hello"; DynamicJsonDocument doc2; JsonArray& arr = doc2.to(); arr.add(str); _array.add(arr[0]); REQUIRE(str == _array[0]); } SECTION("object subscript") { const char* str = "hello"; DynamicJsonDocument doc2; JsonObject& obj = doc2.to(); obj["x"] = str; _array.add(obj["x"]); REQUIRE(str == _array[0]); } SECTION("should not duplicate const char*") { _array.add("world"); const size_t expectedSize = JSON_ARRAY_SIZE(1); REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate char*") { _array.add(const_cast("world")); const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6; REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate std::string") { _array.add(std::string("world")); const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6; REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should not duplicate RawJson(const char*)") { _array.add(RawJson("{}")); const size_t expectedSize = JSON_ARRAY_SIZE(1); REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate RawJson(char*)") { _array.add(RawJson(const_cast("{}"))); const size_t expectedSize = JSON_ARRAY_SIZE(1) + 3; REQUIRE(expectedSize == doc.memoryUsage()); } }