Renamed tests/ into test/

This commit is contained in:
Benoit Blanchon
2014-10-16 16:23:24 +02:00
parent 58f155e135
commit b847576bb4
14 changed files with 1196 additions and 1196 deletions

View File

@ -4,4 +4,4 @@ project(ArduinoJson)
enable_testing() enable_testing()
add_subdirectory(srcs) add_subdirectory(srcs)
add_subdirectory(tests) add_subdirectory(test)

View File

@ -1,19 +1,19 @@
set(GTEST_DIR ../third-party/gtest-1.7.0) set(GTEST_DIR ../third-party/gtest-1.7.0)
file(GLOB_RECURSE INC_FILES ../include/*.h) file(GLOB_RECURSE INC_FILES ../include/*.h)
file(GLOB TESTS_FILES *.cpp) file(GLOB TESTS_FILES *.cpp)
include_directories( include_directories(
../include ../include
${GTEST_DIR} ${GTEST_DIR}
${GTEST_DIR}/include) ${GTEST_DIR}/include)
add_executable(ArduinoJsonTests add_executable(ArduinoJsonTests
${TESTS_FILES} ${TESTS_FILES}
${INC_FILES} ${INC_FILES}
${GTEST_DIR}/src/gtest-all.cc ${GTEST_DIR}/src/gtest-all.cc
${GTEST_DIR}/src/gtest_main.cc) ${GTEST_DIR}/src/gtest_main.cc)
target_link_libraries(ArduinoJsonTests ArduinoJson) target_link_libraries(ArduinoJsonTests ArduinoJson)
add_test(ArduinoJsonTests ArduinoJsonTests) add_test(ArduinoJsonTests ArduinoJsonTests)

View File

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

View File

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

View File

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

View File

@ -1,92 +1,92 @@
/* /*
* Arduino JSON library * Arduino JSON library
* Benoit Blanchon 2014 - MIT License * Benoit Blanchon 2014 - MIT License
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson/JsonArray.h> #include <ArduinoJson/JsonArray.h>
#include <ArduinoJson/JsonObject.h> #include <ArduinoJson/JsonObject.h>
#include <ArduinoJson/JsonValue.h> #include <ArduinoJson/JsonValue.h>
#include <ArduinoJson/StaticJsonBuffer.h> #include <ArduinoJson/StaticJsonBuffer.h>
class JsonArray_PrettyPrintTo_Tests : public testing::Test class JsonArray_PrettyPrintTo_Tests : public testing::Test
{ {
protected: protected:
JsonArray array; JsonArray array;
StaticJsonBuffer<30> json; StaticJsonBuffer<30> json;
virtual void SetUp() virtual void SetUp()
{ {
array = json.createArray(); array = json.createArray();
} }
void outputMustBe(const char* expected) void outputMustBe(const char* expected)
{ {
size_t n = array.prettyPrintTo(buffer, sizeof(buffer)); size_t n = array.prettyPrintTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer); EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n); EXPECT_EQ(strlen(expected), n);
} }
private: private:
char buffer[256]; char buffer[256];
}; };
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) TEST_F(JsonArray_PrettyPrintTo_Tests, Empty)
{ {
outputMustBe("[]"); outputMustBe("[]");
} }
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement)
{ {
array.add(1); array.add(1);
outputMustBe( outputMustBe(
"[\r\n" "[\r\n"
" 1\r\n" " 1\r\n"
"]"); "]");
} }
TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements)
{ {
array.add(1); array.add(1);
array.add(2); array.add(2);
outputMustBe( outputMustBe(
"[\r\n" "[\r\n"
" 1,\r\n" " 1,\r\n"
" 2\r\n" " 2\r\n"
"]"); "]");
} }
TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays)
{ {
array.createNestedArray(); array.createNestedArray();
array.createNestedArray(); array.createNestedArray();
outputMustBe( outputMustBe(
"[\r\n" "[\r\n"
" [],\r\n" " [],\r\n"
" []\r\n" " []\r\n"
"]"); "]");
} }
TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays)
{ {
JsonArray nested1 = array.createNestedArray(); JsonArray nested1 = array.createNestedArray();
nested1.add(1); nested1.add(1);
nested1.add(2); nested1.add(2);
JsonObject nested2 = array.createNestedObject(); JsonObject nested2 = array.createNestedObject();
nested2["key"] = 3; nested2["key"] = 3;
outputMustBe( outputMustBe(
"[\r\n" "[\r\n"
" [\r\n" " [\r\n"
" 1,\r\n" " 1,\r\n"
" 2\r\n" " 2\r\n"
" ],\r\n" " ],\r\n"
" {\r\n" " {\r\n"
" \"key\": 3\r\n" " \"key\": 3\r\n"
" }\r\n" " }\r\n"
"]"); "]");
} }

View File

@ -1,148 +1,148 @@
/* /*
* Arduino JSON library * Arduino JSON library
* Benoit Blanchon 2014 - MIT License * Benoit Blanchon 2014 - MIT License
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson/JsonArray.h> #include <ArduinoJson/JsonArray.h>
#include <ArduinoJson/JsonObject.h> #include <ArduinoJson/JsonObject.h>
#include <ArduinoJson/StaticJsonBuffer.h> #include <ArduinoJson/StaticJsonBuffer.h>
class JsonArray_PrintTo_Tests : public testing::Test class JsonArray_PrintTo_Tests : public testing::Test
{ {
protected: protected:
JsonArray array; JsonArray array;
StaticJsonBuffer<3> json; StaticJsonBuffer<3> json;
virtual void SetUp() virtual void SetUp()
{ {
array = json.createArray(); array = json.createArray();
} }
void outputMustBe(const char* expected) void outputMustBe(const char* expected)
{ {
size_t n = array.printTo(buffer, sizeof(buffer)); size_t n = array.printTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer); EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n); EXPECT_EQ(strlen(expected), n);
} }
private: private:
char buffer[256]; char buffer[256];
}; };
TEST_F(JsonArray_PrintTo_Tests, Empty) TEST_F(JsonArray_PrintTo_Tests, Empty)
{ {
outputMustBe("[]"); outputMustBe("[]");
} }
TEST_F(JsonArray_PrintTo_Tests, Null) TEST_F(JsonArray_PrintTo_Tests, Null)
{ {
array.add((char*) 0); array.add((char*) 0);
outputMustBe("[null]"); outputMustBe("[null]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneString) TEST_F(JsonArray_PrintTo_Tests, OneString)
{ {
array.add("hello"); array.add("hello");
outputMustBe("[\"hello\"]"); outputMustBe("[\"hello\"]");
} }
TEST_F(JsonArray_PrintTo_Tests, TwoStrings) TEST_F(JsonArray_PrintTo_Tests, TwoStrings)
{ {
array.add("hello"); array.add("hello");
array.add("world"); array.add("world");
outputMustBe("[\"hello\",\"world\"]"); outputMustBe("[\"hello\",\"world\"]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity) TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity)
{ {
array.add("hello"); array.add("hello");
array.add("world"); array.add("world");
array.add("lost"); array.add("lost");
outputMustBe("[\"hello\",\"world\"]"); outputMustBe("[\"hello\",\"world\"]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits) TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits)
{ {
array.add(3.14159265358979323846); array.add(3.14159265358979323846);
outputMustBe("[3.14]"); outputMustBe("[3.14]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits) TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits)
{ {
array.add(3.14159265358979323846, 4); array.add(3.14159265358979323846, 4);
outputMustBe("[3.1416]"); outputMustBe("[3.1416]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneInteger) TEST_F(JsonArray_PrintTo_Tests, OneInteger)
{ {
array.add(1); array.add(1);
outputMustBe("[1]"); outputMustBe("[1]");
} }
TEST_F(JsonArray_PrintTo_Tests, TwoIntegers) TEST_F(JsonArray_PrintTo_Tests, TwoIntegers)
{ {
array.add(1); array.add(1);
array.add(2); array.add(2);
outputMustBe("[1,2]"); outputMustBe("[1,2]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity) TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity)
{ {
array.add(1); array.add(1);
array.add(2); array.add(2);
array.add(3); array.add(3);
outputMustBe("[1,2]"); outputMustBe("[1,2]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneTrue) TEST_F(JsonArray_PrintTo_Tests, OneTrue)
{ {
array.add(true); array.add(true);
outputMustBe("[true]"); outputMustBe("[true]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneFalse) TEST_F(JsonArray_PrintTo_Tests, OneFalse)
{ {
array.add(false); array.add(false);
outputMustBe("[false]"); outputMustBe("[false]");
} }
TEST_F(JsonArray_PrintTo_Tests, TwoBooleans) TEST_F(JsonArray_PrintTo_Tests, TwoBooleans)
{ {
array.add(false); array.add(false);
array.add(true); array.add(true);
outputMustBe("[false,true]"); outputMustBe("[false,true]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity) TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity)
{ {
array.add(false); array.add(false);
array.add(true); array.add(true);
array.add(false); array.add(false);
outputMustBe("[false,true]"); outputMustBe("[false,true]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray) TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray)
{ {
array.createNestedArray(); array.createNestedArray();
outputMustBe("[[]]"); outputMustBe("[[]]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash) TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash)
{ {
array.createNestedObject(); array.createNestedObject();
outputMustBe("[{}]"); outputMustBe("[{}]");
} }

View File

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

View File

@ -1,89 +1,89 @@
/* /*
* Arduino JSON library * Arduino JSON library
* Benoit Blanchon 2014 - MIT License * Benoit Blanchon 2014 - MIT License
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson/JsonObject.h> #include <ArduinoJson/JsonObject.h>
#include <ArduinoJson/JsonValue.h> #include <ArduinoJson/JsonValue.h>
#include <ArduinoJson/StaticJsonBuffer.h> #include <ArduinoJson/StaticJsonBuffer.h>
class JsonObject_PrettyPrintTo_Tests : public testing::Test class JsonObject_PrettyPrintTo_Tests : public testing::Test
{ {
protected: protected:
JsonObject object; JsonObject object;
StaticJsonBuffer<30> json; StaticJsonBuffer<30> json;
virtual void SetUp() virtual void SetUp()
{ {
object = json.createObject(); object = json.createObject();
} }
void outputMustBe(const char* expected) void outputMustBe(const char* expected)
{ {
size_t n = object.prettyPrintTo(buffer, sizeof(buffer)); size_t n = object.prettyPrintTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer); EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n); EXPECT_EQ(strlen(expected), n);
} }
private: private:
char buffer[256]; char buffer[256];
}; };
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject)
{ {
outputMustBe("{}"); outputMustBe("{}");
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember)
{ {
object["key"] = "value"; object["key"] = "value";
outputMustBe( outputMustBe(
"{\r\n" "{\r\n"
" \"key\": \"value\"\r\n" " \"key\": \"value\"\r\n"
"}"); "}");
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers)
{ {
object["key1"] = "value1"; object["key1"] = "value1";
object["key2"] = "value2"; object["key2"] = "value2";
outputMustBe( outputMustBe(
"{\r\n" "{\r\n"
" \"key1\": \"value1\",\r\n" " \"key1\": \"value1\",\r\n"
" \"key2\": \"value2\"\r\n" " \"key2\": \"value2\"\r\n"
"}"); "}");
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers)
{ {
object.createNestedObject("key1"); object.createNestedObject("key1");
object.createNestedArray("key2"); object.createNestedArray("key2");
outputMustBe( outputMustBe(
"{\r\n" "{\r\n"
" \"key1\": {},\r\n" " \"key1\": {},\r\n"
" \"key2\": []\r\n" " \"key2\": []\r\n"
"}"); "}");
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers)
{ {
JsonObject nested1 = object.createNestedObject("key1"); JsonObject nested1 = object.createNestedObject("key1");
nested1["a"] = 1; nested1["a"] = 1;
JsonArray nested2 = object.createNestedArray("key2"); JsonArray nested2 = object.createNestedArray("key2");
nested2.add(2); nested2.add(2);
outputMustBe( outputMustBe(
"{\r\n" "{\r\n"
" \"key1\": {\r\n" " \"key1\": {\r\n"
" \"a\": 1\r\n" " \"a\": 1\r\n"
" },\r\n" " },\r\n"
" \"key2\": [\r\n" " \"key2\": [\r\n"
" 2\r\n" " 2\r\n"
" ]\r\n" " ]\r\n"
"}"); "}");
} }

View File

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

View File

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

View File

@ -1,69 +1,69 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson/StaticJsonBuffer.h> #include <ArduinoJson/StaticJsonBuffer.h>
#include <ArduinoJson/JsonValue.h> #include <ArduinoJson/JsonValue.h>
TEST(StaticJsonBuffer, CapacityMatchTemplateParameter) TEST(StaticJsonBuffer, CapacityMatchTemplateParameter)
{ {
StaticJsonBuffer<42> json; StaticJsonBuffer<42> json;
EXPECT_EQ(42, json.capacity()); EXPECT_EQ(42, json.capacity());
} }
TEST(StaticJsonBuffer, InitialSizeIsZero) TEST(StaticJsonBuffer, InitialSizeIsZero)
{ {
StaticJsonBuffer<42> json; StaticJsonBuffer<42> json;
EXPECT_EQ(0, json.size()); EXPECT_EQ(0, json.size());
} }
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenSizeIsIncreasedByOne) TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenSizeIsIncreasedByOne)
{ {
StaticJsonBuffer<42> json; StaticJsonBuffer<42> json;
json.createObject(); json.createObject();
EXPECT_EQ(1, json.size()); EXPECT_EQ(1, json.size());
json.createObject(); json.createObject();
EXPECT_EQ(2, json.size()); EXPECT_EQ(2, json.size());
} }
TEST(StaticJsonBuffer, GivenBufferIsFull_WhenCreateObjectIsCalled_ThenSizeDoesNotChange) TEST(StaticJsonBuffer, GivenBufferIsFull_WhenCreateObjectIsCalled_ThenSizeDoesNotChange)
{ {
StaticJsonBuffer<1> json; StaticJsonBuffer<1> json;
json.createObject(); json.createObject();
EXPECT_EQ(1, json.size()); EXPECT_EQ(1, json.size());
json.createObject(); json.createObject();
EXPECT_EQ(1, json.size()); EXPECT_EQ(1, json.size());
} }
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned) TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned)
{ {
StaticJsonBuffer<42> json; StaticJsonBuffer<42> json;
JsonObject obj = json.createObject(); JsonObject obj = json.createObject();
EXPECT_EQ(0, obj.size()); EXPECT_EQ(0, obj.size());
} }
TEST(StaticJsonBuffer, GivenAJsonObject_WhenValuesAreAdded_ThenSizeIsIncreasedByTwo) TEST(StaticJsonBuffer, GivenAJsonObject_WhenValuesAreAdded_ThenSizeIsIncreasedByTwo)
{ {
StaticJsonBuffer<42> json; StaticJsonBuffer<42> json;
JsonObject obj = json.createObject(); JsonObject obj = json.createObject();
obj["hello"]; obj["hello"];
EXPECT_EQ(3, json.size()); EXPECT_EQ(3, json.size());
obj["world"]; obj["world"];
EXPECT_EQ(5, json.size()); EXPECT_EQ(5, json.size());
} }
TEST(StaticJsonBuffer, GivenAJsonObject_WhenSameValuesAreAddedTwice_ThenSizeIsOnlyIncreasedByTwo) TEST(StaticJsonBuffer, GivenAJsonObject_WhenSameValuesAreAddedTwice_ThenSizeIsOnlyIncreasedByTwo)
{ {
StaticJsonBuffer<42> json; StaticJsonBuffer<42> json;
JsonObject obj = json.createObject(); JsonObject obj = json.createObject();
obj["hello"]; obj["hello"];
EXPECT_EQ(3, json.size()); EXPECT_EQ(3, json.size());
obj["hello"]; obj["hello"];
EXPECT_EQ(3, json.size()); EXPECT_EQ(3, json.size());
} }

View File

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