tests/test_properties.c

changeset 1561
fcebf53de51c
parent 1558
fc863c877a75
equal deleted inserted replaced
1560:f060ecd65575 1561:fcebf53de51c
505 cx_testing_allocator_destroy(&talloc); 505 cx_testing_allocator_destroy(&talloc);
506 if (f) fclose(f); 506 if (f) fclose(f);
507 remove(fname); 507 remove(fname);
508 } 508 }
509 509
510 CX_TEST(test_properties_load_error) {
511 CxTestingAllocator talloc;
512 cx_testing_allocator_init(&talloc);
513 char fname[16] = "ucxtestXXXXXX";
514 int tmpfd = mkstemp(fname);
515 FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w");
516 CX_TEST_DO {
517 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
518 fputs("# test file\n\ntest = value\n = value2\n", f);
519 fclose(f);
520 f = NULL;
521
522 CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS);
523 cxDefineAdvancedDestructor(map, cxFree, &talloc);
524 CxPropertiesStatus status = cxPropertiesLoadDefault(&talloc.base, fname, map);
525 CX_TEST_ASSERT(status == CX_PROPERTIES_INVALID_EMPTY_KEY);
526 // all keys that could be successfully parsed are added
527 CX_TEST_ASSERT(cxMapSize(map) == 1);
528 char *v = cxMapGet(map, "test");
529 CX_TEST_ASSERT(v != NULL);
530 CX_TEST_ASSERT(!strcmp(v, "value"));
531
532 cxMapFree(map);
533 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
534 }
535 cx_testing_allocator_destroy(&talloc);
536 if (f) fclose(f);
537 remove(fname);
538 }
539
540 CX_TEST(test_properties_load_file_not_exists) {
541 CX_TEST_DO {
542 CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS);
543 CxPropertiesStatus status = cxPropertiesLoadDefault(NULL, "does-definitely-not-exist", map);
544 CX_TEST_ASSERT(status == CX_PROPERTIES_FILE_ERROR);
545 cxMapFree(map);
546 }
547 }
548
549 CX_TEST(test_properties_load_cxmutstr_map) {
550 CxTestingAllocator talloc;
551 cx_testing_allocator_init(&talloc);
552 char fname[16] = "ucxtestXXXXXX";
553 int tmpfd = mkstemp(fname);
554 FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w");
555 CX_TEST_DO {
556 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
557 fputs("# test file\n\ntest = value\ntest2 = value2\n", f);
558 fclose(f);
559 f = NULL;
560
561 CxMap *map = cxHashMapCreateSimple(sizeof(cxmutstr));
562 cxDefineAdvancedDestructor(map, cx_strfree_a, &talloc);
563 CxPropertiesStatus status = cxPropertiesLoadDefault(&talloc.base, fname, map);
564 CX_TEST_ASSERT(status == CX_PROPERTIES_NO_ERROR);
565 CX_TEST_ASSERT(cxMapSize(map) == 2);
566 cxstring v1 = CX_STR("value");
567 cxstring v2 = CX_STR("value2");
568 CX_TEST_ASSERT(cx_strcmp_p(cxMapGet(map, "test"), &v1) == 0);
569 CX_TEST_ASSERT(cx_strcmp_p(cxMapGet(map, "test2"), &v2) == 0);
570
571 CX_TEST_ASSERT(cx_testing_allocator_used(&talloc));
572 cxMapFree(map);
573 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
574 }
575 cx_testing_allocator_destroy(&talloc);
576 if (f) fclose(f);
577 remove(fname);
578 }
579
580 CX_TEST(test_properties_load_incompatible_map) {
581 CxTestingAllocator talloc;
582 cx_testing_allocator_init(&talloc);
583 char fname[16] = "ucxtestXXXXXX";
584 int tmpfd = mkstemp(fname);
585 FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w");
586 CX_TEST_DO {
587 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
588 fputs("# test file\n\ntest = value\ntest2 = value2\n", f);
589 fclose(f);
590 f = NULL;
591
592 CxMap *map = cxHashMapCreateSimple(sizeof(CxBuffer));
593 cxDefineAdvancedDestructor(map, cxFree, &talloc);
594 CxPropertiesStatus status = cxPropertiesLoadDefault(&talloc.base, fname, map);
595 CX_TEST_ASSERT(status == CX_PROPERTIES_MAP_ERROR);
596 CX_TEST_ASSERT(cxMapSize(map) == 0);
597
598 cxMapFree(map);
599 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
600 }
601 cx_testing_allocator_destroy(&talloc);
602 if (f) fclose(f);
603 remove(fname);
604 }
605
510 CX_TEST(test_properties_multiple_fill) { 606 CX_TEST(test_properties_multiple_fill) {
511 const char *props1 = "key1 = value1\n"; 607 const char *props1 = "key1 = value1\n";
512 const char *props2 = "key2 = value2\n"; 608 const char *props2 = "key2 = value2\n";
513 const char *props3 = "key3 = value3\n"; 609 const char *props3 = "key3 = value3\n";
514 610
615 cx_test_register(suite, test_properties_next_part); 711 cx_test_register(suite, test_properties_next_part);
616 cx_test_register(suite, test_properties_next_long_lines); 712 cx_test_register(suite, test_properties_next_long_lines);
617 cx_test_register(suite, test_properties_load); 713 cx_test_register(suite, test_properties_load);
618 cx_test_register(suite, test_properties_load_empty_file); 714 cx_test_register(suite, test_properties_load_empty_file);
619 cx_test_register(suite, test_properties_load_only_comments); 715 cx_test_register(suite, test_properties_load_only_comments);
620 // TODO: test_properties_load_invalid_key 716 cx_test_register(suite, test_properties_load_error);
621 // TODO: test_properties_load_missing_delimiter 717 cx_test_register(suite, test_properties_load_file_not_exists);
622 // TODO: test_properties_load_unexpected_end 718 cx_test_register(suite, test_properties_load_cxmutstr_map);
623 // TODO: test_properties_load_file_not_exists 719 cx_test_register(suite, test_properties_load_incompatible_map);
624 // TODO: test_properties_load_exceed_stack
625 // TODO: test_properties_load_incompatible_map
626 cx_test_register(suite, test_properties_multiple_fill); 720 cx_test_register(suite, test_properties_multiple_fill);
627 cx_test_register(suite, test_properties_use_stack); 721 cx_test_register(suite, test_properties_use_stack);
628 cx_test_register(suite, test_properties_empty_key); 722 cx_test_register(suite, test_properties_empty_key);
629 723
630 return suite; 724 return suite;

mercurial