Tue, 09 Dec 2025 19:05:35 +0100
add more tests for cxPropertiesLoad()
resolves #775
| tests/test_properties.c | file | annotate | diff | comparison | revisions |
--- a/tests/test_properties.c Tue Dec 09 18:55:14 2025 +0100 +++ b/tests/test_properties.c Tue Dec 09 19:05:35 2025 +0100 @@ -507,6 +507,102 @@ remove(fname); } +CX_TEST(test_properties_load_error) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + char fname[16] = "ucxtestXXXXXX"; + int tmpfd = mkstemp(fname); + FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w"); + CX_TEST_DO { + CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted"); + fputs("# test file\n\ntest = value\n = value2\n", f); + fclose(f); + f = NULL; + + CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS); + cxDefineAdvancedDestructor(map, cxFree, &talloc); + CxPropertiesStatus status = cxPropertiesLoadDefault(&talloc.base, fname, map); + CX_TEST_ASSERT(status == CX_PROPERTIES_INVALID_EMPTY_KEY); + // all keys that could be successfully parsed are added + CX_TEST_ASSERT(cxMapSize(map) == 1); + char *v = cxMapGet(map, "test"); + CX_TEST_ASSERT(v != NULL); + CX_TEST_ASSERT(!strcmp(v, "value")); + + cxMapFree(map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); + if (f) fclose(f); + remove(fname); +} + +CX_TEST(test_properties_load_file_not_exists) { + CX_TEST_DO { + CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS); + CxPropertiesStatus status = cxPropertiesLoadDefault(NULL, "does-definitely-not-exist", map); + CX_TEST_ASSERT(status == CX_PROPERTIES_FILE_ERROR); + cxMapFree(map); + } +} + +CX_TEST(test_properties_load_cxmutstr_map) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + char fname[16] = "ucxtestXXXXXX"; + int tmpfd = mkstemp(fname); + FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w"); + CX_TEST_DO { + CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted"); + fputs("# test file\n\ntest = value\ntest2 = value2\n", f); + fclose(f); + f = NULL; + + CxMap *map = cxHashMapCreateSimple(sizeof(cxmutstr)); + cxDefineAdvancedDestructor(map, cx_strfree_a, &talloc); + CxPropertiesStatus status = cxPropertiesLoadDefault(&talloc.base, fname, map); + CX_TEST_ASSERT(status == CX_PROPERTIES_NO_ERROR); + CX_TEST_ASSERT(cxMapSize(map) == 2); + cxstring v1 = CX_STR("value"); + cxstring v2 = CX_STR("value2"); + CX_TEST_ASSERT(cx_strcmp_p(cxMapGet(map, "test"), &v1) == 0); + CX_TEST_ASSERT(cx_strcmp_p(cxMapGet(map, "test2"), &v2) == 0); + + CX_TEST_ASSERT(cx_testing_allocator_used(&talloc)); + cxMapFree(map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); + if (f) fclose(f); + remove(fname); +} + +CX_TEST(test_properties_load_incompatible_map) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + char fname[16] = "ucxtestXXXXXX"; + int tmpfd = mkstemp(fname); + FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w"); + CX_TEST_DO { + CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted"); + fputs("# test file\n\ntest = value\ntest2 = value2\n", f); + fclose(f); + f = NULL; + + CxMap *map = cxHashMapCreateSimple(sizeof(CxBuffer)); + cxDefineAdvancedDestructor(map, cxFree, &talloc); + CxPropertiesStatus status = cxPropertiesLoadDefault(&talloc.base, fname, map); + CX_TEST_ASSERT(status == CX_PROPERTIES_MAP_ERROR); + CX_TEST_ASSERT(cxMapSize(map) == 0); + + cxMapFree(map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); + if (f) fclose(f); + remove(fname); +} + CX_TEST(test_properties_multiple_fill) { const char *props1 = "key1 = value1\n"; const char *props2 = "key2 = value2\n"; @@ -617,12 +713,10 @@ cx_test_register(suite, test_properties_load); cx_test_register(suite, test_properties_load_empty_file); cx_test_register(suite, test_properties_load_only_comments); - // TODO: test_properties_load_invalid_key - // TODO: test_properties_load_missing_delimiter - // TODO: test_properties_load_unexpected_end - // TODO: test_properties_load_file_not_exists - // TODO: test_properties_load_exceed_stack - // TODO: test_properties_load_incompatible_map + cx_test_register(suite, test_properties_load_error); + cx_test_register(suite, test_properties_load_file_not_exists); + cx_test_register(suite, test_properties_load_cxmutstr_map); + cx_test_register(suite, test_properties_load_incompatible_map); cx_test_register(suite, test_properties_multiple_fill); cx_test_register(suite, test_properties_use_stack); cx_test_register(suite, test_properties_empty_key);