Cleaned up

This commit is contained in:
Benoit Blanchon
2014-07-08 21:29:19 +02:00
parent 0a0757945e
commit c38af3e32c
9 changed files with 253 additions and 198 deletions

View File

@ -1,3 +1,8 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "JsonHashTable.h" #include "JsonHashTable.h"
using namespace ArduinoJson::Generator; using namespace ArduinoJson::Generator;
@ -8,6 +13,8 @@ size_t JsonHashTableBase::printTo(Print& p) const
n += p.write('{'); n += p.write('{');
// NB: the code has been optimized for a small size on a 8-bit AVR
KeyValuePair* current = items; KeyValuePair* current = items;
for (int i = count; i > 0; i--) for (int i = count; i > 0; i--)
{ {

View File

@ -0,0 +1,97 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "CppUnitTest.h"
#include "EscapedString.h"
#include "StringBuilder.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace ArduinoJson::Internals;
namespace JsonGeneratorTests
{
TEST_CLASS(EscapedStringTests)
{
char buffer[1024];
size_t returnValue;
EscapedString escapedString;
public:
TEST_METHOD(Null)
{
whenInputIs(0);
outputMustBe("null");
}
TEST_METHOD(EmptyString)
{
whenInputIs("");
outputMustBe("\"\"");
}
TEST_METHOD(QuotationMark)
{
whenInputIs("\"");
outputMustBe("\"\\\"\"");
}
TEST_METHOD(ReverseSolidus)
{
whenInputIs("\\");
outputMustBe("\"\\\\\"");
}
TEST_METHOD(Solidus)
{
whenInputIs("/");
outputMustBe("\"/\""); // but the JSON format allows \/
}
TEST_METHOD(Backspace)
{
whenInputIs("\b");
outputMustBe("\"\\b\"");
}
TEST_METHOD(Formfeed)
{
whenInputIs("\f");
outputMustBe("\"\\f\"");
}
TEST_METHOD(Newline)
{
whenInputIs("\n");
outputMustBe("\"\\n\"");
}
TEST_METHOD(CarriageReturn)
{
whenInputIs("\r");
outputMustBe("\"\\r\"");
}
TEST_METHOD(HorizontalTab)
{
whenInputIs("\t");
outputMustBe("\"\\t\"");
}
private:
void whenInputIs(const char* input)
{
StringBuilder sb(buffer, sizeof(buffer));
escapedString.set(input);
returnValue = escapedString.printTo(sb);
}
void outputMustBe(const char* expected)
{
Assert::AreEqual(expected, buffer);
Assert::AreEqual(strlen(expected), returnValue);
}
};
}

View File

@ -1,3 +1,8 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "CppUnitTest.h" #include "CppUnitTest.h"
#include "JsonArray.h" #include "JsonArray.h"
#include "JsonHashTable.h" #include "JsonHashTable.h"
@ -16,137 +21,117 @@ namespace JsonGeneratorTests
TEST_METHOD(Empty) TEST_METHOD(Empty)
{ {
returnValueIs(2); outputMustBe("[]");
jsonIs("[]");
} }
TEST_METHOD(Null) TEST_METHOD(Null)
{ {
addValue((char*)0); add((char*)0);
returnValueIs(6); outputMustBe("[null]");
jsonIs("[null]");
} }
TEST_METHOD(OneString) TEST_METHOD(OneString)
{ {
addValue("hello"); add("hello");
returnValueIs(9); outputMustBe("[\"hello\"]");
jsonIs("[\"hello\"]");
} }
TEST_METHOD(TwoStrings) TEST_METHOD(TwoStrings)
{ {
addValue("hello"); add("hello");
addValue("world"); add("world");
returnValueIs(17); outputMustBe("[\"hello\",\"world\"]");
jsonIs("[\"hello\",\"world\"]");
} }
TEST_METHOD(OneStringOverCapacity) TEST_METHOD(OneStringOverCapacity)
{ {
addValue("hello"); add("hello");
addValue("world"); add("world");
addValue("lost"); add("lost");
returnValueIs(17); outputMustBe("[\"hello\",\"world\"]");
jsonIs("[\"hello\",\"world\"]");
} }
TEST_METHOD(OneDoubleDefaultDigits) TEST_METHOD(OneDoubleDefaultDigits)
{ {
addValue(3.14159265358979323846); add(3.14159265358979323846);
jsonIs("[3.14]"); outputMustBe("[3.14]");
} }
TEST_METHOD(OneDoubleFourDigits) TEST_METHOD(OneDoubleFourDigits)
{ {
addValue<4>(3.14159265358979323846); add<4>(3.14159265358979323846);
jsonIs("[3.1416]"); outputMustBe("[3.1416]");
} }
TEST_METHOD(OneInteger) TEST_METHOD(OneInteger)
{ {
addValue(1); add(1);
returnValueIs(3); outputMustBe("[1]");
jsonIs("[1]");
} }
TEST_METHOD(TwoIntegers) TEST_METHOD(TwoIntegers)
{ {
addValue(1); add(1);
addValue(2); add(2);
returnValueIs(5); outputMustBe("[1,2]");
jsonIs("[1,2]");
} }
TEST_METHOD(OneIntegerOverCapacity) TEST_METHOD(OneIntegerOverCapacity)
{ {
addValue(1); add(1);
addValue(2); add(2);
addValue(3); add(3);
returnValueIs(5); outputMustBe("[1,2]");
jsonIs("[1,2]");
} }
TEST_METHOD(OneTrue) TEST_METHOD(OneTrue)
{ {
addValue(true); add(true);
returnValueIs(6); outputMustBe("[true]");
jsonIs("[true]");
} }
TEST_METHOD(OneFalse) TEST_METHOD(OneFalse)
{ {
addValue(false); add(false);
returnValueIs(7); outputMustBe("[false]");
jsonIs("[false]");
} }
TEST_METHOD(TwoBooleans) TEST_METHOD(TwoBooleans)
{ {
addValue(false); add(false);
addValue(true); add(true);
returnValueIs(12); outputMustBe("[false,true]");
jsonIs("[false,true]");
} }
TEST_METHOD(OneBooleanOverCapacity) TEST_METHOD(OneBooleanOverCapacity)
{ {
addValue(false); add(false);
addValue(true); add(true);
addValue(false); add(false);
returnValueIs(12); outputMustBe("[false,true]");
jsonIs("[false,true]");
} }
TEST_METHOD(OneEmptyNestedArray) TEST_METHOD(OneEmptyNestedArray)
{ {
JsonArray<1> nestedArray; addNested(JsonArray<1>());
outputMustBe("[[]]");
addNested(nestedArray);
returnValueIs(4);
jsonIs("[[]]");
} }
TEST_METHOD(OneEmptyNestedHash) TEST_METHOD(OneEmptyNestedHash)
{ {
JsonHashTable<1> nestedHash; addNested(JsonHashTable<1>());
outputMustBe("[{}]");
addNested(nestedHash);
returnValueIs(4);
jsonIs("[{}]");
} }
TEST_METHOD(OneNestedArrayWithOneInteger) TEST_METHOD(OneNestedArrayWithOneInteger)
@ -156,8 +141,7 @@ namespace JsonGeneratorTests
addNested(nestedArray); addNested(nestedArray);
returnValueIs(5); outputMustBe("[[1]]");
jsonIs("[[1]]");
} }
private: private:
@ -168,27 +152,22 @@ namespace JsonGeneratorTests
} }
template<typename T> template<typename T>
void addValue(T value) void add(T value)
{ {
arr.add(value); arr.add(value);
} }
template<int DIGITS> template<int DIGITS>
void addValue(double value) void add(double value)
{ {
arr.add<DIGITS>(value); arr.add<DIGITS>(value);
} }
void jsonIs(const char* expected) void outputMustBe(const char* expected)
{ {
arr.printTo(buffer, sizeof(buffer)); size_t n = arr.printTo(buffer, sizeof(buffer));
Assert::AreEqual(expected, buffer); Assert::AreEqual(expected, buffer);
} Assert::AreEqual(strlen(expected), n);
void returnValueIs(size_t expected)
{
size_t actual = arr.printTo(buffer, sizeof(buffer));
Assert::AreEqual(expected, actual);
} }
}; };
} }

