tests/test_json.c

changeset 1338
cc153bffea28
parent 1337
6dfa1eb58ce3
equal deleted inserted replaced
1337:6dfa1eb58ce3 1338:cc153bffea28
444 "{\n" 444 "{\n"
445 "\t\"message\":\"success\",\n" 445 "\t\"message\":\"success\",\n"
446 "\t\"data\":{\n" 446 "\t\"data\":{\n"
447 "\t\t\"obj\":{\n" 447 "\t\t\"obj\":{\n"
448 "\t\t\t\"array\": [1, 2, 3, ?syntaxerror? ]\n" 448 "\t\t\t\"array\": [1, 2, 3, ?syntaxerror? ]\n"
449 "\t\t\"}\n" 449 "\t\t}\n"
450 "\t},\n" 450 "\t},\n"
451 "\t\"timestamp\":1729348561,\n" 451 "\t\"timestamp\":1729348561,\n"
452 "}" 452 "}"
453 ); 453 );
454 cxstring text1 = cx_str("{ \"string\" }"); 454 cxstring text1 = cx_str("{ \"string\" }");
478 CX_TEST_ASSERT(result == errors[i]); 478 CX_TEST_ASSERT(result == errors[i]);
479 CX_TEST_ASSERT(obj != NULL && obj->type == CX_JSON_NOTHING); 479 CX_TEST_ASSERT(obj != NULL && obj->type == CX_JSON_NOTHING);
480 cxJsonDestroy(&json); 480 cxJsonDestroy(&json);
481 } 481 }
482 } 482 }
483 }
484
485 CX_TEST(test_json_object_remove_member) {
486 CxTestingAllocator talloc;
487 cx_testing_allocator_init(&talloc);
488 const CxAllocator *alloc = &talloc.base;
489 CX_TEST_DO {
490 CxJson json;
491 cxJsonInit(&json, alloc);
492 cxJsonFill(&json, cx_str(
493 "{\n"
494 "\t\"message\":\"success\",\n"
495 "\t\"data\":{\n"
496 "\t\t\"obj\":{\n"
497 "\t\t\t\"array\": [1, 2, 3]\n"
498 "\t\t}\n"
499 "\t},\n"
500 "\t\"timestamp\":1729348561\n"
501 "}"
502 ));
503 CxJsonValue *obj;
504 CX_TEST_ASSERT(CX_JSON_NO_ERROR == cxJsonNext(&json, &obj));
505 cxJsonDestroy(&json);
506
507 CX_TEST_ASSERT(cxJsonIsObject(cxJsonObjGet(obj, "data")));
508 CxJsonValue *data = cxJsonObjRemove(obj, "data");
509 CX_TEST_ASSERT(cxJsonIsObject(data));
510 CX_TEST_ASSERT(!cxJsonIsObject(cxJsonObjGet(obj, "data")));
511 CX_TEST_ASSERT(cxJsonIsObject(cxJsonObjGet(data, "obj")));
512
513 CX_TEST_ASSERT(NULL == cxJsonObjRemove(obj, "data"));
514
515 // does not verify, yet, because we extracted an object
516 cxJsonValueFree(obj);
517 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
518
519 cxJsonValueFree(data);
520 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
521 }
522 cx_testing_allocator_destroy(&talloc);
483 } 523 }
484 524
485 CX_TEST(test_json_large_nesting_depth) { 525 CX_TEST(test_json_large_nesting_depth) {
486 CxJson json; 526 CxJson json;
487 CxJsonValue *d1; 527 CxJsonValue *d1;
735 CX_TEST_ASSERT(e->type == CX_JSON_NOTHING); 775 CX_TEST_ASSERT(e->type == CX_JSON_NOTHING);
736 CxJsonValue *removed = cxJsonArrRemove(arr, 2); 776 CxJsonValue *removed = cxJsonArrRemove(arr, 2);
737 CX_TEST_ASSERT(cxJsonIsNumber(removed)); 777 CX_TEST_ASSERT(cxJsonIsNumber(removed));
738 CX_TEST_ASSERT(cxJsonAsInteger(removed) == 6); 778 CX_TEST_ASSERT(cxJsonAsInteger(removed) == 6);
739 CX_TEST_ASSERT(cxJsonArrSize(arr) == 5); 779 CX_TEST_ASSERT(cxJsonArrSize(arr) == 5);
780 e = cxJsonArrGet(arr, 3);
781 CX_TEST_ASSERT(cxJsonIsNumber(e));
782 CX_TEST_ASSERT(cxJsonAsInteger(e) == 12);
740 e = cxJsonArrRemove(arr, 5); 783 e = cxJsonArrRemove(arr, 5);
741 CX_TEST_ASSERT(e == NULL); 784 CX_TEST_ASSERT(e == NULL);
742 cxJsonValueFree(arr); 785 cxJsonValueFree(arr);
743 // the removed element still needs to be freed 786 // the removed element still needs to be freed
744 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 787 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1225 cx_test_register(suite, test_json_escaped_unicode_malformed); 1268 cx_test_register(suite, test_json_escaped_unicode_malformed);
1226 cx_test_register(suite, test_json_escaped_end_of_string); 1269 cx_test_register(suite, test_json_escaped_end_of_string);
1227 cx_test_register(suite, test_json_object_incomplete_token); 1270 cx_test_register(suite, test_json_object_incomplete_token);
1228 cx_test_register(suite, test_json_token_wrongly_completed); 1271 cx_test_register(suite, test_json_token_wrongly_completed);
1229 cx_test_register(suite, test_json_object_error); 1272 cx_test_register(suite, test_json_object_error);
1273 cx_test_register(suite, test_json_object_remove_member);
1230 cx_test_register(suite, test_json_subsequent_fill); 1274 cx_test_register(suite, test_json_subsequent_fill);
1231 cx_test_register(suite, test_json_large_nesting_depth); 1275 cx_test_register(suite, test_json_large_nesting_depth);
1232 cx_test_register(suite, test_json_number); 1276 cx_test_register(suite, test_json_number);
1233 cx_test_register(suite, test_json_number_format_errors); 1277 cx_test_register(suite, test_json_number_format_errors);
1234 cx_test_register(suite, test_json_multiple_values); 1278 cx_test_register(suite, test_json_multiple_values);

mercurial