From a4a1a1ac9ee33b3e553958410a3fb539c2ff7cb8 Mon Sep 17 00:00:00 2001 From: Michael Krasnitski Date: Sun, 5 Apr 2020 03:25:05 -0400 Subject: [PATCH] Add tests for parsing lists from conf/cmdline. --- test/data/test-ini | 10 +++++ test/option_parser.c | 93 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/test/data/test-ini b/test/data/test-ini index 6201cd7..639c05c 100644 --- a/test/data/test-ini +++ b/test/data/test-ini @@ -27,6 +27,16 @@ unquoted_comment = String with a # comment color_comment = "#ffffff" # comment +[list] + simple = A,simple,list + spaces = A, list, with, spaces + multiword = A list, with, multiword entries + quoted = "A, quoted, list" + quoted_with_quotes = "A, list, "with quotes"" + unquoted_with_quotes = A, list, "with quotes" + quoted_comment = "List, with, a" # comment + unquoted_comment = List, with, a # comment + [path] expand_tilde = ~/.path/to/tilde diff --git a/test/option_parser.c b/test/option_parser.c index f399c26..b058706 100644 --- a/test/option_parser.c +++ b/test/option_parser.c @@ -8,6 +8,7 @@ TEST test_next_section(void) const char *section = NULL; ASSERT_STR_EQ("bool", (section = next_section(section))); ASSERT_STR_EQ("string", (section = next_section(section))); + ASSERT_STR_EQ("list", (section = next_section(section))); ASSERT_STR_EQ("path", (section = next_section(section))); ASSERT_STR_EQ("int", (section = next_section(section))); ASSERT_STR_EQ("double", (section = next_section(section))); @@ -68,6 +69,55 @@ TEST test_ini_get_string(void) PASS(); } +enum greatest_test_res ARRAY_EQ(char **a, char **b){ + ASSERT(a); + ASSERT(b); + int i = 0; + while (a[i] && b[i]){ + ASSERT_STR_EQ(a[i], b[i]); + i++; + } + ASSERT_FALSE(a[i]); + ASSERT_FALSE(b[i]); + PASS(); +} + +TEST test_ini_get_list(void) +{ + char *list_section = "list"; + + char *cmp1[] = {"A", "simple", "list", NULL}; + char *cmp2[] = {"A", "list", "with", "spaces", NULL}; + char *cmp3[] = {"A list", "with", "multiword entries", NULL}; + char *cmp4[] = {"A", "quoted", "list", NULL}; + char *cmp5[] = {"A", "list", "\"with quotes\"", NULL}; + char *cmp6[] = {"List", "with", "a", NULL}; + + char **ptr; + CHECK_CALL(ARRAY_EQ(cmp1, (ptr = ini_get_list(list_section, "simple", "")))); + free_string_array(ptr); + + CHECK_CALL(ARRAY_EQ(cmp2, (ptr = ini_get_list(list_section, "spaces", "")))); + free_string_array(ptr); + + CHECK_CALL(ARRAY_EQ(cmp3, (ptr = ini_get_list(list_section, "multiword", "")))); + free_string_array(ptr); + CHECK_CALL(ARRAY_EQ(cmp4, (ptr = ini_get_list(list_section, "quoted", "")))); + free_string_array(ptr); + + CHECK_CALL(ARRAY_EQ(cmp5, (ptr = ini_get_list(list_section, "quoted_with_quotes", "")))); + free_string_array(ptr); + CHECK_CALL(ARRAY_EQ(cmp5, (ptr = ini_get_list(list_section, "unquoted_with_quotes", "")))); + free_string_array(ptr); + + CHECK_CALL(ARRAY_EQ(cmp6, (ptr = ini_get_list(list_section, "quoted_comment", "")))); + free_string_array(ptr); + CHECK_CALL(ARRAY_EQ(cmp6, (ptr = ini_get_list(list_section, "unquoted_comment", "")))); + free_string_array(ptr); + + PASS(); +} + TEST test_ini_get_path(void) { char *section = "path"; @@ -151,6 +201,22 @@ TEST test_cmdline_get_string(void) PASS(); } +TEST test_cmdline_get_list(void) +{ + char **ptr; + char *cmp1[] = {"A", "simple", "list", "from", "the", "cmdline", NULL}; + char *cmp2[] = {"A", "list", "with", "spaces", NULL}; + char *cmp3[] = {"A", "default", "list", NULL}; + + CHECK_CALL(ARRAY_EQ(cmp1, (ptr = cmdline_get_list("-list", "", "")))); + free_string_array(ptr); + CHECK_CALL(ARRAY_EQ(cmp2, (ptr = cmdline_get_list("-list2", "", "")))); + free_string_array(ptr); + CHECK_CALL(ARRAY_EQ(cmp3, (ptr = cmdline_get_list("-nonexistent", "A, default, list", "")))); + free_string_array(ptr); + PASS(); +} + TEST test_cmdline_get_int(void) { ASSERT_EQ(3, cmdline_get_int("-int", 0, "")); @@ -221,6 +287,29 @@ TEST test_option_get_string(void) PASS(); } +TEST test_option_get_list(void) +{ + char *list_section = "list"; + char **ptr; + + char *cmp1[] = {"A", "simple", "list", NULL}; + char *cmp2[] = {"A", "list", "with", "spaces", NULL}; + char *cmp3[] = {"A", "simple", "list", "from", "the", "cmdline", NULL}; + char *cmp4[] = {"A", "default", "list", NULL}; + + CHECK_CALL(ARRAY_EQ(cmp1, (ptr = option_get_list(list_section, "simple", "-nonexistent", "", "")))); + free_string_array(ptr); + CHECK_CALL(ARRAY_EQ(cmp2, (ptr = option_get_list(list_section, "quoted", "-list2", "", "")))); + free_string_array(ptr); + CHECK_CALL(ARRAY_EQ(cmp3, (ptr = option_get_list(list_section, "simple", "-list", "", "")))); + free_string_array(ptr); + CHECK_CALL(ARRAY_EQ(cmp3, (ptr = option_get_list(list_section, "simple", "-list/-l", "", "")))); + free_string_array(ptr); + CHECK_CALL(ARRAY_EQ(cmp4, (ptr = option_get_list(list_section, "nonexistent", "-nonexistent", "A, default, list", "")))); + free_string_array(ptr); + PASS(); +} + TEST test_option_get_path(void) { char *section = "path"; @@ -308,11 +397,13 @@ SUITE(suite_option_parser) RUN_TEST(test_next_section); RUN_TEST(test_ini_get_bool); RUN_TEST(test_ini_get_string); + RUN_TEST(test_ini_get_list); RUN_TEST(test_ini_get_path); RUN_TEST(test_ini_get_int); RUN_TEST(test_ini_get_double); char cmdline[] = "dunst -bool -b " "-string \"A simple string from the cmdline\" -s Single_word_string " + "-list A,simple,list,from,the,cmdline -list2 \"A, list, with, spaces\" " "-int 3 -i 2 -negative -7 -zeroes 04 -intdecim 2.5 " "-path ~/path/from/cmdline " "-simple_double 2 -double 5.2" @@ -322,6 +413,7 @@ SUITE(suite_option_parser) g_shell_parse_argv(&cmdline[0], &argc, &argv, NULL); cmdline_load(argc, argv); RUN_TEST(test_cmdline_get_string); + RUN_TEST(test_cmdline_get_list); RUN_TEST(test_cmdline_get_path); RUN_TEST(test_cmdline_get_int); RUN_TEST(test_cmdline_get_double); @@ -329,6 +421,7 @@ SUITE(suite_option_parser) RUN_TEST(test_cmdline_create_usage); RUN_TEST(test_option_get_string); + RUN_TEST(test_option_get_list); RUN_TEST(test_option_get_path); RUN_TEST(test_option_get_int); RUN_TEST(test_option_get_double);