Formated code with clang-format

This commit is contained in:
Benoit Blanchon
2014-10-23 19:54:00 +02:00
parent 888fdc1d54
commit 9175046f35
52 changed files with 2002 additions and 2638 deletions

View File

@ -6,75 +6,65 @@
using namespace ArduinoJson;
struct Person
{
int id;
char name[32];
struct Person {
int id;
char name[32];
};
class Issue10 : public testing::Test
{
class Issue10 : public testing::Test {
protected:
virtual void SetUp() {
Person boss;
boss.id = 1;
strcpy(boss.name, "Jeff");
Person employee;
employee.id = 2;
strcpy(employee.name, "John");
persons[0] = boss;
persons[1] = employee;
}
virtual void SetUp()
{
Person boss;
boss.id = 1;
strcpy(boss.name, "Jeff");
Person employee;
employee.id = 2;
strcpy(employee.name, "John");
persons[0] = boss;
persons[1] = employee;
}
void checkJsonString(JsonContainer &p) {
char buffer[256];
p.printTo(buffer, sizeof(buffer));
void checkJsonString(JsonContainer& p)
{
char buffer[256];
p.printTo(buffer, sizeof(buffer));
EXPECT_STREQ("[{\"id\":1,\"name\":\"Jeff\"},{\"id\":2,\"name\":\"John\"}]",
buffer);
}
EXPECT_STREQ("[{\"id\":1,\"name\":\"Jeff\"},{\"id\":2,\"name\":\"John\"}]", buffer);
}
void nodeCountMustBe(int expected) { EXPECT_EQ(expected, json.size()); }
void nodeCountMustBe(int expected)
{
EXPECT_EQ(expected, json.size());
}
Person persons[2];
StaticJsonBuffer<20> json;
Person persons[2];
StaticJsonBuffer<20> json;
};
TEST_F(Issue10, PopulateArrayByAddingAnObject)
{
JsonArray array= json.createArray();
TEST_F(Issue10, PopulateArrayByAddingAnObject) {
JsonArray array = json.createArray();
for (int i = 0; i < 2; i++)
{
JsonObject object = json.createObject();
object["id"] = persons[i].id;
object["name"] = persons[i].name;
for (int i = 0; i < 2; i++) {
JsonObject object = json.createObject();
array.add(object); // <- adds a reference to an existing objet (creates 2 extra proxy nodes)
}
object["id"] = persons[i].id;
object["name"] = persons[i].name;
checkJsonString(array);
nodeCountMustBe(15);
array.add(object); // <- adds a reference to an existing objet (creates 2
// extra proxy nodes)
}
checkJsonString(array);
nodeCountMustBe(15);
}
TEST_F(Issue10, PopulateArrayByCreatingNestedObjects)
{
JsonArray array = json.createArray();
TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
JsonArray array = json.createArray();
for (int i = 0; i < 2; i++)
{
JsonObject object = array.createNestedObject();
for (int i = 0; i < 2; i++) {
JsonObject object = array.createNestedObject();
object["id"] = persons[i].id;
object["name"] = persons[i].name;
}
object["id"] = persons[i].id;
object["name"] = persons[i].name;
}
checkJsonString(array);
nodeCountMustBe(11);
checkJsonString(array);
nodeCountMustBe(11);
}

View File

@ -4,149 +4,123 @@
using namespace ArduinoJson;
class JsonArray_Container_Tests : public ::testing::Test
{
class JsonArray_Container_Tests : public ::testing::Test {
protected:
virtual void SetUp()
{
array = json.createArray();
}
virtual void SetUp() { array = json.createArray(); }
void nodeCountMustBe(int expected)
{
EXPECT_EQ(expected, json.size());
}
void nodeCountMustBe(int expected) { EXPECT_EQ(expected, json.size()); }
template<typename T>
void firstElementMustBe(T expected)
{
elementAtIndexMustBe(0, expected);
}
template <typename T> void firstElementMustBe(T expected) {
elementAtIndexMustBe(0, expected);
}
template<typename T>
void secondElementMustBe(T expected)
{
elementAtIndexMustBe(1, expected);
}
template <typename T> void secondElementMustBe(T expected) {
elementAtIndexMustBe(1, expected);
}
void sizeMustBe(int expected)
{
EXPECT_EQ(expected, array.size());
}
void sizeMustBe(int expected) { EXPECT_EQ(expected, array.size()); }
StaticJsonBuffer<42> json;
JsonArray array;
StaticJsonBuffer<42> json;
JsonArray array;
private:
template<typename T>
void elementAtIndexMustBe(int index, T expected)
{
EXPECT_EQ(expected, array[index].as<T>());
}
template <typename T> void elementAtIndexMustBe(int index, T expected) {
EXPECT_EQ(expected, array[index].as<T>());
}
};
TEST_F(JsonArray_Container_Tests, InitialSizeIsZero)
{
sizeMustBe(0);
nodeCountMustBe(1);
TEST_F(JsonArray_Container_Tests, InitialSizeIsZero) {
sizeMustBe(0);
nodeCountMustBe(1);
}
TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded)
{
array.add("hello");
sizeMustBe(1);
nodeCountMustBe(2);
TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded) {
array.add("hello");
sizeMustBe(1);
nodeCountMustBe(2);
array.add("world");
sizeMustBe(2);
nodeCountMustBe(3);
array.add("world");
sizeMustBe(2);
nodeCountMustBe(3);
}
TEST_F(JsonArray_Container_Tests, CanStoreIntegers)
{
array.add(123);
array.add(456);
TEST_F(JsonArray_Container_Tests, CanStoreIntegers) {
array.add(123);
array.add(456);
firstElementMustBe(123);
secondElementMustBe(456);
nodeCountMustBe(3);
firstElementMustBe(123);
secondElementMustBe(456);
nodeCountMustBe(3);
}
TEST_F(JsonArray_Container_Tests, CanStoreDoubles)
{
array.add(123.45);
array.add(456.78);
TEST_F(JsonArray_Container_Tests, CanStoreDoubles) {
array.add(123.45);
array.add(456.78);
firstElementMustBe(123.45);
secondElementMustBe(456.78);
nodeCountMustBe(3);
firstElementMustBe(123.45);
secondElementMustBe(456.78);
nodeCountMustBe(3);
}
TEST_F(JsonArray_Container_Tests, CanStoreBooleans)
{
array.add(true);
array.add(false);
firstElementMustBe(true);
secondElementMustBe(false);
nodeCountMustBe(3);
TEST_F(JsonArray_Container_Tests, CanStoreBooleans) {
array.add(true);
array.add(false);
firstElementMustBe(true);
secondElementMustBe(false);
nodeCountMustBe(3);
}
TEST_F(JsonArray_Container_Tests, CanStoreStrings)
{
const char* firstString = "h3110";
const char* secondString = "w0r1d";
TEST_F(JsonArray_Container_Tests, CanStoreStrings) {
const char *firstString = "h3110";
const char *secondString = "w0r1d";
array.add(firstString);
array.add(secondString);
array.add(firstString);
array.add(secondString);
firstElementMustBe(firstString);
secondElementMustBe(secondString);
nodeCountMustBe(3);
firstElementMustBe(firstString);
secondElementMustBe(secondString);
nodeCountMustBe(3);
}
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays)
{
JsonArray innerarray1 = json.createArray();
JsonArray innerarray2 = json.createArray();
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) {
JsonArray innerarray1 = json.createArray();
JsonArray innerarray2 = json.createArray();
array.add(innerarray1);
array.add(innerarray2);
array.add(innerarray1);
array.add(innerarray2);
firstElementMustBe(innerarray1);
secondElementMustBe(innerarray2);
nodeCountMustBe(1 + 3 + 3);
firstElementMustBe(innerarray1);
secondElementMustBe(innerarray2);
nodeCountMustBe(1 + 3 + 3);
}
TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects)
{
JsonObject innerObject1 = json.createObject();
JsonObject innerObject2 = json.createObject();
TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects) {
JsonObject innerObject1 = json.createObject();
JsonObject innerObject2 = json.createObject();
array.add(innerObject1);
array.add(innerObject2);
array.add(innerObject1);
array.add(innerObject2);
firstElementMustBe(innerObject1);
secondElementMustBe(innerObject2);
nodeCountMustBe(1 + 3 + 3);
firstElementMustBe(innerObject1);
secondElementMustBe(innerObject2);
nodeCountMustBe(1 + 3 + 3);
}
TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays)
{
JsonArray innerarray1 = array.createNestedArray();
JsonArray innerarray2 = array.createNestedArray();
TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays) {
JsonArray innerarray1 = array.createNestedArray();
JsonArray innerarray2 = array.createNestedArray();
firstElementMustBe(innerarray1);
secondElementMustBe(innerarray2);
nodeCountMustBe(1 + 1 + 1);
firstElementMustBe(innerarray1);
secondElementMustBe(innerarray2);
nodeCountMustBe(1 + 1 + 1);
}
TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects)
{
JsonObject innerObject1 = array.createNestedObject();
JsonObject innerObject2 = array.createNestedObject();
TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) {
JsonObject innerObject1 = array.createNestedObject();
JsonObject innerObject2 = array.createNestedObject();
firstElementMustBe(innerObject1);
secondElementMustBe(innerObject2);
nodeCountMustBe(3);
firstElementMustBe(innerObject1);
secondElementMustBe(innerObject2);
nodeCountMustBe(3);
}

View File

@ -4,22 +4,21 @@
using namespace ArduinoJson;
TEST(JsonArray_Iterator_Test, SimpleTest)
{
StaticJsonBuffer<42> jsonBuffer;
TEST(JsonArray_Iterator_Test, SimpleTest) {
StaticJsonBuffer<42> jsonBuffer;
JsonArray array = jsonBuffer.createArray();
array.add(12);
array.add(34);
JsonArray array = jsonBuffer.createArray();
array.add(12);
array.add(34);
JsonArrayIterator it = array.begin();
JsonArrayIterator end = array.end();
JsonArrayIterator it = array.begin();
JsonArrayIterator end = array.end();
EXPECT_NE(end, it);
EXPECT_EQ(12, (*it).as<int>()); // TODO: use ->
++it;
EXPECT_NE(end, it);
EXPECT_EQ(34, (*it).as<int>()); // TODO: use ->
++it;
EXPECT_EQ(array.end(), it);
EXPECT_NE(end, it);
EXPECT_EQ(12, (*it).as<int>()); // TODO: use ->
++it;
EXPECT_NE(end, it);
EXPECT_EQ(34, (*it).as<int>()); // TODO: use ->
++it;
EXPECT_EQ(array.end(), it);
}

View File

@ -11,84 +11,68 @@
using namespace ArduinoJson;
class JsonArray_PrettyPrintTo_Tests : public testing::Test
{
class JsonArray_PrettyPrintTo_Tests : public testing::Test {
protected:
JsonArray array;
StaticJsonBuffer<30> json;
JsonArray array;
StaticJsonBuffer<30> json;
virtual void SetUp()
{
array = json.createArray();
}
virtual void SetUp() { array = json.createArray(); }
void outputMustBe(const char* expected)
{
size_t n = array.prettyPrintTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n);
}
void outputMustBe(const char *expected) {
size_t n = array.prettyPrintTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n);
}
private:
char buffer[256];
char buffer[256];
};
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty)
{
outputMustBe("[]");
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, OneElement)
{
array.add(1);
TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
array.add(1);
array.add(2);
outputMustBe(
"[\r\n"
" 1\r\n"
"]");
outputMustBe("[\r\n"
" 1,\r\n"
" 2\r\n"
"]");
}
TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements)
{
array.add(1);
array.add(2);
TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) {
array.createNestedArray();
array.createNestedArray();
outputMustBe(
"[\r\n"
" 1,\r\n"
" 2\r\n"
"]");
outputMustBe("[\r\n"
" [],\r\n"
" []\r\n"
"]");
}
TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays)
{
array.createNestedArray();
array.createNestedArray();
TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
JsonArray nested1 = array.createNestedArray();
nested1.add(1);
nested1.add(2);
outputMustBe(
"[\r\n"
" [],\r\n"
" []\r\n"
"]");
}
JsonObject nested2 = array.createNestedObject();
nested2["key"] = 3;
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"
"]");
outputMustBe("[\r\n"
" [\r\n"
" 1,\r\n"
" 2\r\n"
" ],\r\n"
" {\r\n"
" \"key\": 3\r\n"
" }\r\n"
"]");
}

