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 JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
---|
12 | public:
|
---|
13 | JsonArray_PrettyPrintTo_Tests() : array(jsonBuffer.createArray()) {}
|
---|
14 |
|
---|
15 | protected:
|
---|
16 | DynamicJsonBuffer jsonBuffer;
|
---|
17 | JsonArray& array;
|
---|
18 |
|
---|
19 | void outputMustBe(const char* expected) {
|
---|
20 | char actual[256];
|
---|
21 |
|
---|
22 | size_t actualLen = array.prettyPrintTo(actual, sizeof(actual));
|
---|
23 | size_t measuredLen = array.measurePrettyLength();
|
---|
24 |
|
---|
25 | EXPECT_STREQ(expected, actual);
|
---|
26 | EXPECT_EQ(strlen(expected), actualLen);
|
---|
27 | EXPECT_EQ(strlen(expected), measuredLen);
|
---|
28 | }
|
---|
29 | };
|
---|
30 |
|
---|
31 | TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); }
|
---|
32 |
|
---|
33 | TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
|
---|
34 | array.add(1);
|
---|
35 |
|
---|
36 | outputMustBe(
|
---|
37 | "[\r\n"
|
---|
38 | " 1\r\n"
|
---|
39 | "]");
|
---|
40 | }
|
---|
41 |
|
---|
42 | TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
|
---|
43 | array.add(1);
|
---|
44 | array.add(2);
|
---|
45 |
|
---|
46 | outputMustBe(
|
---|
47 | "[\r\n"
|
---|
48 | " 1,\r\n"
|
---|
49 | " 2\r\n"
|
---|
50 | "]");
|
---|
51 | }
|
---|
52 |
|
---|
53 | TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) {
|
---|
54 | array.createNestedArray();
|
---|
55 | array.createNestedArray();
|
---|
56 |
|
---|
57 | outputMustBe(
|
---|
58 | "[\r\n"
|
---|
59 | " [],\r\n"
|
---|
60 | " []\r\n"
|
---|
61 | "]");
|
---|
62 | }
|
---|
63 |
|
---|
64 | TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
|
---|
65 | JsonArray& nested1 = array.createNestedArray();
|
---|
66 | nested1.add(1);
|
---|
67 | nested1.add(2);
|
---|
68 |
|
---|
69 | JsonObject& nested2 = array.createNestedObject();
|
---|
70 | nested2["key"] = 3;
|
---|
71 |
|
---|
72 | outputMustBe(
|
---|
73 | "[\r\n"
|
---|
74 | " [\r\n"
|
---|
75 | " 1,\r\n"
|
---|
76 | " 2\r\n"
|
---|
77 | " ],\r\n"
|
---|
78 | " {\r\n"
|
---|
79 | " \"key\": 3\r\n"
|
---|
80 | " }\r\n"
|
---|
81 | "]");
|
---|
82 | }
|
---|