Files
ArduinoJson/JsonParserTests/JsonArrayTests.cpp

195 lines
4.7 KiB
C++
Raw Permalink Normal View History

2014-07-03 13:58:08 +02:00
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "CppUnitTest.h"
#include "JsonParser.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
2014-07-03 14:00:51 +02:00
using namespace ArduinoJson::Parser;
namespace ArduinoJsonParserTests
{
2014-07-14 10:33:24 +02:00
TEST_CLASS(JsonArrayTests)
{
2014-07-14 11:48:46 +02:00
JsonArray array;
char json[256];
2014-07-14 14:42:30 +02:00
jsmntok_t tokens[32];
JsonParserBase parser = JsonParserBase(tokens, 32);
public:
2014-02-27 13:12:52 +01:00
TEST_METHOD(EmptyString)
{
2014-07-14 11:48:46 +02:00
whenInputIs("");
parseMustFail();
2014-02-27 13:12:52 +01:00
}
TEST_METHOD(TooFewClosingBrackets)
{
2014-07-14 11:48:46 +02:00
whenInputIs("[[]");
parseMustFail();
2014-02-27 13:12:52 +01:00
}
TEST_METHOD(TooManyClosingBrackets)
{
2014-07-14 11:48:46 +02:00
whenInputIs("[]]");
parseMustFail();
2014-02-27 13:12:52 +01:00
}
2014-07-14 14:42:30 +02:00
TEST_METHOD(EmptyArray)
{
whenInputIs("[]");
parseMustSucceed();
lengthMustBe(0);
}
TEST_METHOD(NotEnoughTokens)
{
setTokenCountTo(2);
whenInputIs("[1,2]");
parseMustFail();
itemMustNotExist(0);
}
2014-07-14 11:48:46 +02:00
TEST_METHOD(TwoIntegers)
{
2014-07-14 14:42:30 +02:00
setTokenCountTo(3);
2014-07-14 11:48:46 +02:00
whenInputIs("[1,2]");
2014-07-14 14:42:30 +02:00
2014-07-14 11:48:46 +02:00
parseMustSucceed();
lengthMustBe(2);
itemMustBe(0, 1L);
itemMustBe(1, 2L);
2014-07-14 14:42:30 +02:00
itemMustNotExist(2);
}
2014-07-14 11:48:46 +02:00
TEST_METHOD(TwoBooleans)
{
2014-07-14 14:42:30 +02:00
setTokenCountTo(3);
2014-07-14 11:48:46 +02:00
whenInputIs("[true,false]");
2014-07-14 14:42:30 +02:00
2014-07-14 11:48:46 +02:00
parseMustSucceed();
lengthMustBe(2);
itemMustBe(0, true);
itemMustBe(1, false);
2014-07-14 14:42:30 +02:00
itemMustNotExist(2);
2014-07-14 11:48:46 +02:00
}
2014-07-14 11:48:46 +02:00
TEST_METHOD(TwoStrings)
{
2014-07-14 14:42:30 +02:00
setTokenCountTo(3);
2014-07-14 11:48:46 +02:00
whenInputIs("[\"hello\",\"world\"]");
2014-07-14 14:42:30 +02:00
2014-07-14 11:48:46 +02:00
parseMustSucceed();
lengthMustBe(2);
itemMustBe(0, "hello");
itemMustBe(1, "world");
2014-07-14 14:42:30 +02:00
itemMustNotExist(2);
2014-07-14 11:48:46 +02:00
}
2014-07-14 11:48:46 +02:00
TEST_METHOD(TwoDimensionsArray)
{
2014-07-14 14:42:30 +02:00
setTokenCountTo(7);
2014-07-14 11:48:46 +02:00
whenInputIs("[[1,2],[3,4]]");
2014-07-14 14:42:30 +02:00
2014-07-14 11:48:46 +02:00
parseMustSucceed();
lengthMustBe(2);
itemMustBe(0, 0, 1L);
itemMustBe(0, 1, 2L);
itemMustBe(1, 0, 3L);
itemMustBe(1, 1, 4L);
2014-07-14 14:42:30 +02:00
itemMustNotExist(2);
}
2014-02-27 13:20:49 +01:00
2014-07-14 11:48:46 +02:00
TEST_METHOD(ThreeDimensionsArray)
{
2014-07-14 14:42:30 +02:00
setTokenCountTo(15);
2014-07-14 11:48:46 +02:00
whenInputIs("[[[1,2],[3,4]],[[5,6],[7,8]]]");
2014-07-14 14:42:30 +02:00
2014-07-14 11:48:46 +02:00
parseMustSucceed();
lengthMustBe(2);
itemMustBe(0, 0, 0, 1L);
itemMustBe(0, 0, 1, 2L);
itemMustBe(0, 1, 0, 3L);
itemMustBe(0, 1, 1, 4L);
itemMustBe(1, 0, 0, 5L);
itemMustBe(1, 0, 1, 6L);
itemMustBe(1, 1, 0, 7L);
itemMustBe(1, 1, 1, 8L);
2014-07-14 14:42:30 +02:00
itemMustNotExist(2);
2014-07-14 11:48:46 +02:00
}
private:
2014-02-27 13:20:49 +01:00
2014-07-14 14:42:30 +02:00
void setTokenCountTo(int n)
{
parser = JsonParserBase(tokens, n);
}
2014-07-14 11:48:46 +02:00
void whenInputIs(const char* input)
{
strcpy(json, input);
array = parser.parseArray(json);
}
2014-02-27 13:20:49 +01:00
2014-07-14 11:48:46 +02:00
void parseMustFail()
{
Assert::IsFalse(array.success());
lengthMustBe(0);
}
2014-02-27 13:20:49 +01:00
2014-07-14 11:48:46 +02:00
void parseMustSucceed()
{
Assert::IsTrue(array.success());
}
2014-02-27 13:20:49 +01:00
2014-07-14 11:48:46 +02:00
void lengthMustBe(int expected)
{
Assert::AreEqual(expected, array.getLength());
}
2014-07-14 10:33:24 +02:00
2014-07-14 11:48:46 +02:00
void itemMustBe(int index, long expected)
2014-07-14 10:33:24 +02:00
{
2014-07-14 11:48:46 +02:00
Assert::AreEqual(expected, array.getLong(index));
}
2014-07-14 10:33:24 +02:00
2014-07-14 11:48:46 +02:00
void itemMustBe(int index, bool expected)
{
Assert::AreEqual(expected, array.getBool(index));
}
2014-07-14 10:33:24 +02:00
2014-07-14 11:48:46 +02:00
void itemMustBe(int index, const char* expected)
{
Assert::AreEqual(expected, array.getString(index));
}
2014-07-14 10:33:24 +02:00
2014-07-14 11:48:46 +02:00
void itemMustBe(int index0, int index1, long expected)
{
Assert::AreEqual(expected, array.getArray(index0).getLong(index1));
}
2014-07-14 10:33:24 +02:00
2014-07-14 11:48:46 +02:00
void itemMustBe(int index0, int index1, int index2, long expected)
{
Assert::AreEqual(expected, array.getArray(index0).getArray(index1).getLong(index2));
2014-07-14 10:33:24 +02:00
}
2014-07-14 14:42:30 +02:00
void itemMustNotExist(int index)
{
Assert::IsFalse(array.getHashTable(index).success());
Assert::IsFalse(array.getArray(index).success());
Assert::IsFalse(array.getBool(index));
Assert::AreEqual(0.0, array.getDouble(index));
Assert::AreEqual(0L, array.getLong(index));
Assert::IsNull(array.getString(index));
}
};
}