View File

@ -10,141 +10,118 @@
using namespace ArduinoJson;
class JsonArray_PrintTo_Tests : public testing::Test
{
class JsonArray_PrintTo_Tests : public testing::Test {
protected:
JsonArray array;
StaticJsonBuffer<3> json;
JsonArray array;
StaticJsonBuffer<3> json;
virtual void SetUp()
{
array = json.createArray();
}
virtual void SetUp() { array = json.createArray(); }
void outputMustBe(const char* expected)
{
size_t n = array.printTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n);
}
void outputMustBe(const char *expected) {
size_t n = array.printTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n);
}
private:
char buffer[256];
char buffer[256];
};
TEST_F(JsonArray_PrintTo_Tests, Empty)
{
outputMustBe("[]");
TEST_F(JsonArray_PrintTo_Tests, Empty) { outputMustBe("[]"); }
TEST_F(JsonArray_PrintTo_Tests, Null) {
array.add((char *)0);
outputMustBe("[null]");
}
TEST_F(JsonArray_PrintTo_Tests, Null)
{
array.add((char*) 0);
TEST_F(JsonArray_PrintTo_Tests, OneString) {
array.add("hello");
outputMustBe("[null]");
outputMustBe("[\"hello\"]");
}
TEST_F(JsonArray_PrintTo_Tests, OneString)
{
array.add("hello");
TEST_F(JsonArray_PrintTo_Tests, TwoStrings) {
array.add("hello");
array.add("world");
outputMustBe("[\"hello\"]");
outputMustBe("[\"hello\",\"world\"]");
}
TEST_F(JsonArray_PrintTo_Tests, TwoStrings)
{
array.add("hello");
array.add("world");
TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity) {
array.add("hello");
array.add("world");
array.add("lost");
outputMustBe("[\"hello\",\"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, 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)
{
array.add(3.14159265358979323846, 4);
outputMustBe("[3.1416]");
TEST_F(JsonArray_PrintTo_Tests, OneInteger) {
array.add(1);
outputMustBe("[1]");
}
TEST_F(JsonArray_PrintTo_Tests, OneInteger)
{
array.add(1);
TEST_F(JsonArray_PrintTo_Tests, TwoIntegers) {
array.add(1);
array.add(2);
outputMustBe("[1]");
outputMustBe("[1,2]");
}
TEST_F(JsonArray_PrintTo_Tests, TwoIntegers)
{
array.add(1);
array.add(2);
TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity) {
array.add(1);
array.add(2);
array.add(3);
outputMustBe("[1,2]");
outputMustBe("[1,2]");
}
TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity)
{
array.add(1);
array.add(2);
array.add(3);
TEST_F(JsonArray_PrintTo_Tests, OneTrue) {
array.add(true);
outputMustBe("[1,2]");
outputMustBe("[true]");
}
TEST_F(JsonArray_PrintTo_Tests, OneTrue)
{
array.add(true);
TEST_F(JsonArray_PrintTo_Tests, OneFalse) {
array.add(false);
outputMustBe("[true]");
outputMustBe("[false]");
}
TEST_F(JsonArray_PrintTo_Tests, OneFalse)
{
array.add(false);
TEST_F(JsonArray_PrintTo_Tests, TwoBooleans) {
array.add(false);
array.add(true);
outputMustBe("[false]");
outputMustBe("[false,true]");
}
TEST_F(JsonArray_PrintTo_Tests, TwoBooleans)
{
array.add(false);
array.add(true);
TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity) {
array.add(false);
array.add(true);
array.add(false);
outputMustBe("[false,true]");
outputMustBe("[false,true]");
}
TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity)
{
array.add(false);
array.add(true);
array.add(false);
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray) {
array.createNestedArray();
outputMustBe("[false,true]");
outputMustBe("[[]]");
}
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray)
{
array.createNestedArray();
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash) {
array.createNestedObject();
outputMustBe("[[]]");
}
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash)
{
array.createNestedObject();
outputMustBe("[{}]");
outputMustBe("[{}]");
}

View File

@ -4,119 +4,105 @@
using namespace ArduinoJson;
class JsonObject_Container_Tests : public ::testing::Test
{
class JsonObject_Container_Tests : public ::testing::Test {
protected:
virtual void SetUp()
{
object = json.createObject();
}
virtual void SetUp() { object = json.createObject(); }
StaticJsonBuffer<42> json;
JsonObject object;
StaticJsonBuffer<42> json;
JsonObject object;
};
TEST_F(JsonObject_Container_Tests, InitialSizeIsZero)
{
EXPECT_EQ(0, object.size());
TEST_F(JsonObject_Container_Tests, InitialSizeIsZero) {
EXPECT_EQ(0, object.size());
}
TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded)
{
object["hello"];
EXPECT_EQ(1, object.size());
TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded) {
object["hello"];
EXPECT_EQ(1, object.size());
object["world"];
EXPECT_EQ(2, object.size());
object["world"];
EXPECT_EQ(2, object.size());
}
TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded)
{
object["hello"];
EXPECT_EQ(1, object.size());
TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded) {
object["hello"];
EXPECT_EQ(1, object.size());
object["hello"];
EXPECT_EQ(1, object.size());
object["hello"];
EXPECT_EQ(1, object.size());
}
TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved)
{
object["hello"];
object["world"];
TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved) {
object["hello"];
object["world"];
object.remove("hello");
EXPECT_EQ(1, object.size());
object.remove("hello");
EXPECT_EQ(1, object.size());
object.remove("world");
EXPECT_EQ(0, object.size());
object.remove("world");
EXPECT_EQ(0, object.size());
}
TEST_F(JsonObject_Container_Tests, DoNotShrink_WhenRemoveIsCalledWithAWrongKey)
{
object["hello"];
object["world"];
TEST_F(JsonObject_Container_Tests,
DoNotShrink_WhenRemoveIsCalledWithAWrongKey) {
object["hello"];
object["world"];
object.remove(":-P");
object.remove(":-P");
EXPECT_EQ(2, object.size());
EXPECT_EQ(2, object.size());
}
TEST_F(JsonObject_Container_Tests, CanStoreIntegers)
{
object["hello"] = 123;
object["world"] = 456;
TEST_F(JsonObject_Container_Tests, CanStoreIntegers) {
object["hello"] = 123;
object["world"] = 456;
EXPECT_EQ(123, (int) object["hello"]);
EXPECT_EQ(456, (int) object["world"]);
EXPECT_EQ(123, (int)object["hello"]);
EXPECT_EQ(456, (int)object["world"]);
}
TEST_F(JsonObject_Container_Tests, CanStoreDoubles)
{
object["hello"] = 123.45;
object["world"] = 456.78;
TEST_F(JsonObject_Container_Tests, CanStoreDoubles) {
object["hello"] = 123.45;
object["world"] = 456.78;
EXPECT_EQ(123.45, (double) object["hello"]);
EXPECT_EQ(456.78, (double) object["world"]);
EXPECT_EQ(123.45, (double)object["hello"]);
EXPECT_EQ(456.78, (double)object["world"]);
}
TEST_F(JsonObject_Container_Tests, CanStoreBooleans)
{
object["hello"] = true;
object["world"] = false;
TEST_F(JsonObject_Container_Tests, CanStoreBooleans) {
object["hello"] = true;
object["world"] = false;
EXPECT_TRUE((bool) object["hello"]);
EXPECT_FALSE((bool) object["world"]);
EXPECT_TRUE((bool)object["hello"]);
EXPECT_FALSE((bool)object["world"]);
}
TEST_F(JsonObject_Container_Tests, CanStoreStrings)
{
object["hello"] = "h3110";
object["world"] = "w0r1d";
TEST_F(JsonObject_Container_Tests, CanStoreStrings) {
object["hello"] = "h3110";
object["world"] = "w0r1d";
EXPECT_STREQ("h3110", (const char*) object["hello"]);
EXPECT_STREQ("w0r1d", (const char*) object["world"]);
EXPECT_STREQ("h3110", (const char *)object["hello"]);
EXPECT_STREQ("w0r1d", (const char *)object["world"]);
}
TEST_F(JsonObject_Container_Tests, CanStoreInnerArrays)
{
JsonArray innerarray1 = json.createArray();
JsonArray innerarray2 = json.createArray();
TEST_F(JsonObject_Container_Tests, CanStoreInnerArrays) {
JsonArray innerarray1 = json.createArray();
JsonArray innerarray2 = json.createArray();
object["hello"] = innerarray1;
object["world"] = innerarray2;
object["hello"] = innerarray1;
object["world"] = innerarray2;
EXPECT_EQ(innerarray1, (JsonArray) object["hello"]);
EXPECT_EQ(innerarray2, (JsonArray) object["world"]);
EXPECT_EQ(innerarray1, (JsonArray)object["hello"]);
EXPECT_EQ(innerarray2, (JsonArray)object["world"]);
}
TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects)
{
JsonObject innerObject1 = json.createObject();
JsonObject innerObject2 = json.createObject();
TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects) {
JsonObject innerObject1 = json.createObject();
JsonObject innerObject2 = json.createObject();
object["hello"] = innerObject1;
object["world"] = innerObject2;
object["hello"] = innerObject1;
object["world"] = innerObject2;
EXPECT_EQ(innerObject1, (JsonObject) object["hello"]);
EXPECT_EQ(innerObject2, (JsonObject) object["world"]);
EXPECT_EQ(innerObject1, (JsonObject)object["hello"]);
EXPECT_EQ(innerObject2, (JsonObject)object["world"]);
}

View File

@ -4,23 +4,23 @@
using namespace ArduinoJson;
TEST(JsonObject_Iterator_Test, SimpleTest)
{
StaticJsonBuffer<42> jsonBuffer;
TEST(JsonObject_Iterator_Test, SimpleTest) {
StaticJsonBuffer<42> jsonBuffer;
JsonObject object = jsonBuffer.createObject();
object["ab"] = 12;
object["cd"] = 34;
JsonObject object = jsonBuffer.createObject();
object["ab"] = 12;
object["cd"] = 34;
JsonObjectIterator it = object.begin();
JsonObjectIterator end = object.end();
JsonObjectIterator it = object.begin();
JsonObjectIterator end = object.end();
EXPECT_NE(end, it);
EXPECT_STREQ("ab", it->key());
EXPECT_EQ(12, it->value().as<int>()); ++it;
EXPECT_NE(end, it);
EXPECT_STREQ("cd", it->key());
EXPECT_EQ(34, it->value().as<int>());
++it;
EXPECT_EQ(object.end(), it);
EXPECT_NE(end, it);
EXPECT_STREQ("ab", it->key());
EXPECT_EQ(12, it->value().as<int>());
++it;
EXPECT_NE(end, it);
EXPECT_STREQ("cd", it->key());
EXPECT_EQ(34, it->value().as<int>());
++it;
EXPECT_EQ(object.end(), it);
}

View File

@ -10,82 +10,66 @@
using namespace ArduinoJson;
class JsonObject_PrettyPrintTo_Tests : public testing::Test
{
class JsonObject_PrettyPrintTo_Tests : public testing::Test {
protected:
JsonObject object;
StaticJsonBuffer<30> json;
JsonObject object;
StaticJsonBuffer<30> json;
virtual void SetUp()
{
object = json.createObject();
}
virtual void SetUp() { object = json.createObject(); }
void outputMustBe(const char* expected)
{
size_t n = object.prettyPrintTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n);
}
void outputMustBe(const char *expected) {
size_t n = object.prettyPrintTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n);
}
private:
char buffer[256];
char buffer[256];
};
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject)
{
outputMustBe("{}");
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
object["key"] = "value";
outputMustBe("{\r\n"
" \"key\": \"value\"\r\n"
"}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember)
{
object["key"] = "value";
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
object["key1"] = "value1";
object["key2"] = "value2";
outputMustBe(
"{\r\n"
" \"key\": \"value\"\r\n"
"}");
outputMustBe("{\r\n"
" \"key1\": \"value1\",\r\n"
" \"key2\": \"value2\"\r\n"
"}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers)
{
object["key1"] = "value1";
object["key2"] = "value2";
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
object.createNestedObject("key1");
object.createNestedArray("key2");
outputMustBe(
"{\r\n"
" \"key1\": \"value1\",\r\n"
" \"key2\": \"value2\"\r\n"
"}");
outputMustBe("{\r\n"
" \"key1\": {},\r\n"
" \"key2\": []\r\n"
"}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers)
{
object.createNestedObject("key1");
object.createNestedArray("key2");
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
JsonObject nested1 = object.createNestedObject("key1");
nested1["a"] = 1;
outputMustBe(
"{\r\n"
" \"key1\": {},\r\n"
" \"key2\": []\r\n"
"}");
}
JsonArray nested2 = object.createNestedArray("key2");
nested2.add(2);
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers)
{
JsonObject nested1 = object.createNestedObject("key1");
nested1["a"] = 1;
JsonArray nested2 = object.createNestedArray("key2");
nested2.add(2);
outputMustBe(
"{\r\n"
" \"key1\": {\r\n"
" \"a\": 1\r\n"
" },\r\n"
" \"key2\": [\r\n"
" 2\r\n"
" ]\r\n"
"}");
outputMustBe("{\r\n"
" \"key1\": {\r\n"
" \"a\": 1\r\n"
" },\r\n"
" \"key2\": [\r\n"
" 2\r\n"
" ]\r\n"
"}");
}

View File

@ -6,155 +6,130 @@
using namespace ArduinoJson;
class JsonObject_Serialization_Tests : public testing::Test
{
class JsonObject_Serialization_Tests : public testing::Test {
protected:
virtual void SetUp()
{
object = json.createObject();
}
virtual void SetUp() { object = json.createObject(); }
void outputMustBe(const char* expected)
{
char actual[256];
int result = object.printTo(actual, sizeof(actual));
void outputMustBe(const char *expected) {
char actual[256];
int result = object.printTo(actual, sizeof(actual));
EXPECT_STREQ(expected, actual);
EXPECT_EQ(strlen(expected), result);
}
EXPECT_STREQ(expected, actual);
EXPECT_EQ(strlen(expected), result);
}
JsonObject object;
StaticJsonBuffer<5> json;
JsonObject object;
StaticJsonBuffer<5> json;
};
TEST_F(JsonObject_Serialization_Tests, EmptyObject)
{
outputMustBe("{}");
TEST_F(JsonObject_Serialization_Tests, EmptyObject) { outputMustBe("{}"); }
TEST_F(JsonObject_Serialization_Tests, OneString) {
object["key"] = "value";
outputMustBe("{\"key\":\"value\"}");
}
TEST_F(JsonObject_Serialization_Tests, OneString)
{
object["key"] = "value";
TEST_F(JsonObject_Serialization_Tests, TwoStrings) {
object["key1"] = "value1";
object["key2"] = "value2";
outputMustBe("{\"key\":\"value\"}");
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
}
TEST_F(JsonObject_Serialization_Tests, TwoStrings)
{
object["key1"] = "value1";
object["key2"] = "value2";
TEST_F(JsonObject_Serialization_Tests, RemoveFirst) {
object["key1"] = "value1";
object["key2"] = "value2";
object.remove("key1");
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
outputMustBe("{\"key2\":\"value2\"}");
}
TEST_F(JsonObject_Serialization_Tests, RemoveFirst)
{
object["key1"] = "value1";
object["key2"] = "value2";
object.remove("key1");
TEST_F(JsonObject_Serialization_Tests, RemoveLast) {
object["key1"] = "value1";
object["key2"] = "value2";
object.remove("key2");
outputMustBe("{\"key2\":\"value2\"}");
outputMustBe("{\"key1\":\"value1\"}");
}
TEST_F(JsonObject_Serialization_Tests, RemoveLast)
{
object["key1"] = "value1";
object["key2"] = "value2";
object.remove("key2");
TEST_F(JsonObject_Serialization_Tests, RemoveUnexistingKey) {
object["key1"] = "value1";
object["key2"] = "value2";
object.remove("key3");
outputMustBe("{\"key1\":\"value1\"}");
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
}
TEST_F(JsonObject_Serialization_Tests, RemoveUnexistingKey)
{
object["key1"] = "value1";
object["key2"] = "value2";
object.remove("key3");
TEST_F(JsonObject_Serialization_Tests, ReplaceExistingKey) {
object["key"] = "value1";
object["key"] = "value2";
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
outputMustBe("{\"key\":\"value2\"}");
}
TEST_F(JsonObject_Serialization_Tests, ReplaceExistingKey)
{
object["key"] = "value1";
object["key"] = "value2";
TEST_F(JsonObject_Serialization_Tests, OneStringOverCapacity) {
object["key1"] = "value1";
object["key2"] = "value2";
object["key3"] = "value3";
outputMustBe("{\"key\":\"value2\"}");
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
}
TEST_F(JsonObject_Serialization_Tests, OneStringOverCapacity)
{
object["key1"] = "value1";
object["key2"] = "value2";
object["key3"] = "value3";
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
TEST_F(JsonObject_Serialization_Tests, OneInteger) {
object["key"] = 1;
outputMustBe("{\"key\":1}");
}
TEST_F(JsonObject_Serialization_Tests, OneInteger)
{
object["key"] = 1;
outputMustBe("{\"key\":1}");
TEST_F(JsonObject_Serialization_Tests, OneDoubleFourDigits) {
object["key"].set(3.14159265358979323846, 4);
outputMustBe("{\"key\":3.1416}");
}
TEST_F(JsonObject_Serialization_Tests, OneDoubleFourDigits)
{
object["key"].set(3.14159265358979323846, 4);
outputMustBe("{\"key\":3.1416}");
TEST_F(JsonObject_Serialization_Tests, OneDoubleDefaultDigits) {
object["key"] = 3.14159265358979323846;
outputMustBe("{\"key\":3.14}");
}
TEST_F(JsonObject_Serialization_Tests, OneDoubleDefaultDigits)
{
object["key"] = 3.14159265358979323846;
outputMustBe("{\"key\":3.14}");
TEST_F(JsonObject_Serialization_Tests, OneNull) {
object["key"] = (char *)0;
outputMustBe("{\"key\":null}");
}
TEST_F(JsonObject_Serialization_Tests, OneNull)
{
object["key"] = (char*) 0;
outputMustBe("{\"key\":null}");
TEST_F(JsonObject_Serialization_Tests, OneTrue) {
object["key"] = true;
outputMustBe("{\"key\":true}");
}
TEST_F(JsonObject_Serialization_Tests, OneTrue)
{
object["key"] = true;
outputMustBe("{\"key\":true}");
TEST_F(JsonObject_Serialization_Tests, OneFalse) {
object["key"] = false;
outputMustBe("{\"key\":false}");
}
TEST_F(JsonObject_Serialization_Tests, OneFalse)
{
object["key"] = false;
outputMustBe("{\"key\":false}");
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArrayViaProxy) {
JsonArray nestedArray = json.createArray();
object["key"] = nestedArray;
outputMustBe("{\"key\":[]}");
}
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArrayViaProxy)
{
JsonArray nestedArray = json.createArray();
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObjectViaProxy) {
JsonObject nestedArray = json.createObject();
object["key"] = nestedArray;
object["key"] = nestedArray;
outputMustBe("{\"key\":[]}");
outputMustBe("{\"key\":{}}");
}
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObjectViaProxy)
{
JsonObject nestedArray = json.createObject();
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObject) {
object.createNestedObject("key");
object["key"] = nestedArray;
outputMustBe("{\"key\":{}}");
outputMustBe("{\"key\":{}}");
}
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObject)
{
object.createNestedObject("key");
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArray) {
object.createNestedArray("key");
outputMustBe("{\"key\":{}}");
}
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArray)
{
object.createNestedArray("key");
outputMustBe("{\"key\":[]}");
outputMustBe("{\"key\":[]}");
}

View File

@ -4,164 +4,136 @@
using namespace ArduinoJson;
class JsonParser_Array_Tests : public testing::Test
{
class JsonParser_Array_Tests : public testing::Test {
protected:
void whenInputIs(const char *json)
{
strcpy(_jsonString, json);
_array = _jsonBuffer.parseArray(_jsonString);
}
void whenInputIs(const char *json) {
strcpy(_jsonString, json);
_array = _jsonBuffer.parseArray(_jsonString);
}
void parseMustSucceed()
{
EXPECT_TRUE(_array.success());
}
void parseMustSucceed() { EXPECT_TRUE(_array.success()); }
void parseMustFail()
{
EXPECT_FALSE(_array.success());
EXPECT_EQ(0, _array.size());
}
void parseMustFail() {
EXPECT_FALSE(_array.success());
EXPECT_EQ(0, _array.size());
}
void sizeMustBe(int expected)
{
EXPECT_EQ(expected, _array.size());
}
void sizeMustBe(int expected) { EXPECT_EQ(expected, _array.size()); }
template<typename T>
void firstElementMustBe(T expected)
{
elementAtIndexMustBe(0, expected);
}
template <typename T> void firstElementMustBe(T expected) {
elementAtIndexMustBe(0, expected);
}
template<typename T>
void secondElementMustBe(T expected)
{
elementAtIndexMustBe(1, expected);
}
template <typename T> void secondElementMustBe(T expected) {
elementAtIndexMustBe(1, expected);
}
template<typename T>
void elementAtIndexMustBe(int index, T expected)
{
EXPECT_EQ(expected, _array[index].as<T>());
}
template <typename T> void elementAtIndexMustBe(int index, T expected) {
EXPECT_EQ(expected, _array[index].as<T>());
}
void elementAtIndexMustBe(int index, const char* expected)
{
EXPECT_STREQ(expected, _array[index].as<const char*>());
}
void elementAtIndexMustBe(int index, const char *expected) {
EXPECT_STREQ(expected, _array[index].as<const char *>());
}
StaticJsonBuffer<42> _jsonBuffer;
JsonArray _array;
char _jsonString[256];
StaticJsonBuffer<42> _jsonBuffer;
JsonArray _array;
char _jsonString[256];
};
TEST_F(JsonParser_Array_Tests, EmptyArray)
{
whenInputIs("[]");
TEST_F(JsonParser_Array_Tests, EmptyArray) {
whenInputIs("[]");
parseMustSucceed();
sizeMustBe(0);
parseMustSucceed();
sizeMustBe(0);
}
TEST_F(JsonParser_Array_Tests, ArrayWithNoEnd)
{
whenInputIs("[");
TEST_F(JsonParser_Array_Tests, ArrayWithNoEnd) {
whenInputIs("[");
parseMustFail();
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, EmptyArrayWithLeadingSpaces)
{
whenInputIs(" []");
TEST_F(JsonParser_Array_Tests, EmptyArrayWithLeadingSpaces) {
whenInputIs(" []");
parseMustSucceed();
sizeMustBe(0);
parseMustSucceed();
sizeMustBe(0);
}
TEST_F(JsonParser_Array_Tests, Garbage)
{
whenInputIs("%*$£¤");
TEST_F(JsonParser_Array_Tests, Garbage) {
whenInputIs("%*$£¤");
parseMustFail();
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, OneInteger)
{
whenInputIs("[42]");
TEST_F(JsonParser_Array_Tests, OneInteger) {
whenInputIs("[42]");
parseMustSucceed();
sizeMustBe(1);
firstElementMustBe(42);
parseMustSucceed();
sizeMustBe(1);
firstElementMustBe(42);
}
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpacesBefore)
{
whenInputIs("[ \t\r\n42]");
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpacesBefore) {
whenInputIs("[ \t\r\n42]");
parseMustSucceed();
sizeMustBe(1);
firstElementMustBe(42);
parseMustSucceed();
sizeMustBe(1);
firstElementMustBe(42);
}
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpaceAfter)
{
whenInputIs("[42 \t\r\n]");
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpaceAfter) {
whenInputIs("[42 \t\r\n]");
parseMustSucceed();
sizeMustBe(1);
firstElementMustBe(42);
parseMustSucceed();
sizeMustBe(1);
firstElementMustBe(42);
}
TEST_F(JsonParser_Array_Tests, TwoIntegers)
{
whenInputIs("[42,84]");
TEST_F(JsonParser_Array_Tests, TwoIntegers) {
whenInputIs("[42,84]");
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe(42);
secondElementMustBe(84);
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe(42);
secondElementMustBe(84);
}
TEST_F(JsonParser_Array_Tests, TwoDoubles)
{
whenInputIs("[4.2,8.4]");
TEST_F(JsonParser_Array_Tests, TwoDoubles) {
whenInputIs("[4.2,8.4]");
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe(4.2);
secondElementMustBe(8.4);
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe(4.2);
secondElementMustBe(8.4);
}
TEST_F(JsonParser_Array_Tests, TwoBooleans)
{
whenInputIs("[true,false]");
TEST_F(JsonParser_Array_Tests, TwoBooleans) {
whenInputIs("[true,false]");
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe(true);
secondElementMustBe(false);
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe(true);
secondElementMustBe(false);
}
TEST_F(JsonParser_Array_Tests, TwoNulls)
{
const char* const nullCharPtr = 0;
TEST_F(JsonParser_Array_Tests, TwoNulls) {
const char *const nullCharPtr = 0;
whenInputIs("[null,null]");
whenInputIs("[null,null]");
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe(nullCharPtr);
secondElementMustBe(nullCharPtr);
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe(nullCharPtr);
secondElementMustBe(nullCharPtr);
}
TEST_F(JsonParser_Array_Tests, TwoStrings)
{
whenInputIs("[\"hello\",\"world\"]");
TEST_F(JsonParser_Array_Tests, TwoStrings) {
whenInputIs("[\"hello\",\"world\"]");
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe("hello");
secondElementMustBe("world");
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe("hello");
secondElementMustBe("world");
}

View File

@ -3,49 +3,48 @@
using namespace ArduinoJson;
TEST(JsonParser_Nested_Tests, ArrayNestedInObject)
{
StaticJsonBuffer<42> jsonBuffer;
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
JsonObject object = jsonBuffer.parseObject(jsonString);
JsonArray array1 = object["ab"];
JsonArray array2 = object["cd"];
ASSERT_TRUE(object.success());
ASSERT_TRUE(array1.success());
ASSERT_TRUE(array2.success());
TEST(JsonParser_Nested_Tests, ArrayNestedInObject) {
StaticJsonBuffer<42> jsonBuffer;
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
ASSERT_EQ(2, array1.size());
ASSERT_EQ(2, array2.size());
EXPECT_EQ(1, array1[0].as<int>());
EXPECT_EQ(2, array1[1].as<int>());
JsonObject object = jsonBuffer.parseObject(jsonString);
JsonArray array1 = object["ab"];
JsonArray array2 = object["cd"];
EXPECT_EQ(3, array2[0].as<int>());
EXPECT_EQ(4, array2[1].as<int>());
ASSERT_TRUE(object.success());
ASSERT_TRUE(array1.success());
ASSERT_TRUE(array2.success());
ASSERT_EQ(2, array1.size());
ASSERT_EQ(2, array2.size());
EXPECT_EQ(1, array1[0].as<int>());
EXPECT_EQ(2, array1[1].as<int>());
EXPECT_EQ(3, array2[0].as<int>());
EXPECT_EQ(4, array2[1].as<int>());
}
TEST(JsonParser_Nested_Tests, ObjectNestedInArray)
{
StaticJsonBuffer<42> jsonBuffer;
char jsonString[] = " [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
JsonArray array = jsonBuffer.parseArray(jsonString);
JsonObject object1 = array[0];
JsonObject object2 = array[1];
ASSERT_TRUE(array.success());
ASSERT_TRUE(object1.success());
ASSERT_TRUE(object2.success());
ASSERT_EQ(2, object1.size());
ASSERT_EQ(2, object2.size());
EXPECT_EQ(1, object1["a"].as<int>());
EXPECT_EQ(2, object1["b"].as<int>());
EXPECT_EQ(3, object2["c"].as<int>());
EXPECT_EQ(4, object2["d"].as<int>());
TEST(JsonParser_Nested_Tests, ObjectNestedInArray) {
StaticJsonBuffer<42> jsonBuffer;
char jsonString[] =
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
JsonArray array = jsonBuffer.parseArray(jsonString);
JsonObject object1 = array[0];
JsonObject object2 = array[1];
ASSERT_TRUE(array.success());
ASSERT_TRUE(object1.success());
ASSERT_TRUE(object2.success());
ASSERT_EQ(2, object1.size());
ASSERT_EQ(2, object2.size());
EXPECT_EQ(1, object1["a"].as<int>());
EXPECT_EQ(2, object1["b"].as<int>());
EXPECT_EQ(3, object2["c"].as<int>());
EXPECT_EQ(4, object2["d"].as<int>());
}

View File

@ -4,198 +4,164 @@
using namespace ArduinoJson;
class JsonParser_Object_Test : public testing::Test
{
class JsonParser_Object_Test : public testing::Test {
protected:
void whenInputIs(const char *jsonString) {
strcpy(_jsonString, jsonString);
_object = _jsonBuffer.parseObject(_jsonString);
}
void whenInputIs(const char* jsonString)
{
strcpy(_jsonString, jsonString);
_object = _jsonBuffer.parseObject(_jsonString);
}
void parseMustSucceed() { EXPECT_TRUE(_object.success()); }
void parseMustSucceed()
{
EXPECT_TRUE(_object.success());
}
void parseMustFail() { EXPECT_FALSE(_object.success()); }
void parseMustFail()
{
EXPECT_FALSE(_object.success());
}
void sizeMustBe(int expected) { EXPECT_EQ(expected, _object.size()); }
void sizeMustBe(int expected)
{
EXPECT_EQ(expected, _object.size());
}
void keyMustHaveValue(const char *key, const char *expected) {
EXPECT_STREQ(expected, _object[key].as<const char *>());
}
void keyMustHaveValue(const char* key, const char* expected)
{
EXPECT_STREQ(expected, _object[key].as<const char*>());
}
template<typename T>
void keyMustHaveValue(const char* key, T expected)
{
EXPECT_EQ(expected, _object[key].as<T>());
}
template <typename T> void keyMustHaveValue(const char *key, T expected) {
EXPECT_EQ(expected, _object[key].as<T>());
}
private:
StaticJsonBuffer<10> _jsonBuffer;
JsonObject _object;
char _jsonString[256];
StaticJsonBuffer<10> _jsonBuffer;
JsonObject _object;
char _jsonString[256];
};
TEST_F(JsonParser_Object_Test, EmptyObject)
{
whenInputIs("{}");
parseMustSucceed();
sizeMustBe(0);
TEST_F(JsonParser_Object_Test, EmptyObject) {
whenInputIs("{}");
parseMustSucceed();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, MissingClosingBrace)
{
whenInputIs("{");
parseMustFail();
sizeMustBe(0);
TEST_F(JsonParser_Object_Test, MissingClosingBrace) {
whenInputIs("{");
parseMustFail();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, MissingColonAndValue)
{
whenInputIs("{\"key\"}");
parseMustFail();
sizeMustBe(0);
TEST_F(JsonParser_Object_Test, MissingColonAndValue) {
whenInputIs("{\"key\"}");
parseMustFail();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, MissingQuotesAndColonAndValue)
{
whenInputIs("{key}");
parseMustFail();
sizeMustBe(0);
TEST_F(JsonParser_Object_Test, MissingQuotesAndColonAndValue) {
whenInputIs("{key}");
parseMustFail();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, OneString)
{
whenInputIs("{\"key\":\"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
TEST_F(JsonParser_Object_Test, OneString) {
whenInputIs("{\"key\":\"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringSingleQuotes)
{
whenInputIs("{'key':'value'}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
TEST_F(JsonParser_Object_Test, OneStringSingleQuotes) {
whenInputIs("{'key':'value'}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeKey)
{
whenInputIs("{ \"key\":\"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeKey) {
whenInputIs("{ \"key\":\"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterKey)
{
whenInputIs("{\"key\" :\"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterKey) {
whenInputIs("{\"key\" :\"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeValue)
{
whenInputIs("{\"key\": \"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeValue) {
whenInputIs("{\"key\": \"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterValue)
{
whenInputIs("{\"key\":\"value\" }");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterValue) {
whenInputIs("{\"key\":\"value\" }");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, TwoStrings)
{
whenInputIs("{\"key1\":\"value1\",\"key2\":\"value2\"}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", "value1");
keyMustHaveValue("key2", "value2");
TEST_F(JsonParser_Object_Test, TwoStrings) {
whenInputIs("{\"key1\":\"value1\",\"key2\":\"value2\"}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", "value1");
keyMustHaveValue("key2", "value2");
}
TEST_F(JsonParser_Object_Test, TwoStringsSpaceBeforeComma)
{
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", "value1");
keyMustHaveValue("key2", "value2");
TEST_F(JsonParser_Object_Test, TwoStringsSpaceBeforeComma) {
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", "value1");
keyMustHaveValue("key2", "value2");
}
TEST_F(JsonParser_Object_Test, TwoStringsSpaceAfterComma)
{
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", "value1");
keyMustHaveValue("key2", "value2");
TEST_F(JsonParser_Object_Test, TwoStringsSpaceAfterComma) {
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", "value1");
keyMustHaveValue("key2", "value2");
}
TEST_F(JsonParser_Object_Test, EndingWithAComma)
{
whenInputIs("{\"key1\":\"value1\",}");
parseMustFail();
sizeMustBe(0);
TEST_F(JsonParser_Object_Test, EndingWithAComma) {
whenInputIs("{\"key1\":\"value1\",}");
parseMustFail();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, TwoIntergers)
{
whenInputIs("{\"key1\":42,\"key2\":-42}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", 42);
keyMustHaveValue("key2", -42);
TEST_F(JsonParser_Object_Test, TwoIntergers) {
whenInputIs("{\"key1\":42,\"key2\":-42}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", 42);
keyMustHaveValue("key2", -42);
}
TEST_F(JsonParser_Object_Test, TwoDoubles)
{
whenInputIs("{\"key1\":12.345,\"key2\":-7.89}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", 12.345);
keyMustHaveValue("key2", -7.89);
TEST_F(JsonParser_Object_Test, TwoDoubles) {
whenInputIs("{\"key1\":12.345,\"key2\":-7.89}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", 12.345);
keyMustHaveValue("key2", -7.89);
}
TEST_F(JsonParser_Object_Test, TwoBooleans)
{
whenInputIs("{\"key1\":true,\"key2\":false}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", true);
keyMustHaveValue("key2", false);
TEST_F(JsonParser_Object_Test, TwoBooleans) {
whenInputIs("{\"key1\":true,\"key2\":false}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", true);
keyMustHaveValue("key2", false);
}
TEST_F(JsonParser_Object_Test, TwoNulls)
{
const char* const nullstr = 0;
TEST_F(JsonParser_Object_Test, TwoNulls) {
const char *const nullstr = 0;
whenInputIs("{\"key1\":null,\"key2\":null}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", nullstr);
keyMustHaveValue("key2", nullstr);
whenInputIs("{\"key1\":null,\"key2\":null}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", nullstr);
keyMustHaveValue("key2", nullstr);
}
TEST_F(JsonParser_Object_Test, NullForKey)
{
whenInputIs("null:\"value\"}");
parseMustFail();
TEST_F(JsonParser_Object_Test, NullForKey) {
whenInputIs("null:\"value\"}");
parseMustFail();
}

View File

@ -4,118 +4,102 @@
using namespace ArduinoJson;
class JsonValueTests : public ::testing::Test
{
class JsonValueTests : public ::testing::Test {
protected:
virtual void SetUp()
{
jsonValue1 = json.createValue();
jsonValue2 = json.createValue();
}
virtual void SetUp() {
jsonValue1 = json.createValue();
jsonValue2 = json.createValue();
}
StaticJsonBuffer<42> json;
JsonValue jsonValue1;
JsonValue jsonValue2;
StaticJsonBuffer<42> json;
JsonValue jsonValue1;
JsonValue jsonValue2;
};
TEST_F(JsonValueTests, CanStoreInteger) {
jsonValue1 = 123;
TEST_F(JsonValueTests, CanStoreInteger)
{
jsonValue1 = 123;
EXPECT_EQ(123, (int) jsonValue1);
EXPECT_EQ(123, (int)jsonValue1);
}
TEST_F(JsonValueTests, CanStoreDouble)
{
jsonValue1 = 123.45;
TEST_F(JsonValueTests, CanStoreDouble) {
jsonValue1 = 123.45;
EXPECT_EQ(123.45, (double) jsonValue1);
EXPECT_EQ(123.45, (double)jsonValue1);
}
TEST_F(JsonValueTests, CanStoreTrue)
{
jsonValue1 = true;
EXPECT_TRUE((bool) jsonValue1);
TEST_F(JsonValueTests, CanStoreTrue) {
jsonValue1 = true;
EXPECT_TRUE((bool)jsonValue1);
}
TEST_F(JsonValueTests, CanStoreFalse)
{
jsonValue1 = false;
EXPECT_FALSE((bool) jsonValue1);
TEST_F(JsonValueTests, CanStoreFalse) {
jsonValue1 = false;
EXPECT_FALSE((bool)jsonValue1);
}
TEST_F(JsonValueTests, CanStoreString)
{
jsonValue1 = "hello";
EXPECT_STREQ("hello", (const char*) jsonValue1);
TEST_F(JsonValueTests, CanStoreString) {
jsonValue1 = "hello";
EXPECT_STREQ("hello", (const char *)jsonValue1);
}
TEST_F(JsonValueTests, CanStoreObject)
{
JsonObject innerObject1 = json.createObject();
TEST_F(JsonValueTests, CanStoreObject) {
JsonObject innerObject1 = json.createObject();
jsonValue1 = innerObject1;
jsonValue1 = innerObject1;
EXPECT_EQ(innerObject1, (JsonObject) jsonValue1);
EXPECT_EQ(innerObject1, (JsonObject)jsonValue1);
}
TEST_F(JsonValueTests, IntegersAreCopiedByValue)
{
jsonValue1 = 123;
jsonValue2 = jsonValue1;
jsonValue1 = 456;
TEST_F(JsonValueTests, IntegersAreCopiedByValue) {
jsonValue1 = 123;
jsonValue2 = jsonValue1;
jsonValue1 = 456;
EXPECT_EQ(123, (int) jsonValue2);
EXPECT_EQ(123, (int)jsonValue2);
}
TEST_F(JsonValueTests, DoublesAreCopiedByValue)
{
jsonValue1 = 123.45;
jsonValue2 = jsonValue1;
jsonValue1 = 456.78;
TEST_F(JsonValueTests, DoublesAreCopiedByValue) {
jsonValue1 = 123.45;
jsonValue2 = jsonValue1;
jsonValue1 = 456.78;
EXPECT_EQ(123.45, (double) jsonValue2);
EXPECT_EQ(123.45, (double)jsonValue2);
}
TEST_F(JsonValueTests, BooleansAreCopiedByValue)
{
jsonValue1 = true;
jsonValue2 = jsonValue1;
jsonValue1 = false;
TEST_F(JsonValueTests, BooleansAreCopiedByValue) {
jsonValue1 = true;
jsonValue2 = jsonValue1;
jsonValue1 = false;
EXPECT_TRUE((bool) jsonValue2);
EXPECT_TRUE((bool)jsonValue2);
}
TEST_F(JsonValueTests, StringsAreCopiedByValue)
{
jsonValue1 = "hello";
jsonValue2 = jsonValue1;
jsonValue1 = "world";
TEST_F(JsonValueTests, StringsAreCopiedByValue) {
jsonValue1 = "hello";
jsonValue2 = jsonValue1;
jsonValue1 = "world";
EXPECT_STREQ("hello", (const char*) jsonValue2);
EXPECT_STREQ("hello", (const char *)jsonValue2);
}
TEST_F(JsonValueTests, ObjectsAreCopiedByReference) {
JsonObject object = json.createObject();
TEST_F(JsonValueTests, ObjectsAreCopiedByReference)
{
JsonObject object = json.createObject();
jsonValue1 = object;
jsonValue1 = object;
object["hello"] = "world";
object["hello"] = "world";
EXPECT_EQ(1, ((JsonObject) jsonValue1).size());
EXPECT_EQ(1, ((JsonObject)jsonValue1).size());
}
TEST_F(JsonValueTests, ArraysAreCopiedByReference)
{
JsonArray array = json.createArray();
TEST_F(JsonValueTests, ArraysAreCopiedByReference) {
JsonArray array = json.createArray();
jsonValue1 = array;
array.add("world");
jsonValue1 = array;
EXPECT_EQ(1, ((JsonObject) jsonValue1).size());
array.add("world");
EXPECT_EQ(1, ((JsonObject)jsonValue1).size());
}

View File

@ -3,147 +3,122 @@
using namespace ArduinoJson::Internals;
class QuotedString_ExtractFrom_Tests : public testing::Test
{
class QuotedString_ExtractFrom_Tests : public testing::Test {
protected:
void whenInputIs(const char* json)
{
strcpy(_jsonString, json);
_result = QuotedString::extractFrom(_jsonString, &_trailing);
}
void whenInputIs(const char *json) {
strcpy(_jsonString, json);
_result = QuotedString::extractFrom(_jsonString, &_trailing);
}
void resultMustBe(const char* expected)
{
EXPECT_STREQ(expected, _result);
}
void resultMustBe(const char *expected) { EXPECT_STREQ(expected, _result); }
void trailingMustBe(const char* expected)
{
EXPECT_STREQ(expected, _trailing);
}
void trailingMustBe(const char *expected) {
EXPECT_STREQ(expected, _trailing);
}
private:
char _jsonString[256];
char* _result;
char* _trailing;
char _jsonString[256];
char *_result;
char *_trailing;
};
TEST_F(QuotedString_ExtractFrom_Tests, EmptyDoubleQuotedString) {
whenInputIs("\"\"");
TEST_F(QuotedString_ExtractFrom_Tests, EmptyDoubleQuotedString)
{
whenInputIs("\"\"");
resultMustBe("");
trailingMustBe("");
resultMustBe("");
trailingMustBe("");
}
TEST_F(QuotedString_ExtractFrom_Tests, NoQuotes)
{
whenInputIs("hello world");
TEST_F(QuotedString_ExtractFrom_Tests, NoQuotes) {
whenInputIs("hello world");
resultMustBe(0);
resultMustBe(0);
}
TEST_F(QuotedString_ExtractFrom_Tests, EmptySingleQuotedString)
{
whenInputIs("''");
TEST_F(QuotedString_ExtractFrom_Tests, EmptySingleQuotedString) {
whenInputIs("''");
resultMustBe("");
trailingMustBe("");
resultMustBe("");
trailingMustBe("");
}
TEST_F(QuotedString_ExtractFrom_Tests, SimpleDoubleQuotedString)
{
whenInputIs("\"hello world\"");
TEST_F(QuotedString_ExtractFrom_Tests, SimpleDoubleQuotedString) {
whenInputIs("\"hello world\"");
resultMustBe("hello world");
trailingMustBe("");
resultMustBe("hello world");
trailingMustBe("");
}
TEST_F(QuotedString_ExtractFrom_Tests, DoubleQuotedStringWithTrailing)
{
whenInputIs("\"hello\" world");
TEST_F(QuotedString_ExtractFrom_Tests, DoubleQuotedStringWithTrailing) {
whenInputIs("\"hello\" world");
resultMustBe("hello");
trailingMustBe(" world");
resultMustBe("hello");
trailingMustBe(" world");
}
TEST_F(QuotedString_ExtractFrom_Tests, SingleQuotedStringWithTrailing)
{
whenInputIs("'hello' world");
TEST_F(QuotedString_ExtractFrom_Tests, SingleQuotedStringWithTrailing) {
whenInputIs("'hello' world");
resultMustBe("hello");
trailingMustBe(" world");
resultMustBe("hello");
trailingMustBe(" world");
}
TEST_F(QuotedString_ExtractFrom_Tests, CurlyBraces)
{
whenInputIs("\"{hello:world}\"");
resultMustBe("{hello:world}");
TEST_F(QuotedString_ExtractFrom_Tests, CurlyBraces) {
whenInputIs("\"{hello:world}\"");
resultMustBe("{hello:world}");
}
TEST_F(QuotedString_ExtractFrom_Tests, SquareBraquets)
{
whenInputIs("\"[hello,world]\"");
resultMustBe("[hello,world]");
TEST_F(QuotedString_ExtractFrom_Tests, SquareBraquets) {
whenInputIs("\"[hello,world]\"");
resultMustBe("[hello,world]");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedDoubleQuote)
{
whenInputIs("\"hello \\\"world\\\"\"");
resultMustBe("hello \"world\"");
TEST_F(QuotedString_ExtractFrom_Tests, EscapedDoubleQuote) {
whenInputIs("\"hello \\\"world\\\"\"");
resultMustBe("hello \"world\"");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSingleQuote)
{
whenInputIs("\"hello \\\'world\\\'\"");
resultMustBe("hello 'world'");
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSingleQuote) {
whenInputIs("\"hello \\\'world\\\'\"");
resultMustBe("hello 'world'");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSolidus)
{
whenInputIs("\"hello \\/world\\/\"");
resultMustBe("hello /world/");
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSolidus) {
whenInputIs("\"hello \\/world\\/\"");
resultMustBe("hello /world/");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedReverseSolidus)
{
whenInputIs("\"hello \\\\world\\\\\"");
resultMustBe("hello \\world\\");
TEST_F(QuotedString_ExtractFrom_Tests, EscapedReverseSolidus) {
whenInputIs("\"hello \\\\world\\\\\"");
resultMustBe("hello \\world\\");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedBackspace)
{
whenInputIs("\"hello \\bworld\\b\"");
resultMustBe("hello \bworld\b");
TEST_F(QuotedString_ExtractFrom_Tests, EscapedBackspace) {
whenInputIs("\"hello \\bworld\\b\"");
resultMustBe("hello \bworld\b");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedFormfeed)
{
whenInputIs("\"hello \\fworld\\f\"");
resultMustBe("hello \fworld\f");
TEST_F(QuotedString_ExtractFrom_Tests, EscapedFormfeed) {
whenInputIs("\"hello \\fworld\\f\"");
resultMustBe("hello \fworld\f");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedNewline)
{
whenInputIs("\"hello \\nworld\\n\"");
resultMustBe("hello \nworld\n");
TEST_F(QuotedString_ExtractFrom_Tests, EscapedNewline) {
whenInputIs("\"hello \\nworld\\n\"");
resultMustBe("hello \nworld\n");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedCarriageReturn)
{
whenInputIs("\"hello \\rworld\\r\"");
resultMustBe("hello \rworld\r");
TEST_F(QuotedString_ExtractFrom_Tests, EscapedCarriageReturn) {
whenInputIs("\"hello \\rworld\\r\"");
resultMustBe("hello \rworld\r");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedTab)
{
whenInputIs("\"hello \\tworld\\t\"");
resultMustBe("hello \tworld\t");
TEST_F(QuotedString_ExtractFrom_Tests, EscapedTab) {
whenInputIs("\"hello \\tworld\\t\"");
resultMustBe("hello \tworld\t");
}
TEST_F(QuotedString_ExtractFrom_Tests, AllEscapedCharsTogether)
{
whenInputIs("\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"");
resultMustBe("1\"2\\3/4\b5\f6\n7\r8\t9");
TEST_F(QuotedString_ExtractFrom_Tests, AllEscapedCharsTogether) {
whenInputIs("\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"");
resultMustBe("1\"2\\3/4\b5\f6\n7\r8\t9");
}

View File

@ -5,82 +5,69 @@
using namespace ArduinoJson::Internals;
class QuotedString_PrintTo_Tests : public testing::Test
{
class QuotedString_PrintTo_Tests : public testing::Test {
protected:
void whenInputIs(const char* input)
{
StringBuilder sb(buffer, sizeof(buffer));
returnValue = QuotedString::printTo(input, &sb);
}
void whenInputIs(const char *input) {
StringBuilder sb(buffer, sizeof(buffer));
returnValue = QuotedString::printTo(input, &sb);
}
void outputMustBe(const char* expected)
{
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), returnValue);
}
void outputMustBe(const char *expected) {
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), returnValue);
}
private:
char buffer[1024];
size_t returnValue;
char buffer[1024];
size_t returnValue;
};
TEST_F(QuotedString_PrintTo_Tests, Null)
{
whenInputIs(0);
outputMustBe("null");
TEST_F(QuotedString_PrintTo_Tests, Null) {
whenInputIs(0);
outputMustBe("null");
}
TEST_F(QuotedString_PrintTo_Tests, EmptyString)
{
whenInputIs("");
outputMustBe("\"\"");
TEST_F(QuotedString_PrintTo_Tests, EmptyString) {
whenInputIs("");
outputMustBe("\"\"");
}
TEST_F(QuotedString_PrintTo_Tests, QuotationMark)
{
whenInputIs("\"");
outputMustBe("\"\\\"\"");
TEST_F(QuotedString_PrintTo_Tests, QuotationMark) {
whenInputIs("\"");
outputMustBe("\"\\\"\"");
}
TEST_F(QuotedString_PrintTo_Tests, ReverseSolidus)
{
whenInputIs("\\");
outputMustBe("\"\\\\\"");
TEST_F(QuotedString_PrintTo_Tests, ReverseSolidus) {
whenInputIs("\\");
outputMustBe("\"\\\\\"");
}
TEST_F(QuotedString_PrintTo_Tests, Solidus)
{
whenInputIs("/");
outputMustBe("\"/\""); // but the JSON format allows \/
TEST_F(QuotedString_PrintTo_Tests, Solidus) {
whenInputIs("/");
outputMustBe("\"/\""); // but the JSON format allows \/
}
TEST_F(QuotedString_PrintTo_Tests, Backspace)
{
whenInputIs("\b");
outputMustBe("\"\\b\"");
TEST_F(QuotedString_PrintTo_Tests, Backspace) {
whenInputIs("\b");
outputMustBe("\"\\b\"");
}
TEST_F(QuotedString_PrintTo_Tests, Formfeed)
{
whenInputIs("\f");
outputMustBe("\"\\f\"");
TEST_F(QuotedString_PrintTo_Tests, Formfeed) {
whenInputIs("\f");
outputMustBe("\"\\f\"");
}
TEST_F(QuotedString_PrintTo_Tests, Newline)
{
whenInputIs("\n");
outputMustBe("\"\\n\"");
TEST_F(QuotedString_PrintTo_Tests, Newline) {
whenInputIs("\n");
outputMustBe("\"\\n\"");
}
TEST_F(QuotedString_PrintTo_Tests, CarriageReturn)
{
whenInputIs("\r");
outputMustBe("\"\\r\"");
TEST_F(QuotedString_PrintTo_Tests, CarriageReturn) {
whenInputIs("\r");
outputMustBe("\"\\r\"");
}
TEST_F(QuotedString_PrintTo_Tests, HorizontalTab)
{
whenInputIs("\t");
outputMustBe("\"\\t\"");
TEST_F(QuotedString_PrintTo_Tests, HorizontalTab) {
whenInputIs("\t");
outputMustBe("\"\\t\"");
}

View File

@ -4,68 +4,66 @@
using namespace ArduinoJson;
TEST(StaticJsonBuffer, CapacityMatchTemplateParameter)
{
StaticJsonBuffer<42> json;
EXPECT_EQ(42, json.capacity());
TEST(StaticJsonBuffer, CapacityMatchTemplateParameter) {
StaticJsonBuffer<42> json;
EXPECT_EQ(42, json.capacity());
}
TEST(StaticJsonBuffer, InitialSizeIsZero)
{
StaticJsonBuffer<42> json;
EXPECT_EQ(0, json.size());
TEST(StaticJsonBuffer, InitialSizeIsZero) {
StaticJsonBuffer<42> json;
EXPECT_EQ(0, json.size());
}
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenSizeIsIncreasedByOne)
{
StaticJsonBuffer<42> json;
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenSizeIsIncreasedByOne) {
StaticJsonBuffer<42> json;
json.createObject();
EXPECT_EQ(1, json.size());
json.createObject();
EXPECT_EQ(2, json.size());
json.createObject();
EXPECT_EQ(1, json.size());
json.createObject();
EXPECT_EQ(2, json.size());
}
TEST(StaticJsonBuffer, GivenBufferIsFull_WhenCreateObjectIsCalled_ThenSizeDoesNotChange)
{
StaticJsonBuffer<1> json;
TEST(StaticJsonBuffer,
GivenBufferIsFull_WhenCreateObjectIsCalled_ThenSizeDoesNotChange) {
StaticJsonBuffer<1> json;
json.createObject();
EXPECT_EQ(1, json.size());
json.createObject();
EXPECT_EQ(1, json.size());
json.createObject();
EXPECT_EQ(1, json.size());
json.createObject();
EXPECT_EQ(1, json.size());
}
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned)
{
StaticJsonBuffer<42> json;
TEST(StaticJsonBuffer,
WhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned) {
StaticJsonBuffer<42> json;
JsonObject obj = json.createObject();
EXPECT_EQ(0, obj.size());
JsonObject obj = json.createObject();
EXPECT_EQ(0, obj.size());
}
TEST(StaticJsonBuffer, GivenAJsonObject_WhenValuesAreAdded_ThenSizeIsIncreasedByTwo)
{
StaticJsonBuffer<42> json;
JsonObject obj = json.createObject();
TEST(StaticJsonBuffer,
GivenAJsonObject_WhenValuesAreAdded_ThenSizeIsIncreasedByTwo) {
StaticJsonBuffer<42> json;
JsonObject obj = json.createObject();
obj["hello"];
EXPECT_EQ(3, json.size());
obj["hello"];
EXPECT_EQ(3, json.size());
obj["world"];
EXPECT_EQ(5, json.size());
obj["world"];
EXPECT_EQ(5, json.size());
}
TEST(StaticJsonBuffer, GivenAJsonObject_WhenSameValuesAreAddedTwice_ThenSizeIsOnlyIncreasedByTwo)
{
StaticJsonBuffer<42> json;
JsonObject obj = json.createObject();
TEST(
StaticJsonBuffer,
GivenAJsonObject_WhenSameValuesAreAddedTwice_ThenSizeIsOnlyIncreasedByTwo) {
StaticJsonBuffer<42> json;
JsonObject obj = json.createObject();
obj["hello"];
EXPECT_EQ(3, json.size());
obj["hello"];
EXPECT_EQ(3, json.size());
obj["hello"];
EXPECT_EQ(3, json.size());
obj["hello"];
EXPECT_EQ(3, json.size());
}

View File

@ -3,73 +3,52 @@
using namespace ArduinoJson::Internals;
class StringBuilderTests : public testing::Test
{
class StringBuilderTests : public testing::Test {
protected:
virtual void SetUp()
{
sb = new StringBuilder(buffer, sizeof(buffer));
}
virtual void SetUp() { sb = new StringBuilder(buffer, sizeof(buffer)); }
void print(const char* value)
{
returnValue = sb->print(value);
}
void print(const char *value) { returnValue = sb->print(value); }
void outputMustBe(const char* expected)
{
EXPECT_STREQ(expected, buffer);
}
void outputMustBe(const char *expected) { EXPECT_STREQ(expected, buffer); }
void resultMustBe(size_t expected)
{
EXPECT_EQ(expected, returnValue);
}
void resultMustBe(size_t expected) { EXPECT_EQ(expected, returnValue); }
private:
char buffer[20];
Print* sb;
size_t returnValue;
char buffer[20];
Print *sb;
size_t returnValue;
};
TEST_F(StringBuilderTests, InitialState)
{
outputMustBe("");
TEST_F(StringBuilderTests, InitialState) { outputMustBe(""); }
TEST_F(StringBuilderTests, OverCapacity) {
print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
resultMustBe(19);
print("ABC");
resultMustBe(0);
outputMustBe("ABCDEFGHIJKLMNOPQRS");
}
TEST_F(StringBuilderTests, OverCapacity)
{
print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
resultMustBe(19);
print("ABC");
resultMustBe(0);
outputMustBe("ABCDEFGHIJKLMNOPQRS");
TEST_F(StringBuilderTests, EmptyString) {
print("");
resultMustBe(0);
outputMustBe("");
}
TEST_F(StringBuilderTests, EmptyString)
{
print("");
resultMustBe(0);
outputMustBe("");
TEST_F(StringBuilderTests, OneString) {
print("ABCD");
resultMustBe(4);
outputMustBe("ABCD");
}
TEST_F(StringBuilderTests, OneString)
{
print("ABCD");
resultMustBe(4);
outputMustBe("ABCD");
}
TEST_F(StringBuilderTests, TwoStrings) {
print("ABCD");
resultMustBe(4);
TEST_F(StringBuilderTests, TwoStrings)
{
print("ABCD");
resultMustBe(4);
print("EFGH");
resultMustBe(4);
print("EFGH");
resultMustBe(4);
outputMustBe("ABCDEFGH");
outputMustBe("ABCDEFGH");
}