View File

@ -89,6 +89,7 @@
<ClCompile Include="..\JsonGenerator\JsonHashTableBase.cpp" /> <ClCompile Include="..\JsonGenerator\JsonHashTableBase.cpp" />
<ClCompile Include="..\JsonGenerator\JsonValue.cpp" /> <ClCompile Include="..\JsonGenerator\JsonValue.cpp" />
<ClCompile Include="..\JsonGenerator\StringBuilder.cpp" /> <ClCompile Include="..\JsonGenerator\StringBuilder.cpp" />
<ClCompile Include="EscapedStringTests.cpp" />
<ClCompile Include="JsonArrayTests.cpp" /> <ClCompile Include="JsonArrayTests.cpp" />
<ClCompile Include="JsonHashTableTests.cpp" /> <ClCompile Include="JsonHashTableTests.cpp" />
<ClCompile Include="JsonValueTests.cpp" /> <ClCompile Include="JsonValueTests.cpp" />

View File

@ -45,6 +45,9 @@
<ClCompile Include="..\JsonGenerator\JsonArrayBase.cpp"> <ClCompile Include="..\JsonGenerator\JsonArrayBase.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="EscapedStringTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\JsonGenerator\JsonArray.h"> <ClInclude Include="..\JsonGenerator\JsonArray.h">

