Fixed object keys not being duplicated

This commit is contained in:
Benoit Blanchon
2018-10-04 11:16:23 +02:00
parent 527dc19794
commit 6b985b2d1f
30 changed files with 368 additions and 218 deletions

View File

@ -17,6 +17,8 @@ struct NoMemoryAllocator {
TEST_CASE("DynamicMemoryPool no memory") {
DynamicMemoryPoolBase<NoMemoryAllocator> _memoryPool;
typedef DynamicMemoryPoolBase<NoMemoryAllocator>::StringBuilder StringBuilder;
SECTION("FixCodeCoverage") {
// call this function to fix code coverage
NoMemoryAllocator().deallocate(NULL);
@ -33,9 +35,8 @@ TEST_CASE("DynamicMemoryPool no memory") {
// }
SECTION("startString()") {
DynamicMemoryPoolBase<NoMemoryAllocator>::String str =
_memoryPool.startString();
StringBuilder str = _memoryPool.startString();
str.append('!');
REQUIRE(0 == str.c_str());
REQUIRE(str.complete().isNull());
}
}

View File

@ -7,43 +7,45 @@
using namespace ARDUINOJSON_NAMESPACE;
typedef DynamicMemoryPool::StringBuilder StringBuilder;
TEST_CASE("DynamicMemoryPool::startString()") {
SECTION("WorksWhenBufferIsBigEnough") {
DynamicMemoryPool memoryPool(6);
DynamicMemoryPool::String str = memoryPool.startString();
StringBuilder str = memoryPool.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
REQUIRE(std::string("hello") == str.c_str());
REQUIRE(str.complete().equals("hello"));
}
SECTION("GrowsWhenBufferIsTooSmall") {
DynamicMemoryPool memoryPool(5);
DynamicMemoryPool::String str = memoryPool.startString();
StringBuilder str = memoryPool.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
REQUIRE(std::string("hello") == str.c_str());
REQUIRE(str.complete().equals("hello"));
}
SECTION("SizeIncreases") {
DynamicMemoryPool memoryPool(5);
DynamicMemoryPool::String str = memoryPool.startString();
StringBuilder str = memoryPool.startString();
REQUIRE(0 == memoryPool.size());
str.append('h');
REQUIRE(1 == memoryPool.size());
str.c_str();
str.complete();
REQUIRE(2 == memoryPool.size());
}
}

View File

@ -4,6 +4,7 @@
add_executable(JsonObjectTests
containsKey.cpp
copy.cpp
createNestedArray.cpp
createNestedObject.cpp
get.cpp

59
test/JsonObject/copy.cpp Normal file
View File

@ -0,0 +1,59 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::copyFrom()") {
DynamicJsonDocument doc1;
DynamicJsonDocument doc2;
JsonObject obj1 = doc1.to<JsonObject>();
JsonObject obj2 = doc2.to<JsonObject>();
SECTION("doesn't copy static string in key or value") {
obj1["hello"] = "world";
obj2.copyFrom(obj1);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
}
SECTION("copy local string value") {
obj1["hello"] = std::string("world");
obj2.copyFrom(obj1);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
}
SECTION("copy local key") {
obj1[std::string("hello")] = "world";
obj2.copyFrom(obj1);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
}
SECTION("copy string from deserializeJson()") {
deserializeJson(doc1, "{'hello':'world'}");
obj2.copyFrom(obj1);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
}
SECTION("copy string from deserializeMsgPack()") {
deserializeMsgPack(doc1, "\x81\xA5hello\xA5world");
obj2.copyFrom(obj1);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
}
}

View File

@ -16,11 +16,11 @@ TEST_CASE("JsonObject::begin()/end()") {
SECTION("NonConstIterator") {
JsonObject::iterator it = obj.begin();
REQUIRE(obj.end() != it);
REQUIRE_THAT(it->key(), Equals("ab"));
REQUIRE(it->key() == "ab");
REQUIRE(12 == it->value());
++it;
REQUIRE(obj.end() != it);
REQUIRE_THAT(it->key(), Equals("cd"));
REQUIRE(it->key() == "cd");
REQUIRE(34 == it->value());
++it;
REQUIRE(obj.end() == it);
@ -42,7 +42,7 @@ TEST_CASE("JsonObject::begin()/end()") {
// }
SECTION("Dereferencing end() is safe") {
REQUIRE(obj.end()->key() == 0);
REQUIRE(obj.end()->key().isNull());
REQUIRE(obj.end()->value().isNull());
}
}

View File

@ -13,21 +13,21 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
SECTION("stores JsonArray by copy") {
JsonArray arr = doc2.to<JsonArray>();
arr.add(42);
var1.set(doc2.as<JsonVariant>());
arr[0] = 666;
var1.set(arr);
arr[0] = 666;
REQUIRE(var1.as<std::string>() == "[42]");
}
SECTION("stores JsonObject by copy") {
JsonObject obj = doc2.to<JsonObject>();
obj["value"] = 42;
var1.set(doc2.as<JsonVariant>());
obj["value"] = 666;
var1.set(obj);
obj["value"] = 666;
REQUIRE(var1.as<std::string>() == "{\"value\":42}");
}

View File

@ -8,42 +8,44 @@
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("StaticMemoryPool::startString()") {
typedef StaticMemoryPoolBase::StringBuilder StringBuilder;
SECTION("WorksWhenBufferIsBigEnough") {
StaticMemoryPool<6> memoryPool;
StaticMemoryPoolBase::String str = memoryPool.startString();
StringBuilder str = memoryPool.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
REQUIRE(std::string("hello") == str.c_str());
REQUIRE(str.complete().equals("hello"));
}
SECTION("ReturnsNullWhenTooSmall") {
StaticMemoryPool<5> memoryPool;
StaticMemoryPoolBase::String str = memoryPool.startString();
StringBuilder str = memoryPool.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
REQUIRE(0 == str.c_str());
REQUIRE(str.complete().isNull());
}
SECTION("SizeIncreases") {
StaticMemoryPool<5> memoryPool;
StaticMemoryPoolBase::String str = memoryPool.startString();
StringBuilder str = memoryPool.startString();
REQUIRE(0 == memoryPool.size());
str.append('h');
REQUIRE(1 == memoryPool.size());
str.c_str();
str.complete();
REQUIRE(2 == memoryPool.size());
}
}