mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-14 19:16:35 +02:00
Changed unit testing framework from Google Test to Catch
This commit is contained in:
@ -19,5 +19,5 @@ add_executable(JsonArrayTests
|
||||
subscript.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(JsonArrayTests gtest)
|
||||
target_link_libraries(JsonArrayTests catch)
|
||||
add_test(JsonArray JsonArrayTests)
|
||||
|
@ -6,85 +6,83 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonArray_Add_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Add_Tests() : _array(_jsonBuffer.createArray()) {}
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::add()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
};
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
|
||||
#define TEST_(name) TEST_F(JsonArray_Add_Tests, name)
|
||||
SECTION("SizeIncreased_WhenValuesAreAdded") {
|
||||
_array.add("hello");
|
||||
REQUIRE(1U == _array.size());
|
||||
}
|
||||
|
||||
TEST_(SizeIncreased_WhenValuesAreAdded) {
|
||||
_array.add("hello");
|
||||
EXPECT_EQ(1U, _array.size());
|
||||
}
|
||||
|
||||
TEST_(StoreInteger) {
|
||||
_array.add(123);
|
||||
EXPECT_EQ(123, _array[0].as<int>());
|
||||
EXPECT_TRUE(_array[0].is<int>());
|
||||
EXPECT_FALSE(_array[0].is<double>());
|
||||
}
|
||||
|
||||
TEST_(StoreDouble) {
|
||||
_array.add(123.45);
|
||||
EXPECT_EQ(123.45, _array[0].as<double>());
|
||||
EXPECT_TRUE(_array[0].is<double>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreBoolean) {
|
||||
_array.add(true);
|
||||
EXPECT_EQ(true, _array[0].as<bool>());
|
||||
EXPECT_TRUE(_array[0].is<bool>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreString) {
|
||||
_array.add("hello");
|
||||
EXPECT_STREQ("hello", _array[0].as<const char*>());
|
||||
EXPECT_TRUE(_array[0].is<const char*>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreNestedArray) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
|
||||
_array.add(arr);
|
||||
|
||||
EXPECT_EQ(&arr, &_array[0].as<JsonArray&>());
|
||||
EXPECT_TRUE(_array[0].is<JsonArray&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreNestedObject) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
|
||||
_array.add(obj);
|
||||
|
||||
EXPECT_EQ(&obj, &_array[0].as<JsonObject&>());
|
||||
EXPECT_TRUE(_array[0].is<JsonObject&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreArraySubscript) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
|
||||
_array.add(arr[0]);
|
||||
|
||||
EXPECT_STREQ("hello", _array[0]);
|
||||
}
|
||||
|
||||
TEST_(StoreObjectSubscript) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj["x"] = "hello";
|
||||
|
||||
_array.add(obj["x"]);
|
||||
|
||||
EXPECT_STREQ("hello", _array[0]);
|
||||
SECTION("StoreInteger") {
|
||||
_array.add(123);
|
||||
REQUIRE(123 == _array[0].as<int>());
|
||||
REQUIRE(_array[0].is<int>());
|
||||
REQUIRE_FALSE(_array[0].is<double>());
|
||||
}
|
||||
|
||||
SECTION("StoreDouble") {
|
||||
_array.add(123.45);
|
||||
REQUIRE(123.45 == _array[0].as<double>());
|
||||
REQUIRE(_array[0].is<double>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("StoreBoolean") {
|
||||
_array.add(true);
|
||||
REQUIRE(true == _array[0].as<bool>());
|
||||
REQUIRE(_array[0].is<bool>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("StoreString") {
|
||||
const char* str = "hello";
|
||||
_array.add(str);
|
||||
REQUIRE(str == _array[0].as<const char*>());
|
||||
REQUIRE(_array[0].is<const char*>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("StoreNestedArray") {
|
||||
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("StoreNestedObject") {
|
||||
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("StoreArraySubscript") {
|
||||
const char* str = "hello";
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add(str);
|
||||
|
||||
_array.add(arr[0]);
|
||||
|
||||
REQUIRE(str == _array[0]);
|
||||
}
|
||||
|
||||
SECTION("StoreObjectSubscript") {
|
||||
const char* str = "hello";
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj["x"] = str;
|
||||
|
||||
_array.add(obj["x"]);
|
||||
|
||||
REQUIRE(str == _array[0]);
|
||||
}
|
||||
}
|
||||
|
@ -6,36 +6,27 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#define TEST_(name) TEST(JsonArray_Basic_Tests, name)
|
||||
TEST_CASE("JsonArray basics") {
|
||||
DynamicJsonBuffer jb;
|
||||
JsonArray& array = jb.createArray();
|
||||
|
||||
TEST_(SuccessIsTrue) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& array = _jsonBuffer.createArray();
|
||||
SECTION("SuccessIsTrue") {
|
||||
REQUIRE(array.success());
|
||||
}
|
||||
|
||||
EXPECT_TRUE(array.success());
|
||||
}
|
||||
|
||||
TEST_(InitialSizeIsZero) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& array = _jsonBuffer.createArray();
|
||||
|
||||
EXPECT_EQ(0U, array.size());
|
||||
}
|
||||
|
||||
TEST_(CreateNestedArray) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& array = _jsonBuffer.createArray();
|
||||
|
||||
JsonArray& arr = array.createNestedArray();
|
||||
EXPECT_EQ(&arr, &array[0].as<JsonArray&>());
|
||||
}
|
||||
|
||||
TEST_(CreateNestedObject) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& array = _jsonBuffer.createArray();
|
||||
|
||||
JsonObject& obj = array.createNestedObject();
|
||||
EXPECT_EQ(&obj, &array[0].as<JsonObject&>());
|
||||
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&>());
|
||||
}
|
||||
}
|
||||
|
@ -6,59 +6,61 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST(JsonArray_CopyFrom_Tests, OneDimension) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
char json[32];
|
||||
int source[] = {1, 2, 3};
|
||||
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);
|
||||
ASSERT_TRUE(ok);
|
||||
bool ok = array.copyFrom(source);
|
||||
REQUIRE(ok);
|
||||
|
||||
array.printTo(json, sizeof(json));
|
||||
ASSERT_STREQ("[1,2,3]", json);
|
||||
}
|
||||
|
||||
TEST(JsonArray_CopyFrom_Tests, 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);
|
||||
ASSERT_FALSE(ok);
|
||||
|
||||
array.printTo(json, sizeof(json));
|
||||
ASSERT_STREQ("[1,2]", json);
|
||||
}
|
||||
|
||||
TEST(JsonArray_CopyFrom_Tests, TwoDimensions) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
char json[32];
|
||||
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
||||
|
||||
bool ok = array.copyFrom(source);
|
||||
ASSERT_TRUE(ok);
|
||||
|
||||
array.printTo(json, sizeof(json));
|
||||
ASSERT_STREQ("[[1,2,3],[4,5,6]]", json);
|
||||
}
|
||||
|
||||
TEST(JsonArray_CopyFrom_Tests, 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);
|
||||
ASSERT_FALSE(ok);
|
||||
|
||||
array.printTo(json, sizeof(json));
|
||||
ASSERT_STREQ("[[1,2,3],[4,5]]", json);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -6,51 +6,50 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(JsonArray_CopyTo_Tests, BiggerOneDimensionIntegerArray) {
|
||||
char json[] = "[1,2,3]";
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::copyTo()") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.parseArray(json);
|
||||
|
||||
int destination[4] = {0};
|
||||
size_t result = array.copyTo(destination);
|
||||
SECTION("BiggerOneDimensionIntegerArray") {
|
||||
char json[] = "[1,2,3]";
|
||||
JsonArray& array = jsonBuffer.parseArray(json);
|
||||
|
||||
ASSERT_EQ(3, result);
|
||||
ASSERT_EQ(1, destination[0]);
|
||||
ASSERT_EQ(2, destination[1]);
|
||||
ASSERT_EQ(3, destination[2]);
|
||||
ASSERT_EQ(0, destination[3]);
|
||||
}
|
||||
|
||||
TEST(JsonArray_CopyTo_Tests, SmallerOneDimensionIntegerArray) {
|
||||
char json[] = "[1,2,3]";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.parseArray(json);
|
||||
|
||||
int destination[2] = {0};
|
||||
size_t result = array.copyTo(destination);
|
||||
|
||||
ASSERT_EQ(2, result);
|
||||
ASSERT_EQ(1, destination[0]);
|
||||
ASSERT_EQ(2, destination[1]);
|
||||
}
|
||||
|
||||
TEST(JsonArray_CopyTo_Tests, TwoOneDimensionIntegerArray) {
|
||||
char json[] = "[[1,2],[3],[4]]";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.parseArray(json);
|
||||
|
||||
int destination[3][2] = {{0}};
|
||||
array.copyTo(destination);
|
||||
|
||||
ASSERT_EQ(1, destination[0][0]);
|
||||
ASSERT_EQ(2, destination[0][1]);
|
||||
ASSERT_EQ(3, destination[1][0]);
|
||||
ASSERT_EQ(0, destination[1][1]);
|
||||
ASSERT_EQ(4, destination[2][0]);
|
||||
ASSERT_EQ(0, destination[2][1]);
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
@ -6,28 +6,32 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST(JsonArray_Invalid_Tests, SubscriptFails) {
|
||||
ASSERT_FALSE(JsonArray::invalid()[0].success());
|
||||
}
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST(JsonArray_Invalid_Tests, AddFails) {
|
||||
JsonArray& array = JsonArray::invalid();
|
||||
array.add(1);
|
||||
ASSERT_EQ(0, array.size());
|
||||
}
|
||||
TEST_CASE("JsonArray::invalid()") {
|
||||
SECTION("SubscriptFails") {
|
||||
REQUIRE_FALSE(JsonArray::invalid()[0].success());
|
||||
}
|
||||
|
||||
TEST(JsonArray_Invalid_Tests, CreateNestedArrayFails) {
|
||||
ASSERT_FALSE(JsonArray::invalid().createNestedArray().success());
|
||||
}
|
||||
SECTION("AddFails") {
|
||||
JsonArray& array = JsonArray::invalid();
|
||||
array.add(1);
|
||||
REQUIRE(0 == array.size());
|
||||
}
|
||||
|
||||
TEST(JsonArray_Invalid_Tests, CreateNestedObjectFails) {
|
||||
ASSERT_FALSE(JsonArray::invalid().createNestedObject().success());
|
||||
}
|
||||
SECTION("CreateNestedArrayFails") {
|
||||
REQUIRE_FALSE(JsonArray::invalid().createNestedArray().success());
|
||||
}
|
||||
|
||||
TEST(JsonArray_Invalid_Tests, PrintToWritesBrackets) {
|
||||
char buffer[32];
|
||||
JsonArray::invalid().printTo(buffer, sizeof(buffer));
|
||||
ASSERT_STREQ("[]", buffer);
|
||||
SECTION("CreateNestedObjectFails") {
|
||||
REQUIRE_FALSE(JsonArray::invalid().createNestedObject().success());
|
||||
}
|
||||
|
||||
SECTION("PrintToWritesBrackets") {
|
||||
char buffer[32];
|
||||
JsonArray::invalid().printTo(buffer, sizeof(buffer));
|
||||
REQUIRE_THAT(buffer, Equals("[]"));
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
template <typename TIterator>
|
||||
static void run_iterator_test() {
|
||||
@ -19,21 +19,23 @@ static void run_iterator_test() {
|
||||
TIterator it = array.begin();
|
||||
TIterator end = array.end();
|
||||
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_EQ(12, it->template as<int>());
|
||||
EXPECT_EQ(12, static_cast<int>(*it));
|
||||
REQUIRE(end != it);
|
||||
REQUIRE(12 == it->template as<int>());
|
||||
REQUIRE(12 == static_cast<int>(*it));
|
||||
++it;
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_EQ(34, it->template as<int>());
|
||||
EXPECT_EQ(34, static_cast<int>(*it));
|
||||
REQUIRE(end != it);
|
||||
REQUIRE(34 == it->template as<int>());
|
||||
REQUIRE(34 == static_cast<int>(*it));
|
||||
++it;
|
||||
EXPECT_EQ(end, it);
|
||||
REQUIRE(end == it);
|
||||
}
|
||||
|
||||
TEST(JsonArray_Iterator_Test, RunItertorToEnd) {
|
||||
run_iterator_test<JsonArray::iterator>();
|
||||
}
|
||||
TEST_CASE("JsonArray::begin()/end()") {
|
||||
SECTION("Mutable") {
|
||||
run_iterator_test<JsonArray::iterator>();
|
||||
}
|
||||
|
||||
TEST(JsonArray_Iterator_Test, RunConstItertorToEnd) {
|
||||
run_iterator_test<JsonArray::const_iterator>();
|
||||
SECTION("Const") {
|
||||
run_iterator_test<JsonArray::const_iterator>();
|
||||
}
|
||||
}
|
||||
|
@ -6,79 +6,73 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
class JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
JsonArray_PrettyPrintTo_Tests() : array(jsonBuffer.createArray()) {}
|
||||
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);
|
||||
}
|
||||
|
||||
protected:
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array;
|
||||
TEST_CASE("JsonArray::prettyPrintTo()") {
|
||||
DynamicJsonBuffer jb;
|
||||
JsonArray& array = jb.createArray();
|
||||
|
||||
void outputMustBe(const char* expected) {
|
||||
char actual[256];
|
||||
|
||||
size_t actualLen = array.prettyPrintTo(actual);
|
||||
size_t measuredLen = array.measurePrettyLength();
|
||||
|
||||
EXPECT_STREQ(expected, actual);
|
||||
EXPECT_EQ(strlen(expected), actualLen);
|
||||
EXPECT_EQ(strlen(expected), measuredLen);
|
||||
SECTION("Empty") {
|
||||
check(array, "[]");
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) {
|
||||
outputMustBe("[]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
|
||||
array.add(1);
|
||||
|
||||
outputMustBe(
|
||||
"[\r\n"
|
||||
" 1\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
|
||||
outputMustBe(
|
||||
"[\r\n"
|
||||
" 1,\r\n"
|
||||
" 2\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) {
|
||||
array.createNestedArray();
|
||||
array.createNestedArray();
|
||||
|
||||
outputMustBe(
|
||||
"[\r\n"
|
||||
" [],\r\n"
|
||||
" []\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
|
||||
JsonArray& nested1 = array.createNestedArray();
|
||||
nested1.add(1);
|
||||
nested1.add(2);
|
||||
|
||||
JsonObject& nested2 = array.createNestedObject();
|
||||
nested2["key"] = 3;
|
||||
|
||||
outputMustBe(
|
||||
"[\r\n"
|
||||
" [\r\n"
|
||||
" 1,\r\n"
|
||||
" 2\r\n"
|
||||
" ],\r\n"
|
||||
" {\r\n"
|
||||
" \"key\": 3\r\n"
|
||||
" }\r\n"
|
||||
"]");
|
||||
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"
|
||||
"]");
|
||||
}
|
||||
}
|
||||
|
@ -6,147 +6,140 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
class JsonArray_PrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
JsonArray_PrintTo_Tests() : array(json.createArray()) {}
|
||||
static void check(JsonArray &array, std::string expected) {
|
||||
std::string actual;
|
||||
size_t actualLen = array.printTo(actual);
|
||||
size_t measuredLen = array.measureLength();
|
||||
CHECK(actualLen == expected.size());
|
||||
CHECK(measuredLen == expected.size());
|
||||
REQUIRE(expected == actual);
|
||||
}
|
||||
|
||||
protected:
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
|
||||
JsonArray &array;
|
||||
TEST_CASE("JsonArray::printTo()") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> jb;
|
||||
JsonArray &array = jb.createArray();
|
||||
|
||||
void outputMustBe(const char *expected) {
|
||||
size_t actualLen = array.printTo(buffer);
|
||||
size_t measuredLen = array.measureLength();
|
||||
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), actualLen);
|
||||
EXPECT_EQ(strlen(expected), measuredLen);
|
||||
SECTION("Empty") {
|
||||
check(array, "[]");
|
||||
}
|
||||
|
||||
private:
|
||||
char buffer[256];
|
||||
};
|
||||
SECTION("Null") {
|
||||
array.add(static_cast<char *>(0));
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, Empty) {
|
||||
outputMustBe("[]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, Null) {
|
||||
array.add(static_cast<char *>(0));
|
||||
|
||||
outputMustBe("[null]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneString) {
|
||||
array.add("hello");
|
||||
|
||||
outputMustBe("[\"hello\"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, TwoStrings) {
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
|
||||
outputMustBe("[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity) {
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
array.add("lost");
|
||||
|
||||
outputMustBe("[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits) {
|
||||
array.add(3.14159265358979323846);
|
||||
outputMustBe("[3.14]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits) {
|
||||
array.add(3.14159265358979323846, 4);
|
||||
outputMustBe("[3.1416]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits_AlternativeSyntax) {
|
||||
array.add(double_with_n_digits(3.14159265358979323846, 4));
|
||||
outputMustBe("[3.1416]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneFloatDefaultDigits) {
|
||||
array.add(3.14159f);
|
||||
outputMustBe("[3.14]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneFloatFourDigits) {
|
||||
array.add(3.14159f, 4);
|
||||
outputMustBe("[3.1416]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneInteger) {
|
||||
array.add(1);
|
||||
|
||||
outputMustBe("[1]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, TwoIntegers) {
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
|
||||
outputMustBe("[1,2]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, RawJson) {
|
||||
array.add(RawJson("{\"key\":\"value\"}"));
|
||||
|
||||
outputMustBe("[{\"key\":\"value\"}]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity) {
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
array.add(3);
|
||||
|
||||
outputMustBe("[1,2]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneTrue) {
|
||||
array.add(true);
|
||||
|
||||
outputMustBe("[true]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneFalse) {
|
||||
array.add(false);
|
||||
|
||||
outputMustBe("[false]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, TwoBooleans) {
|
||||
array.add(false);
|
||||
array.add(true);
|
||||
|
||||
outputMustBe("[false,true]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity) {
|
||||
array.add(false);
|
||||
array.add(true);
|
||||
array.add(false);
|
||||
|
||||
outputMustBe("[false,true]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray) {
|
||||
array.createNestedArray();
|
||||
|
||||
outputMustBe("[[]]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash) {
|
||||
array.createNestedObject();
|
||||
|
||||
outputMustBe("[{}]");
|
||||
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("OneDoubleDefaultDigits") {
|
||||
array.add(3.14159265358979323846);
|
||||
check(array, "[3.14]");
|
||||
}
|
||||
|
||||
SECTION("OneDoubleFourDigits") {
|
||||
array.add(3.14159265358979323846, 4);
|
||||
check(array, "[3.1416]");
|
||||
}
|
||||
|
||||
SECTION("OneDoubleFourDigits_AlternativeSyntax") {
|
||||
array.add(double_with_n_digits(3.14159265358979323846, 4));
|
||||
check(array, "[3.1416]");
|
||||
}
|
||||
|
||||
SECTION("OneFloatDefaultDigits") {
|
||||
array.add(3.14159f);
|
||||
check(array, "[3.14]");
|
||||
}
|
||||
|
||||
SECTION("OneFloatFourDigits") {
|
||||
array.add(3.14159f, 4);
|
||||
check(array, "[3.1416]");
|
||||
}
|
||||
|
||||
SECTION("OneInteger") {
|
||||
array.add(1);
|
||||
|
||||
check(array, "[1]");
|
||||
}
|
||||
|
||||
SECTION("TwoIntegers") {
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
|
||||
check(array, "[1,2]");
|
||||
}
|
||||
|
||||
SECTION("RawJson") {
|
||||
array.add(RawJson("{\"key\":\"value\"}"));
|
||||
|
||||
check(array, "[{\"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, "[{}]");
|
||||
}
|
||||
}
|
||||
|
@ -6,72 +6,66 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
class JsonArray_Remove_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Remove_Tests() : _array(_jsonBuffer.createArray()) {
|
||||
_array.add("one");
|
||||
_array.add("two");
|
||||
_array.add("three");
|
||||
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);
|
||||
}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
};
|
||||
SECTION("RemoveMiddleByIndex") {
|
||||
_array.remove(1);
|
||||
|
||||
#define TEST_(name) TEST_F(JsonArray_Remove_Tests, name)
|
||||
REQUIRE(2 == _array.size());
|
||||
REQUIRE(_array[0] == 1);
|
||||
REQUIRE(_array[1] == 3);
|
||||
}
|
||||
|
||||
TEST_(RemoveFirstByIndex) {
|
||||
_array.remove(0);
|
||||
SECTION("RemoveLastByIndex") {
|
||||
_array.remove(2);
|
||||
|
||||
EXPECT_EQ(2, _array.size());
|
||||
EXPECT_STREQ("two", _array[0]);
|
||||
EXPECT_STREQ("three", _array[1]);
|
||||
}
|
||||
|
||||
TEST_(RemoveMiddleByIndex) {
|
||||
_array.remove(1);
|
||||
|
||||
EXPECT_EQ(2, _array.size());
|
||||
EXPECT_STREQ("one", _array[0]);
|
||||
EXPECT_STREQ("three", _array[1]);
|
||||
}
|
||||
|
||||
TEST_(RemoveLastByIndex) {
|
||||
_array.remove(2);
|
||||
|
||||
EXPECT_EQ(2, _array.size());
|
||||
EXPECT_STREQ("one", _array[0]);
|
||||
EXPECT_STREQ("two", _array[1]);
|
||||
}
|
||||
|
||||
TEST_(RemoveFirstByIterator) {
|
||||
JsonArray::iterator it = _array.begin();
|
||||
_array.remove(it);
|
||||
|
||||
EXPECT_EQ(2, _array.size());
|
||||
EXPECT_STREQ("two", _array[0]);
|
||||
EXPECT_STREQ("three", _array[1]);
|
||||
}
|
||||
|
||||
TEST_(RemoveMiddleByIterator) {
|
||||
JsonArray::iterator it = _array.begin();
|
||||
++it;
|
||||
_array.remove(it);
|
||||
|
||||
EXPECT_EQ(2, _array.size());
|
||||
EXPECT_STREQ("one", _array[0]);
|
||||
EXPECT_STREQ("three", _array[1]);
|
||||
}
|
||||
|
||||
TEST_(RemoveLastByIterator) {
|
||||
JsonArray::iterator it = _array.begin();
|
||||
++it;
|
||||
++it;
|
||||
_array.remove(it);
|
||||
|
||||
EXPECT_EQ(2, _array.size());
|
||||
EXPECT_STREQ("one", _array[0]);
|
||||
EXPECT_STREQ("two", _array[1]);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -6,87 +6,83 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
class JsonArray_Set_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Set_Tests() : _array(_jsonBuffer.createArray()) {
|
||||
_array.add(0);
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("JsonArray::set()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
_array.add(0);
|
||||
|
||||
SECTION("SizeIsUnchanged") {
|
||||
_array.set(0, "hello");
|
||||
REQUIRE(1U == _array.size());
|
||||
}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
};
|
||||
SECTION("StoreInteger") {
|
||||
_array.set(0, 123);
|
||||
REQUIRE(123 == _array[0].as<int>());
|
||||
REQUIRE(_array[0].is<int>());
|
||||
REQUIRE_FALSE(_array[0].is<double>());
|
||||
}
|
||||
|
||||
#define TEST_(name) TEST_F(JsonArray_Set_Tests, name)
|
||||
SECTION("StoreDouble") {
|
||||
_array.set(0, 123.45);
|
||||
REQUIRE(123.45 == _array[0].as<double>());
|
||||
REQUIRE(_array[0].is<double>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(SizeIsUnchanged) {
|
||||
_array.set(0, "hello");
|
||||
EXPECT_EQ(1U, _array.size());
|
||||
}
|
||||
|
||||
TEST_(StoreInteger) {
|
||||
_array.set(0, 123);
|
||||
EXPECT_EQ(123, _array[0].as<int>());
|
||||
EXPECT_TRUE(_array[0].is<int>());
|
||||
EXPECT_FALSE(_array[0].is<double>());
|
||||
}
|
||||
|
||||
TEST_(StoreDouble) {
|
||||
_array.set(0, 123.45);
|
||||
EXPECT_EQ(123.45, _array[0].as<double>());
|
||||
EXPECT_TRUE(_array[0].is<double>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreBoolean) {
|
||||
_array.set(0, true);
|
||||
EXPECT_EQ(true, _array[0].as<bool>());
|
||||
EXPECT_TRUE(_array[0].is<bool>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreString) {
|
||||
_array.set(0, "hello");
|
||||
EXPECT_STREQ("hello", _array[0].as<const char*>());
|
||||
EXPECT_TRUE(_array[0].is<const char*>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreNestedArray) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
|
||||
_array.set(0, arr);
|
||||
|
||||
EXPECT_EQ(&arr, &_array[0].as<JsonArray&>());
|
||||
EXPECT_TRUE(_array[0].is<JsonArray&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreNestedObject) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
|
||||
_array.set(0, obj);
|
||||
|
||||
EXPECT_EQ(&obj, &_array[0].as<JsonObject&>());
|
||||
EXPECT_TRUE(_array[0].is<JsonObject&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreArraySubscript) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
|
||||
_array.set(0, arr[0]);
|
||||
|
||||
EXPECT_STREQ("hello", _array[0]);
|
||||
}
|
||||
|
||||
TEST_(StoreObjectSubscript) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj["x"] = "hello";
|
||||
|
||||
_array.set(0, obj["x"]);
|
||||
|
||||
EXPECT_STREQ("hello", _array[0]);
|
||||
SECTION("StoreBoolean") {
|
||||
_array.set(0, true);
|
||||
REQUIRE(true == _array[0].as<bool>());
|
||||
REQUIRE(_array[0].is<bool>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("StoreString") {
|
||||
_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("StoreNestedArray") {
|
||||
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("StoreNestedObject") {
|
||||
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("StoreArraySubscript") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
|
||||
_array.set(0, arr[0]);
|
||||
|
||||
REQUIRE_THAT(_array[0].as<char*>(), Equals("hello"));
|
||||
}
|
||||
|
||||
SECTION("StoreObjectSubscript") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj["x"] = "hello";
|
||||
|
||||
_array.set(0, obj["x"]);
|
||||
|
||||
REQUIRE_THAT(_array[0].as<char*>(), Equals("hello"));
|
||||
}
|
||||
}
|
||||
|
@ -6,111 +6,111 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <stdint.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
class JsonArray_Subscript_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Subscript_Tests() : _array(_jsonBuffer.createArray()) {
|
||||
_array.add(0);
|
||||
TEST_CASE("JsonArray::operator[]") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array = _jsonBuffer.createArray();
|
||||
_array.add(0);
|
||||
|
||||
SECTION("SizeIsUnchanged") {
|
||||
_array[0] = "hello";
|
||||
REQUIRE(1U == _array.size());
|
||||
}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
};
|
||||
|
||||
#define TEST_(name) TEST_F(JsonArray_Subscript_Tests, name)
|
||||
|
||||
TEST_(SizeIsUnchanged) {
|
||||
_array[0] = "hello";
|
||||
EXPECT_EQ(1U, _array.size());
|
||||
}
|
||||
|
||||
TEST_(StoreInteger) {
|
||||
_array[0] = 123;
|
||||
EXPECT_EQ(123, _array[0].as<int>());
|
||||
EXPECT_TRUE(_array[0].is<int>());
|
||||
EXPECT_FALSE(_array[0].is<double>());
|
||||
}
|
||||
SECTION("StoreInteger") {
|
||||
_array[0] = 123;
|
||||
REQUIRE(123 == _array[0].as<int>());
|
||||
REQUIRE(true == _array[0].is<int>());
|
||||
REQUIRE(false == _array[0].is<double>());
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
|
||||
TEST_(StoreLongLong) {
|
||||
_array[0] = 9223372036854775807;
|
||||
EXPECT_EQ(9223372036854775807, _array[0].as<long long>());
|
||||
EXPECT_TRUE(_array[0].is<int>());
|
||||
EXPECT_FALSE(_array[0].is<double>());
|
||||
}
|
||||
SECTION("StoreLongLong") {
|
||||
_array[0] = 9223372036854775807;
|
||||
REQUIRE(9223372036854775807 == _array[0].as<long long>());
|
||||
REQUIRE(true == _array[0].is<int>());
|
||||
REQUIRE(false == _array[0].is<double>());
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_(StoreDouble) {
|
||||
_array[0] = 123.45;
|
||||
EXPECT_EQ(123.45, _array[0].as<double>());
|
||||
EXPECT_TRUE(_array[0].is<double>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreDoubleWithDecimals) {
|
||||
_array[0].set(123.45, 2);
|
||||
EXPECT_EQ(123.45, _array[0].as<double>());
|
||||
EXPECT_TRUE(_array[0].is<double>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreBoolean) {
|
||||
_array[0] = true;
|
||||
EXPECT_EQ(true, _array[0].as<bool>());
|
||||
EXPECT_TRUE(_array[0].is<bool>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreString) {
|
||||
_array[0] = "hello";
|
||||
EXPECT_STREQ("hello", _array[0].as<const char*>());
|
||||
EXPECT_STREQ("hello", _array[0].as<char*>()); // <- short hand
|
||||
EXPECT_TRUE(_array[0].is<const char*>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreNestedArray) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
|
||||
_array[0] = arr;
|
||||
|
||||
EXPECT_EQ(&arr, &_array[0].as<JsonArray&>());
|
||||
EXPECT_EQ(&arr, &_array[0].as<JsonArray>()); // <- short hand
|
||||
EXPECT_EQ(&arr, &_array[0].as<const JsonArray&>());
|
||||
EXPECT_EQ(&arr, &_array[0].as<const JsonArray>()); // <- short hand
|
||||
EXPECT_TRUE(_array[0].is<JsonArray&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreNestedObject) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
|
||||
_array[0] = obj;
|
||||
|
||||
EXPECT_EQ(&obj, &_array[0].as<JsonObject&>());
|
||||
EXPECT_EQ(&obj, &_array[0].as<JsonObject>()); // <- short hand
|
||||
EXPECT_EQ(&obj, &_array[0].as<const JsonObject&>());
|
||||
EXPECT_EQ(&obj, &_array[0].as<const JsonObject>()); // <- short hand
|
||||
EXPECT_TRUE(_array[0].is<JsonObject&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreArraySubscript) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
|
||||
_array[0] = arr[0];
|
||||
|
||||
EXPECT_STREQ("hello", _array[0]);
|
||||
}
|
||||
|
||||
TEST_(StoreObjectSubscript) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj["x"] = "hello";
|
||||
|
||||
_array[0] = obj["x"];
|
||||
|
||||
EXPECT_STREQ("hello", _array[0]);
|
||||
SECTION("StoreDouble") {
|
||||
_array[0] = 123.45;
|
||||
REQUIRE(123.45 == _array[0].as<double>());
|
||||
REQUIRE(true == _array[0].is<double>());
|
||||
REQUIRE(false == _array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("StoreDoubleWithDecimals") {
|
||||
_array[0].set(123.45, 2);
|
||||
REQUIRE(123.45 == _array[0].as<double>());
|
||||
REQUIRE(true == _array[0].is<double>());
|
||||
REQUIRE(false == _array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("StoreBoolean") {
|
||||
_array[0] = true;
|
||||
REQUIRE(true == _array[0].as<bool>());
|
||||
REQUIRE(true == _array[0].is<bool>());
|
||||
REQUIRE(false == _array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("StoreString") {
|
||||
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("StoreNestedArray") {
|
||||
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("StoreNestedObject") {
|
||||
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("StoreArraySubscript") {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
const char* str = "hello";
|
||||
|
||||
arr.add(str);
|
||||
|
||||
_array[0] = arr[0];
|
||||
|
||||
REQUIRE(str == _array[0]);
|
||||
}
|
||||
|
||||
SECTION("StoreObjectSubscript") {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
const char* str = "hello";
|
||||
|
||||
obj["x"] = str;
|
||||
|
||||
_array[0] = obj["x"];
|
||||
|
||||
REQUIRE(str == _array[0]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user