Fixed duplication of char*

This commit is contained in:
Benoit Blanchon
2018-08-22 14:37:17 +02:00
parent 7683667b3c
commit 6d290bd001
25 changed files with 601 additions and 531 deletions

View File

@ -4,8 +4,11 @@
add_executable(JsonObjectTests
containsKey.cpp
createNestedArray.cpp
createNestedObject.cpp
get.cpp
invalid.cpp
is.cpp
isNull.cpp
iterator.cpp
remove.cpp

View File

@ -8,23 +8,29 @@
TEST_CASE("JsonObject::containsKey()") {
DynamicJsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
obj.set("hello", 42);
SECTION("ContainsKeyReturnsFalseForNonExistingKey") {
obj.set("hello", 42);
SECTION("returns false if key not present") {
REQUIRE(false == obj.containsKey("world"));
}
SECTION("ContainsKeyReturnsTrueForDefinedValue") {
obj.set("hello", 42);
SECTION("returns true if key present") {
REQUIRE(true == obj.containsKey("hello"));
}
SECTION("ContainsKeyReturnsFalseAfterRemove") {
obj.set("hello", 42);
SECTION("returns false after remove()") {
obj.remove("hello");
REQUIRE(false == obj.containsKey("hello"));
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("key is a VLA") {
int i = 16;
char vla[i];
strcpy(vla, "hello");
REQUIRE(true == obj.containsKey(vla));
}
#endif
}

View File

@ -0,0 +1,25 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::createNestedArray()") {
DynamicJsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
SECTION("key is a const char*") {
obj.createNestedArray("hello");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("key is a VLA") {
int i = 16;
char vla[i];
strcpy(vla, "hello");
obj.createNestedArray(vla);
}
#endif
}

View File

@ -0,0 +1,25 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::createNestedObject()") {
DynamicJsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
SECTION("key is a const char*") {
obj.createNestedObject("hello");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("key is a VLA") {
int i = 16;
char vla[i];
strcpy(vla, "hello");
obj.createNestedObject(vla);
}
#endif
}

View File

@ -11,9 +11,20 @@ TEST_CASE("JsonObject::get()") {
DynamicJsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
SECTION("GetConstCharPointer_GivenStringLiteral") {
SECTION("get<const char*>(const char*)") {
obj.set("hello", "world");
const char* value = obj.get<const char*>("hello");
REQUIRE_THAT(value, Equals("world"));
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("get<const char*>(VLA)") {
obj.set("hello", "world");
int i = 16;
char vla[i];
strcpy(vla, "hello");
REQUIRE(std::string("world") == obj.get<char*>(vla));
}
#endif
}

28
test/JsonObject/is.cpp Normal file
View File

@ -0,0 +1,28 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::is<T>()") {
DynamicJsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
obj["int"] = 42;
obj["str"] = "hello";
SECTION("is<int>(const char*)") {
REQUIRE(true == obj.is<int>("int"));
REQUIRE(false == obj.is<int>("str"));
}
#if HAS_VARIABLE_LENGTH_ARRAY
SECTION("is<T>(VLA)") {
int i = 16;
char vla[i];
strcpy(vla, "int");
REQUIRE(true == obj.is<int>(vla));
}
#endif
}

View File

@ -39,4 +39,17 @@ TEST_CASE("JsonObject::remove()") {
serializeJson(obj, result);
REQUIRE("{\"a\":0,\"c\":2}" == result);
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("key is a vla") {
obj["hello"] = 1;
int i = 16;
char vla[i];
strcpy(vla, "hello");
obj.remove(vla);
REQUIRE(0 == obj.size());
}
#endif
}

View File

@ -42,6 +42,38 @@ TEST_CASE("JsonObject::set()") {
REQUIRE_FALSE(obj["hello"].is<long>());
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("key is a VLA") {
int i = 16;
char vla[i];
strcpy(vla, "hello");
obj.set(vla, "world");
REQUIRE(std::string("world") == obj["hello"]);
}
SECTION("value is a VLA") {
int i = 16;
char vla[i];
strcpy(vla, "world");
obj.set("hello", vla);
REQUIRE(std::string("world") == obj["hello"]);
}
SECTION("key and value are VLAs") {
int i = 16;
char vla[i];
strcpy(vla, "world");
obj.set(vla, vla);
REQUIRE(std::string("world") == obj["world"]);
}
#endif
SECTION("nested array") {
DynamicJsonDocument doc2;
JsonArray arr = doc2.to<JsonArray>();

View File

@ -159,4 +159,58 @@ TEST_CASE("JsonObject::operator[]") {
REQUIRE(obj.size() == 1);
REQUIRE(obj[null] == 0);
}
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
!defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
SECTION("obj[VLA] = str") {
int i = 16;
char vla[i];
strcpy(vla, "hello");
obj[vla] = "world";
REQUIRE(std::string("world") == obj["hello"]);
}
SECTION("obj[str] = VLA") { // issue #416
int i = 32;
char vla[i];
strcpy(vla, "world");
obj["hello"] = vla;
REQUIRE(std::string("world") == obj["hello"].as<char*>());
}
SECTION("obj.set(VLA, str)") {
int i = 16;
char vla[i];
strcpy(vla, "hello");
obj[vla] = "world";
REQUIRE(std::string("world") == obj["hello"]);
}
SECTION("obj.set(str, VLA)") {
int i = 32;
char vla[i];
strcpy(vla, "world");
obj["hello"].set(vla);
REQUIRE(std::string("world") == obj["hello"].as<char*>());
}
SECTION("obj[VLA]") {
int i = 16;
char vla[i];
strcpy(vla, "hello");
deserializeJson(doc, "{\"hello\":\"world\"}");
obj = doc.as<JsonObject>();
REQUIRE(std::string("world") == obj[vla]);
}
#endif
}