Files
ArduinoJson/tests/JsonArray_PrintTo_Tests.cpp

152 lines
2.7 KiB
C++
Raw Normal View History

2014-10-05 14:40:03 +02:00
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include <gtest/gtest.h>
#include <JsonArray.h>
#include <JsonObject.h>
#include <StaticJsonBuffer.h>
2014-10-07 11:22:10 +02:00
class JsonArray_PrintTo_Tests : public testing::Test
2014-10-05 14:40:03 +02:00
{
protected:
2014-10-05 15:23:52 +02:00
JsonArray array;
StaticJsonBuffer<3> json;
2014-10-05 14:40:03 +02:00
virtual void SetUp()
{
array = json.createArray();
}
void outputMustBe(const char* expected)
{
size_t n = array.printTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n);
}
private:
char buffer[256];
};
2014-10-05 15:23:52 +02:00
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, Empty)
2014-10-05 14:40:03 +02:00
{
outputMustBe("[]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, Null)
2014-10-05 14:40:03 +02:00
{
array.add((char*) 0);
outputMustBe("[null]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, OneString)
2014-10-05 14:40:03 +02:00
{
array.add("hello");
outputMustBe("[\"hello\"]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, TwoStrings)
2014-10-05 14:40:03 +02:00
{
array.add("hello");
array.add("world");
outputMustBe("[\"hello\",\"world\"]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity)
2014-10-05 14:40:03 +02:00
{
array.add("hello");
array.add("world");
array.add("lost");
outputMustBe("[\"hello\",\"world\"]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits)
2014-10-05 14:40:03 +02:00
{
array.add(3.14159265358979323846);
outputMustBe("[3.14]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits)
2014-10-05 14:40:03 +02:00
{
2014-10-05 15:23:52 +02:00
array.add(3.14159265358979323846, 4);
2014-10-05 14:40:03 +02:00
outputMustBe("[3.1416]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, OneInteger)
2014-10-05 14:40:03 +02:00
{
array.add(1);
outputMustBe("[1]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, TwoIntegers)
2014-10-05 14:40:03 +02:00
{
array.add(1);
array.add(2);
outputMustBe("[1,2]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity)
2014-10-05 14:40:03 +02:00
{
array.add(1);
array.add(2);
array.add(3);
outputMustBe("[1,2]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, OneTrue)
2014-10-05 14:40:03 +02:00
{
array.add(true);
outputMustBe("[true]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, OneFalse)
2014-10-05 14:40:03 +02:00
{
array.add(false);
outputMustBe("[false]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, TwoBooleans)
2014-10-05 14:40:03 +02:00
{
array.add(false);
array.add(true);
outputMustBe("[false,true]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity)
2014-10-05 14:40:03 +02:00
{
array.add(false);
array.add(true);
array.add(false);
outputMustBe("[false,true]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray)
2014-10-05 14:40:03 +02:00
{
2014-10-05 15:23:52 +02:00
JsonArray nestedArray = json.createArray();
2014-10-05 14:40:03 +02:00
array.add(nestedArray);
outputMustBe("[[]]");
}
2014-10-07 11:22:10 +02:00
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash)
2014-10-05 14:40:03 +02:00
{
2014-10-05 15:23:52 +02:00
JsonObject nestedObject = json.createObject();
2014-10-05 14:40:03 +02:00
array.add(nestedObject);
outputMustBe("[{}]");
2014-10-05 15:23:52 +02:00
}