Changed the rules of string duplication (fixes #658)

This commit is contained in:
Benoit Blanchon
2018-01-14 13:46:28 +01:00
parent 5c33fd4b94
commit e92612b511
22 changed files with 416 additions and 224 deletions

View File

@ -13,6 +13,7 @@ add_executable(JsonArrayTests
printTo.cpp
remove.cpp
set.cpp
size.cpp
subscript.cpp
)

View File

@ -9,11 +9,6 @@ TEST_CASE("JsonArray::add()") {
DynamicJsonBuffer _jsonBuffer;
JsonArray& _array = _jsonBuffer.createArray();
SECTION("SizeIncreased_WhenValuesAreAdded") {
_array.add("hello");
REQUIRE(1U == _array.size());
}
SECTION("int") {
_array.add(123);
REQUIRE(123 == _array[0].as<int>());
@ -38,7 +33,7 @@ TEST_CASE("JsonArray::add()") {
SECTION("const char*") {
const char* str = "hello";
_array.add(str);
REQUIRE(str == _array[0].as<const char*>());
REQUIRE(str == _array[0].as<std::string>());
REQUIRE(_array[0].is<const char*>());
REQUIRE_FALSE(_array[0].is<int>());
}
@ -82,4 +77,22 @@ TEST_CASE("JsonArray::add()") {
REQUIRE(str == _array[0]);
}
SECTION("should not duplicate const char*") {
_array.add("world");
const size_t expectedSize = JSON_ARRAY_SIZE(1);
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate char*") {
_array.add(const_cast<char*>("world"));
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate std::string") {
_array.add(std::string("world"));
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
}

View File

@ -12,11 +12,6 @@ TEST_CASE("JsonArray::set()") {
JsonArray& _array = _jsonBuffer.createArray();
_array.add(0);
SECTION("SizeIsUnchanged") {
_array.set(0, "hello");
REQUIRE(1U == _array.size());
}
SECTION("int") {
_array.set(0, 123);
REQUIRE(123 == _array[0].as<int>());
@ -82,4 +77,22 @@ TEST_CASE("JsonArray::set()") {
REQUIRE_THAT(_array[0].as<char*>(), Equals("hello"));
}
SECTION("should not duplicate const char*") {
_array.set(0, "world");
const size_t expectedSize = JSON_ARRAY_SIZE(1);
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate char*") {
_array.set(0, const_cast<char*>("world"));
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate std::string") {
_array.set(0, std::string("world"));
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
}

35
test/JsonArray/size.cpp Normal file
View File

@ -0,0 +1,35 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonArray::size()") {
DynamicJsonBuffer _jsonBuffer;
JsonArray& _array = _jsonBuffer.createArray();
SECTION("increases after add()") {
_array.add("hello");
REQUIRE(1U == _array.size());
_array.add("world");
REQUIRE(2U == _array.size());
}
SECTION("remains the same after set()") {
_array.add("hello");
REQUIRE(1U == _array.size());
_array.set(0, "hello");
REQUIRE(1U == _array.size());
}
SECTION("remains the same after assigment") {
_array.add("hello");
REQUIRE(1U == _array.size());
_array[0] = "hello";
REQUIRE(1U == _array.size());
}
}

View File

@ -11,11 +11,6 @@ TEST_CASE("JsonArray::operator[]") {
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<int>());
@ -103,4 +98,22 @@ TEST_CASE("JsonArray::operator[]") {
REQUIRE(str == _array[0]);
}
SECTION("should not duplicate const char*") {
_array[0] = "world";
const size_t expectedSize = JSON_ARRAY_SIZE(1);
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate char*") {
_array[0] = const_cast<char*>("world");
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate std::string") {
_array[0] = std::string("world");
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
}

View File

@ -12,6 +12,7 @@ add_executable(JsonObjectTests
printTo.cpp
remove.cpp
set.cpp
size.cpp
subscript.cpp
)

View File

@ -10,17 +10,6 @@ TEST_CASE("JsonObject::set()") {
DynamicJsonBuffer jb;
JsonObject& _object = jb.createObject();
SECTION("SizeIncreased_WhenValuesAreAdded") {
_object.set("hello", 42);
REQUIRE(1 == _object.size());
}
SECTION("SizeUntouched_WhenSameValueIsAdded") {
_object["hello"] = 1;
_object["hello"] = 2;
REQUIRE(1 == _object.size());
}
SECTION("int") {
_object.set("hello", 123);
@ -91,17 +80,59 @@ TEST_CASE("JsonObject::set()") {
REQUIRE(42 == _object["a"]);
}
SECTION("ShouldReturnTrue_WhenAllocationSucceeds") {
SECTION("returns true when allocation succeeds") {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
JsonObject& obj = jsonBuffer.createObject();
REQUIRE(true == obj.set(std::string("hello"), std::string("world")));
}
SECTION("ShouldReturnFalse_WhenAllocationFails") {
SECTION("returns false when allocation fails") {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
JsonObject& obj = jsonBuffer.createObject();
REQUIRE(false == obj.set(std::string("hello"), std::string("world")));
}
SECTION("should not duplicate const char*") {
_object.set("hello", "world");
const size_t expectedSize = JSON_OBJECT_SIZE(1);
REQUIRE(expectedSize == jb.size());
}
SECTION("should duplicate char* value") {
_object.set("hello", const_cast<char*>("world"));
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == jb.size());
}
SECTION("should duplicate char* key") {
_object.set(const_cast<char*>("hello"), "world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == jb.size());
}
SECTION("should duplicate char* key&value") {
_object.set(const_cast<char*>("hello"), const_cast<char*>("world"));
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= jb.size());
}
SECTION("should duplicate std::string value") {
_object.set("hello", std::string("world"));
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == jb.size());
}
SECTION("should duplicate std::string key") {
_object.set(std::string("hello"), "world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == jb.size());
}
SECTION("should duplicate std::string key&value") {
_object.set(std::string("hello"), std::string("world"));
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= jb.size());
}
}

23
test/JsonObject/size.cpp Normal file
View File

@ -0,0 +1,23 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
TEST_CASE("JsonObject::size()") {
DynamicJsonBuffer jb;
JsonObject& _object = jb.createObject();
SECTION("increases when values are added") {
_object.set("hello", 42);
REQUIRE(1 == _object.size());
}
SECTION("doesn't increase when the smae key is added twice") {
_object["hello"] = 1;
_object["hello"] = 2;
REQUIRE(1 == _object.size());
}
}

View File

@ -9,17 +9,6 @@ TEST_CASE("JsonObject::operator[]") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
SECTION("SizeIncreased_WhenValuesAreAdded") {
_object["hello"] = 1;
REQUIRE(1 == _object.size());
}
SECTION("SizeUntouched_WhenSameValueIsAdded") {
_object["hello"] = 1;
_object["hello"] = 2;
REQUIRE(1 == _object.size());
}
SECTION("int") {
_object["hello"] = 123;
@ -113,9 +102,51 @@ TEST_CASE("JsonObject::operator[]") {
REQUIRE(42 == _object["a"]);
}
SECTION("KeyAsCharArray") { // issue #423
SECTION("char key[]") { // issue #423
char key[] = "hello";
_object[key] = 42;
REQUIRE(42 == _object[key]);
}
SECTION("should not duplicate const char*") {
_object["hello"] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1);
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate char* value") {
_object["hello"] = const_cast<char*>("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate char* key") {
_object[const_cast<char*>("hello")] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate char* key&value") {
_object[const_cast<char*>("hello")] = const_cast<char*>("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= _jsonBuffer.size());
}
SECTION("should duplicate std::string value") {
_object["hello"] = std::string("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate std::string key") {
_object[std::string("hello")] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate std::string key&value") {
_object[std::string("hello")] = std::string("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= _jsonBuffer.size());
}
}

View File

@ -36,4 +36,9 @@ TEST_CASE("TypeTraits") {
REQUIRE((IsString<std::string>::value));
REQUIRE_FALSE((IsString<double>::value));
}
SECTION("IsConst") {
REQUIRE_FALSE((IsConst<char>::value));
REQUIRE((IsConst<const char>::value));
}
}