123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- // ArduinoJson - https://arduinojson.org
- // Copyright © 2014-2022, Benoit BLANCHON
- // MIT License
- #include <ArduinoJson.h>
- #include <catch.hpp>
- typedef ARDUINOJSON_NAMESPACE::ElementProxy<JsonDocument&> ElementProxy;
- TEST_CASE("ElementProxy::add()") {
- DynamicJsonDocument doc(4096);
- doc.add();
- ElementProxy ep = doc[0];
- SECTION("add(int)") {
- ep.add(42);
- REQUIRE(doc.as<std::string>() == "[[42]]");
- }
- SECTION("add(const char*)") {
- ep.add("world");
- REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
- }
- SECTION("set(char[])") {
- char s[] = "world";
- ep.add(s);
- strcpy(s, "!!!!!");
- REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
- }
- }
- TEST_CASE("ElementProxy::clear()") {
- DynamicJsonDocument doc(4096);
- doc.add();
- ElementProxy ep = doc[0];
- SECTION("size goes back to zero") {
- ep.add(42);
- ep.clear();
- REQUIRE(ep.size() == 0);
- }
- SECTION("isNull() return true") {
- ep.add("hello");
- ep.clear();
- REQUIRE(ep.isNull() == true);
- }
- }
- TEST_CASE("ElementProxy::operator==()") {
- DynamicJsonDocument doc(4096);
- SECTION("1 vs 1") {
- doc.add(1);
- doc.add(1);
- REQUIRE(doc[0] <= doc[1]);
- REQUIRE(doc[0] == doc[1]);
- REQUIRE(doc[0] >= doc[1]);
- REQUIRE_FALSE(doc[0] != doc[1]);
- REQUIRE_FALSE(doc[0] < doc[1]);
- REQUIRE_FALSE(doc[0] > doc[1]);
- }
- SECTION("1 vs 2") {
- doc.add(1);
- doc.add(2);
- REQUIRE(doc[0] != doc[1]);
- REQUIRE(doc[0] < doc[1]);
- REQUIRE(doc[0] <= doc[1]);
- REQUIRE_FALSE(doc[0] == doc[1]);
- REQUIRE_FALSE(doc[0] > doc[1]);
- REQUIRE_FALSE(doc[0] >= doc[1]);
- }
- SECTION("'abc' vs 'bcd'") {
- doc.add("abc");
- doc.add("bcd");
- REQUIRE(doc[0] != doc[1]);
- REQUIRE(doc[0] < doc[1]);
- REQUIRE(doc[0] <= doc[1]);
- REQUIRE_FALSE(doc[0] == doc[1]);
- REQUIRE_FALSE(doc[0] > doc[1]);
- REQUIRE_FALSE(doc[0] >= doc[1]);
- }
- }
- TEST_CASE("ElementProxy::remove()") {
- DynamicJsonDocument doc(4096);
- doc.add();
- ElementProxy ep = doc[0];
- SECTION("remove(int)") {
- ep.add(1);
- ep.add(2);
- ep.add(3);
- ep.remove(1);
- REQUIRE(ep.as<std::string>() == "[1,3]");
- }
- SECTION("remove(const char *)") {
- ep["a"] = 1;
- ep["b"] = 2;
- ep.remove("a");
- REQUIRE(ep.as<std::string>() == "{\"b\":2}");
- }
- SECTION("remove(std::string)") {
- ep["a"] = 1;
- ep["b"] = 2;
- ep.remove(std::string("b"));
- REQUIRE(ep.as<std::string>() == "{\"a\":1}");
- }
- #ifdef HAS_VARIABLE_LENGTH_ARRAY
- SECTION("remove(vla)") {
- ep["a"] = 1;
- ep["b"] = 2;
- size_t i = 4;
- char vla[i];
- strcpy(vla, "b");
- ep.remove(vla);
- REQUIRE(ep.as<std::string>() == "{\"a\":1}");
- }
- #endif
- }
- TEST_CASE("ElementProxy::set()") {
- DynamicJsonDocument doc(4096);
- ElementProxy ep = doc[0];
- SECTION("set(int)") {
- ep.set(42);
- REQUIRE(doc.as<std::string>() == "[42]");
- }
- SECTION("set(const char*)") {
- ep.set("world");
- REQUIRE(doc.as<std::string>() == "[\"world\"]");
- }
- SECTION("set(char[])") {
- char s[] = "world";
- ep.set(s);
- strcpy(s, "!!!!!");
- REQUIRE(doc.as<std::string>() == "[\"world\"]");
- }
- }
- TEST_CASE("ElementProxy::size()") {
- DynamicJsonDocument doc(4096);
- doc.add();
- ElementProxy ep = doc[0];
- SECTION("returns 0") {
- REQUIRE(ep.size() == 0);
- }
- SECTION("as an array, returns 2") {
- ep.add(1);
- ep.add(2);
- REQUIRE(ep.size() == 2);
- }
- SECTION("as an object, returns 2") {
- ep["a"] = 1;
- ep["b"] = 2;
- REQUIRE(ep.size() == 2);
- }
- }
- TEST_CASE("ElementProxy::memoryUsage()") {
- DynamicJsonDocument doc(4096);
- doc.add();
- ElementProxy ep = doc[0];
- SECTION("returns 0 for null") {
- REQUIRE(ep.memoryUsage() == 0);
- }
- SECTION("returns size for string") {
- ep.set(std::string("hello"));
- REQUIRE(ep.memoryUsage() == 6);
- }
- }
- TEST_CASE("ElementProxy::operator[]") {
- DynamicJsonDocument doc(4096);
- ElementProxy ep = doc[1];
- SECTION("set member") {
- ep["world"] = 42;
- REQUIRE(doc.as<std::string>() == "[null,{\"world\":42}]");
- }
- SECTION("set element") {
- ep[2] = 42;
- REQUIRE(doc.as<std::string>() == "[null,[null,null,42]]");
- }
- }
- TEST_CASE("ElementProxy cast to JsonVariantConst") {
- DynamicJsonDocument doc(4096);
- doc[0] = "world";
- const ElementProxy ep = doc[0];
- JsonVariantConst var = ep;
- CHECK(var.as<std::string>() == "world");
- }
- TEST_CASE("ElementProxy cast to JsonVariant") {
- DynamicJsonDocument doc(4096);
- doc[0] = "world";
- ElementProxy ep = doc[0];
- JsonVariant var = ep;
- CHECK(var.as<std::string>() == "world");
- var.set("toto");
- CHECK(doc.as<std::string>() == "[\"toto\"]");
- }
- TEST_CASE("ElementProxy::shallowCopy()") {
- StaticJsonDocument<1024> doc1, doc2;
- doc2["hello"] = "world";
- doc1[0].shallowCopy(doc2);
- CHECK(doc1.as<std::string>() == "[{\"hello\":\"world\"}]");
- }
|