2017-01-06 21:07:34 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2017
|
2014-10-23 23:39:22 +02:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
2017-03-25 22:05:06 +01:00
|
|
|
// https://bblanchon.github.io/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>
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
|
|
|
|
|
|
|
static void check(JsonArray& array, std::string expected) {
|
|
|
|
std::string actual;
|
|
|
|
size_t actualLen = array.prettyPrintTo(actual);
|
|
|
|
size_t measuredLen = array.measurePrettyLength();
|
|
|
|
CHECK(actualLen == expected.size());
|
|
|
|
CHECK(measuredLen == expected.size());
|
|
|
|
REQUIRE(expected == actual);
|
2016-12-10 15:59:48 +01:00
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
TEST_CASE("JsonArray::prettyPrintTo()") {
|
|
|
|
DynamicJsonBuffer jb;
|
|
|
|
JsonArray& array = jb.createArray();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("Empty") {
|
|
|
|
check(array, "[]");
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("OneElement") {
|
|
|
|
array.add(1);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
check(array,
|
|
|
|
"[\r\n"
|
|
|
|
" 1\r\n"
|
|
|
|
"]");
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("TwoElements") {
|
|
|
|
array.add(1);
|
|
|
|
array.add(2);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
check(array,
|
|
|
|
"[\r\n"
|
|
|
|
" 1,\r\n"
|
|
|
|
" 2\r\n"
|
|
|
|
"]");
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("EmptyNestedArrays") {
|
|
|
|
array.createNestedArray();
|
|
|
|
array.createNestedArray();
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
check(array,
|
|
|
|
"[\r\n"
|
|
|
|
" [],\r\n"
|
|
|
|
" []\r\n"
|
|
|
|
"]");
|
|
|
|
}
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("NestedArrays") {
|
|
|
|
JsonArray& nested1 = array.createNestedArray();
|
|
|
|
nested1.add(1);
|
|
|
|
nested1.add(2);
|
|
|
|
|
|
|
|
JsonObject& nested2 = array.createNestedObject();
|
|
|
|
nested2["key"] = 3;
|
|
|
|
|
|
|
|
check(array,
|
|
|
|
"[\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
|
|
|
}
|