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 ArduinoStringTests : public ::testing::Test {
|
---|
12 | protected:
|
---|
13 | static void eraseString(String &str) {
|
---|
14 | char *p = const_cast<char *>(str.c_str());
|
---|
15 | while (*p) *p++ = '*';
|
---|
16 | }
|
---|
17 |
|
---|
18 | DynamicJsonBuffer _jsonBuffer;
|
---|
19 | };
|
---|
20 |
|
---|
21 | TEST_F(ArduinoStringTests, JsonBuffer_ParseArray) {
|
---|
22 | String json("[\"hello\"]");
|
---|
23 | JsonArray &array = _jsonBuffer.parseArray(json);
|
---|
24 | eraseString(json);
|
---|
25 | ASSERT_TRUE(array.success());
|
---|
26 | ASSERT_STREQ("hello", array[0]);
|
---|
27 | }
|
---|
28 |
|
---|
29 | TEST_F(ArduinoStringTests, JsonBuffer_ParseObject) {
|
---|
30 | String json("{\"hello\":\"world\"}");
|
---|
31 | JsonObject &object = _jsonBuffer.parseObject(json);
|
---|
32 | eraseString(json);
|
---|
33 | ASSERT_TRUE(object.success());
|
---|
34 | ASSERT_STREQ("world", object["hello"]);
|
---|
35 | }
|
---|
36 |
|
---|
37 | TEST_F(ArduinoStringTests, JsonObject_Subscript) {
|
---|
38 | char json[] = "{\"key\":\"value\"}";
|
---|
39 | JsonObject &object = _jsonBuffer.parseObject(json);
|
---|
40 | ASSERT_STREQ("value", object[String("key")]);
|
---|
41 | }
|
---|
42 |
|
---|
43 | TEST_F(ArduinoStringTests, JsonObject_ConstSubscript) {
|
---|
44 | char json[] = "{\"key\":\"value\"}";
|
---|
45 | const JsonObject &object = _jsonBuffer.parseObject(json);
|
---|
46 | ASSERT_STREQ("value", object[String("key")]);
|
---|
47 | }
|
---|
48 |
|
---|
49 | TEST_F(ArduinoStringTests, JsonObject_SetKey) {
|
---|
50 | JsonObject &object = _jsonBuffer.createObject();
|
---|
51 | String key("hello");
|
---|
52 | object.set(key, "world");
|
---|
53 | eraseString(key);
|
---|
54 | ASSERT_STREQ("world", object["hello"]);
|
---|
55 | }
|
---|
56 |
|
---|
57 | TEST_F(ArduinoStringTests, JsonObject_SetValue) {
|
---|
58 | JsonObject &object = _jsonBuffer.createObject();
|
---|
59 | String value("world");
|
---|
60 | object.set("hello", value);
|
---|
61 | eraseString(value);
|
---|
62 | ASSERT_STREQ("world", object["hello"]);
|
---|
63 | }
|
---|
64 |
|
---|
65 | TEST_F(ArduinoStringTests, JsonObject_SetKeyValue) {
|
---|
66 | JsonObject &object = _jsonBuffer.createObject();
|
---|
67 | String key("hello");
|
---|
68 | String value("world");
|
---|
69 | object.set(key, value);
|
---|
70 | eraseString(key);
|
---|
71 | eraseString(value);
|
---|
72 | ASSERT_STREQ("world", object["hello"]);
|
---|
73 | }
|
---|
74 |
|
---|
75 | TEST_F(ArduinoStringTests, JsonObject_SetToArraySubscript) {
|
---|
76 | JsonArray &arr = _jsonBuffer.createArray();
|
---|
77 | arr.add("world");
|
---|
78 |
|
---|
79 | JsonObject &object = _jsonBuffer.createObject();
|
---|
80 | object.set(String("hello"), arr[0]);
|
---|
81 |
|
---|
82 | ASSERT_STREQ("world", object["hello"]);
|
---|
83 | }
|
---|
84 |
|
---|
85 | TEST_F(ArduinoStringTests, JsonObject_SetToObjectSubscript) {
|
---|
86 | JsonObject &arr = _jsonBuffer.createObject();
|
---|
87 | arr.set("x", "world");
|
---|
88 |
|
---|
89 | JsonObject &object = _jsonBuffer.createObject();
|
---|
90 | object.set(String("hello"), arr["x"]);
|
---|
91 |
|
---|
92 | ASSERT_STREQ("world", object["hello"]);
|
---|
93 | }
|
---|
94 |
|
---|
95 | TEST_F(ArduinoStringTests, JsonObject_Get) {
|
---|
96 | char json[] = "{\"key\":\"value\"}";
|
---|
97 | const JsonObject &object = _jsonBuffer.parseObject(json);
|
---|
98 | ASSERT_STREQ("value", object.get(String("key")));
|
---|
99 | }
|
---|
100 |
|
---|
101 | TEST_F(ArduinoStringTests, JsonObject_GetT) {
|
---|
102 | char json[] = "{\"key\":\"value\"}";
|
---|
103 | const JsonObject &object = _jsonBuffer.parseObject(json);
|
---|
104 | ASSERT_STREQ("value", object.get<const char *>(String("key")));
|
---|
105 | }
|
---|
106 |
|
---|
107 | TEST_F(ArduinoStringTests, JsonObject_IsT) {
|
---|
108 | char json[] = "{\"key\":\"value\"}";
|
---|
109 | const JsonObject &object = _jsonBuffer.parseObject(json);
|
---|
110 | ASSERT_TRUE(object.is<const char *>(String("key")));
|
---|
111 | }
|
---|
112 |
|
---|
113 | TEST_F(ArduinoStringTests, JsonObject_CreateNestedObject) {
|
---|
114 | String key = "key";
|
---|
115 | char json[64];
|
---|
116 | JsonObject &object = _jsonBuffer.createObject();
|
---|
117 | object.createNestedObject(key);
|
---|
118 | eraseString(key);
|
---|
119 | object.printTo(json, sizeof(json));
|
---|
120 | ASSERT_STREQ("{\"key\":{}}", json);
|
---|
121 | }
|
---|
122 |
|
---|
123 | TEST_F(ArduinoStringTests, JsonObject_CreateNestedArray) {
|
---|
124 | String key = "key";
|
---|
125 | char json[64];
|
---|
126 | JsonObject &object = _jsonBuffer.createObject();
|
---|
127 | object.createNestedArray(key);
|
---|
128 | eraseString(key);
|
---|
129 | object.printTo(json, sizeof(json));
|
---|
130 | ASSERT_STREQ("{\"key\":[]}", json);
|
---|
131 | }
|
---|
132 |
|
---|
133 | TEST_F(ArduinoStringTests, JsonObject_ContainsKey) {
|
---|
134 | char json[] = "{\"key\":\"value\"}";
|
---|
135 | const JsonObject &object = _jsonBuffer.parseObject(json);
|
---|
136 | ASSERT_TRUE(object.containsKey(String("key")));
|
---|
137 | }
|
---|
138 |
|
---|
139 | TEST_F(ArduinoStringTests, JsonObject_Remove) {
|
---|
140 | char json[] = "{\"key\":\"value\"}";
|
---|
141 | JsonObject &object = _jsonBuffer.parseObject(json);
|
---|
142 | ASSERT_EQ(1, object.size());
|
---|
143 | object.remove(String("key"));
|
---|
144 | ASSERT_EQ(0, object.size());
|
---|
145 | }
|
---|
146 |
|
---|
147 | TEST_F(ArduinoStringTests, JsonObjectSubscript_SetKey) {
|
---|
148 | JsonObject &object = _jsonBuffer.createObject();
|
---|
149 | String key("hello");
|
---|
150 | object[key] = "world";
|
---|
151 | eraseString(key);
|
---|
152 | ASSERT_STREQ("world", object["hello"]);
|
---|
153 | }
|
---|
154 |
|
---|
155 | TEST_F(ArduinoStringTests, JsonObjectSubscript_SetValue) {
|
---|
156 | JsonObject &object = _jsonBuffer.createObject();
|
---|
157 | String value("world");
|
---|
158 | object["hello"] = value;
|
---|
159 | eraseString(value);
|
---|
160 | ASSERT_STREQ("world", object["hello"]);
|
---|
161 | }
|
---|
162 |
|
---|
163 | TEST_F(ArduinoStringTests, JsonArray_Add) {
|
---|
164 | JsonArray &array = _jsonBuffer.createArray();
|
---|
165 | String value("hello");
|
---|
166 | array.add(value);
|
---|
167 | eraseString(value);
|
---|
168 | ASSERT_STREQ("hello", array[0]);
|
---|
169 | }
|
---|
170 |
|
---|
171 | TEST_F(ArduinoStringTests, JsonArray_Set) {
|
---|
172 | JsonArray &array = _jsonBuffer.createArray();
|
---|
173 | String value("world");
|
---|
174 | array.add("hello");
|
---|
175 | array.set(0, value);
|
---|
176 | eraseString(value);
|
---|
177 | ASSERT_STREQ("world", array[0]);
|
---|
178 | }
|
---|
179 |
|
---|
180 | TEST_F(ArduinoStringTests, JsonArraySubscript) {
|
---|
181 | JsonArray &array = _jsonBuffer.createArray();
|
---|
182 | String value("world");
|
---|
183 | array.add("hello");
|
---|
184 | array[0] = value;
|
---|
185 | eraseString(value);
|
---|
186 | ASSERT_STREQ("world", array[0]);
|
---|
187 | }
|
---|
188 |
|
---|
189 | TEST_F(ArduinoStringTests, JsonArray_PrintTo) {
|
---|
190 | JsonArray &array = _jsonBuffer.createArray();
|
---|
191 | array.add(4);
|
---|
192 | array.add(2);
|
---|
193 | String json;
|
---|
194 | array.printTo(json);
|
---|
195 | ASSERT_EQ(String("[4,2]"), json);
|
---|
196 | }
|
---|
197 |
|
---|
198 | TEST_F(ArduinoStringTests, JsonArray_PrettyPrintTo) {
|
---|
199 | JsonArray &array = _jsonBuffer.createArray();
|
---|
200 | array.add(4);
|
---|
201 | array.add(2);
|
---|
202 | String json;
|
---|
203 | array.prettyPrintTo(json);
|
---|
204 | ASSERT_EQ(String("[\r\n 4,\r\n 2\r\n]"), json);
|
---|
205 | }
|
---|
206 |
|
---|
207 | TEST_F(ArduinoStringTests, JsonObject_PrintTo) {
|
---|
208 | JsonObject &object = _jsonBuffer.createObject();
|
---|
209 | object["key"] = "value";
|
---|
210 | String json;
|
---|
211 | object.printTo(json);
|
---|
212 | ASSERT_EQ(String("{\"key\":\"value\"}"), json);
|
---|
213 | }
|
---|
214 |
|
---|
215 | TEST_F(ArduinoStringTests, JsonObject_PrettyPrintTo) {
|
---|
216 | JsonObject &object = _jsonBuffer.createObject();
|
---|
217 | object["key"] = "value";
|
---|
218 | String json;
|
---|
219 | object.prettyPrintTo(json);
|
---|
220 | ASSERT_EQ(String("{\r\n \"key\": \"value\"\r\n}"), json);
|
---|
221 | }
|
---|
222 |
|
---|
223 | TEST_F(ArduinoStringTests, JsonBuffer_GrowWhenAddingNewKey) {
|
---|
224 | JsonObject &object = _jsonBuffer.createObject();
|
---|
225 | String key1("hello"), key2("world");
|
---|
226 |
|
---|
227 | object[key1] = 1;
|
---|
228 | size_t sizeBefore = _jsonBuffer.size();
|
---|
229 | object[key2] = 2;
|
---|
230 | size_t sizeAfter = _jsonBuffer.size();
|
---|
231 |
|
---|
232 | ASSERT_GT(sizeAfter - sizeBefore, key2.size());
|
---|
233 | }
|
---|
234 |
|
---|
235 | TEST_F(ArduinoStringTests, JsonBuffer_DontGrowWhenReusingKey) {
|
---|
236 | JsonObject &object = _jsonBuffer.createObject();
|
---|
237 | String key("hello");
|
---|
238 |
|
---|
239 | object[key] = 1;
|
---|
240 | size_t sizeBefore = _jsonBuffer.size();
|
---|
241 | object[key] = 2;
|
---|
242 | size_t sizeAfter = _jsonBuffer.size();
|
---|
243 |
|
---|
244 | ASSERT_EQ(sizeBefore, sizeAfter);
|
---|
245 | }
|
---|