Files
ArduinoJson/test/JsonArray_PrintTo_Tests.cpp

127 lines
2.4 KiB
C++
Raw Normal View History

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