Files
ArduinoJson/JsonGeneratorTests/JsonArrayTests.cpp

156 lines
3.0 KiB
C++
Raw Normal View History

2014-06-24 13:19:23 +02:00
#include "CppUnitTest.h"
#include "JsonArray.h"
#include "JsonHashTable.h"
2014-06-24 13:19:23 +02:00
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace JsonGeneratorTests
{
TEST_CLASS(JsonArrayTests)
{
public:
2014-06-27 13:00:27 +02:00
TEST_METHOD(Empty)
2014-06-24 13:19:23 +02:00
{
2014-06-27 13:09:52 +02:00
jsonIs("[]");
}
TEST_METHOD(Null)
{
2014-06-27 13:24:10 +02:00
addValue((char*)0);
2014-06-27 13:09:52 +02:00
jsonIs("[null]");
}
TEST_METHOD(OneString)
{
2014-06-27 13:24:10 +02:00
addValue("hello");
2014-06-24 13:19:23 +02:00
2014-06-27 13:09:52 +02:00
jsonIs("[\"hello\"]");
}
TEST_METHOD(TwoStrings)
{
2014-06-27 13:24:10 +02:00
addValue("hello");
addValue("world");
2014-06-27 13:09:52 +02:00
jsonIs("[\"hello\",\"world\"]");
}
TEST_METHOD(OneStringOverCapacity)
{
2014-06-27 13:24:10 +02:00
addValue("hello");
addValue("world");
addValue("lost");
2014-06-27 13:09:52 +02:00
jsonIs("[\"hello\",\"world\"]");
}
TEST_METHOD(OneNumber)
{
2014-06-27 13:24:10 +02:00
addValue(3.14);
2014-06-27 13:09:52 +02:00
jsonIs("[3.14]");
}
TEST_METHOD(AddTwoNumbers)
{
2014-06-27 13:24:10 +02:00
addValue(3.14);
addValue(2.72);
2014-06-27 13:09:52 +02:00
jsonIs("[3.14,2.72]");
}
TEST_METHOD(AddOneNumberOverCapacity)
{
2014-06-27 13:24:10 +02:00
addValue(3.14);
addValue(2.72);
addValue(1.41);
2014-06-27 13:09:52 +02:00
jsonIs("[3.14,2.72]");
}
TEST_METHOD(AddTrue)
{
2014-06-27 13:24:10 +02:00
addValue(true);
2014-06-27 13:09:52 +02:00
jsonIs("[true]");
}
TEST_METHOD(AddFalse)
{
2014-06-27 13:24:10 +02:00
addValue(false);
2014-06-27 13:09:52 +02:00
jsonIs("[false]");
}
TEST_METHOD(AddTwoBooleans)
{
2014-06-27 13:24:10 +02:00
addValue(false);
addValue(true);
2014-06-27 13:09:52 +02:00
jsonIs("[false,true]");
}
TEST_METHOD(AddOneBooleanOverCapacity)
{
2014-06-27 13:24:10 +02:00
addValue(false);
addValue(true);
addValue(false);
2014-06-27 13:09:52 +02:00
jsonIs("[false,true]");
}
2014-06-25 13:14:10 +02:00
TEST_METHOD(AddOneEmptyNestedArray)
{
JsonArray<1> nestedArray;
2014-06-25 13:14:10 +02:00
2014-06-27 13:24:10 +02:00
addNested(nestedArray);
2014-06-25 13:14:10 +02:00
2014-06-27 13:09:52 +02:00
jsonIs("[[]]");
2014-06-25 13:14:10 +02:00
}
TEST_METHOD(AddOneEmptyNestedHash)
{
JsonHashTable<1> nestedHash;
addNested(nestedHash);
jsonIs("[{}]");
}
TEST_METHOD(AddOneNestedArrayWithOneItem)
{
JsonArray<1> nestedArray;
nestedArray.add(3.14);
2014-06-27 13:24:10 +02:00
addNested(nestedArray);
2014-06-27 13:09:52 +02:00
jsonIs("[[3.14]]");
}
2014-06-27 13:00:27 +02:00
private:
2014-06-27 13:09:52 +02:00
JsonArray<2> arr;
2014-06-27 13:24:10 +02:00
void addNested(JsonObjectBase& value)
{
arr.add(value);
}
2014-06-27 13:09:52 +02:00
template<typename T>
2014-06-27 13:24:10 +02:00
void addValue(T value)
2014-06-27 13:09:52 +02:00
{
arr.add(value);
}
void jsonIs(const char* expected)
{
2014-06-24 13:19:23 +02:00
char buffer[256];
2014-06-24 13:19:23 +02:00
arr.writeTo(buffer, sizeof(buffer));
Assert::AreEqual(expected, buffer);
2014-06-24 13:19:23 +02:00
}
};
}