tests/test_properties.c

changeset 1124
fcd5d86c472f
parent 1031
8a90552bba29
--- a/tests/test_properties.c	Sun Jan 12 13:04:32 2025 +0100
+++ b/tests/test_properties.c	Sun Jan 12 13:25:50 2025 +0100
@@ -519,6 +519,64 @@
     cx_testing_allocator_destroy(&talloc);
 }
 
+CX_TEST(test_properties_load_incomplete) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    CxAllocator *alloc = &talloc.base;
+    CX_TEST_DO {
+        char buffer[512];
+        CxProperties prop;
+        cxPropertiesInitDefault(&prop);
+        cxPropertiesUseStack(&prop, buffer, 512);
+
+        CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS);
+        cxDefineAdvancedDestructor(map, cxFree, alloc);
+        CxPropertiesSink sink = cxPropertiesMapSink(map);
+        sink.data = alloc; // use the testing allocator
+        CxPropertiesSource src = cxPropertiesCstrSource("key1 = value1\nkey2 = value2\n\n#comment\n\nkey3");
+        CxPropertiesStatus status = cxPropertiesLoad(&prop, sink, src);
+
+        CX_TEST_ASSERT(status == CX_PROPERTIES_INCOMPLETE_DATA);
+        CX_TEST_ASSERT(cxMapSize(map) == 2);
+
+        char *v1 = cxMapGet(map, "key1");
+        char *v2 = cxMapGet(map, "key2");
+        char *v3 = cxMapGet(map, "key3");
+
+        CX_TEST_ASSERTM(v1, "value for key1 not found");
+        CX_TEST_ASSERTM(v2, "value for key2 not found");
+        CX_TEST_ASSERT(v3 == NULL);
+
+        CX_TEST_ASSERT(!strcmp(v1, "value1"));
+        CX_TEST_ASSERT(!strcmp(v2, "value2"));
+
+        // provide a source with the remaining data
+        src = cxPropertiesCstrSource(" = value3\n");
+        status = cxPropertiesLoad(&prop, sink, src);
+
+        CX_TEST_ASSERT(status == CX_PROPERTIES_NO_ERROR);
+        CX_TEST_ASSERT(cxMapSize(map) == 3);
+
+        v1 = cxMapGet(map, "key1");
+        v2 = cxMapGet(map, "key2");
+        v3 = cxMapGet(map, "key3");
+
+        CX_TEST_ASSERTM(v1, "value for key1 not found");
+        CX_TEST_ASSERTM(v2, "value for key2 not found");
+        CX_TEST_ASSERTM(v3, "value for key3 not found");
+
+        CX_TEST_ASSERT(!strcmp(v1, "value1"));
+        CX_TEST_ASSERT(!strcmp(v2, "value2"));
+        CX_TEST_ASSERT(!strcmp(v3, "value3"));
+
+        cxMapFree(map);
+        cxPropertiesDestroy(&prop);
+
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
 CX_TEST(test_properties_multiple_fill) {
     const char *props1 = "key1 = value1\n";
     const char *props2 = "key2 = value2\n";
@@ -628,6 +686,7 @@
     cx_test_register(suite, test_properties_next_long_lines);
     cx_test_register(suite, test_properties_load_string_to_map);
     cx_test_register(suite, test_properties_load_file_to_map);
+    cx_test_register(suite, test_properties_load_incomplete);
     cx_test_register(suite, test_properties_multiple_fill);
     cx_test_register(suite, test_properties_use_stack);
     cx_test_register(suite, test_properties_empty_key);

mercurial