1 | // Copyright Benoit Blanchon 2014-2016
|
---|
2 | // MIT License
|
---|
3 | //
|
---|
4 | // Arduino JSON library
|
---|
5 | // https://github.com/bblanchon/ArduinoJson
|
---|
6 | // If you like this project, please add a star!
|
---|
7 |
|
---|
8 | #include <gtest/gtest.h>
|
---|
9 | #include <ArduinoJson.h>
|
---|
10 |
|
---|
11 | class JsonObject_PrettyPrintTo_Tests : public testing::Test {
|
---|
12 | public:
|
---|
13 | JsonObject_PrettyPrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
|
---|
14 |
|
---|
15 | protected:
|
---|
16 | DynamicJsonBuffer _jsonBuffer;
|
---|
17 | JsonObject &_object;
|
---|
18 |
|
---|
19 | void outputMustBe(const char *expected) {
|
---|
20 | char buffer[256];
|
---|
21 |
|
---|
22 | size_t actualLen = _object.prettyPrintTo(buffer, sizeof(buffer));
|
---|
23 | size_t measuredLen = _object.measurePrettyLength();
|
---|
24 |
|
---|
25 | EXPECT_STREQ(expected, buffer);
|
---|
26 | EXPECT_EQ(strlen(expected), actualLen);
|
---|
27 | EXPECT_EQ(strlen(expected), measuredLen);
|
---|
28 | }
|
---|
29 | };
|
---|
30 |
|
---|
31 | TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
|
---|
32 |
|
---|
33 | TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
|
---|
34 | _object["key"] = "value";
|
---|
35 |
|
---|
36 | outputMustBe(
|
---|
37 | "{\r\n"
|
---|
38 | " \"key\": \"value\"\r\n"
|
---|
39 | "}");
|
---|
40 | }
|
---|
41 |
|
---|
42 | TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
|
---|
43 | _object["key1"] = "value1";
|
---|
44 | _object["key2"] = "value2";
|
---|
45 |
|
---|
46 | outputMustBe(
|
---|
47 | "{\r\n"
|
---|
48 | " \"key1\": \"value1\",\r\n"
|
---|
49 | " \"key2\": \"value2\"\r\n"
|
---|
50 | "}");
|
---|
51 | }
|
---|
52 |
|
---|
53 | TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
|
---|
54 | _object.createNestedObject("key1");
|
---|
55 | _object.createNestedArray("key2");
|
---|
56 |
|
---|
57 | outputMustBe(
|
---|
58 | "{\r\n"
|
---|
59 | " \"key1\": {},\r\n"
|
---|
60 | " \"key2\": []\r\n"
|
---|
61 | "}");
|
---|
62 | }
|
---|
63 |
|
---|
64 | TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
|
---|
65 | JsonObject &nested1 = _object.createNestedObject("key1");
|
---|
66 | nested1["a"] = 1;
|
---|
67 |
|
---|
68 | JsonArray &nested2 = _object.createNestedArray("key2");
|
---|
69 | nested2.add(2);
|
---|
70 |
|
---|
71 | outputMustBe(
|
---|
72 | "{\r\n"
|
---|
73 | " \"key1\": {\r\n"
|
---|
74 | " \"a\": 1\r\n"
|
---|
75 | " },\r\n"
|
---|
76 | " \"key2\": [\r\n"
|
---|
77 | " 2\r\n"
|
---|
78 | " ]\r\n"
|
---|
79 | "}");
|
---|
80 | }
|
---|