tests/test_json.c

changeset 1563
6e2f83ef6f5a
parent 1556
afdaa70034f8
child 1567
f60f23b362e9
equal deleted inserted replaced
1562:f2b63cad2142 1563:6e2f83ef6f5a
197 197
198 cxJsonValueFree(obj); 198 cxJsonValueFree(obj);
199 } 199 }
200 } 200 }
201 201
202 CX_TEST(test_json_from_string_errors) {
203 CX_TEST_DO {
204 CxJsonValue *obj = NULL;
205 CX_TEST_ASSERT(cxJsonFromString(NULL, "", &obj) == CX_JSON_NO_DATA);
206 CX_TEST_ASSERT(cxJsonFromString(NULL, cx_str(NULL), &obj) == CX_JSON_NO_DATA);
207 CX_TEST_ASSERT(cxJsonFromString(NULL, "\"incomplete", &obj) == CX_JSON_INCOMPLETE_DATA);
208 CX_TEST_ASSERT(cxJsonFromString(NULL, "{ \"incomplete\": ", &obj) == CX_JSON_INCOMPLETE_DATA);
209 CX_TEST_ASSERT(cxJsonFromString(NULL, "{\"number\": 47110815!}", &obj) == CX_JSON_FORMAT_ERROR_NUMBER);
210 CX_TEST_ASSERT(cxJsonFromString(NULL, "[ \"unexpected token\" : true ]", &obj) == CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
211 CX_TEST_ASSERT(cxJsonFromString(NULL, "} [", &obj) == CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
212 CX_TEST_ASSERT(obj && obj->type == CX_JSON_NOTHING);
213 }
214 }
215
216 CX_TEST(test_json_from_string_multiple_values) {
217 CxJsonStatus status;
218 CxJsonValue *obj = NULL;
219 CX_TEST_DO {
220 status = cxJsonFromString(NULL, "{ \"obj1\": \"hello\" }\n\"value2\"\n", &obj);
221
222 // TODO: what is the expected behavior here? Is this an error or do we ignore the second value?
223 CX_TEST_ASSERT(status == CX_JSON_NO_ERROR);
224 CX_TEST_ASSERT(cxJsonIsObject(obj));
225 // CX_TEST_ASSERT(status == CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
226
227 cxJsonValueFree(obj);
228
229 // TODO: this really should be an error in theory
230 status = cxJsonFromString(NULL, "\"value\" \n ] syntax error [", &obj);
231 CX_TEST_ASSERT(status == CX_JSON_NO_ERROR);
232 CX_TEST_ASSERT(cxJsonIsString(obj));
233 // CX_TEST_ASSERT(status == CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
234
235 cxJsonValueFree(obj);
236 }
237 }
238
202 CX_TEST(test_json_escaped_strings) { 239 CX_TEST(test_json_escaped_strings) {
203 cxstring text = cx_str( 240 cxstring text = cx_str(
204 "{\n" 241 "{\n"
205 "\t\"object\":\"{\\n\\t\\\"object\\\":null\\n}\",\n" 242 "\t\"object\":\"{\\n\\t\\\"object\\\":null\\n}\",\n"
206 "\t\"ctrl-chars\":\"\\\\foo\\r\\nbar\\f*ring\\/ring*\\b\"\n" 243 "\t\"ctrl-chars\":\"\\\\foo\\r\\nbar\\f*ring\\/ring*\\b\"\n"
447 cxJsonValueFree(obj); 484 cxJsonValueFree(obj);
448 485
449 // now there is everything read 486 // now there is everything read
450 result = cxJsonNext(&json, &obj); 487 result = cxJsonNext(&json, &obj);
451 CX_TEST_ASSERT(result == CX_JSON_NO_DATA); 488 CX_TEST_ASSERT(result == CX_JSON_NO_DATA);
489
490 // Test 2: abort after incomplete token
491 cxJsonReset(&json);
492
493 cxJsonFill(&json, "\"incomplete token");
494 result = cxJsonNext(&json, &obj);
495 CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA);
452 496
453 cxJsonDestroy(&json); 497 cxJsonDestroy(&json);
454 } 498 }
455 } 499 }
456 500
1517 1561
1518 cx_test_register(suite, test_json_init_default); 1562 cx_test_register(suite, test_json_init_default);
1519 cx_test_register(suite, test_json_simple_object); 1563 cx_test_register(suite, test_json_simple_object);
1520 cx_test_register(suite, test_json_large_object); 1564 cx_test_register(suite, test_json_large_object);
1521 cx_test_register(suite, test_json_from_string); 1565 cx_test_register(suite, test_json_from_string);
1566 cx_test_register(suite, test_json_from_string_errors);
1567 cx_test_register(suite, test_json_from_string_multiple_values);
1522 cx_test_register(suite, test_json_escaped_strings); 1568 cx_test_register(suite, test_json_escaped_strings);
1523 cx_test_register(suite, test_json_escaped_unicode_strings); 1569 cx_test_register(suite, test_json_escaped_unicode_strings);
1524 cx_test_register(suite, test_json_escaped_unicode_malformed); 1570 cx_test_register(suite, test_json_escaped_unicode_malformed);
1525 cx_test_register(suite, test_json_escaped_end_of_string); 1571 cx_test_register(suite, test_json_escaped_end_of_string);
1526 cx_test_register(suite, test_json_object_incomplete_token); 1572 cx_test_register(suite, test_json_object_incomplete_token);

mercurial