tests: Add tests for string_matches_one_of()

Writing this test I'm unsure whether a function like
string_matches_one_of() should actually call cons_show().
Let's just note that for later.
This commit is contained in:
Michael Vetter
2026-02-24 20:00:57 +01:00
parent a7be9f1280
commit b7d53adba6
3 changed files with 60 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
#include "common.h"
#include "prof_cmocka.h"
#include <stdlib.h>
#include "tests/unittests/ui/stub_ui.h" // Include for mocking cons_show
void
replace_one_substr(void** state)
@@ -1041,3 +1042,60 @@ prof_whole_occurrences_tests(void** state)
g_slist_free(expected);
expected = NULL;
}
void test_string_matches_one_of_edge_cases(void** state) {
// is is NULL, is_can_be_null is TRUE -> should return TRUE
assert_true(string_matches_one_of(NULL, NULL, TRUE, "option1", "option2", NULL));
// is is NULL, is_can_be_null is FALSE -> should return FALSE
assert_false(string_matches_one_of(NULL, NULL, FALSE, "option1", "option2", NULL));
// is matches one of the options
assert_true(string_matches_one_of("Test", "option1", FALSE, "option1", "option2", NULL));
assert_true(string_matches_one_of("Test", "option2", FALSE, "option1", "option2", NULL));
// is does not match any of the options
expect_any_cons_show(); // For "Invalid Test: 'option3'"
expect_any_cons_show(); // For "Test must be one of: 'option1', or 'option2'."
assert_false(string_matches_one_of("Test", "option3", FALSE, "option1", "option2", NULL));
// what is NULL (no error message printed)
assert_false(string_matches_one_of(NULL, "option3", FALSE, "option1", "option2", NULL));
// Empty options list (first is NULL)
expect_any_cons_show(); // For "Invalid Test: 'option1'"
expect_any_cons_show(); // For "Test must be one of: ." (empty options list error message)
assert_false(string_matches_one_of("Test", "option1", FALSE, NULL, NULL));
assert_false(string_matches_one_of(NULL, NULL, FALSE, NULL, NULL));
assert_true(string_matches_one_of(NULL, NULL, TRUE, NULL, NULL));
// Single option, matches
assert_true(string_matches_one_of("Test", "single", FALSE, "single", NULL));
// Single option, no match
expect_any_cons_show(); // For "Invalid Test: 'nomatch'"
expect_any_cons_show(); // For "Test must be one of: 'single'."
assert_false(string_matches_one_of("Test", "nomatch", FALSE, "single", NULL));
// Multiple options, first matches
assert_true(string_matches_one_of("Test", "first", FALSE, "first", "second", "third", NULL));
// Multiple options, middle matches
assert_true(string_matches_one_of("Test", "second", FALSE, "first", "second", "third", NULL));
// Multiple options, last matches
assert_true(string_matches_one_of("Test", "third", FALSE, "first", "second", "third", NULL));
// Multiple options, no match
expect_any_cons_show(); // For "Invalid Test: 'none'"
expect_any_cons_show(); // For "Test must be one of: 'first', 'second', or 'third'."
assert_false(string_matches_one_of("Test", "none", FALSE, "first", "second", "third", NULL));
// is is an empty string, options are not
expect_any_cons_show(); // For "Invalid Test: ''"
expect_any_cons_show(); // For "Test must be one of: 'option1', or 'option2'."
assert_false(string_matches_one_of("Test", "", FALSE, "option1", "option2", NULL));
// is is an empty string, one of the options is an empty string
assert_true(string_matches_one_of("Test", "", FALSE, "option1", "", "option2", NULL));
}

View File

@@ -41,3 +41,4 @@ void test_strtoi_range_out_of_range(void** state);
void test_strtoi_range_invalid_input(void** state);
void test_strtoi_range_null_empty_input(void** state);
void test_strtoi_range_null_err_msg(void** state);
void test_string_matches_one_of_edge_cases(void** state);

View File

@@ -101,6 +101,7 @@ main(int argc, char* argv[])
cmocka_unit_test(test_strtoi_range_invalid_input),
cmocka_unit_test(test_strtoi_range_null_empty_input),
cmocka_unit_test(test_strtoi_range_null_err_msg),
cmocka_unit_test(test_string_matches_one_of_edge_cases),
cmocka_unit_test(clear_empty),
cmocka_unit_test(reset_after_create),