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>
|
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 JsonObject_PrettyPrintTo_Tests : public testing::Test {
|
2014-10-30 14:03:33 +01:00
|
|
|
public:
|
2014-12-20 15:42:43 +01:00
|
|
|
JsonObject_PrettyPrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
|
2014-10-30 14:03:33 +01:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
protected:
|
2014-12-20 15:42:43 +01:00
|
|
|
DynamicJsonBuffer _jsonBuffer;
|
|
|
|
JsonObject &_object;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
void outputMustBe(const char *expected) {
|
2015-05-09 16:53:48 +02:00
|
|
|
char buffer[256];
|
|
|
|
|
2016-12-10 15:59:48 +01:00
|
|
|
size_t actualLen = _object.prettyPrintTo(buffer);
|
2015-05-09 16:53:48 +02:00
|
|
|
size_t measuredLen = _object.measurePrettyLength();
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
EXPECT_STREQ(expected, buffer);
|
2015-05-09 16:53:48 +02:00
|
|
|
EXPECT_EQ(strlen(expected), actualLen);
|
|
|
|
EXPECT_EQ(strlen(expected), measuredLen);
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2016-12-10 15:59:48 +01:00
|
|
|
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) {
|
|
|
|
outputMustBe("{}");
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
|
2014-12-20 15:42:43 +01:00
|
|
|
_object["key"] = "value";
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"{\r\n"
|
|
|
|
" \"key\": \"value\"\r\n"
|
|
|
|
"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
|
2014-12-20 15:42:43 +01:00
|
|
|
_object["key1"] = "value1";
|
|
|
|
_object["key2"] = "value2";
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"{\r\n"
|
|
|
|
" \"key1\": \"value1\",\r\n"
|
|
|
|
" \"key2\": \"value2\"\r\n"
|
|
|
|
"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
|
2014-12-20 15:42:43 +01:00
|
|
|
_object.createNestedObject("key1");
|
|
|
|
_object.createNestedArray("key2");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"{\r\n"
|
|
|
|
" \"key1\": {},\r\n"
|
|
|
|
" \"key2\": []\r\n"
|
|
|
|
"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
|
2014-12-20 15:42:43 +01:00
|
|
|
JsonObject &nested1 = _object.createNestedObject("key1");
|
2014-10-23 19:54:00 +02:00
|
|
|
nested1["a"] = 1;
|
|
|
|
|
2014-12-20 15:42:43 +01:00
|
|
|
JsonArray &nested2 = _object.createNestedArray("key2");
|
2014-10-23 19:54:00 +02:00
|
|
|
nested2.add(2);
|
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"{\r\n"
|
|
|
|
" \"key1\": {\r\n"
|
|
|
|
" \"a\": 1\r\n"
|
|
|
|
" },\r\n"
|
|
|
|
" \"key2\": [\r\n"
|
|
|
|
" 2\r\n"
|
|
|
|
" ]\r\n"
|
|
|
|
"}");
|
2014-10-23 23:45:36 +02:00
|
|
|
}
|