2014-07-08 21:29:19 +02:00
|
|
|
/*
|
|
|
|
* Arduino JSON library
|
|
|
|
* Benoit Blanchon 2014 - MIT License
|
|
|
|
*/
|
|
|
|
|
2014-06-26 12:50:48 +02:00
|
|
|
#include "CppUnitTest.h"
|
|
|
|
#include "StringBuilder.h"
|
2014-06-30 19:19:39 +02:00
|
|
|
#include "JsonValue.h"
|
2014-06-26 12:50:48 +02:00
|
|
|
|
|
|
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
2014-07-07 14:05:41 +02:00
|
|
|
using namespace ArduinoJson::Internals;
|
2014-06-26 12:50:48 +02:00
|
|
|
|
|
|
|
namespace JsonGeneratorTests
|
|
|
|
{
|
2014-06-30 19:19:39 +02:00
|
|
|
TEST_CLASS(JsonValueTests)
|
2014-06-26 12:50:48 +02:00
|
|
|
{
|
2014-07-02 13:16:44 +02:00
|
|
|
char buffer[1024];
|
2014-07-01 13:20:47 +02:00
|
|
|
size_t returnValue;
|
2014-06-26 12:50:48 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2014-07-08 21:29:19 +02:00
|
|
|
TEST_METHOD(String)
|
2014-07-02 13:24:33 +02:00
|
|
|
{
|
2014-07-08 21:29:19 +02:00
|
|
|
whenInputIs("hello");
|
|
|
|
outputMustBe("\"hello\"");
|
2014-07-02 13:24:33 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 21:29:19 +02:00
|
|
|
TEST_METHOD(Float)
|
2014-07-02 13:24:33 +02:00
|
|
|
{
|
2014-07-08 21:29:19 +02:00
|
|
|
whenInputIs(3.1415f);
|
|
|
|
outputMustBe("3.14");
|
2014-06-26 12:55:40 +02:00
|
|
|
}
|
2014-07-02 13:46:25 +02:00
|
|
|
|
2014-07-03 13:35:39 +02:00
|
|
|
TEST_METHOD(DoubleZeroDigits)
|
2014-06-30 19:19:39 +02:00
|
|
|
{
|
2014-07-08 21:29:19 +02:00
|
|
|
whenInputIs<0>(3.14159265358979323846);
|
|
|
|
outputMustBe("3");
|
2014-07-03 13:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_METHOD(DoubleOneDigit)
|
|
|
|
{
|
2014-07-08 21:29:19 +02:00
|
|
|
whenInputIs<1>(3.14159265358979323846);
|
|
|
|
outputMustBe("3.1");
|
2014-07-03 13:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_METHOD(DoubleTwoDigits)
|
|
|
|
{
|
2014-07-08 21:29:19 +02:00
|
|
|
whenInputIs<2>(3.14159265358979323846);
|
|
|
|
outputMustBe("3.14");
|
2014-06-30 19:19:39 +02:00
|
|
|
}
|
2014-07-07 13:59:31 +02:00
|
|
|
|
2014-07-02 13:06:38 +02:00
|
|
|
TEST_METHOD(Integer)
|
|
|
|
{
|
2014-07-08 21:29:19 +02:00
|
|
|
whenInputIs(314);
|
|
|
|
outputMustBe("314");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_METHOD(Char)
|
|
|
|
{
|
|
|
|
whenInputIs('A');
|
|
|
|
outputMustBe("65");
|
2014-07-02 13:06:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_METHOD(Short)
|
|
|
|
{
|
2014-07-08 21:29:19 +02:00
|
|
|
whenInputIs((short)314);
|
|
|
|
outputMustBe("314");
|
2014-07-02 13:06:38 +02:00
|
|
|
}
|
|
|
|
|
2014-07-02 13:02:28 +02:00
|
|
|
TEST_METHOD(Long)
|
|
|
|
{
|
2014-07-08 21:29:19 +02:00
|
|
|
whenInputIs(314159265L);
|
|
|
|
outputMustBe("314159265");
|
2014-07-02 13:02:28 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 21:29:19 +02:00
|
|
|
private:
|
|
|
|
|
2014-07-07 13:59:31 +02:00
|
|
|
template<int DIGITS>
|
2014-07-08 21:29:19 +02:00
|
|
|
void whenInputIs(double value)
|
2014-06-26 12:52:56 +02:00
|
|
|
{
|
2014-07-02 13:16:44 +02:00
|
|
|
StringBuilder sb(buffer, sizeof(buffer));
|
2014-07-07 13:24:14 +02:00
|
|
|
JsonValue jsonValue;
|
2014-07-07 13:59:31 +02:00
|
|
|
jsonValue.set<DIGITS>(value);
|
2014-07-07 13:24:14 +02:00
|
|
|
returnValue = jsonValue.printTo(sb);
|
2014-06-26 12:52:56 +02:00
|
|
|
}
|
2014-06-26 12:50:48 +02:00
|
|
|
|
2014-07-07 13:59:31 +02:00
|
|
|
template<typename T>
|
2014-07-08 21:29:19 +02:00
|
|
|
void whenInputIs(T value)
|
2014-06-26 12:50:48 +02:00
|
|
|
{
|
2014-07-03 13:35:39 +02:00
|
|
|
StringBuilder sb(buffer, sizeof(buffer));
|
2014-07-07 13:24:14 +02:00
|
|
|
JsonValue jsonValue;
|
2014-07-07 13:59:31 +02:00
|
|
|
jsonValue.set(value);
|
2014-07-07 13:24:14 +02:00
|
|
|
returnValue = jsonValue.printTo(sb);
|
2014-06-26 12:50:48 +02:00
|
|
|
}
|
2014-07-01 13:20:47 +02:00
|
|
|
|
2014-07-08 21:29:19 +02:00
|
|
|
void outputMustBe(const char* expected)
|
2014-07-01 13:20:47 +02:00
|
|
|
{
|
2014-07-03 13:35:39 +02:00
|
|
|
Assert::AreEqual(expected, buffer);
|
|
|
|
Assert::AreEqual(strlen(expected), returnValue);
|
2014-07-01 13:20:47 +02:00
|
|
|
}
|
2014-06-26 12:50:48 +02:00
|
|
|
};
|
|
|
|
}
|