tests/test_json.c

branch
docs/3.1
changeset 1164
148b7c7ccaf9
parent 1157
3565ae61a5a2
equal deleted inserted replaced
1148:8ff82697f2c3 1164:148b7c7ccaf9
118 } 118 }
119 119
120 CX_TEST(test_json_escaped_strings) { 120 CX_TEST(test_json_escaped_strings) {
121 cxstring text = cx_str( 121 cxstring text = cx_str(
122 "{\n" 122 "{\n"
123 "\t\"object\":\"{\\n\\t\\\"object\\\":null\\n}\"}\"\n" 123 "\t\"object\":\"{\\n\\t\\\"object\\\":null\\n}\",\n"
124 "\t\"ctrl-chars\":\"\\\\foo\\r\\nbar\\f*ring\\/ring*\\b\"\n"
124 "}" 125 "}"
125 ); 126 );
126 127
127 CxJson json; 128 CxJson json;
128 cxJsonInit(&json, NULL); 129 cxJsonInit(&json, NULL);
136 CX_TEST_ASSERT(cxJsonIsString(object)); 137 CX_TEST_ASSERT(cxJsonIsString(object));
137 CX_TEST_ASSERT(0 == cx_strcmp( 138 CX_TEST_ASSERT(0 == cx_strcmp(
138 cxJsonAsCxString(object), 139 cxJsonAsCxString(object),
139 CX_STR("{\n\t\"object\":null\n}")) 140 CX_STR("{\n\t\"object\":null\n}"))
140 ); 141 );
142 CxJsonValue *ctrl = cxJsonObjGet(obj, "ctrl-chars");
143 CX_TEST_ASSERT(cxJsonIsString(ctrl));
144 CX_TEST_ASSERT(0 == cx_strcmp(
145 cxJsonAsCxString(ctrl),
146 CX_STR("\\foo\r\nbar\f*ring/ring*\b"))
147 );
148 cxJsonValueFree(obj);
149 }
150 cxJsonDestroy(&json);
151 }
152
153 CX_TEST(test_json_escaped_unicode_strings) {
154 cxstring text = cx_str(
155 "{\n"
156 "\"ascii\":\"\\u0041\\u0053\\u0043\\u0049\\u0049\",\n"
157 "\"unicode\":\"\\u00df\\u00DF\",\n"
158 "\"mixed\":\"mixed ä ö \\u00e4 \\u00f6\",\n"
159 "\"wide\":\"\\u03a3\\u29b0\",\n"
160 "\"surrogatepair1\":\"\\ud83e\\udff5\",\n"
161 "\"surrogatepair2\":\"test\\ud83e\\udff1AA\"\n,"
162 "\"mixed2\":\"123\\u03a3\\ud83e\\udfc5\\u00df\""
163 "}"
164 );
165
166 CxJson json;
167 cxJsonInit(&json, NULL);
168 CX_TEST_DO {
169 cxJsonFill(&json, text);
170 CxJsonValue *obj;
171 CxJsonStatus result = cxJsonNext(&json, &obj);
172 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
173 CX_TEST_ASSERT(cxJsonIsObject(obj));
174
175 CxJsonValue *ascii = cxJsonObjGet(obj, "ascii");
176 CX_TEST_ASSERT(cxJsonIsString(ascii));
177 CX_TEST_ASSERT(0 == cx_strcmp(
178 cxJsonAsCxString(ascii),
179 CX_STR("ASCII"))
180 );
181
182 CxJsonValue *unicode = cxJsonObjGet(obj, "unicode");
183 CX_TEST_ASSERT(cxJsonIsString(unicode));
184 CX_TEST_ASSERT(0 == cx_strcmp(
185 cxJsonAsCxString(unicode),
186 CX_STR("ßß"))
187 );
188
189 CxJsonValue *mixed = cxJsonObjGet(obj, "mixed");
190 CX_TEST_ASSERT(cxJsonIsString(mixed));
191 CX_TEST_ASSERT(0 == cx_strcmp(
192 cxJsonAsCxString(mixed),
193 CX_STR("mixed ä ö ä ö"))
194 );
195
196 CxJsonValue *wide = cxJsonObjGet(obj, "wide");
197 CX_TEST_ASSERT(cxJsonIsString(wide));
198 CX_TEST_ASSERT(0 == cx_strcmp(
199 cxJsonAsCxString(wide),
200 CX_STR("\u03a3\u29b0"))
201 );
202
203 CxJsonValue *surrogatepair1 = cxJsonObjGet(obj, "surrogatepair1");
204 CX_TEST_ASSERT(cxJsonIsString(surrogatepair1));
205 CX_TEST_ASSERT(0 == cx_strcmp(
206 cxJsonAsCxString(surrogatepair1),
207 CX_STR("\xf0\x9f\xaf\xb5"))
208 );
209
210 CxJsonValue *surrogatepair2 = cxJsonObjGet(obj, "surrogatepair2");
211 CX_TEST_ASSERT(cxJsonIsString(surrogatepair2));
212 CX_TEST_ASSERT(0 == cx_strcmp(
213 cxJsonAsCxString(surrogatepair2),
214 CX_STR("test\xf0\x9f\xaf\xb1" "AA"))
215 );
216
217 CxJsonValue *mixed2 = cxJsonObjGet(obj, "mixed2");
218 CX_TEST_ASSERT(cxJsonIsString(mixed2));
219 CX_TEST_ASSERT(0 == cx_strcmp(
220 cxJsonAsCxString(mixed2),
221 CX_STR("123\u03a3\xf0\x9f\xaf\x85ß"))
222 );
223
224 cxJsonValueFree(obj);
225 }
226 cxJsonDestroy(&json);
227 }
228
229 CX_TEST(test_json_escaped_unicode_malformed) {
230 CxJson json;
231 cxJsonInit(&json, NULL);
232 CxJsonValue *obj;
233 CxJsonStatus result;
234 CX_TEST_DO {
235 cxJsonFill(&json, "\"too few \\u123 digits\"");
236 result = cxJsonNext(&json, &obj);
237 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
238 CX_TEST_ASSERT(cxJsonIsString(obj));
239 CX_TEST_ASSERT(0 == cx_strcmp(
240 cxJsonAsCxString(obj),
241 CX_STR("too few \\u123 digits")
242 ));
243 cxJsonValueFree(obj);
244 cxJsonFill(&json, "\"too many \\u00E456 digits\"");
245 result = cxJsonNext(&json, &obj);
246 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
247 CX_TEST_ASSERT(cxJsonIsString(obj));
248 CX_TEST_ASSERT(0 == cx_strcmp(
249 cxJsonAsCxString(obj),
250 CX_STR("too many ä56 digits")
251 ));
252 cxJsonValueFree(obj);
253 cxJsonFill(&json, "\"only high \\uD800 surrogate\"");
254 result = cxJsonNext(&json, &obj);
255 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
256 CX_TEST_ASSERT(cxJsonIsString(obj));
257 CX_TEST_ASSERT(0 == cx_strcmp(
258 cxJsonAsCxString(obj),
259 CX_STR("only high \\uD800 surrogate")
260 ));
261 cxJsonValueFree(obj);
262 cxJsonFill(&json, "\"only low \\uDC00 surrogate\"");
263 result = cxJsonNext(&json, &obj);
264 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
265 CX_TEST_ASSERT(cxJsonIsString(obj));
266 CX_TEST_ASSERT(0 == cx_strcmp(
267 cxJsonAsCxString(obj),
268 CX_STR("only low \\uDC00 surrogate")
269 ));
270 cxJsonValueFree(obj);
271 cxJsonFill(&json, "\"two high \\uD800\\uD800 surrogates\"");
272 result = cxJsonNext(&json, &obj);
273 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
274 CX_TEST_ASSERT(cxJsonIsString(obj));
275 CX_TEST_ASSERT(0 == cx_strcmp(
276 cxJsonAsCxString(obj),
277 CX_STR("two high \\uD800\\uD800 surrogates")
278 ));
279 cxJsonValueFree(obj);
280 cxJsonFill(&json, "\"high plus bullshit \\uD800\\u567 foo\"");
281 result = cxJsonNext(&json, &obj);
282 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
283 CX_TEST_ASSERT(cxJsonIsString(obj));
284 CX_TEST_ASSERT(0 == cx_strcmp(
285 cxJsonAsCxString(obj),
286 CX_STR("high plus bullshit \\uD800\\u567 foo")
287 ));
141 cxJsonValueFree(obj); 288 cxJsonValueFree(obj);
142 } 289 }
143 cxJsonDestroy(&json); 290 cxJsonDestroy(&json);
144 } 291 }
145 292
1040 CxTestSuite *suite = cx_test_suite_new("json"); 1187 CxTestSuite *suite = cx_test_suite_new("json");
1041 1188
1042 cx_test_register(suite, test_json_init_default); 1189 cx_test_register(suite, test_json_init_default);
1043 cx_test_register(suite, test_json_simple_object); 1190 cx_test_register(suite, test_json_simple_object);
1044 cx_test_register(suite, test_json_escaped_strings); 1191 cx_test_register(suite, test_json_escaped_strings);
1192 cx_test_register(suite, test_json_escaped_unicode_strings);
1193 cx_test_register(suite, test_json_escaped_unicode_malformed);
1045 cx_test_register(suite, test_json_escaped_end_of_string); 1194 cx_test_register(suite, test_json_escaped_end_of_string);
1046 cx_test_register(suite, test_json_object_incomplete_token); 1195 cx_test_register(suite, test_json_object_incomplete_token);
1047 cx_test_register(suite, test_json_token_wrongly_completed); 1196 cx_test_register(suite, test_json_token_wrongly_completed);
1048 cx_test_register(suite, test_json_object_error); 1197 cx_test_register(suite, test_json_object_error);
1049 cx_test_register(suite, test_json_subsequent_fill); 1198 cx_test_register(suite, test_json_subsequent_fill);

mercurial