Files
ArduinoJson/JsonGeneratorTests/JsonArrayTests.cpp

162 lines
3.3 KiB
C++
Raw Normal View History

2014-07-08 21:29:19 +02:00
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
2014-06-24 13:19:23 +02:00
#include "CppUnitTest.h"
#include "JsonArray.h"
2014-07-22 21:02:16 +02:00
#include "JsonObject.h"
2014-06-24 13:19:23 +02:00
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
2014-07-03 13:54:27 +02:00
using namespace ArduinoJson::Generator;
2014-06-24 13:19:23 +02:00
namespace JsonGeneratorTests
{
TEST_CLASS(JsonArrayTests)
{
2014-08-04 12:18:17 +02:00
JsonArray<2> array;
2014-07-01 13:29:54 +02:00
char buffer[256];
2014-06-24 13:19:23 +02:00
public:
2014-06-27 13:00:27 +02:00
TEST_METHOD(Empty)
2014-06-24 13:19:23 +02:00
{
2014-07-08 21:29:19 +02:00
outputMustBe("[]");
}
TEST_METHOD(Null)
{
2014-08-04 12:18:17 +02:00
array.add((char*) 0);
2014-07-08 21:29:19 +02:00
outputMustBe("[null]");
}
TEST_METHOD(OneString)
{
2014-08-04 12:18:17 +02:00
array.add("hello");
2014-06-24 13:19:23 +02:00
2014-07-08 21:29:19 +02:00
outputMustBe("[\"hello\"]");
}
TEST_METHOD(TwoStrings)
{
2014-08-04 12:18:17 +02:00
array.add("hello");
array.add("world");
2014-07-08 21:29:19 +02:00
outputMustBe("[\"hello\",\"world\"]");
}
TEST_METHOD(OneStringOverCapacity)
{
2014-08-04 12:18:17 +02:00
array.add("hello");
array.add("world");
array.add("lost");
2014-07-08 21:29:19 +02:00
outputMustBe("[\"hello\",\"world\"]");
}
TEST_METHOD(OneDoubleDefaultDigits)
{
2014-08-04 12:18:17 +02:00
array.add(3.14159265358979323846);
2014-07-08 21:29:19 +02:00
outputMustBe("[3.14]");
}
TEST_METHOD(OneDoubleFourDigits)
{
2014-08-04 12:18:17 +02:00
array.add<4>(3.14159265358979323846);
2014-07-08 21:29:19 +02:00
outputMustBe("[3.1416]");
}
TEST_METHOD(OneInteger)
{
2014-08-04 12:18:17 +02:00
array.add(1);
2014-07-08 21:29:19 +02:00
outputMustBe("[1]");
}
TEST_METHOD(TwoIntegers)
{
2014-08-04 12:18:17 +02:00
array.add(1);
array.add(2);
2014-07-08 21:29:19 +02:00
outputMustBe("[1,2]");
}
TEST_METHOD(OneIntegerOverCapacity)
{
2014-08-04 12:18:17 +02:00
array.add(1);
array.add(2);
array.add(3);
2014-07-08 21:29:19 +02:00
outputMustBe("[1,2]");
}
TEST_METHOD(OneTrue)
{
2014-08-04 12:18:17 +02:00
array.add(true);
2014-07-08 21:29:19 +02:00
outputMustBe("[true]");
}
TEST_METHOD(OneFalse)
{
2014-08-04 12:18:17 +02:00
array.add(false);
2014-07-08 21:29:19 +02:00
outputMustBe("[false]");
}
TEST_METHOD(TwoBooleans)
{
2014-08-04 12:18:17 +02:00
array.add(false);
array.add(true);
2014-07-08 21:29:19 +02:00
outputMustBe("[false,true]");
}
TEST_METHOD(OneBooleanOverCapacity)
{
2014-08-04 12:18:17 +02:00
array.add(false);
array.add(true);
array.add(false);
2014-07-08 21:29:19 +02:00
outputMustBe("[false,true]");
}
TEST_METHOD(OneEmptyNestedArray)
2014-06-25 13:14:10 +02:00
{
JsonArray<1> nestedArray;
2014-08-04 12:18:17 +02:00
array.add(nestedArray);
2014-07-08 21:29:19 +02:00
outputMustBe("[[]]");
2014-06-25 13:14:10 +02:00
}
TEST_METHOD(OneEmptyNestedHash)
{
JsonObject<1> nestedObject;
2014-08-04 12:18:17 +02:00
array.add(nestedObject);
2014-07-08 21:29:19 +02:00
outputMustBe("[{}]");
}
TEST_METHOD(OneNestedArrayWithOneInteger)
{
JsonArray<1> nestedArray;
nestedArray.add(1);
2014-08-04 12:18:17 +02:00
array.add(nestedArray);
2014-07-08 21:29:19 +02:00
outputMustBe("[[1]]");
}
2014-06-27 13:00:27 +02:00
private:
2014-07-08 21:29:19 +02:00
void outputMustBe(const char* expected)
{
2014-08-04 12:18:17 +02:00
size_t n = array.printTo(buffer, sizeof(buffer));
Assert::AreEqual(expected, buffer);
2014-07-08 21:29:19 +02:00
Assert::AreEqual(strlen(expected), n);
2014-07-01 13:25:21 +02:00
}
2014-06-24 13:19:23 +02:00
};
}