diff --git a/lib/common/tests/xml_element/Makefile.am b/lib/common/tests/xml_element/Makefile.am index 33a33e5481..70d005fca5 100644 --- a/lib/common/tests/xml_element/Makefile.am +++ b/lib/common/tests/xml_element/Makefile.am @@ -1,24 +1,25 @@ # # Copyright 2024 the Pacemaker project contributors # # The version control history for this file may have further details. # # This source code is licensed under the GNU General Public License version 2 # or later (GPLv2+) WITHOUT ANY WARRANTY. # include $(top_srcdir)/mk/common.mk include $(top_srcdir)/mk/tap.mk include $(top_srcdir)/mk/unittest.mk # Add "_test" to the end of all test program names to simplify .gitignore check_PROGRAMS = \ pcmk__xe_copy_attrs_test \ pcmk__xe_first_child_test \ pcmk__xe_foreach_child_test \ pcmk__xe_get_score_test \ + pcmk__xe_next_test \ pcmk__xe_set_id_test \ pcmk__xe_set_score_test \ pcmk__xe_sort_attrs_test TESTS = $(check_PROGRAMS) diff --git a/lib/common/tests/xml_element/pcmk__xe_next_test.c b/lib/common/tests/xml_element/pcmk__xe_next_test.c new file mode 100644 index 0000000000..a57dce43bc --- /dev/null +++ b/lib/common/tests/xml_element/pcmk__xe_next_test.c @@ -0,0 +1,93 @@ +/* + * Copyright 2024 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU General Public License version 2 + * or later (GPLv2+) WITHOUT ANY WARRANTY. + */ + +#include + +#include +#include +#include + +static void +null_xml(void **state) +{ + assert_null(pcmk__xe_next(NULL, NULL)); + assert_null(pcmk__xe_next(NULL, "test")); +} + + +#define XML_NO_SIBLINGS \ + "\n" \ + " " \ + " text" \ + " " \ + "" + +static void +no_siblings(void **state) +{ + xmlNode *xml = pcmk__xml_parse(XML_NO_SIBLINGS); + xmlNode *child = NULL; + + assert_non_null(xml); + + child = pcmk__xe_first_child(xml, NULL, NULL, NULL); + assert_non_null(child); + assert_string_equal(pcmk__xe_id(child), "child1"); + + assert_null(pcmk__xe_next(child, NULL)); + assert_null(pcmk__xe_next(child, "foo")); + + pcmk__xml_free(xml); +} + +#define XML_SIBLINGS \ + "\n" \ + " " \ + " text" \ + " " \ + " text" \ + " " \ + " text" \ + "" + +static void +with_siblings(void **state) +{ + xmlNode *xml = pcmk__xml_parse(XML_SIBLINGS); + xmlNode *child = NULL; + xmlNode *next = NULL; + + assert_non_null(xml); + + child = pcmk__xe_first_child(xml, NULL, NULL, NULL); + assert_non_null(child); + assert_string_equal(pcmk__xe_id(child), "child1"); + + next = pcmk__xe_next(child, NULL); + assert_non_null(next); + assert_string_equal(pcmk__xe_id(next), "child2"); + + next = pcmk__xe_next(child, "bar"); + assert_non_null(next); + assert_string_equal(pcmk__xe_id(next), "child2"); + + next = pcmk__xe_next(child, "foo"); + assert_non_null(next); + assert_string_equal(pcmk__xe_id(next), "child3"); + + next = pcmk__xe_next(child, "foobar"); + assert_null(next); + + pcmk__xml_free(xml); +} + +PCMK__UNIT_TEST(pcmk__xml_test_setup_group, pcmk__xml_test_teardown_group, + cmocka_unit_test(null_xml), + cmocka_unit_test(no_siblings), + cmocka_unit_test(with_siblings));