mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-17 12:32:17 +02:00
Added methods to clarify the tests
This commit is contained in:
@ -5,12 +5,46 @@
|
|||||||
class JsonArray_Parser_Tests : public testing::Test
|
class JsonArray_Parser_Tests : public testing::Test
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
void parse(const char* json)
|
void whenInputIs(const char *json)
|
||||||
{
|
{
|
||||||
strcpy(_jsonString, json);
|
strcpy(_jsonString, json);
|
||||||
_array = _jsonBuffer.parseArray(_jsonString);
|
_array = _jsonBuffer.parseArray(_jsonString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void parseMustSucceed()
|
||||||
|
{
|
||||||
|
EXPECT_TRUE(_array.success());
|
||||||
|
}
|
||||||
|
|
||||||
|
void parseMustFail()
|
||||||
|
{
|
||||||
|
EXPECT_FALSE(_array.success());
|
||||||
|
EXPECT_EQ(0, _array.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
void sizeMustBe(int expected)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(expected, _array.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void firstElementMustBe(T expected)
|
||||||
|
{
|
||||||
|
elementAtIndexMustBe(0, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void secondElementMustBe(T expected)
|
||||||
|
{
|
||||||
|
elementAtIndexMustBe(1, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void elementAtIndexMustBe(int index, T expected)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(expected, static_cast<T>(_array[index]));
|
||||||
|
}
|
||||||
|
|
||||||
StaticJsonBuffer<42> _jsonBuffer;
|
StaticJsonBuffer<42> _jsonBuffer;
|
||||||
JsonArray _array;
|
JsonArray _array;
|
||||||
char _jsonString[256];
|
char _jsonString[256];
|
||||||
@ -18,70 +52,67 @@ protected:
|
|||||||
|
|
||||||
TEST_F(JsonArray_Parser_Tests, EmptyArray)
|
TEST_F(JsonArray_Parser_Tests, EmptyArray)
|
||||||
{
|
{
|
||||||
parse("[]");
|
whenInputIs("[]");
|
||||||
|
|
||||||
EXPECT_TRUE(_array.success());
|
parseMustSucceed();
|
||||||
EXPECT_EQ(0, _array.size());
|
sizeMustBe(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Parser_Tests, ArrayWithNoEnd)
|
TEST_F(JsonArray_Parser_Tests, ArrayWithNoEnd)
|
||||||
{
|
{
|
||||||
parse("[");
|
whenInputIs("[");
|
||||||
|
|
||||||
EXPECT_FALSE(_array.success());
|
parseMustFail();
|
||||||
EXPECT_EQ(0, _array.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Parser_Tests, EmptyArrayWithLeadingSpaces)
|
TEST_F(JsonArray_Parser_Tests, EmptyArrayWithLeadingSpaces)
|
||||||
{
|
{
|
||||||
parse(" []");
|
whenInputIs(" []");
|
||||||
|
|
||||||
EXPECT_TRUE(_array.success());
|
parseMustSucceed();
|
||||||
EXPECT_EQ(0, _array.size());
|
sizeMustBe(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Parser_Tests, Garbage)
|
TEST_F(JsonArray_Parser_Tests, Garbage)
|
||||||
{
|
{
|
||||||
parse("%*$£¤");
|
whenInputIs("%*$£¤");
|
||||||
|
|
||||||
EXPECT_FALSE(_array.success());
|
parseMustFail();
|
||||||
EXPECT_EQ(0, _array.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Parser_Tests, OneInteger)
|
TEST_F(JsonArray_Parser_Tests, OneInteger)
|
||||||
{
|
{
|
||||||
parse("[42]");
|
whenInputIs("[42]");
|
||||||
|
|
||||||
EXPECT_TRUE(_array.success());
|
parseMustSucceed();
|
||||||
EXPECT_EQ(1, _array.size());
|
sizeMustBe(1);
|
||||||
EXPECT_EQ(42, static_cast<int>(_array[0]));
|
firstElementMustBe(42);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Parser_Tests, OneIntegerWithSpacesBefore)
|
TEST_F(JsonArray_Parser_Tests, OneIntegerWithSpacesBefore)
|
||||||
{
|
{
|
||||||
parse("[ \t\r\n42]");
|
whenInputIs("[ \t\r\n42]");
|
||||||
|
|
||||||
EXPECT_TRUE(_array.success());
|
parseMustSucceed();
|
||||||
EXPECT_EQ(1, _array.size());
|
sizeMustBe(1);
|
||||||
EXPECT_EQ(42, static_cast<int>(_array[0]));
|
firstElementMustBe(42);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Parser_Tests, OneIntegerWithSpaceAfter)
|
TEST_F(JsonArray_Parser_Tests, OneIntegerWithSpaceAfter)
|
||||||
{
|
{
|
||||||
parse("[42 \t\r\n]");
|
whenInputIs("[42 \t\r\n]");
|
||||||
|
|
||||||
EXPECT_TRUE(_array.success());
|
parseMustSucceed();
|
||||||
EXPECT_EQ(1, _array.size());
|
sizeMustBe(1);
|
||||||
EXPECT_EQ(42, static_cast<int>(_array[0]));
|
firstElementMustBe(42);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Parser_Tests, TwoIntegers)
|
TEST_F(JsonArray_Parser_Tests, TwoIntegers)
|
||||||
{
|
{
|
||||||
parse("[42,84]");
|
whenInputIs("[42,84]");
|
||||||
|
|
||||||
EXPECT_TRUE(_array.success());
|
parseMustSucceed();
|
||||||
|
sizeMustBe(2);
|
||||||
EXPECT_EQ(2, _array.size());
|
firstElementMustBe(42);
|
||||||
EXPECT_EQ(42, static_cast<int>(_array[0]));
|
secondElementMustBe(84);
|
||||||
EXPECT_EQ(84, static_cast<int>(_array[1]));
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user