2016-01-07 22:35:12 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2016
|
2014-10-23 23:39:22 +02:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
2016-01-07 22:35:12 +01:00
|
|
|
// If you like this project, please add a star!
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-11-11 16:54:46 +01:00
|
|
|
#include <ArduinoJson.h>
|
2016-12-10 15:59:48 +01:00
|
|
|
#include <gtest/gtest.h>
|
2014-10-18 23:05:54 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
class JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
2014-10-30 10:49:02 +01:00
|
|
|
public:
|
2014-12-20 15:42:43 +01:00
|
|
|
JsonArray_PrettyPrintTo_Tests() : array(jsonBuffer.createArray()) {}
|
2014-10-30 10:49:02 +01:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
protected:
|
2014-12-20 15:42:43 +01:00
|
|
|
DynamicJsonBuffer jsonBuffer;
|
2014-10-30 10:49:02 +01:00
|
|
|
JsonArray& array;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-30 10:49:02 +01:00
|
|
|
void outputMustBe(const char* expected) {
|
2015-05-09 16:53:48 +02:00
|
|
|
char actual[256];
|
|
|
|
|
2016-12-10 15:59:48 +01:00
|
|
|
size_t actualLen = array.prettyPrintTo(actual);
|
2015-05-09 16:53:48 +02:00
|
|
|
size_t measuredLen = array.measurePrettyLength();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2015-05-09 16:53:48 +02:00
|
|
|
EXPECT_STREQ(expected, actual);
|
|
|
|
EXPECT_EQ(strlen(expected), actualLen);
|
|
|
|
EXPECT_EQ(strlen(expected), measuredLen);
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2016-12-10 15:59:48 +01:00
|
|
|
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) {
|
|
|
|
outputMustBe("[]");
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
|
|
|
|
array.add(1);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"[\r\n"
|
|
|
|
" 1\r\n"
|
|
|
|
"]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
|
|
|
|
array.add(1);
|
|
|
|
array.add(2);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"[\r\n"
|
|
|
|
" 1,\r\n"
|
|
|
|
" 2\r\n"
|
|
|
|
"]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) {
|
|
|
|
array.createNestedArray();
|
|
|
|
array.createNestedArray();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"[\r\n"
|
|
|
|
" [],\r\n"
|
|
|
|
" []\r\n"
|
|
|
|
"]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
|
2014-10-30 10:49:02 +01:00
|
|
|
JsonArray& nested1 = array.createNestedArray();
|
2014-10-23 19:54:00 +02:00
|
|
|
nested1.add(1);
|
|
|
|
nested1.add(2);
|
|
|
|
|
2014-10-30 10:49:02 +01:00
|
|
|
JsonObject& nested2 = array.createNestedObject();
|
2014-10-23 19:54:00 +02:00
|
|
|
nested2["key"] = 3;
|
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"[\r\n"
|
|
|
|
" [\r\n"
|
|
|
|
" 1,\r\n"
|
|
|
|
" 2\r\n"
|
|
|
|
" ],\r\n"
|
|
|
|
" {\r\n"
|
|
|
|
" \"key\": 3\r\n"
|
|
|
|
" }\r\n"
|
|
|
|
"]");
|
2014-10-23 23:45:36 +02:00
|
|
|
}
|