Files
ArduinoJson/JsonGeneratorTests/JsonValueTests.cpp

102 lines
2.3 KiB
C++
Raw Permalink Normal View History

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;
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");
}
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");
}
TEST_METHOD(DoubleOneDigit)
{
2014-07-08 21:29:19 +02:00
whenInputIs<1>(3.14159265358979323846);
outputMustBe("3.1");
}
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
}
TEST_METHOD(Integer)
{
2014-07-08 21:29:19 +02:00
whenInputIs(314);
outputMustBe("314");
}
TEST_METHOD(Char)
{
whenInputIs('A');
outputMustBe("65");
}
TEST_METHOD(Short)
{
2014-07-08 21:29:19 +02:00
whenInputIs((short)314);
outputMustBe("314");
}
TEST_METHOD(Long)
{
2014-07-08 21:29:19 +02:00
whenInputIs(314159265L);
outputMustBe("314159265");
}
2014-07-08 21:29:19 +02:00
private:
template<int DIGITS>
2014-07-08 21:29:19 +02:00
void whenInputIs(double value)
{
2014-07-02 13:16:44 +02:00
StringBuilder sb(buffer, sizeof(buffer));
JsonValue jsonValue;
jsonValue.set<DIGITS>(value);
returnValue = jsonValue.printTo(sb);
}
2014-06-26 12:50:48 +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
{
StringBuilder sb(buffer, sizeof(buffer));
JsonValue jsonValue;
jsonValue.set(value);
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
{
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
};
}