Wed, 21 Feb 2024 18:53:55 +0100
add xml test case for the tree iterator
closes #371
tests/test_tree.c | file | annotate | diff | comparison | revisions |
--- a/tests/test_tree.c Wed Feb 21 18:32:38 2024 +0100 +++ b/tests/test_tree.c Wed Feb 21 18:53:55 2024 +0100 @@ -388,6 +388,83 @@ } } +typedef struct test_xml_node { + struct test_xml_node *parent; + struct test_xml_node *next; + struct test_xml_node *prev; + struct test_xml_node *children; + char const* name; +} test_xml_node; + +CX_TEST(test_tree_iterator_xml) { + test_xml_node project = {0}; + test_xml_node config = {0}; + test_xml_node var1 = {0}; + test_xml_node var2 = {0}; + test_xml_node var3 = {0}; + test_xml_node dependency1 = {0}; + test_xml_node dependency1make = {0}; + test_xml_node dependency2 = {0}; + test_xml_node dependency2lang = {0}; + test_xml_node dependency2make = {0}; + test_xml_node target = {0}; + test_xml_node target_feature = {0}; + test_xml_node target_feature_dependencies = {0}; + test_xml_node target_dependencies = {0}; + + project.name = "project"; + config.name = "config"; + var1.name = "var"; + var2.name = "var"; + var3.name = "var"; + dependency1.name = "dependency"; + dependency1make.name = "make"; + dependency2.name = "dependency"; + dependency2lang.name = "lang"; + dependency2make.name = "make"; + target.name = "target"; + target_feature.name = "feature"; + target_dependencies.name = "dependencies"; + target_feature_dependencies.name = "dependencies"; + + cx_tree_link(&project, &target, tree_node_layout); + cx_tree_link(&project, &dependency2, tree_node_layout); + cx_tree_link(&project, &dependency1, tree_node_layout); + cx_tree_link(&project, &config, tree_node_layout); + cx_tree_link(&config, &var3, tree_node_layout); + cx_tree_link(&config, &var2, tree_node_layout); + cx_tree_link(&config, &var1, tree_node_layout); + cx_tree_link(&dependency1, &dependency1make, tree_node_layout); + cx_tree_link(&dependency2, &dependency2make, tree_node_layout); + cx_tree_link(&dependency2, &dependency2lang, tree_node_layout); + cx_tree_link(&target, &target_dependencies, tree_node_layout); + cx_tree_link(&target, &target_feature, tree_node_layout); + cx_tree_link(&target_feature, &target_feature_dependencies, tree_node_layout); + + char const *expected = + "<project><config><var></var><var></var><var></var></config>" + "<dependency><make></make></dependency><dependency><lang></lang><make></make></dependency>" + "<target><feature><dependencies></dependencies></feature><dependencies></dependencies></target></project>"; + char *actual = malloc(512); + CX_TEST_DO { + CxTreeIterator iter = cx_tree_iterator(&project, true, tree_child_list); + size_t i = 0; + cx_foreach(test_xml_node*, node, iter) { + size_t len = strlen(node->name); + actual[i++] = '<'; + if (iter.exiting) { + actual[i++] = '/'; + } + memcpy(actual+i, node->name, len); + i += len; + actual[i++] = '>'; + } + actual[i] = '\0'; + CX_TEST_ASSERT(0 == strcmp(expected, actual)); + } + free(actual); +} + CxTestSuite *cx_test_suite_tree_low_level(void) { CxTestSuite *suite = cx_test_suite_new("tree (low level)"); @@ -399,6 +476,7 @@ cx_test_register(suite, test_tree_iterator_create_and_dispose); cx_test_register(suite, test_tree_iterator_basic_only_enter); cx_test_register(suite, test_tree_iterator_basic_enter_and_exit); + cx_test_register(suite, test_tree_iterator_xml); return suite; }