add missing lib
This commit is contained in:
1 parent
6318592a61
commit
75deae6846
218 files changed
+26961
No files matched your search
@@ -0,0 +1,21 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(JsonArrayTests
|
||||
add.cpp
|
||||
basics.cpp
|
||||
copyFrom.cpp
|
||||
copyTo.cpp
|
||||
invalid.cpp
|
||||
iterator.cpp
|
||||
prettyPrintTo.cpp
|
||||
printTo.cpp
|
||||
remove.cpp
|
||||
set.cpp
|
||||
size.cpp
|
||||
subscript.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(JsonArrayTests catch)
|
||||
add_test(JsonArray JsonArrayTests)
|
||||
@@ -0,0 +1,110 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::add()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
|
||||
SECTION("int") {
|
||||
_array.add(123);
|
||||
REQUIRE(123 == _array[0].as<int>());
|
||||
REQUIRE(_array[0].is<int>());
|
||||
REQUIRE(_array[0].is<double>());
|
||||
}
|
||||
|
||||
SECTION("double") {
|
||||
_array.add(123.45);
|
||||
REQUIRE(123.45 == _array[0].as<double>());
|
||||
REQUIRE(_array[0].is<double>());
|
||||
REQUIRE_FALSE(_array[0].is<bool>());
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
_array.add(true);
|
||||
REQUIRE(true == _array[0].as<bool>());
|
||||
REQUIRE(_array[0].is<bool>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
const char* str = "hello";
|
||||
_array.add(str);
|
||||
REQUIRE(str == _array[0].as<std::string>());
|
||||
REQUIRE(_array[0].is<const char*>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("nested array") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
|
||||
_array.add(arr);
|
||||
|
||||
REQUIRE(&arr == &_array[0].as<JsonArray&>());
|
||||
REQUIRE(_array[0].is<JsonArray&>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
|
||||
_array.add(obj);
|
||||
|
||||
REQUIRE(&obj == &_array[0].as<JsonObject&>());
|
||||
REQUIRE(_array[0].is<JsonObject&>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
const char* str = "hello";
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add(str);
|
||||
|
||||
_array.add(arr[0]);
|
||||
|
||||
REQUIRE(str == _array[0]);
|
||||
}
|
||||
|
||||
SECTION("object subscript") {
|
||||
const char* str = "hello";
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj["x"] = str;
|
||||
|
||||
_array.add(obj["x"]);
|
||||
|
||||
REQUIRE(str == _array[0]);
|
||||
}
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
_array.add("world");
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char*") {
|
||||
_array.add(const_cast<char*>("world"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string") {
|
||||
_array.add(std::string("world"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
}
|
||||
|
||||
SECTION("should not duplicate RawJson(const char*)") {
|
||||
_array.add(RawJson("{}"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
}
|
||||
|
||||
SECTION("should duplicate RawJson(char*)") {
|
||||
_array.add(RawJson(const_cast<char*>("{}")));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 3;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray basics") {
|
||||
DynamicJsonBuffer jb;
|
||||
JsonArray& array = jb.createArray();
|
||||
|
||||
SECTION("SuccessIsTrue") {
|
||||
REQUIRE(array.success());
|
||||
}
|
||||
|
||||
SECTION("InitialSizeIsZero") {
|
||||
REQUIRE(0U == array.size());
|
||||
}
|
||||
|
||||
SECTION("CreateNestedArray") {
|
||||
JsonArray& arr = array.createNestedArray();
|
||||
REQUIRE(&arr == &array[0].as<JsonArray&>());
|
||||
}
|
||||
|
||||
SECTION("CreateNestedObject") {
|
||||
JsonObject& obj = array.createNestedObject();
|
||||
REQUIRE(&obj == &array[0].as<JsonObject&>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::copyFrom()") {
|
||||
SECTION("OneDimension") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
char json[32];
|
||||
int source[] = {1, 2, 3};
|
||||
|
||||
bool ok = array.copyFrom(source);
|
||||
REQUIRE(ok);
|
||||
|
||||
array.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("[1,2,3]") == json);
|
||||
}
|
||||
|
||||
SECTION("OneDimension_JsonBufferTooSmall") {
|
||||
const size_t SIZE = JSON_ARRAY_SIZE(2);
|
||||
StaticJsonBuffer<SIZE> jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
char json[32];
|
||||
int source[] = {1, 2, 3};
|
||||
|
||||
bool ok = array.copyFrom(source);
|
||||
REQUIRE_FALSE(ok);
|
||||
|
||||
array.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("[1,2]") == json);
|
||||
}
|
||||
|
||||
SECTION("TwoDimensions") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
char json[32];
|
||||
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
||||
|
||||
bool ok = array.copyFrom(source);
|
||||
REQUIRE(ok);
|
||||
|
||||
array.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json);
|
||||
}
|
||||
|
||||
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();
|
||||
char json[32];
|
||||
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
||||
|
||||
bool ok = array.copyFrom(source);
|
||||
REQUIRE_FALSE(ok);
|
||||
|
||||
array.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("[[1,2,3],[4,5]]") == json);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::copyTo()") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
|
||||
SECTION("BiggerOneDimensionIntegerArray") {
|
||||
char json[] = "[1,2,3]";
|
||||
JsonArray& array = jsonBuffer.parseArray(json);
|
||||
|
||||
int destination[4] = {0};
|
||||
size_t result = array.copyTo(destination);
|
||||
|
||||
REQUIRE(3 == result);
|
||||
REQUIRE(1 == destination[0]);
|
||||
REQUIRE(2 == destination[1]);
|
||||
REQUIRE(3 == destination[2]);
|
||||
REQUIRE(0 == destination[3]);
|
||||
}
|
||||
|
||||
SECTION("SmallerOneDimensionIntegerArray") {
|
||||
char json[] = "[1,2,3]";
|
||||
JsonArray& array = jsonBuffer.parseArray(json);
|
||||
|
||||
int destination[2] = {0};
|
||||
size_t result = array.copyTo(destination);
|
||||
|
||||
REQUIRE(2 == result);
|
||||
REQUIRE(1 == destination[0]);
|
||||
REQUIRE(2 == destination[1]);
|
||||
}
|
||||
|
||||
SECTION("TwoOneDimensionIntegerArray") {
|
||||
char json[] = "[[1,2],[3],[4]]";
|
||||
|
||||
JsonArray& array = jsonBuffer.parseArray(json);
|
||||
|
||||
int destination[3][2] = {{0}};
|
||||
array.copyTo(destination);
|
||||
|
||||
REQUIRE(1 == destination[0][0]);
|
||||
REQUIRE(2 == destination[0][1]);
|
||||
REQUIRE(3 == destination[1][0]);
|
||||
REQUIRE(0 == destination[1][1]);
|
||||
REQUIRE(4 == destination[2][0]);
|
||||
REQUIRE(0 == destination[2][1]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("JsonArray::invalid()") {
|
||||
SECTION("SubscriptFails") {
|
||||
REQUIRE_FALSE(JsonArray::invalid()[0].success());
|
||||
}
|
||||
|
||||
SECTION("AddFails") {
|
||||
JsonArray& array = JsonArray::invalid();
|
||||
array.add(1);
|
||||
REQUIRE(0 == array.size());
|
||||
}
|
||||
|
||||
SECTION("CreateNestedArrayFails") {
|
||||
REQUIRE_FALSE(JsonArray::invalid().createNestedArray().success());
|
||||
}
|
||||
|
||||
SECTION("CreateNestedObjectFails") {
|
||||
REQUIRE_FALSE(JsonArray::invalid().createNestedObject().success());
|
||||
}
|
||||
|
||||
SECTION("PrintToWritesBrackets") {
|
||||
char buffer[32];
|
||||
JsonArray::invalid().printTo(buffer, sizeof(buffer));
|
||||
REQUIRE_THAT(buffer, Equals("[]"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
template <typename TIterator>
|
||||
static void run_iterator_test() {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> jsonBuffer;
|
||||
|
||||
JsonArray &array = jsonBuffer.createArray();
|
||||
array.add(12);
|
||||
array.add(34);
|
||||
|
||||
TIterator it = array.begin();
|
||||
TIterator end = array.end();
|
||||
|
||||
REQUIRE(end != it);
|
||||
REQUIRE(12 == it->template as<int>());
|
||||
REQUIRE(12 == static_cast<int>(*it));
|
||||
++it;
|
||||
REQUIRE(end != it);
|
||||
REQUIRE(34 == it->template as<int>());
|
||||
REQUIRE(34 == static_cast<int>(*it));
|
||||
++it;
|
||||
REQUIRE(end == it);
|
||||
}
|
||||
|
||||
TEST_CASE("JsonArray::begin()/end()") {
|
||||
SECTION("Mutable") {
|
||||
run_iterator_test<JsonArray::iterator>();
|
||||
}
|
||||
|
||||
SECTION("Const") {
|
||||
run_iterator_test<JsonArray::const_iterator>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void check(JsonArray& array, std::string expected) {
|
||||
std::string actual;
|
||||
size_t actualLen = array.prettyPrintTo(actual);
|
||||
size_t measuredLen = array.measurePrettyLength();
|
||||
CHECK(actualLen == expected.size());
|
||||
CHECK(measuredLen == expected.size());
|
||||
REQUIRE(expected == actual);
|
||||
}
|
||||
|
||||
TEST_CASE("JsonArray::prettyPrintTo()") {
|
||||
DynamicJsonBuffer jb;
|
||||
JsonArray& array = jb.createArray();
|
||||
|
||||
SECTION("Empty") {
|
||||
check(array, "[]");
|
||||
}
|
||||
|
||||
SECTION("OneElement") {
|
||||
array.add(1);
|
||||
|
||||
check(array,
|
||||
"[\r\n"
|
||||
" 1\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
SECTION("TwoElements") {
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
|
||||
check(array,
|
||||
"[\r\n"
|
||||
" 1,\r\n"
|
||||
" 2\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
SECTION("EmptyNestedArrays") {
|
||||
array.createNestedArray();
|
||||
array.createNestedArray();
|
||||
|
||||
check(array,
|
||||
"[\r\n"
|
||||
" [],\r\n"
|
||||
" []\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
SECTION("NestedArrays") {
|
||||
JsonArray& nested1 = array.createNestedArray();
|
||||
nested1.add(1);
|
||||
nested1.add(2);
|
||||
|
||||
JsonObject& nested2 = array.createNestedObject();
|
||||
nested2["key"] = 3;
|
||||
|
||||
check(array,
|
||||
"[\r\n"
|
||||
" [\r\n"
|
||||
" 1,\r\n"
|
||||
" 2\r\n"
|
||||
" ],\r\n"
|
||||
" {\r\n"
|
||||
" \"key\": 3\r\n"
|
||||
" }\r\n"
|
||||
"]");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void check(JsonArray &array, std::string expected) {
|
||||
std::string actual;
|
||||
size_t actualLen = array.printTo(actual);
|
||||
REQUIRE(expected == actual);
|
||||
REQUIRE(actualLen == expected.size());
|
||||
size_t measuredLen = array.measureLength();
|
||||
REQUIRE(measuredLen == expected.size());
|
||||
}
|
||||
|
||||
TEST_CASE("JsonArray::printTo()") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> jb;
|
||||
JsonArray &array = jb.createArray();
|
||||
|
||||
SECTION("Empty") {
|
||||
check(array, "[]");
|
||||
}
|
||||
|
||||
SECTION("Null") {
|
||||
array.add(static_cast<char *>(0));
|
||||
|
||||
check(array, "[null]");
|
||||
}
|
||||
|
||||
SECTION("OneString") {
|
||||
array.add("hello");
|
||||
|
||||
check(array, "[\"hello\"]");
|
||||
}
|
||||
|
||||
SECTION("TwoStrings") {
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
|
||||
check(array, "[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
SECTION("OneStringOverCapacity") {
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
array.add("lost");
|
||||
|
||||
check(array, "[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
SECTION("One double") {
|
||||
array.add(3.1415927);
|
||||
check(array, "[3.1415927]");
|
||||
}
|
||||
|
||||
SECTION("OneInteger") {
|
||||
array.add(1);
|
||||
|
||||
check(array, "[1]");
|
||||
}
|
||||
|
||||
SECTION("TwoIntegers") {
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
|
||||
check(array, "[1,2]");
|
||||
}
|
||||
|
||||
SECTION("RawJson(const char*)") {
|
||||
array.add(RawJson("{\"key\":\"value\"}"));
|
||||
|
||||
check(array, "[{\"key\":\"value\"}]");
|
||||
}
|
||||
|
||||
SECTION("RawJson(char*)") {
|
||||
DynamicJsonBuffer jb2;
|
||||
JsonArray &arr = jb2.createArray();
|
||||
|
||||
char tmp[] = "{\"key\":\"value\"}";
|
||||
arr.add(RawJson(tmp));
|
||||
|
||||
check(arr, "[{\"key\":\"value\"}]");
|
||||
}
|
||||
|
||||
SECTION("OneIntegerOverCapacity") {
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
array.add(3);
|
||||
|
||||
check(array, "[1,2]");
|
||||
}
|
||||
|
||||
SECTION("OneTrue") {
|
||||
array.add(true);
|
||||
|
||||
check(array, "[true]");
|
||||
}
|
||||
|
||||
SECTION("OneFalse") {
|
||||
array.add(false);
|
||||
|
||||
check(array, "[false]");
|
||||
}
|
||||
|
||||
SECTION("TwoBooleans") {
|
||||
array.add(false);
|
||||
array.add(true);
|
||||
|
||||
check(array, "[false,true]");
|
||||
}
|
||||
|
||||
SECTION("OneBooleanOverCapacity") {
|
||||
array.add(false);
|
||||
array.add(true);
|
||||
array.add(false);
|
||||
|
||||
check(array, "[false,true]");
|
||||
}
|
||||
|
||||
SECTION("OneEmptyNestedArray") {
|
||||
array.createNestedArray();
|
||||
|
||||
check(array, "[[]]");
|
||||
}
|
||||
|
||||
SECTION("OneEmptyNestedHash") {
|
||||
array.createNestedObject();
|
||||
|
||||
check(array, "[{}]");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::remove()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
_array.add(1);
|
||||
_array.add(2);
|
||||
_array.add(3);
|
||||
|
||||
SECTION("RemoveFirstByIndex") {
|
||||
_array.remove(0);
|
||||
|
||||
REQUIRE(2 == _array.size());
|
||||
REQUIRE(_array[0] == 2);
|
||||
REQUIRE(_array[1] == 3);
|
||||
}
|
||||
|
||||
SECTION("RemoveMiddleByIndex") {
|
||||
_array.remove(1);
|
||||
|
||||
REQUIRE(2 == _array.size());
|
||||
REQUIRE(_array[0] == 1);
|
||||
REQUIRE(_array[1] == 3);
|
||||
}
|
||||
|
||||
SECTION("RemoveLastByIndex") {
|
||||
_array.remove(2);
|
||||
|
||||
REQUIRE(2 == _array.size());
|
||||
REQUIRE(_array[0] == 1);
|
||||
REQUIRE(_array[1] == 2);
|
||||
}
|
||||
|
||||
SECTION("RemoveFirstByIterator") {
|
||||
JsonArray::iterator it = _array.begin();
|
||||
_array.remove(it);
|
||||
|
||||
REQUIRE(2 == _array.size());
|
||||
REQUIRE(_array[0] == 2);
|
||||
REQUIRE(_array[1] == 3);
|
||||
}
|
||||
|
||||
SECTION("RemoveMiddleByIterator") {
|
||||
JsonArray::iterator it = _array.begin();
|
||||
++it;
|
||||
_array.remove(it);
|
||||
|
||||
REQUIRE(2 == _array.size());
|
||||
REQUIRE(_array[0] == 1);
|
||||
REQUIRE(_array[1] == 3);
|
||||
}
|
||||
|
||||
SECTION("RemoveLastByIterator") {
|
||||
JsonArray::iterator it = _array.begin();
|
||||
++it;
|
||||
++it;
|
||||
_array.remove(it);
|
||||
|
||||
REQUIRE(2 == _array.size());
|
||||
REQUIRE(_array[0] == 1);
|
||||
REQUIRE(_array[1] == 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("JsonArray::set()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
_array.add(0);
|
||||
|
||||
SECTION("int") {
|
||||
_array.set(0, 123);
|
||||
REQUIRE(123 == _array[0].as<int>());
|
||||
REQUIRE(_array[0].is<int>());
|
||||
REQUIRE_FALSE(_array[0].is<bool>());
|
||||
}
|
||||
|
||||
SECTION("double") {
|
||||
_array.set(0, 123.45);
|
||||
REQUIRE(123.45 == _array[0].as<double>());
|
||||
REQUIRE(_array[0].is<double>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
_array.set(0, true);
|
||||
REQUIRE(true == _array[0].as<bool>());
|
||||
REQUIRE(_array[0].is<bool>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
_array.set(0, "hello");
|
||||
REQUIRE_THAT(_array[0].as<const char*>(), Equals("hello"));
|
||||
REQUIRE(_array[0].is<const char*>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("nested array") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
|
||||
_array.set(0, arr);
|
||||
|
||||
REQUIRE(&arr == &_array[0].as<JsonArray&>());
|
||||
REQUIRE(_array[0].is<JsonArray&>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
|
||||
_array.set(0, obj);
|
||||
|
||||
REQUIRE(&obj == &_array[0].as<JsonObject&>());
|
||||
REQUIRE(_array[0].is<JsonObject&>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
|
||||
_array.set(0, arr[0]);
|
||||
|
||||
REQUIRE_THAT(_array[0].as<char*>(), Equals("hello"));
|
||||
}
|
||||
|
||||
SECTION("object subscript") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj["x"] = "hello";
|
||||
|
||||
_array.set(0, obj["x"]);
|
||||
|
||||
REQUIRE_THAT(_array[0].as<char*>(), Equals("hello"));
|
||||
}
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
_array.set(0, "world");
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char*") {
|
||||
_array.set(0, const_cast<char*>("world"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string") {
|
||||
_array.set(0, std::string("world"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::size()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
|
||||
SECTION("increases after add()") {
|
||||
_array.add("hello");
|
||||
REQUIRE(1U == _array.size());
|
||||
|
||||
_array.add("world");
|
||||
REQUIRE(2U == _array.size());
|
||||
}
|
||||
|
||||
SECTION("remains the same after set()") {
|
||||
_array.add("hello");
|
||||
REQUIRE(1U == _array.size());
|
||||
|
||||
_array.set(0, "hello");
|
||||
REQUIRE(1U == _array.size());
|
||||
}
|
||||
|
||||
SECTION("remains the same after assigment") {
|
||||
_array.add("hello");
|
||||
REQUIRE(1U == _array.size());
|
||||
|
||||
_array[0] = "hello";
|
||||
REQUIRE(1U == _array.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <stdint.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::operator[]") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
_array.add(0);
|
||||
|
||||
SECTION("int") {
|
||||
_array[0] = 123;
|
||||
REQUIRE(123 == _array[0].as<int>());
|
||||
REQUIRE(true == _array[0].is<int>());
|
||||
REQUIRE(false == _array[0].is<bool>());
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
|
||||
SECTION("long long") {
|
||||
_array[0] = 9223372036854775807;
|
||||
REQUIRE(9223372036854775807 == _array[0].as<long long>());
|
||||
REQUIRE(true == _array[0].is<int>());
|
||||
REQUIRE(false == _array[0].is<bool>());
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("double") {
|
||||
_array[0] = 123.45;
|
||||
REQUIRE(123.45 == _array[0].as<double>());
|
||||
REQUIRE(true == _array[0].is<double>());
|
||||
REQUIRE(false == _array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
_array[0] = true;
|
||||
REQUIRE(true == _array[0].as<bool>());
|
||||
REQUIRE(true == _array[0].is<bool>());
|
||||
REQUIRE(false == _array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
const char* str = "hello";
|
||||
|
||||
_array[0] = str;
|
||||
REQUIRE(str == _array[0].as<const char*>());
|
||||
REQUIRE(str == _array[0].as<char*>()); // <- short hand
|
||||
REQUIRE(true == _array[0].is<const char*>());
|
||||
REQUIRE(false == _array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("nested array") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
|
||||
_array[0] = arr;
|
||||
|
||||
REQUIRE(&arr == &_array[0].as<JsonArray&>());
|
||||
REQUIRE(&arr == &_array[0].as<JsonArray>()); // <- short hand
|
||||
REQUIRE(&arr == &_array[0].as<const JsonArray&>());
|
||||
REQUIRE(&arr == &_array[0].as<const JsonArray>()); // <- short hand
|
||||
REQUIRE(true == _array[0].is<JsonArray&>());
|
||||
REQUIRE(false == _array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
|
||||
_array[0] = obj;
|
||||
|
||||
REQUIRE(&obj == &_array[0].as<JsonObject&>());
|
||||
REQUIRE(&obj == &_array[0].as<JsonObject>()); // <- short hand
|
||||
REQUIRE(&obj == &_array[0].as<const JsonObject&>());
|
||||
REQUIRE(&obj == &_array[0].as<const JsonObject>()); // <- short hand
|
||||
REQUIRE(true == _array[0].is<JsonObject&>());
|
||||
REQUIRE(false == _array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
const char* str = "hello";
|
||||
|
||||
arr.add(str);
|
||||
|
||||
_array[0] = arr[0];
|
||||
|
||||
REQUIRE(str == _array[0]);
|
||||
}
|
||||
|
||||
SECTION("object subscript") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
const char* str = "hello";
|
||||
|
||||
obj["x"] = str;
|
||||
|
||||
_array[0] = obj["x"];
|
||||
|
||||
REQUIRE(str == _array[0]);
|
||||
}
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
_array[0] = "world";
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char*") {
|
||||
_array[0] = const_cast<char*>("world");
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string") {
|
||||
_array[0] = std::string("world");
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == _jsonBuffer.size());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user