mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-25 00:07:34 +02:00
Don't use JsonBuffer to create or parse objects and arrays.
* Added DynamicJsonArray and StaticJsonArray * Added DynamicJsonObject and StaticJsonObject * Added DynamicJsonVariant and StaticJsonVariant * Added deserializeJson() * Removed JsonBuffer::parseArray(), parseObject() and parse() * Removed JsonBuffer::createArray() and createObject()
This commit is contained in:
@ -67,10 +67,10 @@ endif()
|
||||
add_subdirectory(DynamicJsonBuffer)
|
||||
add_subdirectory(IntegrationTests)
|
||||
add_subdirectory(JsonArray)
|
||||
add_subdirectory(JsonBuffer)
|
||||
add_subdirectory(JsonObject)
|
||||
add_subdirectory(JsonParser)
|
||||
add_subdirectory(JsonVariant)
|
||||
add_subdirectory(JsonWriter)
|
||||
add_subdirectory(Misc)
|
||||
add_subdirectory(Polyfills)
|
||||
add_subdirectory(StaticJsonBuffer)
|
||||
add_subdirectory(StaticJsonBuffer)
|
||||
|
@ -4,8 +4,6 @@
|
||||
|
||||
add_executable(DynamicJsonBufferTests
|
||||
alloc.cpp
|
||||
createArray.cpp
|
||||
createObject.cpp
|
||||
no_memory.cpp
|
||||
size.cpp
|
||||
startString.cpp
|
||||
|
@ -1,28 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("DynamicJsonBuffer::createArray()") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray &array = jsonBuffer.createArray();
|
||||
|
||||
SECTION("GrowsWithArray") {
|
||||
REQUIRE(JSON_ARRAY_SIZE(0) == jsonBuffer.size());
|
||||
|
||||
array.add("hello");
|
||||
REQUIRE(JSON_ARRAY_SIZE(1) == jsonBuffer.size());
|
||||
|
||||
array.add("world");
|
||||
REQUIRE(JSON_ARRAY_SIZE(2) == jsonBuffer.size());
|
||||
}
|
||||
|
||||
SECTION("CanAdd1000Values") {
|
||||
for (size_t i = 1; i <= 1000; i++) {
|
||||
array.add("hello");
|
||||
REQUIRE(array.size() == i);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("DynamicJsonBuffer::createObject()") {
|
||||
DynamicJsonBuffer json;
|
||||
|
||||
JsonObject &obj = json.createObject();
|
||||
REQUIRE(JSON_OBJECT_SIZE(0) == json.size());
|
||||
|
||||
obj["hello"] = 1;
|
||||
REQUIRE(JSON_OBJECT_SIZE(1) == json.size());
|
||||
|
||||
obj["world"] = 2;
|
||||
REQUIRE(JSON_OBJECT_SIZE(2) == json.size());
|
||||
|
||||
obj["world"] = 3; // <- same key, should not grow
|
||||
REQUIRE(JSON_OBJECT_SIZE(2) == json.size());
|
||||
}
|
@ -22,23 +22,25 @@ TEST_CASE("DynamicJsonBuffer no memory") {
|
||||
NoMemoryAllocator().deallocate(NULL);
|
||||
}
|
||||
|
||||
SECTION("createArray()") {
|
||||
REQUIRE_FALSE(_jsonBuffer.createArray().success());
|
||||
}
|
||||
// TODO: uncomment
|
||||
// SECTION("parseArray()") {
|
||||
// char json[] = "[{}]";
|
||||
// DynamicJsonArray arr;
|
||||
|
||||
SECTION("createObject()") {
|
||||
REQUIRE_FALSE(_jsonBuffer.createObject().success());
|
||||
}
|
||||
// bool success = deserializeJson(arr, json);
|
||||
|
||||
SECTION("parseArray()") {
|
||||
char json[] = "[]";
|
||||
REQUIRE_FALSE(_jsonBuffer.parseArray(json).success());
|
||||
}
|
||||
// REQUIRE(success == false);
|
||||
// }
|
||||
|
||||
SECTION("parseObject()") {
|
||||
char json[] = "{}";
|
||||
REQUIRE_FALSE(_jsonBuffer.parseObject(json).success());
|
||||
}
|
||||
// TODO: uncomment
|
||||
// SECTION("parseObject()") {
|
||||
// char json[] = "{[]}";
|
||||
// DynamicJsonObject obj;
|
||||
|
||||
// bool success = deserializeJson(obj, json);
|
||||
|
||||
// REQUIRE(success == false);
|
||||
// }
|
||||
|
||||
SECTION("startString()") {
|
||||
DynamicJsonBufferBase<NoMemoryAllocator>::String str =
|
||||
|
@ -6,9 +6,10 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("Gbathree") {
|
||||
DynamicJsonBuffer _buffer;
|
||||
DynamicJsonObject _object;
|
||||
|
||||
const JsonObject& _object = _buffer.parseObject(
|
||||
bool success = deserializeJson(
|
||||
_object,
|
||||
"{\"protocol_name\":\"fluorescence\",\"repeats\":1,\"wait\":0,"
|
||||
"\"averages\":1,\"measurements\":3,\"meas2_light\":15,\"meas1_"
|
||||
"baseline\":0,\"act_light\":20,\"pulsesize\":25,\"pulsedistance\":"
|
||||
@ -21,7 +22,7 @@ TEST_CASE("Gbathree") {
|
||||
"[15,15,15,15]],\"altc\":[2,2,2,2],\"altd\":[2,2,2,2]}");
|
||||
|
||||
SECTION("Success") {
|
||||
REQUIRE(_object.success());
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("ProtocolName") {
|
||||
|
@ -6,13 +6,15 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
void check(std::string originalJson) {
|
||||
DynamicJsonBuffer jb;
|
||||
DynamicJsonObject obj;
|
||||
|
||||
std::string prettyJson;
|
||||
jb.parseObject(originalJson).prettyPrintTo(prettyJson);
|
||||
deserializeJson(obj, originalJson);
|
||||
obj.prettyPrintTo(prettyJson);
|
||||
|
||||
std::string finalJson;
|
||||
jb.parseObject(prettyJson).printTo(finalJson);
|
||||
deserializeJson(obj, originalJson);
|
||||
obj.printTo(finalJson);
|
||||
|
||||
REQUIRE(originalJson == finalJson);
|
||||
}
|
||||
|
@ -6,8 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::add()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
DynamicJsonArray _array;
|
||||
|
||||
SECTION("int") {
|
||||
_array.add(123);
|
||||
@ -39,7 +38,7 @@ TEST_CASE("JsonArray::add()") {
|
||||
}
|
||||
|
||||
SECTION("nested array") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
|
||||
_array.add(arr);
|
||||
|
||||
@ -49,7 +48,7 @@ TEST_CASE("JsonArray::add()") {
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
|
||||
_array.add(obj);
|
||||
|
||||
@ -60,7 +59,7 @@ TEST_CASE("JsonArray::add()") {
|
||||
|
||||
SECTION("array subscript") {
|
||||
const char* str = "hello";
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(str);
|
||||
|
||||
_array.add(arr[0]);
|
||||
@ -70,7 +69,7 @@ TEST_CASE("JsonArray::add()") {
|
||||
|
||||
SECTION("object subscript") {
|
||||
const char* str = "hello";
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj["x"] = str;
|
||||
|
||||
_array.add(obj["x"]);
|
||||
@ -81,30 +80,30 @@ TEST_CASE("JsonArray::add()") {
|
||||
SECTION("should not duplicate const char*") {
|
||||
_array.add("world");
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _array.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char*") {
|
||||
_array.add(const_cast<char*>("world"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _array.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string") {
|
||||
_array.add(std::string("world"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _array.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should not duplicate RawJson(const char*)") {
|
||||
_array.add(RawJson("{}"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _array.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate RawJson(char*)") {
|
||||
_array.add(RawJson(const_cast<char*>("{}")));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 3;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _array.memoryUsage());
|
||||
}
|
||||
}
|
||||
|
@ -6,8 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray basics") {
|
||||
DynamicJsonBuffer jb;
|
||||
JsonArray& array = jb.createArray();
|
||||
DynamicJsonArray array;
|
||||
|
||||
SECTION("SuccessIsTrue") {
|
||||
REQUIRE(array.success());
|
||||
|
@ -7,8 +7,7 @@
|
||||
|
||||
TEST_CASE("JsonArray::copyFrom()") {
|
||||
SECTION("OneDimension") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
DynamicJsonArray array;
|
||||
char json[32];
|
||||
int source[] = {1, 2, 3};
|
||||
|
||||
@ -21,8 +20,7 @@ TEST_CASE("JsonArray::copyFrom()") {
|
||||
|
||||
SECTION("OneDimension_JsonBufferTooSmall") {
|
||||
const size_t SIZE = JSON_ARRAY_SIZE(2);
|
||||
StaticJsonBuffer<SIZE> jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
StaticJsonArray<SIZE> array;
|
||||
char json[32];
|
||||
int source[] = {1, 2, 3};
|
||||
|
||||
@ -34,8 +32,7 @@ TEST_CASE("JsonArray::copyFrom()") {
|
||||
}
|
||||
|
||||
SECTION("TwoDimensions") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
DynamicJsonArray array;
|
||||
char json[32];
|
||||
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
||||
|
||||
@ -49,8 +46,7 @@ TEST_CASE("JsonArray::copyFrom()") {
|
||||
SECTION("TwoDimensions_JsonBufferTooSmall") {
|
||||
const size_t SIZE =
|
||||
JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
|
||||
StaticJsonBuffer<SIZE> jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
StaticJsonArray<SIZE> array;
|
||||
char json[32];
|
||||
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
||||
|
||||
|
@ -6,11 +6,12 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::copyTo()") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
DynamicJsonArray array;
|
||||
|
||||
SECTION("BiggerOneDimensionIntegerArray") {
|
||||
char json[] = "[1,2,3]";
|
||||
JsonArray& array = jsonBuffer.parseArray(json);
|
||||
bool success = deserializeJson(array, json);
|
||||
REQUIRE(success == true);
|
||||
|
||||
int destination[4] = {0};
|
||||
size_t result = array.copyTo(destination);
|
||||
@ -24,7 +25,8 @@ TEST_CASE("JsonArray::copyTo()") {
|
||||
|
||||
SECTION("SmallerOneDimensionIntegerArray") {
|
||||
char json[] = "[1,2,3]";
|
||||
JsonArray& array = jsonBuffer.parseArray(json);
|
||||
bool success = deserializeJson(array, json);
|
||||
REQUIRE(success == true);
|
||||
|
||||
int destination[2] = {0};
|
||||
size_t result = array.copyTo(destination);
|
||||
@ -37,7 +39,8 @@ TEST_CASE("JsonArray::copyTo()") {
|
||||
SECTION("TwoOneDimensionIntegerArray") {
|
||||
char json[] = "[[1,2],[3],[4]]";
|
||||
|
||||
JsonArray& array = jsonBuffer.parseArray(json);
|
||||
bool success = deserializeJson(array, json);
|
||||
REQUIRE(success == true);
|
||||
|
||||
int destination[3][2] = {{0}};
|
||||
array.copyTo(destination);
|
||||
|
@ -7,9 +7,7 @@
|
||||
|
||||
template <typename TIterator>
|
||||
static void run_iterator_test() {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> jsonBuffer;
|
||||
|
||||
JsonArray &array = jsonBuffer.createArray();
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(2)> array;
|
||||
array.add(12);
|
||||
array.add(34);
|
||||
|
||||
|
@ -15,8 +15,7 @@ static void check(JsonArray& array, std::string expected) {
|
||||
}
|
||||
|
||||
TEST_CASE("JsonArray::prettyPrintTo()") {
|
||||
DynamicJsonBuffer jb;
|
||||
JsonArray& array = jb.createArray();
|
||||
DynamicJsonArray array;
|
||||
|
||||
SECTION("Empty") {
|
||||
check(array, "[]");
|
||||
|
@ -15,8 +15,7 @@ static void check(JsonArray &array, std::string expected) {
|
||||
}
|
||||
|
||||
TEST_CASE("JsonArray::printTo()") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> jb;
|
||||
JsonArray &array = jb.createArray();
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(2)> array;
|
||||
|
||||
SECTION("Empty") {
|
||||
check(array, "[]");
|
||||
@ -74,13 +73,10 @@ TEST_CASE("JsonArray::printTo()") {
|
||||
}
|
||||
|
||||
SECTION("RawJson(char*)") {
|
||||
DynamicJsonBuffer jb2;
|
||||
JsonArray &arr = jb2.createArray();
|
||||
|
||||
char tmp[] = "{\"key\":\"value\"}";
|
||||
arr.add(RawJson(tmp));
|
||||
array.add(RawJson(tmp));
|
||||
|
||||
check(arr, "[{\"key\":\"value\"}]");
|
||||
check(array, "[{\"key\":\"value\"}]");
|
||||
}
|
||||
|
||||
SECTION("OneIntegerOverCapacity") {
|
||||
|
@ -6,8 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::remove()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
DynamicJsonArray _array;
|
||||
_array.add(1);
|
||||
_array.add(2);
|
||||
_array.add(3);
|
||||
|
@ -8,8 +8,7 @@
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("JsonArray::set()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
DynamicJsonArray _array;
|
||||
_array.add(0);
|
||||
|
||||
SECTION("int") {
|
||||
@ -41,7 +40,7 @@ TEST_CASE("JsonArray::set()") {
|
||||
}
|
||||
|
||||
SECTION("nested array") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
|
||||
_array.set(0, arr);
|
||||
|
||||
@ -51,7 +50,7 @@ TEST_CASE("JsonArray::set()") {
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
|
||||
_array.set(0, obj);
|
||||
|
||||
@ -61,7 +60,7 @@ TEST_CASE("JsonArray::set()") {
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
|
||||
_array.set(0, arr[0]);
|
||||
@ -70,7 +69,7 @@ TEST_CASE("JsonArray::set()") {
|
||||
}
|
||||
|
||||
SECTION("object subscript") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj["x"] = "hello";
|
||||
|
||||
_array.set(0, obj["x"]);
|
||||
@ -81,18 +80,18 @@ TEST_CASE("JsonArray::set()") {
|
||||
SECTION("should not duplicate const char*") {
|
||||
_array.set(0, "world");
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _array.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char*") {
|
||||
_array.set(0, const_cast<char*>("world"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _array.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string") {
|
||||
_array.set(0, std::string("world"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _array.memoryUsage());
|
||||
}
|
||||
}
|
||||
|
@ -6,8 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::size()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
DynamicJsonArray _array;
|
||||
|
||||
SECTION("increases after add()") {
|
||||
_array.add("hello");
|
||||
|
@ -7,8 +7,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::operator[]") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
DynamicJsonArray _array;
|
||||
_array.add(0);
|
||||
|
||||
SECTION("int") {
|
||||
@ -52,7 +51,7 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("nested array") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
|
||||
_array[0] = arr;
|
||||
|
||||
@ -65,7 +64,7 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
|
||||
_array[0] = obj;
|
||||
|
||||
@ -78,7 +77,7 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
const char* str = "hello";
|
||||
|
||||
arr.add(str);
|
||||
@ -89,7 +88,7 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("object subscript") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
const char* str = "hello";
|
||||
|
||||
obj["x"] = str;
|
||||
@ -102,18 +101,18 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
SECTION("should not duplicate const char*") {
|
||||
_array[0] = "world";
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _array.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char*") {
|
||||
_array[0] = const_cast<char*>("world");
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _array.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string") {
|
||||
_array[0] = std::string("world");
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _array.memoryUsage());
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +0,0 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(JsonBufferTests
|
||||
nested.cpp
|
||||
nestingLimit.cpp
|
||||
parse.cpp
|
||||
parseArray.cpp
|
||||
parseObject.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(JsonBufferTests catch)
|
||||
add_test(JsonBuffer JsonBufferTests)
|
@ -1,63 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonBuffer nested objects") {
|
||||
SECTION("ArrayNestedInObject") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
|
||||
|
||||
JsonObject &object = jsonBuffer.parseObject(jsonString);
|
||||
JsonArray &array1 = object["ab"];
|
||||
const JsonArray &array2 = object["cd"];
|
||||
JsonArray &array3 = object["ef"];
|
||||
|
||||
REQUIRE(true == object.success());
|
||||
|
||||
REQUIRE(true == array1.success());
|
||||
REQUIRE(true == array2.success());
|
||||
REQUIRE(false == array3.success());
|
||||
|
||||
REQUIRE(2 == array1.size());
|
||||
REQUIRE(2 == array2.size());
|
||||
REQUIRE(0 == array3.size());
|
||||
|
||||
REQUIRE(1 == array1[0].as<int>());
|
||||
REQUIRE(2 == array1[1].as<int>());
|
||||
|
||||
REQUIRE(3 == array2[0].as<int>());
|
||||
REQUIRE(4 == array2[1].as<int>());
|
||||
|
||||
REQUIRE(0 == array3[0].as<int>());
|
||||
}
|
||||
|
||||
SECTION("ObjectNestedInArray") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
char jsonString[] =
|
||||
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
|
||||
|
||||
JsonArray &array = jsonBuffer.parseArray(jsonString);
|
||||
JsonObject &object1 = array[0];
|
||||
const JsonObject &object2 = array[1];
|
||||
JsonObject &object3 = array[2];
|
||||
|
||||
REQUIRE(true == array.success());
|
||||
|
||||
REQUIRE(true == object1.success());
|
||||
REQUIRE(true == object2.success());
|
||||
REQUIRE(false == object3.success());
|
||||
|
||||
REQUIRE(2 == object1.size());
|
||||
REQUIRE(2 == object2.size());
|
||||
REQUIRE(0 == object3.size());
|
||||
|
||||
REQUIRE(1 == object1["a"].as<int>());
|
||||
REQUIRE(2 == object1["b"].as<int>());
|
||||
REQUIRE(3 == object2["c"].as<int>());
|
||||
REQUIRE(4 == object2["d"].as<int>());
|
||||
REQUIRE(0 == object3["e"].as<int>());
|
||||
}
|
||||
}
|
@ -1,318 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonBuffer::parseArray()") {
|
||||
DynamicJsonBuffer jb;
|
||||
|
||||
SECTION("EmptyArray") {
|
||||
JsonArray& arr = jb.parseArray("[]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(0 == arr.size());
|
||||
}
|
||||
|
||||
SECTION("MissingOpeningBracket") {
|
||||
JsonArray& arr = jb.parseArray("]");
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("ArrayWithNoEnd") {
|
||||
JsonArray& arr = jb.parseArray("[");
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("EmptyArrayWithLeadingSpaces") {
|
||||
JsonArray& arr = jb.parseArray(" []");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(0 == arr.size());
|
||||
}
|
||||
|
||||
SECTION("Garbage") {
|
||||
JsonArray& arr = jb.parseArray("%*$£¤");
|
||||
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("OneInteger") {
|
||||
JsonArray& arr = jb.parseArray("[42]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == 42);
|
||||
}
|
||||
|
||||
SECTION("OneIntegerWithSpacesBefore") {
|
||||
JsonArray& arr = jb.parseArray("[ \t\r\n42]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == 42);
|
||||
}
|
||||
|
||||
SECTION("OneIntegerWithSpaceAfter") {
|
||||
JsonArray& arr = jb.parseArray("[42 \t\r\n]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == 42);
|
||||
}
|
||||
|
||||
SECTION("TwoIntegers") {
|
||||
JsonArray& arr = jb.parseArray("[42,84]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == 42);
|
||||
REQUIRE(arr[1] == 84);
|
||||
}
|
||||
|
||||
SECTION("TwoDoubles") {
|
||||
JsonArray& arr = jb.parseArray("[4.2,1e2]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == 4.2);
|
||||
REQUIRE(arr[1] == 1e2);
|
||||
}
|
||||
|
||||
SECTION("UnsignedLong") {
|
||||
JsonArray& arr = jb.parseArray("[4294967295]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == 4294967295UL);
|
||||
}
|
||||
|
||||
SECTION("TwoBooleans") {
|
||||
JsonArray& arr = jb.parseArray("[true,false]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == true);
|
||||
REQUIRE(arr[1] == false);
|
||||
}
|
||||
|
||||
SECTION("TwoNulls") {
|
||||
JsonArray& arr = jb.parseArray("[null,null]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0].as<char*>() == 0);
|
||||
REQUIRE(arr[1].as<char*>() == 0);
|
||||
}
|
||||
|
||||
SECTION("TwoStringsDoubleQuotes") {
|
||||
JsonArray& arr = jb.parseArray("[ \"hello\" , \"world\" ]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("TwoStringsSingleQuotes") {
|
||||
JsonArray& arr = jb.parseArray("[ 'hello' , 'world' ]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("TwoStringsNoQuotes") {
|
||||
JsonArray& arr = jb.parseArray("[ hello , world ]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("EmptyStringsDoubleQuotes") {
|
||||
JsonArray& arr = jb.parseArray("[\"\",\"\"]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "");
|
||||
REQUIRE(arr[1] == "");
|
||||
}
|
||||
|
||||
SECTION("EmptyStringSingleQuotes") {
|
||||
JsonArray& arr = jb.parseArray("[\'\',\'\']");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "");
|
||||
REQUIRE(arr[1] == "");
|
||||
}
|
||||
|
||||
SECTION("EmptyStringNoQuotes") {
|
||||
JsonArray& arr = jb.parseArray("[,]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "");
|
||||
REQUIRE(arr[1] == "");
|
||||
}
|
||||
|
||||
SECTION("ClosingDoubleQuoteMissing") {
|
||||
JsonArray& arr = jb.parseArray("[\"]");
|
||||
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("ClosingSignleQuoteMissing") {
|
||||
JsonArray& arr = jb.parseArray("[\']");
|
||||
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("StringWithEscapedChars") {
|
||||
JsonArray& arr = jb.parseArray("[\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "1\"2\\3/4\b5\f6\n7\r8\t9");
|
||||
}
|
||||
|
||||
SECTION("StringWithUnterminatedEscapeSequence") {
|
||||
JsonArray& arr = jb.parseArray("\"\\\0\"", 4);
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("CCommentBeforeOpeningBracket") {
|
||||
JsonArray& arr = jb.parseArray("/*COMMENT*/ [\"hello\"]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CCommentAfterOpeningBracket") {
|
||||
JsonArray& arr = jb.parseArray("[/*COMMENT*/ \"hello\"]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CCommentBeforeClosingBracket") {
|
||||
JsonArray& arr = jb.parseArray("[\"hello\"/*COMMENT*/]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CCommentAfterClosingBracket") {
|
||||
JsonArray& arr = jb.parseArray("[\"hello\"]/*COMMENT*/");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CCommentBeforeComma") {
|
||||
JsonArray& arr = jb.parseArray("[\"hello\"/*COMMENT*/,\"world\"]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("CCommentAfterComma") {
|
||||
JsonArray& arr = jb.parseArray("[\"hello\",/*COMMENT*/ \"world\"]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("CppCommentBeforeOpeningBracket") {
|
||||
JsonArray& arr = jb.parseArray("//COMMENT\n\t[\"hello\"]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CppCommentAfterOpeningBracket") {
|
||||
JsonArray& arr = jb.parseArray("[//COMMENT\n\"hello\"]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CppCommentBeforeClosingBracket") {
|
||||
JsonArray& arr = jb.parseArray("[\"hello\"//COMMENT\r\n]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CppCommentAfterClosingBracket") {
|
||||
JsonArray& arr = jb.parseArray("[\"hello\"]//COMMENT\n");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CppCommentBeforeComma") {
|
||||
JsonArray& arr = jb.parseArray("[\"hello\"//COMMENT\n,\"world\"]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("CppCommentAfterComma") {
|
||||
JsonArray& arr = jb.parseArray("[\"hello\",//COMMENT\n\"world\"]");
|
||||
|
||||
REQUIRE(arr.success());
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("InvalidCppComment") {
|
||||
JsonArray& arr = jb.parseArray("[/COMMENT\n]");
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("InvalidComment") {
|
||||
JsonArray& arr = jb.parseArray("[/*/\n]");
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("UnfinishedCComment") {
|
||||
JsonArray& arr = jb.parseArray("[/*COMMENT]");
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("EndsInCppComment") {
|
||||
JsonArray& arr = jb.parseArray("[//COMMENT");
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("AfterClosingStar") {
|
||||
JsonArray& arr = jb.parseArray("[/*COMMENT*");
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("DeeplyNested") {
|
||||
JsonArray& arr =
|
||||
jb.parseArray("[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]");
|
||||
REQUIRE(arr.success());
|
||||
}
|
||||
}
|
@ -1,170 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonBuffer::parseObject()") {
|
||||
DynamicJsonBuffer jb;
|
||||
|
||||
SECTION("An empty object") {
|
||||
JsonObject& obj = jb.parseObject("{}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Quotes") {
|
||||
SECTION("Double quotes") {
|
||||
JsonObject& obj = jb.parseObject("{\"key\":\"value\"}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("Single quotes") {
|
||||
JsonObject& obj = jb.parseObject("{'key':'value'}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("No quotes") {
|
||||
JsonObject& obj = jb.parseObject("{key:value}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("No quotes, allow underscore in key") {
|
||||
JsonObject& obj = jb.parseObject("{_k_e_y_:42}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["_k_e_y_"] == 42);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Spaces") {
|
||||
SECTION("Before the key") {
|
||||
JsonObject& obj = jb.parseObject("{ \"key\":\"value\"}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("After the key") {
|
||||
JsonObject& obj = jb.parseObject("{\"key\" :\"value\"}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("Before the value") {
|
||||
JsonObject& obj = jb.parseObject("{\"key\": \"value\"}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("After the value") {
|
||||
JsonObject& obj = jb.parseObject("{\"key\":\"value\" }");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("Before the colon") {
|
||||
JsonObject& obj =
|
||||
jb.parseObject("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == "value1");
|
||||
REQUIRE(obj["key2"] == "value2");
|
||||
}
|
||||
|
||||
SECTION("After the colon") {
|
||||
JsonObject& obj =
|
||||
jb.parseObject("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == "value1");
|
||||
REQUIRE(obj["key2"] == "value2");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Values types") {
|
||||
SECTION("String") {
|
||||
JsonObject& obj =
|
||||
jb.parseObject("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == "value1");
|
||||
REQUIRE(obj["key2"] == "value2");
|
||||
}
|
||||
|
||||
SECTION("Integer") {
|
||||
JsonObject& obj = jb.parseObject("{\"key1\":42,\"key2\":-42}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == 42);
|
||||
REQUIRE(obj["key2"] == -42);
|
||||
}
|
||||
|
||||
SECTION("Double") {
|
||||
JsonObject& obj = jb.parseObject("{\"key1\":12.345,\"key2\":-7E89}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == 12.345);
|
||||
REQUIRE(obj["key2"] == -7E89);
|
||||
}
|
||||
|
||||
SECTION("Booleans") {
|
||||
JsonObject& obj = jb.parseObject("{\"key1\":true,\"key2\":false}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == true);
|
||||
REQUIRE(obj["key2"] == false);
|
||||
}
|
||||
|
||||
SECTION("Null") {
|
||||
JsonObject& obj = jb.parseObject("{\"key1\":null,\"key2\":null}");
|
||||
REQUIRE(obj.success());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"].as<char*>() == 0);
|
||||
REQUIRE(obj["key2"].as<char*>() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Misc") {
|
||||
SECTION("The opening brace is missing") {
|
||||
JsonObject& obj = jb.parseObject("}");
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("The closing brace is missing") {
|
||||
JsonObject& obj = jb.parseObject("{");
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("A quoted key without value") {
|
||||
JsonObject& obj = jb.parseObject("{\"key\"}");
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("A non-quoted key without value") {
|
||||
JsonObject& obj = jb.parseObject("{key}");
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("A dangling comma") {
|
||||
JsonObject& obj = jb.parseObject("{\"key1\":\"value1\",}");
|
||||
REQUIRE_FALSE(obj.success());
|
||||
REQUIRE(obj.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("null as a key") {
|
||||
JsonObject& obj = jb.parseObject("null:\"value\"}");
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
}
|
||||
}
|
@ -6,8 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonObject basics") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object = _jsonBuffer.createObject();
|
||||
DynamicJsonObject _object;
|
||||
|
||||
SECTION("InitialSizeIsZero") {
|
||||
REQUIRE(0 == _object.size());
|
||||
|
@ -6,8 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonObject::containsKey()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object = _jsonBuffer.createObject();
|
||||
DynamicJsonObject _object;
|
||||
|
||||
SECTION("ContainsKeyReturnsFalseForNonExistingKey") {
|
||||
_object.set("hello", 42);
|
||||
|
@ -8,8 +8,7 @@
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("JsonObject::get()") {
|
||||
DynamicJsonBuffer jb;
|
||||
JsonObject& obj = jb.createObject();
|
||||
DynamicJsonObject obj;
|
||||
|
||||
SECTION("GetConstCharPointer_GivenStringLiteral") {
|
||||
obj.set("hello", "world");
|
||||
|
@ -8,8 +8,7 @@
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("JsonObject::begin()/end()") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(2)> jb;
|
||||
JsonObject& obj = jb.createObject();
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(2)> obj;
|
||||
obj["ab"] = 12;
|
||||
obj["cd"] = 34;
|
||||
|
||||
|
@ -18,8 +18,7 @@ void check(const JsonObject &obj, const std::string expected) {
|
||||
}
|
||||
|
||||
TEST_CASE("JsonObject::prettyPrintTo()") {
|
||||
DynamicJsonBuffer jb;
|
||||
JsonObject &obj = jb.createObject();
|
||||
DynamicJsonObject obj;
|
||||
|
||||
SECTION("EmptyObject") {
|
||||
check(obj, "{}");
|
||||
|
@ -16,8 +16,7 @@ void check(const JsonObject &obj, const std::string &expected) {
|
||||
REQUIRE(expected.size() == measuredLen);
|
||||
}
|
||||
TEST_CASE("JsonObject::printTo()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject &obj = _jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
|
||||
SECTION("EmptyObject") {
|
||||
check(obj, "{}");
|
||||
@ -92,17 +91,22 @@ TEST_CASE("JsonObject::printTo()") {
|
||||
}
|
||||
|
||||
SECTION("ThreeNestedArrays") {
|
||||
DynamicJsonArray b, c;
|
||||
|
||||
obj.createNestedArray("a");
|
||||
obj["b"] = _jsonBuffer.createArray();
|
||||
obj.set("c", _jsonBuffer.createArray());
|
||||
obj["b"] = b;
|
||||
obj.set("c", c);
|
||||
|
||||
check(obj, "{\"a\":[],\"b\":[],\"c\":[]}");
|
||||
}
|
||||
|
||||
SECTION("ThreeNestedObjects") {
|
||||
DynamicJsonObject b;
|
||||
DynamicJsonObject c;
|
||||
|
||||
obj.createNestedObject("a");
|
||||
obj["b"] = _jsonBuffer.createObject();
|
||||
obj.set("c", _jsonBuffer.createObject());
|
||||
obj["b"] = b;
|
||||
obj.set("c", c);
|
||||
|
||||
check(obj, "{\"a\":{},\"b\":{},\"c\":{}}");
|
||||
}
|
||||
|
@ -7,10 +7,9 @@
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("JsonObject::remove()") {
|
||||
DynamicJsonBuffer jb;
|
||||
DynamicJsonObject obj;
|
||||
|
||||
SECTION("SizeDecreased_WhenValuesAreRemoved") {
|
||||
JsonObject& obj = jb.createObject();
|
||||
obj["hello"] = 1;
|
||||
|
||||
obj.remove("hello");
|
||||
@ -19,7 +18,6 @@ TEST_CASE("JsonObject::remove()") {
|
||||
}
|
||||
|
||||
SECTION("SizeUntouched_WhenRemoveIsCalledWithAWrongKey") {
|
||||
JsonObject& obj = jb.createObject();
|
||||
obj["hello"] = 1;
|
||||
|
||||
obj.remove("world");
|
||||
@ -28,7 +26,7 @@ TEST_CASE("JsonObject::remove()") {
|
||||
}
|
||||
|
||||
SECTION("RemoveByIterator") {
|
||||
JsonObject& obj = jb.parseObject("{\"a\":0,\"b\":1,\"c\":2}");
|
||||
deserializeJson(obj, "{\"a\":0,\"b\":1,\"c\":2}");
|
||||
|
||||
for (JsonObject::iterator it = obj.begin(); it != obj.end(); ++it) {
|
||||
if (it->value == 1) obj.remove(it);
|
||||
|
@ -7,8 +7,7 @@
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("JsonObject::set()") {
|
||||
DynamicJsonBuffer jb;
|
||||
JsonObject& _object = jb.createObject();
|
||||
DynamicJsonObject _object;
|
||||
|
||||
SECTION("int") {
|
||||
_object.set("hello", 123);
|
||||
@ -43,7 +42,7 @@ TEST_CASE("JsonObject::set()") {
|
||||
}
|
||||
|
||||
SECTION("nested array") {
|
||||
JsonArray& arr = jb.createArray();
|
||||
DynamicJsonArray arr;
|
||||
|
||||
_object.set("hello", arr);
|
||||
|
||||
@ -53,7 +52,7 @@ TEST_CASE("JsonObject::set()") {
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
JsonObject& obj = jb.createObject();
|
||||
DynamicJsonObject obj;
|
||||
|
||||
_object.set("hello", obj);
|
||||
|
||||
@ -63,7 +62,7 @@ TEST_CASE("JsonObject::set()") {
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
JsonArray& arr = jb.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(42);
|
||||
|
||||
_object.set("a", arr[0]);
|
||||
@ -72,7 +71,7 @@ TEST_CASE("JsonObject::set()") {
|
||||
}
|
||||
|
||||
SECTION("object subscript") {
|
||||
JsonObject& obj = jb.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set("x", 42);
|
||||
|
||||
_object.set("a", obj["x"]);
|
||||
@ -81,15 +80,13 @@ TEST_CASE("JsonObject::set()") {
|
||||
}
|
||||
|
||||
SECTION("returns true when allocation succeeds") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1) + 15> obj;
|
||||
|
||||
REQUIRE(true == obj.set(std::string("hello"), std::string("world")));
|
||||
}
|
||||
|
||||
SECTION("returns false when allocation fails") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1) + 10> obj;
|
||||
|
||||
REQUIRE(false == obj.set(std::string("hello"), std::string("world")));
|
||||
}
|
||||
@ -97,42 +94,42 @@ TEST_CASE("JsonObject::set()") {
|
||||
SECTION("should not duplicate const char*") {
|
||||
_object.set("hello", "world");
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1);
|
||||
REQUIRE(expectedSize == jb.size());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
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());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
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());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
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());
|
||||
REQUIRE(expectedSize <= _object.memoryUsage());
|
||||
}
|
||||
|
||||
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());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
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());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
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());
|
||||
REQUIRE(expectedSize <= _object.memoryUsage());
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,7 @@
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("JsonObject::size()") {
|
||||
DynamicJsonBuffer jb;
|
||||
JsonObject& _object = jb.createObject();
|
||||
DynamicJsonObject _object;
|
||||
|
||||
SECTION("increases when values are added") {
|
||||
_object.set("hello", 42);
|
||||
|
@ -6,8 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonObject::operator[]") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object = _jsonBuffer.createObject();
|
||||
DynamicJsonObject _object;
|
||||
|
||||
SECTION("int") {
|
||||
_object["hello"] = 123;
|
||||
@ -53,7 +52,7 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("array") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
|
||||
_object["hello"] = arr;
|
||||
|
||||
@ -69,7 +68,7 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("object") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
|
||||
_object["hello"] = obj;
|
||||
|
||||
@ -85,7 +84,7 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(42);
|
||||
|
||||
_object["a"] = arr[0];
|
||||
@ -94,7 +93,7 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("object subscript") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set("x", 42);
|
||||
|
||||
_object["a"] = obj["x"];
|
||||
@ -111,42 +110,42 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
SECTION("should not duplicate const char*") {
|
||||
_object["hello"] = "world";
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1);
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* value") {
|
||||
_object["hello"] = const_cast<char*>("world");
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* key") {
|
||||
_object[const_cast<char*>("hello")] = "world";
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
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());
|
||||
REQUIRE(expectedSize <= _object.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string value") {
|
||||
_object["hello"] = std::string("world");
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string key") {
|
||||
_object[std::string("hello")] = "world";
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
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());
|
||||
REQUIRE(expectedSize <= _object.memoryUsage());
|
||||
}
|
||||
}
|
||||
|
15
test/JsonParser/CMakeLists.txt
Normal file
15
test/JsonParser/CMakeLists.txt
Normal file
@ -0,0 +1,15 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(JsonParserTests
|
||||
JsonArray.cpp
|
||||
JsonObject.cpp
|
||||
JsonVariant.cpp
|
||||
nestingLimit.cpp
|
||||
StaticJsonArray.cpp
|
||||
StaticJsonObject.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(JsonParserTests catch)
|
||||
add_test(JsonParser JsonParserTests)
|
346
test/JsonParser/JsonArray.cpp
Normal file
346
test/JsonParser/JsonArray.cpp
Normal file
@ -0,0 +1,346 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeJson(JsonArray&)") {
|
||||
DynamicJsonArray arr;
|
||||
|
||||
SECTION("EmptyArray") {
|
||||
bool success = deserializeJson(arr, "[]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(0 == arr.size());
|
||||
}
|
||||
|
||||
SECTION("MissingOpeningBracket") {
|
||||
bool success = deserializeJson(arr, "]");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("ArrayWithNoEnd") {
|
||||
bool success = deserializeJson(arr, "[");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("EmptyArrayWithLeadingSpaces") {
|
||||
bool success = deserializeJson(arr, " []");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(0 == arr.size());
|
||||
}
|
||||
|
||||
SECTION("Garbage") {
|
||||
bool success = deserializeJson(arr, "%*$£¤");
|
||||
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("OneInteger") {
|
||||
bool success = deserializeJson(arr, "[42]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == 42);
|
||||
}
|
||||
|
||||
SECTION("OneIntegerWithSpacesBefore") {
|
||||
bool success = deserializeJson(arr, "[ \t\r\n42]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == 42);
|
||||
}
|
||||
|
||||
SECTION("OneIntegerWithSpaceAfter") {
|
||||
bool success = deserializeJson(arr, "[42 \t\r\n]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == 42);
|
||||
}
|
||||
|
||||
SECTION("TwoIntegers") {
|
||||
bool success = deserializeJson(arr, "[42,84]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == 42);
|
||||
REQUIRE(arr[1] == 84);
|
||||
}
|
||||
|
||||
SECTION("TwoDoubles") {
|
||||
bool success = deserializeJson(arr, "[4.2,1e2]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == 4.2);
|
||||
REQUIRE(arr[1] == 1e2);
|
||||
}
|
||||
|
||||
SECTION("UnsignedLong") {
|
||||
bool success = deserializeJson(arr, "[4294967295]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == 4294967295UL);
|
||||
}
|
||||
|
||||
SECTION("TwoBooleans") {
|
||||
bool success = deserializeJson(arr, "[true,false]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == true);
|
||||
REQUIRE(arr[1] == false);
|
||||
}
|
||||
|
||||
SECTION("TwoNulls") {
|
||||
bool success = deserializeJson(arr, "[null,null]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0].as<char *>() == 0);
|
||||
REQUIRE(arr[1].as<char *>() == 0);
|
||||
}
|
||||
|
||||
SECTION("TwoStringsDoubleQuotes") {
|
||||
bool success = deserializeJson(arr, "[ \"hello\" , \"world\" ]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("TwoStringsSingleQuotes") {
|
||||
bool success = deserializeJson(arr, "[ 'hello' , 'world' ]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("TwoStringsNoQuotes") {
|
||||
bool success = deserializeJson(arr, "[ hello , world ]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("EmptyStringsDoubleQuotes") {
|
||||
bool success = deserializeJson(arr, "[\"\",\"\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "");
|
||||
REQUIRE(arr[1] == "");
|
||||
}
|
||||
|
||||
SECTION("EmptyStringSingleQuotes") {
|
||||
bool success = deserializeJson(arr, "[\'\',\'\']");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "");
|
||||
REQUIRE(arr[1] == "");
|
||||
}
|
||||
|
||||
SECTION("EmptyStringNoQuotes") {
|
||||
bool success = deserializeJson(arr, "[,]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "");
|
||||
REQUIRE(arr[1] == "");
|
||||
}
|
||||
|
||||
SECTION("ClosingDoubleQuoteMissing") {
|
||||
bool success = deserializeJson(arr, "[\"]");
|
||||
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("ClosingSignleQuoteMissing") {
|
||||
bool success = deserializeJson(arr, "[\']");
|
||||
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("StringWithEscapedChars") {
|
||||
bool success =
|
||||
deserializeJson(arr, "[\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "1\"2\\3/4\b5\f6\n7\r8\t9");
|
||||
}
|
||||
|
||||
SECTION("StringWithUnterminatedEscapeSequence") {
|
||||
bool success = deserializeJson(arr, "\"\\\0\"", 4);
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("CCommentBeforeOpeningBracket") {
|
||||
bool success = deserializeJson(arr, "/*COMMENT*/ [\"hello\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CCommentAfterOpeningBracket") {
|
||||
bool success = deserializeJson(arr, "[/*COMMENT*/ \"hello\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CCommentBeforeClosingBracket") {
|
||||
bool success = deserializeJson(arr, "[\"hello\"/*COMMENT*/]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CCommentAfterClosingBracket") {
|
||||
bool success = deserializeJson(arr, "[\"hello\"]/*COMMENT*/");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CCommentBeforeComma") {
|
||||
bool success = deserializeJson(arr, "[\"hello\"/*COMMENT*/,\"world\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("CCommentAfterComma") {
|
||||
bool success = deserializeJson(arr, "[\"hello\",/*COMMENT*/ \"world\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("CppCommentBeforeOpeningBracket") {
|
||||
bool success = deserializeJson(arr, "//COMMENT\n\t[\"hello\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CppCommentAfterOpeningBracket") {
|
||||
bool success = deserializeJson(arr, "[//COMMENT\n\"hello\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CppCommentBeforeClosingBracket") {
|
||||
bool success = deserializeJson(arr, "[\"hello\"//COMMENT\r\n]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CppCommentAfterClosingBracket") {
|
||||
bool success = deserializeJson(arr, "[\"hello\"]//COMMENT\n");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CppCommentBeforeComma") {
|
||||
bool success = deserializeJson(arr, "[\"hello\"//COMMENT\n,\"world\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("CppCommentAfterComma") {
|
||||
bool success = deserializeJson(arr, "[\"hello\",//COMMENT\n\"world\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("InvalidCppComment") {
|
||||
bool success = deserializeJson(arr, "[/COMMENT\n]");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("InvalidComment") {
|
||||
bool success = deserializeJson(arr, "[/*/\n]");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("UnfinishedCComment") {
|
||||
bool success = deserializeJson(arr, "[/*COMMENT]");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("EndsInCppComment") {
|
||||
bool success = deserializeJson(arr, "[//COMMENT");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("AfterClosingStar") {
|
||||
bool success = deserializeJson(arr, "[/*COMMENT*");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("DeeplyNested") {
|
||||
bool success = deserializeJson(
|
||||
arr, "[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]");
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("ObjectNestedInArray") {
|
||||
char jsonString[] =
|
||||
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
|
||||
|
||||
bool success = deserializeJson(arr, jsonString);
|
||||
|
||||
JsonObject &object1 = arr[0];
|
||||
const JsonObject &object2 = arr[1];
|
||||
JsonObject &object3 = arr[2];
|
||||
|
||||
REQUIRE(true == success);
|
||||
|
||||
REQUIRE(true == object1.success());
|
||||
REQUIRE(true == object2.success());
|
||||
REQUIRE(false == object3.success());
|
||||
|
||||
REQUIRE(2 == object1.size());
|
||||
REQUIRE(2 == object2.size());
|
||||
REQUIRE(0 == object3.size());
|
||||
|
||||
REQUIRE(1 == object1["a"].as<int>());
|
||||
REQUIRE(2 == object1["b"].as<int>());
|
||||
REQUIRE(3 == object2["c"].as<int>());
|
||||
REQUIRE(4 == object2["d"].as<int>());
|
||||
REQUIRE(0 == object3["e"].as<int>());
|
||||
}
|
||||
}
|
197
test/JsonParser/JsonObject.cpp
Normal file
197
test/JsonParser/JsonObject.cpp
Normal file
@ -0,0 +1,197 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeJson(JsonObject&)") {
|
||||
DynamicJsonObject obj;
|
||||
|
||||
SECTION("An empty object") {
|
||||
bool success = deserializeJson(obj, "{}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Quotes") {
|
||||
SECTION("Double quotes") {
|
||||
bool success = deserializeJson(obj, "{\"key\":\"value\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("Single quotes") {
|
||||
bool success = deserializeJson(obj, "{'key':'value'}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("No quotes") {
|
||||
bool success = deserializeJson(obj, "{key:value}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("No quotes, allow underscore in key") {
|
||||
bool success = deserializeJson(obj, "{_k_e_y_:42}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["_k_e_y_"] == 42);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Spaces") {
|
||||
SECTION("Before the key") {
|
||||
bool success = deserializeJson(obj, "{ \"key\":\"value\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("After the key") {
|
||||
bool success = deserializeJson(obj, "{\"key\" :\"value\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("Before the value") {
|
||||
bool success = deserializeJson(obj, "{\"key\": \"value\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("After the value") {
|
||||
bool success = deserializeJson(obj, "{\"key\":\"value\" }");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("Before the colon") {
|
||||
bool success =
|
||||
deserializeJson(obj, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == "value1");
|
||||
REQUIRE(obj["key2"] == "value2");
|
||||
}
|
||||
|
||||
SECTION("After the colon") {
|
||||
bool success =
|
||||
deserializeJson(obj, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == "value1");
|
||||
REQUIRE(obj["key2"] == "value2");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Values types") {
|
||||
SECTION("String") {
|
||||
bool success =
|
||||
deserializeJson(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == "value1");
|
||||
REQUIRE(obj["key2"] == "value2");
|
||||
}
|
||||
|
||||
SECTION("Integer") {
|
||||
bool success = deserializeJson(obj, "{\"key1\":42,\"key2\":-42}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == 42);
|
||||
REQUIRE(obj["key2"] == -42);
|
||||
}
|
||||
|
||||
SECTION("Double") {
|
||||
bool success = deserializeJson(obj, "{\"key1\":12.345,\"key2\":-7E89}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == 12.345);
|
||||
REQUIRE(obj["key2"] == -7E89);
|
||||
}
|
||||
|
||||
SECTION("Booleans") {
|
||||
bool success = deserializeJson(obj, "{\"key1\":true,\"key2\":false}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == true);
|
||||
REQUIRE(obj["key2"] == false);
|
||||
}
|
||||
|
||||
SECTION("Null") {
|
||||
bool success = deserializeJson(obj, "{\"key1\":null,\"key2\":null}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"].as<char *>() == 0);
|
||||
REQUIRE(obj["key2"].as<char *>() == 0);
|
||||
}
|
||||
|
||||
SECTION("Array") {
|
||||
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
|
||||
|
||||
bool success = deserializeJson(obj, jsonString);
|
||||
|
||||
JsonArray &array1 = obj["ab"];
|
||||
const JsonArray &array2 = obj["cd"];
|
||||
JsonArray &array3 = obj["ef"];
|
||||
|
||||
REQUIRE(true == success);
|
||||
|
||||
REQUIRE(true == array1.success());
|
||||
REQUIRE(true == array2.success());
|
||||
REQUIRE(false == array3.success());
|
||||
|
||||
REQUIRE(2 == array1.size());
|
||||
REQUIRE(2 == array2.size());
|
||||
REQUIRE(0 == array3.size());
|
||||
|
||||
REQUIRE(1 == array1[0].as<int>());
|
||||
REQUIRE(2 == array1[1].as<int>());
|
||||
|
||||
REQUIRE(3 == array2[0].as<int>());
|
||||
REQUIRE(4 == array2[1].as<int>());
|
||||
|
||||
REQUIRE(0 == array3[0].as<int>());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Misc") {
|
||||
SECTION("The opening brace is missing") {
|
||||
bool success = deserializeJson(obj, "}");
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("The closing brace is missing") {
|
||||
bool success = deserializeJson(obj, "{");
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("A quoted key without value") {
|
||||
bool success = deserializeJson(obj, "{\"key\"}");
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("A non-quoted key without value") {
|
||||
bool success = deserializeJson(obj, "{key}");
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("A dangling comma") {
|
||||
bool success = deserializeJson(obj, "{\"key1\":\"value1\",}");
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("null as a key") {
|
||||
bool success = deserializeJson(obj, "null:\"value\"}");
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
}
|
||||
}
|
@ -7,73 +7,83 @@
|
||||
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("JsonBuffer::parse()") {
|
||||
DynamicJsonBuffer jb;
|
||||
TEST_CASE("deserializeJson(JsonVariant&)") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("EmptyObject") {
|
||||
JsonVariant variant = jb.parse("{}");
|
||||
REQUIRE(variant.success());
|
||||
bool success = deserializeJson(variant, "{}");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<JsonObject>());
|
||||
}
|
||||
|
||||
SECTION("EmptyArray") {
|
||||
JsonVariant variant = jb.parse("[]");
|
||||
REQUIRE(variant.success());
|
||||
bool success = deserializeJson(variant, "[]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<JsonArray>());
|
||||
}
|
||||
|
||||
SECTION("Integer") {
|
||||
JsonVariant variant = jb.parse("-42");
|
||||
REQUIRE(variant.success());
|
||||
bool success = deserializeJson(variant, "-42");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<int>());
|
||||
REQUIRE_FALSE(variant.is<bool>());
|
||||
REQUIRE(variant == -42);
|
||||
}
|
||||
|
||||
SECTION("Double") {
|
||||
JsonVariant variant = jb.parse("-1.23e+4");
|
||||
REQUIRE(variant.success());
|
||||
bool success = deserializeJson(variant, "-1.23e+4");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE_FALSE(variant.is<int>());
|
||||
REQUIRE(variant.is<double>());
|
||||
REQUIRE(variant.as<double>() == Approx(-1.23e+4));
|
||||
}
|
||||
|
||||
SECTION("Double quoted string") {
|
||||
JsonVariant variant = jb.parse("\"hello world\"");
|
||||
REQUIRE(variant.success());
|
||||
bool success = deserializeJson(variant, "\"hello world\"");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<char*>());
|
||||
REQUIRE_THAT(variant.as<char*>(), Equals("hello world"));
|
||||
}
|
||||
|
||||
SECTION("Single quoted string") {
|
||||
JsonVariant variant = jb.parse("\'hello world\'");
|
||||
REQUIRE(variant.success());
|
||||
bool success = deserializeJson(variant, "\'hello world\'");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<char*>());
|
||||
REQUIRE_THAT(variant.as<char*>(), Equals("hello world"));
|
||||
}
|
||||
|
||||
SECTION("True") {
|
||||
JsonVariant variant = jb.parse("true");
|
||||
REQUIRE(variant.success());
|
||||
bool success = deserializeJson(variant, "true");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<bool>());
|
||||
REQUIRE(variant == true);
|
||||
}
|
||||
|
||||
SECTION("False") {
|
||||
JsonVariant variant = jb.parse("false");
|
||||
REQUIRE(variant.success());
|
||||
bool success = deserializeJson(variant, "false");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<bool>());
|
||||
REQUIRE(variant == false);
|
||||
}
|
||||
|
||||
SECTION("OpenBrace") {
|
||||
JsonVariant variant = jb.parse("{");
|
||||
REQUIRE_FALSE(variant.success());
|
||||
bool success = deserializeJson(variant, "{");
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("Incomplete string") {
|
||||
JsonVariant variant = jb.parse("\"hello");
|
||||
REQUIRE(variant.success());
|
||||
bool success = deserializeJson(variant, "\"hello");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<char*>());
|
||||
REQUIRE_THAT(variant.as<char*>(), Equals("hello"));
|
||||
}
|
79
test/JsonParser/StaticJsonArray.cpp
Normal file
79
test/JsonParser/StaticJsonArray.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeJson(StaticJsonArray&)") {
|
||||
SECTION("BufferOfTheRightSizeForEmptyArray") {
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(0)> arr;
|
||||
char input[] = "[]";
|
||||
|
||||
bool success = deserializeJson(arr, input);
|
||||
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForArrayWithOneValue") {
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(1) - 1> arr;
|
||||
char input[] = "[1]";
|
||||
|
||||
bool success = deserializeJson(arr, input);
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(1)> arr;
|
||||
char input[] = "[1]";
|
||||
|
||||
bool success = deserializeJson(arr, input);
|
||||
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForArrayWithNestedObject") {
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> arr;
|
||||
char input[] = "[{}]";
|
||||
|
||||
bool success = deserializeJson(arr, input);
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> arr;
|
||||
char input[] = "[{}]";
|
||||
|
||||
bool success = deserializeJson(arr, input);
|
||||
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("CharPtrNull") {
|
||||
StaticJsonArray<100> arr;
|
||||
|
||||
bool success = deserializeJson(arr, static_cast<char*>(0));
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("ConstCharPtrNull") {
|
||||
StaticJsonArray<100> arr;
|
||||
|
||||
bool success = deserializeJson(arr, static_cast<const char*>(0));
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("CopyStringNotSpaces") {
|
||||
StaticJsonArray<100> arr;
|
||||
|
||||
deserializeJson(arr, " [ \"1234567\" ] ");
|
||||
|
||||
REQUIRE(JSON_ARRAY_SIZE(1) + sizeof("1234567") == arr.memoryUsage());
|
||||
// note: we use a string of 8 bytes to be sure that the StaticJsonBuffer
|
||||
// will not insert bytes to enforce alignement
|
||||
}
|
||||
}
|
69
test/JsonParser/StaticJsonObject.cpp
Normal file
69
test/JsonParser/StaticJsonObject.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeJson(StaticJsonObject&)") {
|
||||
SECTION("BufferOfTheRightSizeForEmptyObject") {
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(0)> obj;
|
||||
char input[] = "{}";
|
||||
|
||||
bool success = deserializeJson(obj, input);
|
||||
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForObjectWithOneValue") {
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1) - 1> obj;
|
||||
char input[] = "{\"a\":1}";
|
||||
|
||||
bool success = deserializeJson(obj, input);
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1)> obj;
|
||||
char input[] = "{\"a\":1}";
|
||||
|
||||
bool success = deserializeJson(obj, input);
|
||||
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForObjectWithNestedObject") {
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1> obj;
|
||||
char input[] = "{\"a\":[]}";
|
||||
|
||||
bool success = deserializeJson(obj, input);
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> obj;
|
||||
char input[] = "{\"a\":[]}";
|
||||
|
||||
bool success = deserializeJson(obj, input);
|
||||
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("CharPtrNull") {
|
||||
StaticJsonObject<100> obj;
|
||||
|
||||
bool success = deserializeJson(obj, static_cast<char*>(0));
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("ConstCharPtrNull") {
|
||||
StaticJsonObject<100> obj;
|
||||
|
||||
bool success = deserializeJson(obj, static_cast<const char*>(0));
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
}
|
@ -6,13 +6,13 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
bool tryParseArray(const char *json, uint8_t nestingLimit) {
|
||||
DynamicJsonBuffer buffer;
|
||||
return buffer.parseArray(json, nestingLimit).success();
|
||||
DynamicJsonArray array;
|
||||
return deserializeJson(array, json, nestingLimit);
|
||||
}
|
||||
|
||||
bool tryParseObject(const char *json, uint8_t nestingLimit) {
|
||||
DynamicJsonBuffer buffer;
|
||||
return buffer.parseObject(json, nestingLimit).success();
|
||||
DynamicJsonObject obj;
|
||||
return deserializeJson(obj, json, nestingLimit);
|
||||
}
|
||||
|
||||
TEST_CASE("JsonParser nestingLimit") {
|
@ -192,9 +192,8 @@ TEST_CASE("JsonVariant::as()") {
|
||||
}
|
||||
|
||||
SECTION("ObjectAsString") {
|
||||
DynamicJsonBuffer buffer;
|
||||
DynamicJsonObject obj;
|
||||
|
||||
JsonObject& obj = buffer.createObject();
|
||||
obj["key"] = "value";
|
||||
|
||||
JsonVariant variant = obj;
|
||||
@ -202,9 +201,7 @@ TEST_CASE("JsonVariant::as()") {
|
||||
}
|
||||
|
||||
SECTION("ArrayAsString") {
|
||||
DynamicJsonBuffer buffer;
|
||||
|
||||
JsonArray& arr = buffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(4);
|
||||
arr.add(2);
|
||||
|
||||
@ -213,8 +210,7 @@ TEST_CASE("JsonVariant::as()") {
|
||||
}
|
||||
|
||||
SECTION("ArrayAsJsonArray") {
|
||||
DynamicJsonBuffer buffer;
|
||||
JsonArray& arr = buffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
|
||||
JsonVariant variant = arr;
|
||||
REQUIRE(&arr == &variant.as<JsonArray&>());
|
||||
@ -222,11 +218,10 @@ TEST_CASE("JsonVariant::as()") {
|
||||
}
|
||||
|
||||
SECTION("ObjectAsJsonObject") {
|
||||
DynamicJsonBuffer buffer;
|
||||
JsonObject& arr = buffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
|
||||
JsonVariant variant = arr;
|
||||
REQUIRE(&arr == &variant.as<JsonObject&>());
|
||||
REQUIRE(&arr == &variant.as<JsonObject>()); // <- shorthand
|
||||
JsonVariant variant = obj;
|
||||
REQUIRE(&obj == &variant.as<JsonObject&>());
|
||||
REQUIRE(&obj == &variant.as<JsonObject>()); // <- shorthand
|
||||
}
|
||||
}
|
||||
|
@ -97,8 +97,8 @@ TEST_CASE("JsonVariant comparisons") {
|
||||
}
|
||||
|
||||
SECTION("StringLiteral") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parse("\"hello\"");
|
||||
DynamicJsonVariant variant;
|
||||
deserializeJson(variant, "\"hello\"");
|
||||
|
||||
REQUIRE(variant == "hello");
|
||||
REQUIRE_FALSE(variant != "hello");
|
||||
@ -114,8 +114,8 @@ TEST_CASE("JsonVariant comparisons") {
|
||||
}
|
||||
|
||||
SECTION("String") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parse("\"hello\"");
|
||||
DynamicJsonVariant variant;
|
||||
deserializeJson(variant, "\"hello\"");
|
||||
|
||||
REQUIRE(variant == std::string("hello"));
|
||||
REQUIRE_FALSE(variant != std::string("hello"));
|
||||
@ -179,9 +179,7 @@ TEST_CASE("JsonVariant comparisons") {
|
||||
}
|
||||
|
||||
SECTION("ArrayInVariant") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array1 = jsonBuffer.createArray();
|
||||
JsonArray& array2 = jsonBuffer.createArray();
|
||||
DynamicJsonArray array1, array2;
|
||||
|
||||
JsonVariant variant1 = array1;
|
||||
JsonVariant variant2 = array1;
|
||||
@ -195,9 +193,8 @@ TEST_CASE("JsonVariant comparisons") {
|
||||
}
|
||||
|
||||
SECTION("ObjectInVariant") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj1 = jsonBuffer.createObject();
|
||||
JsonObject& obj2 = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj1;
|
||||
DynamicJsonObject obj2;
|
||||
|
||||
JsonVariant variant1 = obj1;
|
||||
JsonVariant variant2 = obj1;
|
||||
@ -211,14 +208,10 @@ TEST_CASE("JsonVariant comparisons") {
|
||||
}
|
||||
|
||||
SECTION("VariantsOfDifferentTypes") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
DynamicJsonObject obj;
|
||||
DynamicJsonArray arr;
|
||||
JsonVariant variants[] = {
|
||||
true,
|
||||
42,
|
||||
666.667,
|
||||
"hello",
|
||||
jsonBuffer.createArray(),
|
||||
jsonBuffer.createObject(),
|
||||
true, 42, 666.667, "hello", arr, obj,
|
||||
};
|
||||
size_t n = sizeof(variants) / sizeof(variants[0]);
|
||||
|
||||
|
@ -6,7 +6,6 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonVariant copy") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonVariant _variant1;
|
||||
JsonVariant _variant2;
|
||||
|
||||
@ -43,7 +42,7 @@ TEST_CASE("JsonVariant copy") {
|
||||
}
|
||||
|
||||
SECTION("ObjectsAreCopiedByReference") {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
DynamicJsonObject object;
|
||||
|
||||
_variant1 = object;
|
||||
|
||||
@ -53,7 +52,7 @@ TEST_CASE("JsonVariant copy") {
|
||||
}
|
||||
|
||||
SECTION("ArraysAreCopiedByReference") {
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
DynamicJsonArray array;
|
||||
|
||||
_variant1 = array;
|
||||
|
||||
|
@ -72,7 +72,8 @@ TEST_CASE("JsonVariant::is()") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
|
||||
SECTION("JsonArray") {
|
||||
checkIsArray(jsonBuffer.createArray());
|
||||
DynamicJsonArray array;
|
||||
checkIsArray(array);
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
|
@ -122,9 +122,8 @@ TEST_CASE("JsonVariant set()/get()") {
|
||||
#endif
|
||||
|
||||
SECTION("CanStoreObject") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject &object = jsonBuffer.createObject();
|
||||
DynamicJsonObject object;
|
||||
|
||||
checkReference(object);
|
||||
checkReference<JsonObject>(object);
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,8 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonVariant::operator[]") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
|
||||
SECTION("Array") {
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
DynamicJsonArray array;
|
||||
array.add("element at index 0");
|
||||
array.add("element at index 1");
|
||||
|
||||
@ -26,7 +24,7 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("Object") {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
DynamicJsonObject object;
|
||||
object["a"] = "element at key \"a\"";
|
||||
object["b"] = "element at key \"b\"";
|
||||
|
||||
@ -54,21 +52,24 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("ObjectSetValue") {
|
||||
JsonVariant var = _jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
JsonVariant var = obj;
|
||||
var["hello"] = "world";
|
||||
REQUIRE(1 == var.size());
|
||||
REQUIRE(std::string("world") == var["hello"]);
|
||||
}
|
||||
|
||||
SECTION("ArraySetValue") {
|
||||
JsonVariant var = _jsonBuffer.parseArray("[\"hello\"]");
|
||||
DynamicJsonVariant var;
|
||||
deserializeJson(var, "[\"hello\"]");
|
||||
var[0] = "world";
|
||||
REQUIRE(1 == var.size());
|
||||
REQUIRE(std::string("world") == var[0]);
|
||||
}
|
||||
|
||||
SECTION("NestedObjectSetValue") {
|
||||
JsonVariant var = _jsonBuffer.parseArray("[{}]");
|
||||
DynamicJsonVariant var;
|
||||
deserializeJson(var, "[{}]");
|
||||
var[0]["hello"] = "world";
|
||||
REQUIRE(1 == var.size());
|
||||
REQUIRE(1 == var[0].size());
|
||||
|
@ -17,16 +17,16 @@ TEST_CASE("JsonVariant::success()") {
|
||||
}
|
||||
|
||||
SECTION("ReturnsTrue_WhenEmptyArray") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
DynamicJsonArray array;
|
||||
|
||||
JsonVariant variant = jsonBuffer.createArray();
|
||||
JsonVariant variant = array;
|
||||
REQUIRE(true == variant.success());
|
||||
}
|
||||
|
||||
SECTION("ReturnsTrue_WhenEmptyObject") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
DynamicJsonObject obj;
|
||||
|
||||
JsonVariant variant = jsonBuffer.createObject();
|
||||
JsonVariant variant = obj;
|
||||
REQUIRE(true == variant.success());
|
||||
}
|
||||
|
||||
|
@ -16,15 +16,15 @@
|
||||
#endif
|
||||
|
||||
TEST_CASE("Deprecated functions") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
|
||||
SECTION("JsonVariant::asArray()") {
|
||||
JsonVariant variant = jsonBuffer.createArray();
|
||||
DynamicJsonArray array;
|
||||
JsonVariant variant = array;
|
||||
REQUIRE(variant.asArray().success());
|
||||
}
|
||||
|
||||
SECTION("JsonVariant::asObject()") {
|
||||
JsonVariant variant = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
JsonVariant variant = obj;
|
||||
REQUIRE(variant.asObject().success());
|
||||
}
|
||||
|
||||
@ -34,7 +34,7 @@ TEST_CASE("Deprecated functions") {
|
||||
}
|
||||
|
||||
SECTION("JsonArray::removeAt()") {
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.removeAt(0);
|
||||
}
|
||||
|
||||
@ -59,7 +59,7 @@ TEST_CASE("Deprecated functions") {
|
||||
}
|
||||
|
||||
SECTION("JsonArraySubscript::set(double, uint8_t)") {
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(666);
|
||||
arr[0].set(123.45, 2);
|
||||
REQUIRE(123.45 == arr[0].as<double>());
|
||||
@ -68,26 +68,26 @@ TEST_CASE("Deprecated functions") {
|
||||
}
|
||||
|
||||
SECTION("JsonArray::add(double, uint8_t)") {
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(3.14159265358979323846, 4);
|
||||
}
|
||||
|
||||
SECTION("JsonArray::add(float, uint8_t)") {
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(3.14159265358979323846f, 4);
|
||||
}
|
||||
|
||||
SECTION("JsonObject::set(unsigned char[], double, uint8_t)") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set(key, 3.14, 2);
|
||||
|
||||
REQUIRE(3.14 == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject::set(const char*, double, uint8_t)") {
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set("hello", 123.45, 2);
|
||||
|
||||
REQUIRE(123.45 == obj["hello"].as<double>());
|
||||
@ -96,7 +96,7 @@ TEST_CASE("Deprecated functions") {
|
||||
}
|
||||
|
||||
SECTION("JsonObjectSubscript::set(double, uint8_t)") {
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj["hello"].set(123.45, 2);
|
||||
|
||||
REQUIRE(true == obj["hello"].is<double>());
|
||||
|
@ -23,8 +23,7 @@ TEST_CASE("std::stream") {
|
||||
|
||||
SECTION("JsonObject") {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& object = jsonBuffer.createObject();
|
||||
DynamicJsonObject object;
|
||||
object["key"] = "value";
|
||||
os << object;
|
||||
REQUIRE("{\"key\":\"value\"}" == os.str());
|
||||
@ -32,8 +31,7 @@ TEST_CASE("std::stream") {
|
||||
|
||||
SECTION("JsonObjectSubscript") {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& object = jsonBuffer.createObject();
|
||||
DynamicJsonObject object;
|
||||
object["key"] = "value";
|
||||
os << object["key"];
|
||||
REQUIRE("\"value\"" == os.str());
|
||||
@ -41,8 +39,7 @@ TEST_CASE("std::stream") {
|
||||
|
||||
SECTION("JsonArray") {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
DynamicJsonArray array;
|
||||
array.add("value");
|
||||
os << array;
|
||||
REQUIRE("[\"value\"]" == os.str());
|
||||
@ -50,8 +47,7 @@ TEST_CASE("std::stream") {
|
||||
|
||||
SECTION("JsonArraySubscript") {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
DynamicJsonArray array;
|
||||
array.add("value");
|
||||
os << array[0];
|
||||
REQUIRE("\"value\"" == os.str());
|
||||
@ -59,26 +55,28 @@ TEST_CASE("std::stream") {
|
||||
|
||||
SECTION("ParseArray") {
|
||||
std::istringstream json(" [ 42 /* comment */ ] ");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(json);
|
||||
REQUIRE(true == arr.success());
|
||||
DynamicJsonArray arr;
|
||||
bool success = deserializeJson(arr, json);
|
||||
|
||||
REQUIRE(true == success);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(42 == arr[0]);
|
||||
}
|
||||
|
||||
SECTION("ParseObject") {
|
||||
std::istringstream json(" { hello : world // comment\n }");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(json);
|
||||
REQUIRE(true == obj.success());
|
||||
DynamicJsonObject obj;
|
||||
bool success = deserializeJson(obj, json);
|
||||
|
||||
REQUIRE(true == success);
|
||||
REQUIRE(1 == obj.size());
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("ShouldNotReadPastTheEnd") {
|
||||
std::istringstream json("{}123");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
jsonBuffer.parseObject(json);
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, json);
|
||||
REQUIRE('1' == json.get());
|
||||
}
|
||||
}
|
||||
|
@ -13,231 +13,224 @@ static void eraseString(std::string &str) {
|
||||
TEST_CASE("std::string") {
|
||||
DynamicJsonBuffer jb;
|
||||
|
||||
SECTION("JsonBuffer_ParseArray") {
|
||||
std::string json("[\"hello\"]");
|
||||
JsonArray &array = jb.parseArray(json);
|
||||
eraseString(json);
|
||||
REQUIRE(true == array.success());
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
SECTION("JsonArray") {
|
||||
DynamicJsonArray array;
|
||||
|
||||
SECTION("deserializeJson") {
|
||||
std::string json("[\"hello\"]");
|
||||
|
||||
bool success = deserializeJson(array, json);
|
||||
eraseString(json);
|
||||
|
||||
REQUIRE(true == success);
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
}
|
||||
|
||||
SECTION("add()") {
|
||||
std::string value("hello");
|
||||
array.add(value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
}
|
||||
|
||||
SECTION("set()") {
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array.set(0, value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == array[0]);
|
||||
}
|
||||
|
||||
SECTION("operator[]") {
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array[0] = value;
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == array[0]);
|
||||
}
|
||||
|
||||
SECTION("printTo()") {
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
std::string json;
|
||||
array.printTo(json);
|
||||
REQUIRE(std::string("[4,2]") == json);
|
||||
}
|
||||
|
||||
SECTION("prettyPrintTo") {
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
std::string json;
|
||||
array.prettyPrintTo(json);
|
||||
REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("JsonBuffer_ParseObject") {
|
||||
std::string json("{\"hello\":\"world\"}");
|
||||
JsonObject &object = jb.parseObject(json);
|
||||
eraseString(json);
|
||||
REQUIRE(true == object.success());
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
SECTION("JsonObject") {
|
||||
DynamicJsonObject object;
|
||||
|
||||
SECTION("JsonObject_Subscript") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(std::string("value") == object[std::string("key")]);
|
||||
}
|
||||
SECTION("deserializeJson") {
|
||||
std::string json("{\"hello\":\"world\"}");
|
||||
|
||||
SECTION("JsonObject_ConstSubscript") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(std::string("value") == object[std::string("key")]);
|
||||
}
|
||||
bool success = deserializeJson(object, json);
|
||||
eraseString(json);
|
||||
|
||||
SECTION("JsonObject_SetKey") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key("hello");
|
||||
object.set(key, "world");
|
||||
eraseString(key);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
REQUIRE(true == success);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_SetValue") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string value("world");
|
||||
object.set("hello", value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
SECTION("operator[]") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
|
||||
SECTION("JsonObject_SetKeyValue") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key("hello");
|
||||
std::string value("world");
|
||||
object.set(key, value);
|
||||
eraseString(key);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
deserializeJson(object, json);
|
||||
|
||||
SECTION("JsonObject_SetToArraySubscript") {
|
||||
JsonArray &arr = jb.createArray();
|
||||
arr.add("world");
|
||||
REQUIRE(std::string("value") == object[std::string("key")]);
|
||||
}
|
||||
|
||||
JsonObject &object = jb.createObject();
|
||||
object.set(std::string("hello"), arr[0]);
|
||||
SECTION("operator[] const") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
deserializeJson(object, json);
|
||||
const JsonObject &obj = object;
|
||||
|
||||
SECTION("JsonObject_SetToObjectSubscript") {
|
||||
JsonObject &arr = jb.createObject();
|
||||
arr.set("x", "world");
|
||||
REQUIRE(std::string("value") == obj[std::string("key")]);
|
||||
}
|
||||
|
||||
JsonObject &object = jb.createObject();
|
||||
object.set(std::string("hello"), arr["x"]);
|
||||
SECTION("set(key)") {
|
||||
std::string key("hello");
|
||||
object.set(key, "world");
|
||||
eraseString(key);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
SECTION("set(value)") {
|
||||
std::string value("world");
|
||||
object.set("hello", value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_Get") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(std::string("value") ==
|
||||
object.get<const char *>(std::string("key")));
|
||||
}
|
||||
SECTION("set(key,value)") {
|
||||
std::string key("hello");
|
||||
std::string value("world");
|
||||
object.set(key, value);
|
||||
eraseString(key);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_GetT") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(std::string("value") ==
|
||||
object.get<const char *>(std::string("key")));
|
||||
}
|
||||
SECTION("set(JsonArraySubscript)") {
|
||||
DynamicJsonArray arr;
|
||||
arr.add("world");
|
||||
|
||||
SECTION("JsonObject_IsT") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(true == object.is<const char *>(std::string("key")));
|
||||
}
|
||||
object.set(std::string("hello"), arr[0]);
|
||||
|
||||
SECTION("JsonObject_CreateNestedObject") {
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
JsonObject &object = jb.createObject();
|
||||
object.createNestedObject(key);
|
||||
eraseString(key);
|
||||
object.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("{\"key\":{}}") == json);
|
||||
}
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_CreateNestedArray") {
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
JsonObject &object = jb.createObject();
|
||||
object.createNestedArray(key);
|
||||
eraseString(key);
|
||||
object.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("{\"key\":[]}") == json);
|
||||
}
|
||||
SECTION("set(JsonObjectSubscript)") {
|
||||
DynamicJsonObject obj;
|
||||
obj.set("x", "world");
|
||||
|
||||
SECTION("JsonObject_ContainsKey") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(true == object.containsKey(std::string("key")));
|
||||
}
|
||||
object.set(std::string("hello"), obj["x"]);
|
||||
|
||||
SECTION("JsonObject_Remove") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(1 == object.size());
|
||||
object.remove(std::string("key"));
|
||||
REQUIRE(0 == object.size());
|
||||
}
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObjectSubscript_SetKey") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key("hello");
|
||||
object[key] = "world";
|
||||
eraseString(key);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
SECTION("get<T>()") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
deserializeJson(object, json);
|
||||
|
||||
SECTION("JsonObjectSubscript_SetValue") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string value("world");
|
||||
object["hello"] = value;
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
REQUIRE(std::string("value") ==
|
||||
object.get<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
SECTION("JsonArray_Add") {
|
||||
JsonArray &array = jb.createArray();
|
||||
std::string value("hello");
|
||||
array.add(value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
}
|
||||
SECTION("is<T>()") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
deserializeJson(object, json);
|
||||
REQUIRE(true == object.is<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
SECTION("JsonArray_Set") {
|
||||
JsonArray &array = jb.createArray();
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array.set(0, value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == array[0]);
|
||||
}
|
||||
SECTION("createNestedObject()") {
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
object.createNestedObject(key);
|
||||
eraseString(key);
|
||||
object.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("{\"key\":{}}") == json);
|
||||
}
|
||||
|
||||
SECTION("JsonArraySubscript") {
|
||||
JsonArray &array = jb.createArray();
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array[0] = value;
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == array[0]);
|
||||
}
|
||||
SECTION("createNestedArray()") {
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
object.createNestedArray(key);
|
||||
eraseString(key);
|
||||
object.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("{\"key\":[]}") == json);
|
||||
}
|
||||
|
||||
SECTION("JsonArray_PrintTo") {
|
||||
JsonArray &array = jb.createArray();
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
std::string json;
|
||||
array.printTo(json);
|
||||
REQUIRE(std::string("[4,2]") == json);
|
||||
}
|
||||
SECTION("containsKey()") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
deserializeJson(object, json);
|
||||
REQUIRE(true == object.containsKey(std::string("key")));
|
||||
}
|
||||
|
||||
SECTION("JsonArray_PrettyPrintTo") {
|
||||
JsonArray &array = jb.createArray();
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
std::string json;
|
||||
array.prettyPrintTo(json);
|
||||
REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json);
|
||||
}
|
||||
SECTION("remove()") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
deserializeJson(object, json);
|
||||
REQUIRE(1 == object.size());
|
||||
object.remove(std::string("key"));
|
||||
REQUIRE(0 == object.size());
|
||||
}
|
||||
|
||||
SECTION("JsonObject_PrintTo") {
|
||||
JsonObject &object = jb.createObject();
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.printTo(json);
|
||||
REQUIRE(std::string("{\"key\":\"value\"}") == json);
|
||||
}
|
||||
SECTION("operator[], set key") {
|
||||
std::string key("hello");
|
||||
object[key] = "world";
|
||||
eraseString(key);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_PrettyPrintTo") {
|
||||
JsonObject &object = jb.createObject();
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.prettyPrintTo(json);
|
||||
REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json);
|
||||
}
|
||||
SECTION("operator[], set value") {
|
||||
std::string value("world");
|
||||
object["hello"] = value;
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonBuffer_GrowWhenAddingNewKey") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key1("hello"), key2("world");
|
||||
SECTION("printTo") {
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.printTo(json);
|
||||
REQUIRE(std::string("{\"key\":\"value\"}") == json);
|
||||
}
|
||||
|
||||
object[key1] = 1;
|
||||
size_t sizeBefore = jb.size();
|
||||
object[key2] = 2;
|
||||
size_t sizeAfter = jb.size();
|
||||
SECTION("prettyPrintTo") {
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.prettyPrintTo(json);
|
||||
REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json);
|
||||
}
|
||||
|
||||
REQUIRE(sizeAfter - sizeBefore >= key2.size());
|
||||
}
|
||||
SECTION("memoryUsage() increases when adding a new key") {
|
||||
std::string key1("hello"), key2("world");
|
||||
|
||||
SECTION("JsonBuffer_DontGrowWhenReusingKey") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key("hello");
|
||||
object[key1] = 1;
|
||||
size_t sizeBefore = object.memoryUsage();
|
||||
object[key2] = 2;
|
||||
size_t sizeAfter = object.memoryUsage();
|
||||
|
||||
object[key] = 1;
|
||||
size_t sizeBefore = jb.size();
|
||||
object[key] = 2;
|
||||
size_t sizeAfter = jb.size();
|
||||
REQUIRE(sizeAfter - sizeBefore >= key2.size());
|
||||
}
|
||||
|
||||
REQUIRE(sizeBefore == sizeAfter);
|
||||
SECTION("memoryUsage() remains when adding the same key") {
|
||||
std::string key("hello");
|
||||
|
||||
object[key] = 1;
|
||||
size_t sizeBefore = object.memoryUsage();
|
||||
object[key] = 2;
|
||||
size_t sizeAfter = object.memoryUsage();
|
||||
|
||||
REQUIRE(sizeBefore == sizeAfter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,19 +13,19 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonBuffer::parseArray") {
|
||||
unsigned char json[] = "[42]";
|
||||
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(json);
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(1)> arr;
|
||||
bool success = deserializeJson(arr, json);
|
||||
|
||||
REQUIRE(true == arr.success());
|
||||
REQUIRE(true == success);
|
||||
}
|
||||
|
||||
SECTION("JsonBuffer::parseObject") {
|
||||
unsigned char json[] = "{\"a\":42}";
|
||||
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(json);
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1)> obj;
|
||||
bool success = deserializeJson(obj, json);
|
||||
|
||||
REQUIRE(true == obj.success());
|
||||
REQUIRE(true == success);
|
||||
}
|
||||
|
||||
SECTION("JsonVariant constructor") {
|
||||
@ -49,8 +49,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonVariant::operator[]") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonVariant variant;
|
||||
deserializeJson(variant, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == variant[key]);
|
||||
}
|
||||
@ -60,8 +60,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonVariant::operator[] const") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonVariant variant;
|
||||
deserializeJson(variant, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == variant[key]);
|
||||
}
|
||||
@ -70,8 +70,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonVariant::operator==") {
|
||||
unsigned char comparand[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "hello";
|
||||
DynamicJsonVariant variant;
|
||||
variant = "hello";
|
||||
|
||||
REQUIRE(comparand == variant);
|
||||
REQUIRE(variant == comparand);
|
||||
@ -82,8 +82,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonVariant::operator!=") {
|
||||
unsigned char comparand[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "world";
|
||||
DynamicJsonVariant variant;
|
||||
variant = "world";
|
||||
|
||||
REQUIRE(comparand != variant);
|
||||
REQUIRE(variant != comparand);
|
||||
@ -95,8 +95,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::operator[]") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj[key] = "world";
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -106,8 +105,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObjectSubscript::operator=") { // issue #416
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj["hello"] = value;
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -116,8 +114,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObjectSubscript::set()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj["hello"].set(value);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -127,8 +124,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::operator[] const") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == obj[key]);
|
||||
}
|
||||
@ -137,8 +134,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::get()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == obj.get<char*>(key));
|
||||
}
|
||||
@ -146,8 +143,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::set() key") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set(key, "world");
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -156,8 +152,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::set() value") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set("hello", value);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -166,8 +161,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::set key&value") {
|
||||
unsigned char key[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set(key, key);
|
||||
|
||||
REQUIRE(std::string("world") == obj["world"]);
|
||||
@ -176,8 +170,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::containsKey()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(true == obj.containsKey(key));
|
||||
}
|
||||
@ -185,8 +179,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::remove()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
obj.remove(key);
|
||||
|
||||
REQUIRE(0 == obj.size());
|
||||
@ -195,8 +189,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::is()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":42}");
|
||||
|
||||
REQUIRE(true == obj.is<int>(key));
|
||||
}
|
||||
@ -204,24 +198,21 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::createNestedArray()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.createNestedArray(key);
|
||||
}
|
||||
|
||||
SECTION("JsonObject::createNestedObject()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.createNestedObject(key);
|
||||
}
|
||||
|
||||
SECTION("JsonArray::add()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(value);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
@ -230,8 +221,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonArray::set()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
arr.set(0, value);
|
||||
|
||||
@ -241,8 +231,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonArraySubscript::set()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
arr[0].set(value);
|
||||
|
||||
@ -252,8 +241,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonArraySubscript::operator=") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
arr[0] = value;
|
||||
|
||||
|
@ -22,10 +22,10 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "[42]");
|
||||
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(vla);
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(1)> arr;
|
||||
bool success = deserializeJson(arr, vla);
|
||||
|
||||
REQUIRE(true == arr.success());
|
||||
REQUIRE(true == success);
|
||||
}
|
||||
|
||||
SECTION("ParseObject") {
|
||||
@ -33,8 +33,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "{\"a\":42}");
|
||||
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(vla);
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1)> obj;
|
||||
deserializeJson(obj, vla);
|
||||
|
||||
REQUIRE(true == obj.success());
|
||||
}
|
||||
@ -44,8 +44,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "42");
|
||||
|
||||
StaticJsonBuffer<1> jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parse(vla);
|
||||
StaticJsonVariant<> variant;
|
||||
deserializeJson(variant, vla);
|
||||
|
||||
REQUIRE(42 == variant.as<int>());
|
||||
}
|
||||
@ -77,8 +77,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonVariant variant;
|
||||
deserializeJson(variant, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == variant[vla]);
|
||||
}
|
||||
@ -90,8 +90,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonVariant variant;
|
||||
deserializeJson(variant, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == variant[vla]);
|
||||
}
|
||||
@ -102,8 +102,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "hello";
|
||||
DynamicJsonVariant variant;
|
||||
variant = "hello";
|
||||
|
||||
REQUIRE((vla == variant));
|
||||
REQUIRE((variant == vla));
|
||||
@ -116,8 +116,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "world";
|
||||
DynamicJsonVariant variant;
|
||||
variant = "world";
|
||||
|
||||
REQUIRE((vla != variant));
|
||||
REQUIRE((variant != vla));
|
||||
@ -131,8 +131,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj[vla] = "world";
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -144,8 +143,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj["hello"] = vla;
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"].as<char*>());
|
||||
@ -156,8 +154,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj["hello"].set(vla);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"].as<char*>());
|
||||
@ -169,8 +166,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == obj[vla]);
|
||||
}
|
||||
@ -181,8 +178,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == obj.get<char*>(vla));
|
||||
}
|
||||
@ -192,8 +189,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set(vla, "world");
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -204,8 +200,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set("hello", vla);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -216,8 +211,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set(vla, vla);
|
||||
|
||||
REQUIRE(std::string("world") == obj["world"]);
|
||||
@ -228,8 +222,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(true == obj.containsKey(vla));
|
||||
}
|
||||
@ -239,8 +233,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
obj.remove(vla);
|
||||
|
||||
REQUIRE(0 == obj.size());
|
||||
@ -251,8 +245,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":42}");
|
||||
|
||||
REQUIRE(true == obj.is<int>(vla));
|
||||
}
|
||||
@ -262,8 +256,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.createNestedArray(vla);
|
||||
}
|
||||
|
||||
@ -272,8 +265,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.createNestedObject(vla);
|
||||
}
|
||||
|
||||
@ -282,8 +274,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(vla);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
@ -294,8 +285,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
arr.set(0, vla);
|
||||
|
||||
@ -307,8 +297,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
arr[0].set(vla);
|
||||
|
||||
@ -320,8 +309,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
arr[0] = vla;
|
||||
|
||||
|
@ -4,10 +4,6 @@
|
||||
|
||||
add_executable(StaticJsonBufferTests
|
||||
alloc.cpp
|
||||
createArray.cpp
|
||||
createObject.cpp
|
||||
parseArray.cpp
|
||||
parseObject.cpp
|
||||
size.cpp
|
||||
startString.cpp
|
||||
)
|
||||
|
@ -1,45 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("StaticJsonBuffer::createArray()") {
|
||||
SECTION("GrowsWithArray") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
REQUIRE(JSON_ARRAY_SIZE(0) == json.size());
|
||||
|
||||
array.add("hello");
|
||||
REQUIRE(JSON_ARRAY_SIZE(1) == json.size());
|
||||
|
||||
array.add("world");
|
||||
REQUIRE(JSON_ARRAY_SIZE(2) == json.size());
|
||||
}
|
||||
|
||||
SECTION("SucceedWhenBigEnough") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
REQUIRE(array.success());
|
||||
}
|
||||
|
||||
SECTION("FailsWhenTooSmall") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
REQUIRE_FALSE(array.success());
|
||||
}
|
||||
|
||||
SECTION("ArrayDoesntGrowWhenFull") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
|
||||
REQUIRE(1 == array.size());
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("StaticJsonBuffer::createObject()") {
|
||||
SECTION("GrowsWithObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(3)> buffer;
|
||||
|
||||
JsonObject &obj = buffer.createObject();
|
||||
REQUIRE(JSON_OBJECT_SIZE(0) == buffer.size());
|
||||
|
||||
obj["hello"];
|
||||
REQUIRE(JSON_OBJECT_SIZE(0) == buffer.size());
|
||||
|
||||
obj["hello"] = 1;
|
||||
REQUIRE(JSON_OBJECT_SIZE(1) == buffer.size());
|
||||
|
||||
obj["world"] = 2;
|
||||
REQUIRE(JSON_OBJECT_SIZE(2) == buffer.size());
|
||||
|
||||
obj["world"] = 3; // <- same key, should not grow
|
||||
REQUIRE(JSON_OBJECT_SIZE(2) == buffer.size());
|
||||
}
|
||||
|
||||
SECTION("SucceedWhenBigEnough") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> buffer;
|
||||
|
||||
JsonObject &object = buffer.createObject();
|
||||
REQUIRE(object.success());
|
||||
}
|
||||
|
||||
SECTION("FailsWhenTooSmall") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> buffer;
|
||||
|
||||
JsonObject &object = buffer.createObject();
|
||||
REQUIRE_FALSE(object.success());
|
||||
}
|
||||
|
||||
SECTION("ObjectDoesntGrowWhenFull") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> buffer;
|
||||
|
||||
JsonObject &obj = buffer.createObject();
|
||||
obj["hello"] = 1;
|
||||
obj["world"] = 2;
|
||||
|
||||
REQUIRE(JSON_OBJECT_SIZE(1) == buffer.size());
|
||||
REQUIRE(1 == obj.size());
|
||||
|
||||
char json[64];
|
||||
obj.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("{\"hello\":1}") == json);
|
||||
}
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("StaticJsonBuffer::parseArray()") {
|
||||
SECTION("TooSmallBufferForEmptyArray") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
|
||||
char input[] = "[]";
|
||||
JsonArray& arr = bufferTooSmall.parseArray(input);
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForEmptyArray") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
|
||||
char input[] = "[]";
|
||||
JsonArray& arr = bufferOfRightSize.parseArray(input);
|
||||
REQUIRE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForArrayWithOneValue") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
|
||||
char input[] = "[1]";
|
||||
JsonArray& arr = bufferTooSmall.parseArray(input);
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
|
||||
char input[] = "[1]";
|
||||
JsonArray& arr = bufferOfRightSize.parseArray(input);
|
||||
REQUIRE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForArrayWithNestedObject") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1>
|
||||
bufferTooSmall;
|
||||
char input[] = "[{}]";
|
||||
JsonArray& arr = bufferTooSmall.parseArray(input);
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)>
|
||||
bufferOfRightSize;
|
||||
char input[] = "[{}]";
|
||||
JsonArray& arr = bufferOfRightSize.parseArray(input);
|
||||
REQUIRE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("CharPtrNull") {
|
||||
REQUIRE_FALSE(
|
||||
StaticJsonBuffer<100>().parseArray(static_cast<char*>(0)).success());
|
||||
}
|
||||
|
||||
SECTION("ConstCharPtrNull") {
|
||||
REQUIRE_FALSE(StaticJsonBuffer<100>()
|
||||
.parseArray(static_cast<const char*>(0))
|
||||
.success());
|
||||
}
|
||||
|
||||
SECTION("CopyStringNotSpaces") {
|
||||
StaticJsonBuffer<100> jsonBuffer;
|
||||
jsonBuffer.parseArray(" [ \"1234567\" ] ");
|
||||
REQUIRE(JSON_ARRAY_SIZE(1) + sizeof("1234567") == jsonBuffer.size());
|
||||
// note we use a string of 8 bytes to be sure that the StaticJsonBuffer
|
||||
// will not insert bytes to enforce alignement
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
TEST_CASE("StaticJsonBuffer::parseObject()") {
|
||||
SECTION("TooSmallBufferForEmptyObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
|
||||
char input[] = "{}";
|
||||
JsonObject& obj = bufferTooSmall.parseObject(input);
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForEmptyObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
|
||||
char input[] = "{}";
|
||||
JsonObject& obj = bufferOfRightSize.parseObject(input);
|
||||
REQUIRE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForObjectWithOneValue") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall;
|
||||
char input[] = "{\"a\":1}";
|
||||
JsonObject& obj = bufferTooSmall.parseObject(input);
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize;
|
||||
char input[] = "{\"a\":1}";
|
||||
JsonObject& obj = bufferOfRightSize.parseObject(input);
|
||||
REQUIRE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForObjectWithNestedObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1>
|
||||
bufferTooSmall;
|
||||
char input[] = "{\"a\":[]}";
|
||||
JsonObject& obj = bufferTooSmall.parseObject(input);
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)>
|
||||
bufferOfRightSize;
|
||||
char input[] = "{\"a\":[]}";
|
||||
JsonObject& obj = bufferOfRightSize.parseObject(input);
|
||||
REQUIRE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("CharPtrNull") {
|
||||
REQUIRE_FALSE(
|
||||
StaticJsonBuffer<100>().parseObject(static_cast<char*>(0)).success());
|
||||
}
|
||||
|
||||
SECTION("ConstCharPtrNull") {
|
||||
REQUIRE_FALSE(StaticJsonBuffer<100>()
|
||||
.parseObject(static_cast<const char*>(0))
|
||||
.success());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user