mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-15 03:26:39 +02:00
Organized test files in subfolders
This commit is contained in:
90
test/JsonArray/add.cpp
Normal file
90
test/JsonArray/add.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// 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()) {}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
};
|
||||
|
||||
#define TEST_(name) TEST_F(JsonArray_Add_Tests, name)
|
||||
|
||||
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]);
|
||||
}
|
41
test/JsonArray/basics.cpp
Normal file
41
test/JsonArray/basics.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#define TEST_(name) TEST(JsonArray_Basic_Tests, name)
|
||||
|
||||
TEST_(SuccessIsTrue) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& array = _jsonBuffer.createArray();
|
||||
|
||||
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&>());
|
||||
}
|
64
test/JsonArray/copyFrom.cpp
Normal file
64
test/JsonArray/copyFrom.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(JsonArray_CopyFrom_Tests, OneDimension) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
char json[32];
|
||||
int source[] = {1, 2, 3};
|
||||
|
||||
bool ok = array.copyFrom(source);
|
||||
ASSERT_TRUE(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);
|
||||
}
|
56
test/JsonArray/copyTo.cpp
Normal file
56
test/JsonArray/copyTo.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// 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]";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.parseArray(json);
|
||||
|
||||
int destination[4] = {0};
|
||||
size_t result = array.copyTo(destination);
|
||||
|
||||
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]);
|
||||
}
|
33
test/JsonArray/invalid.cpp
Normal file
33
test/JsonArray/invalid.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(JsonArray_Invalid_Tests, SubscriptFails) {
|
||||
ASSERT_FALSE(JsonArray::invalid()[0].success());
|
||||
}
|
||||
|
||||
TEST(JsonArray_Invalid_Tests, AddFails) {
|
||||
JsonArray& array = JsonArray::invalid();
|
||||
array.add(1);
|
||||
ASSERT_EQ(0, array.size());
|
||||
}
|
||||
|
||||
TEST(JsonArray_Invalid_Tests, CreateNestedArrayFails) {
|
||||
ASSERT_FALSE(JsonArray::invalid().createNestedArray().success());
|
||||
}
|
||||
|
||||
TEST(JsonArray_Invalid_Tests, CreateNestedObjectFails) {
|
||||
ASSERT_FALSE(JsonArray::invalid().createNestedObject().success());
|
||||
}
|
||||
|
||||
TEST(JsonArray_Invalid_Tests, PrintToWritesBrackets) {
|
||||
char buffer[32];
|
||||
JsonArray::invalid().printTo(buffer, sizeof(buffer));
|
||||
ASSERT_STREQ("[]", buffer);
|
||||
}
|
39
test/JsonArray/iterator.cpp
Normal file
39
test/JsonArray/iterator.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
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();
|
||||
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_EQ(12, it->template as<int>());
|
||||
EXPECT_EQ(12, static_cast<int>(*it));
|
||||
++it;
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_EQ(34, it->template as<int>());
|
||||
EXPECT_EQ(34, static_cast<int>(*it));
|
||||
++it;
|
||||
EXPECT_EQ(end, it);
|
||||
}
|
||||
|
||||
TEST(JsonArray_Iterator_Test, RunItertorToEnd) {
|
||||
run_iterator_test<JsonArray::iterator>();
|
||||
}
|
||||
|
||||
TEST(JsonArray_Iterator_Test, RunConstItertorToEnd) {
|
||||
run_iterator_test<JsonArray::const_iterator>();
|
||||
}
|
84
test/JsonArray/prettyPrintTo.cpp
Normal file
84
test/JsonArray/prettyPrintTo.cpp
Normal file
@ -0,0 +1,84 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
JsonArray_PrettyPrintTo_Tests() : array(jsonBuffer.createArray()) {}
|
||||
|
||||
protected:
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array;
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
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"
|
||||
"]");
|
||||
}
|
152
test/JsonArray/printTo.cpp
Normal file
152
test/JsonArray/printTo.cpp
Normal file
@ -0,0 +1,152 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonArray_PrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
JsonArray_PrintTo_Tests() : array(json.createArray()) {}
|
||||
|
||||
protected:
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
|
||||
JsonArray &array;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private:
|
||||
char buffer[256];
|
||||
};
|
||||
|
||||
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("[{}]");
|
||||
}
|
47
test/JsonArray/removeAt.cpp
Normal file
47
test/JsonArray/removeAt.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonArray_Remove_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Remove_Tests() : _array(_jsonBuffer.createArray()) {
|
||||
_array.add("one");
|
||||
_array.add("two");
|
||||
_array.add("three");
|
||||
}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
};
|
||||
|
||||
#define TEST_(name) TEST_F(JsonArray_Remove_Tests, name)
|
||||
|
||||
TEST_(RemoveFirstElement) {
|
||||
_array.removeAt(0);
|
||||
|
||||
EXPECT_EQ(2, _array.size());
|
||||
EXPECT_STREQ("two", _array[0]);
|
||||
EXPECT_STREQ("three", _array[1]);
|
||||
}
|
||||
|
||||
TEST_(RemoveMiddleElement) {
|
||||
_array.removeAt(1);
|
||||
|
||||
EXPECT_EQ(2, _array.size());
|
||||
EXPECT_STREQ("one", _array[0]);
|
||||
EXPECT_STREQ("three", _array[1]);
|
||||
}
|
||||
|
||||
TEST_(RemoveLastElement) {
|
||||
_array.removeAt(2);
|
||||
|
||||
EXPECT_EQ(2, _array.size());
|
||||
EXPECT_STREQ("one", _array[0]);
|
||||
EXPECT_STREQ("two", _array[1]);
|
||||
}
|
92
test/JsonArray/set.cpp
Normal file
92
test/JsonArray/set.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonArray_Set_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Set_Tests() : _array(_jsonBuffer.createArray()) {
|
||||
_array.add(0);
|
||||
}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
};
|
||||
|
||||
#define TEST_(name) TEST_F(JsonArray_Set_Tests, name)
|
||||
|
||||
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]);
|
||||
}
|
116
test/JsonArray/subscript.cpp
Normal file
116
test/JsonArray/subscript.cpp
Normal file
@ -0,0 +1,116 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <stdint.h>
|
||||
|
||||
class JsonArray_Subscript_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Subscript_Tests() : _array(_jsonBuffer.createArray()) {
|
||||
_array.add(0);
|
||||
}
|
||||
|
||||
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>());
|
||||
}
|
||||
|
||||
#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>());
|
||||
}
|
||||
#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]);
|
||||
}
|
Reference in New Issue
Block a user