View File

@ -1,3 +1,8 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "CppUnitTest.h" #include "CppUnitTest.h"
#include "JsonArray.h" #include "JsonArray.h"
#include "JsonHashTable.h" #include "JsonHashTable.h"
@ -16,83 +21,78 @@ namespace JsonGeneratorTests
TEST_METHOD(Empty) TEST_METHOD(Empty)
{ {
jsonIs("{}"); outputMustBe("{}");
} }
TEST_METHOD(OneString) TEST_METHOD(OneString)
{ {
addValue("key", "value"); add("key", "value");
outputMustBe("{\"key\":\"value\"}");
jsonIs("{\"key\":\"value\"}");
} }
TEST_METHOD(TwoStrings) TEST_METHOD(TwoStrings)
{ {
addValue("key1", "value1"); add("key1", "value1");
addValue("key2", "value2"); add("key2", "value2");
jsonIs("{\"key1\":\"value1\",\"key2\":\"value2\"}"); outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
} }
TEST_METHOD(OneStringOverCapacity) TEST_METHOD(OneStringOverCapacity)
{ {
addValue("key1", "value1"); add("key1", "value1");
addValue("key2", "value2"); add("key2", "value2");
addValue("key3", "value3"); add("key3", "value3");
jsonIs("{\"key1\":\"value1\",\"key2\":\"value2\"}"); outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
} }
TEST_METHOD(OneInteger) TEST_METHOD(OneInteger)
{ {
addValue("key", 1); add("key", 1);
jsonIs("{\"key\":1}"); outputMustBe("{\"key\":1}");
} }
TEST_METHOD(OneDoubleFourDigits) TEST_METHOD(OneDoubleFourDigits)
{ {
addValue<4>("key", 3.14159265358979323846); add<4>("key", 3.14159265358979323846);
jsonIs("{\"key\":3.1416}"); outputMustBe("{\"key\":3.1416}");
} }
TEST_METHOD(OneDoubleDefaultDigits) TEST_METHOD(OneDoubleDefaultDigits)
{ {
addValue("key", 3.14159265358979323846); add("key", 3.14159265358979323846);
jsonIs("{\"key\":3.14}"); outputMustBe("{\"key\":3.14}");
} }
TEST_METHOD(OneNull) TEST_METHOD(OneNull)
{ {
addValue("key", (char*) 0); add("key", (char*) 0);
jsonIs("{\"key\":null}"); outputMustBe("{\"key\":null}");
} }
TEST_METHOD(OneTrue) TEST_METHOD(OneTrue)
{ {
addValue("key", true); add("key", true);
jsonIs("{\"key\":true}"); outputMustBe("{\"key\":true}");
} }
TEST_METHOD(OneFalse) TEST_METHOD(OneFalse)
{ {
addValue("key", false); add("key", false);
jsonIs("{\"key\":false}"); outputMustBe("{\"key\":false}");
} }
TEST_METHOD(OneEmptyNestedArray) TEST_METHOD(OneEmptyNestedArray)
{ {
JsonArray<1> nestedArray; addNested("key", JsonArray<1>());
addNested("key", nestedArray); outputMustBe("{\"key\":[]}");
jsonIs("{\"key\":[]}");
} }
TEST_METHOD(OneEmptyNestedHash) TEST_METHOD(OneEmptyNestedHash)
{ {
JsonHashTable<1> nestedHash; addNested("key", JsonHashTable<1>());
addNested("key", nestedHash); outputMustBe("{\"key\":{}}");
jsonIs("{\"key\":{}}");
} }
private: private:
@ -103,18 +103,18 @@ namespace JsonGeneratorTests
} }
template<typename T> template<typename T>
void addValue(const char* key, T value) void add(const char* key, T value)
{ {
hash.add(key, value); hash.add(key, value);
} }
template<int DIGITS> template<int DIGITS>
void addValue(const char* key, double value) void add(const char* key, double value)
{ {
hash.add<DIGITS>(key, value); hash.add<DIGITS>(key, value);
} }
void jsonIs(const char* expected) void outputMustBe(const char* expected)
{ {
size_t actual = hash.printTo(buffer, sizeof(buffer)); size_t actual = hash.printTo(buffer, sizeof(buffer));
Assert::AreEqual(expected, buffer); Assert::AreEqual(expected, buffer);

View File

@ -1,3 +1,8 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "CppUnitTest.h" #include "CppUnitTest.h"
#include "StringBuilder.h" #include "StringBuilder.h"
#include "JsonValue.h" #include "JsonValue.h"
@ -14,104 +19,64 @@ namespace JsonGeneratorTests
public: public:
TEST_METHOD(Null) TEST_METHOD(String)
{ {
write((char*)0); whenInputIs("hello");
assertResultIs("null"); outputMustBe("\"hello\"");
} }
TEST_METHOD(EmptyString) TEST_METHOD(Float)
{ {
write(""); whenInputIs(3.1415f);
assertResultIs("\"\""); outputMustBe("3.14");
}
TEST_METHOD(QuotationMark)
{
write("\"");
assertResultIs("\"\\\"\"");
}
TEST_METHOD(ReverseSolidus)
{
write("\\");
assertResultIs("\"\\\\\"");
}
TEST_METHOD(Solidus)
{
write("/");
assertResultIs("\"/\""); // but the JSON format allows \/
}
TEST_METHOD(Backspace)
{
write("\b");
assertResultIs("\"\\b\"");
}
TEST_METHOD(Formfeed)
{
write("\f");
assertResultIs("\"\\f\"");
}
TEST_METHOD(Newline)
{
write("\n");
assertResultIs("\"\\n\"");
}
TEST_METHOD(CarriageReturn)
{
write("\r");
assertResultIs("\"\\r\"");
}
TEST_METHOD(HorizontalTab)
{
write("\t");
assertResultIs("\"\\t\"");
} }
TEST_METHOD(DoubleZeroDigits) TEST_METHOD(DoubleZeroDigits)
{ {
write<0>(3.14159265358979323846); whenInputIs<0>(3.14159265358979323846);
assertResultIs("3"); outputMustBe("3");
} }
TEST_METHOD(DoubleOneDigit) TEST_METHOD(DoubleOneDigit)
{ {
write<1>(3.14159265358979323846); whenInputIs<1>(3.14159265358979323846);
assertResultIs("3.1"); outputMustBe("3.1");
} }
TEST_METHOD(DoubleTwoDigits) TEST_METHOD(DoubleTwoDigits)
{ {
write<2>(3.14159265358979323846); whenInputIs<2>(3.14159265358979323846);
assertResultIs("3.14"); outputMustBe("3.14");
} }
TEST_METHOD(Integer) TEST_METHOD(Integer)
{ {
write(314); whenInputIs(314);
assertResultIs("314"); outputMustBe("314");
}
TEST_METHOD(Char)
{
whenInputIs('A');
outputMustBe("65");
} }
TEST_METHOD(Short) TEST_METHOD(Short)
{ {
write((short)314); whenInputIs((short)314);
assertResultIs("314"); outputMustBe("314");
} }
TEST_METHOD(Long) TEST_METHOD(Long)
{ {
write(314L); whenInputIs(314159265L);
assertResultIs("314"); outputMustBe("314159265");
} }
private:
template<int DIGITS> template<int DIGITS>
void write(double value) void whenInputIs(double value)
{ {
StringBuilder sb(buffer, sizeof(buffer)); StringBuilder sb(buffer, sizeof(buffer));
JsonValue jsonValue; JsonValue jsonValue;
@ -120,7 +85,7 @@ namespace JsonGeneratorTests
} }
template<typename T> template<typename T>
void write(T value) void whenInputIs(T value)
{ {
StringBuilder sb(buffer, sizeof(buffer)); StringBuilder sb(buffer, sizeof(buffer));
JsonValue jsonValue; JsonValue jsonValue;
@ -128,7 +93,7 @@ namespace JsonGeneratorTests
returnValue = jsonValue.printTo(sb); returnValue = jsonValue.printTo(sb);
} }
void assertResultIs(const char* expected) void outputMustBe(const char* expected)
{ {
Assert::AreEqual(expected, buffer); Assert::AreEqual(expected, buffer);
Assert::AreEqual(strlen(expected), returnValue); Assert::AreEqual(strlen(expected), returnValue);

View File

@ -7,7 +7,6 @@
#include "Print.h" #include "Print.h"
#include <cstdio> #include <cstdio>
//#include <cstring>
size_t Print::print(const char s[]) size_t Print::print(const char s[])
{ {

View File

@ -1,3 +1,8 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "CppUnitTest.h" #include "CppUnitTest.h"
#include "StringBuilder.h" #include "StringBuilder.h"
@ -21,59 +26,58 @@ namespace JsonGeneratorTests
TEST_METHOD(InitialState) TEST_METHOD(InitialState)
{ {
assertResultIs(""); outputMustBe("");
} }
TEST_METHOD(OverCapacity) TEST_METHOD(OverCapacity)
{ {
write("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
assertReturns(19); resultMustBe(19);
assertResultIs("ABCDEFGHIJKLMNOPQRS");
write("ABC"); print("ABC");
assertReturns(0); resultMustBe(0);
assertResultIs("ABCDEFGHIJKLMNOPQRS");
outputMustBe("ABCDEFGHIJKLMNOPQRS");
} }
TEST_METHOD(EmptyString) TEST_METHOD(EmptyString)
{ {
write(""); print("");
resultMustBe(0);
assertReturns(0); outputMustBe("");
assertResultIs("");
} }
TEST_METHOD(OneString) TEST_METHOD(OneString)
{ {
write("ABCD"); print("ABCD");
assertReturns(4); resultMustBe(4);
assertResultIs("ABCD"); outputMustBe("ABCD");
} }
TEST_METHOD(TwoStrings) TEST_METHOD(TwoStrings)
{ {
write("ABCD"); print("ABCD");
assertReturns(4); resultMustBe(4);
write("EFGH"); print("EFGH");
assertReturns(4); resultMustBe(4);
assertResultIs("ABCDEFGH"); outputMustBe("ABCDEFGH");
} }
private: private:
void write(const char* value) void print(const char* value)
{ {
returnValue = sb->print(value); returnValue = sb->print(value);
} }
void assertResultIs(const char* expected) void outputMustBe(const char* expected)
{ {
Assert::AreEqual(expected, buffer); Assert::AreEqual(expected, buffer);
} }
void assertReturns(size_t expected) void resultMustBe(size_t expected)
{ {
Assert::AreEqual(expected, returnValue); Assert::AreEqual(expected, returnValue);
} }