diff --git a/test/data/test-ini b/test/data/test-ini new file mode 100644 index 0000000..4afb1d2 --- /dev/null +++ b/test/data/test-ini @@ -0,0 +1,38 @@ +#General comment +[bool] + booltrue = true #This is a test inline comment + booltrue_capital = TRUE + + #This is a comment + boolfalse = false + boolfalse_capital = FALSE + + boolyes = yes + boolyes_capital = YES + + boolno = no + boolno_capital = NO + + boolbin0 = 0 + boolbin1 = 1 + + boolinvalid = invalidbool + +[string] + simple = A simple string + quoted = "A quoted string" + quoted_with_quotes = "A string "with quotes"" + +[int] + simple = 5 + negative = -10 + decimal = 2.71828 + leading_zeroes = 007 + multi_char = 1024 + +[double] + simple = 1 + decimal = 1.5 + negative = -1.2 + zeroes = 0.005 + long = 3.141592653589793 diff --git a/test/option_parser.c b/test/option_parser.c new file mode 100644 index 0000000..3a44b7a --- /dev/null +++ b/test/option_parser.c @@ -0,0 +1,226 @@ +#include "greatest.h" + +#include +#include + +#include "src/option_parser.h" + +TEST test_next_section(void) +{ + char *section = NULL; + ASSERT_STR_EQ("bool", (section = next_section(section))); + ASSERT_STR_EQ("string", (section = next_section(section))); + ASSERT_STR_EQ("int", (section = next_section(section))); + ASSERT_STR_EQ("double", (section = next_section(section))); + PASS(); +} + +TEST test_ini_get_bool(void) +{ + char *bool_section = "bool"; + ASSERT(ini_get_bool(bool_section, "booltrue", false)); + ASSERT(ini_get_bool(bool_section, "booltrue_capital", false)); + + ASSERT_FALSE(ini_get_bool(bool_section, "boolfalse", true)); + ASSERT_FALSE(ini_get_bool(bool_section, "boolfalse_capital", true)); + + ASSERT(ini_get_bool(bool_section, "boolyes", false)); + ASSERT(ini_get_bool(bool_section, "boolyes_capital", false)); + + ASSERT_FALSE(ini_get_bool(bool_section, "boolno", true)); + ASSERT_FALSE(ini_get_bool(bool_section, "boolno_capital", true)); + + ASSERT(ini_get_bool(bool_section, "boolbin1", false)); + ASSERT_FALSE(ini_get_bool(bool_section, "boolbin0", true)); + + ASSERT_FALSE(ini_get_bool(bool_section, "boolinvalid", false)); + + ASSERT(ini_get_bool(bool_section, "nonexistent", true)); + ASSERT_FALSE(ini_get_bool(bool_section, "nonexistent", false)); + PASS(); +} + +TEST test_ini_get_string(void) +{ + char *string_section = "string"; + ASSERT_STR_EQ("A simple string", ini_get_string(string_section, "simple", "")); + + ASSERT_STR_EQ("A quoted string", ini_get_string(string_section, "quoted", "")); + ASSERT_STR_EQ("A string \"with quotes\"", ini_get_string(string_section, "quoted_with_quotes", "")); + + ASSERT_STR_EQ("default value", ini_get_string(string_section, "nonexistent", "default value")); + + PASS(); +} + +TEST test_ini_get_int(void) +{ + char *int_section = "int"; + + ASSERT_EQ(5, ini_get_int(int_section, "simple", 0)); + ASSERT_EQ(-10, ini_get_int(int_section, "negative", 0)); + ASSERT_EQ(2, ini_get_int(int_section, "decimal", 0)); + ASSERT_EQ(7, ini_get_int(int_section, "leading_zeroes", 0)); + ASSERT_EQ(1024, ini_get_int(int_section, "multi_char", 0)); + + ASSERT_EQ(10, ini_get_int(int_section, "nonexistent", 10)); + PASS(); +} + +TEST test_ini_get_double(void) +{ + char *double_section = "double"; + ASSERT_EQ(1, ini_get_double(double_section, "simple", 0)); + ASSERT_EQ(1.5, ini_get_double(double_section, "decimal", 0)); + ASSERT_EQ(-1.2, ini_get_double(double_section, "negative", 0)); + ASSERT_EQ(0.005, ini_get_double(double_section, "zeroes", 0)); + ASSERT_EQ(3.141592653589793, ini_get_double(double_section, "long", 0)); + + ASSERT_EQ(10.5, ini_get_double(double_section, "nonexistent", 10.5)); + PASS(); +} + +TEST test_cmdline_get_string(void) +{ + ASSERT_STR_EQ("A simple string from the cmdline", cmdline_get_string("-string", "", "")); + ASSERT_STR_EQ("Single_word_string", cmdline_get_string("-str/-s", "", "")); + ASSERT_STR_EQ("Default", cmdline_get_string("-nonexistent", "Default", "")); + PASS(); +} + +TEST test_cmdline_get_int(void) +{ + ASSERT_EQ(3, cmdline_get_int("-int", 0, "")); + ASSERT_EQ(2, cmdline_get_int("-int2/-i", 0, "")); + ASSERT_EQ(-7, cmdline_get_int("-negative", 0, "")); + ASSERT_EQ(4, cmdline_get_int("-zeroes", 0, "")); + ASSERT_EQ(2, cmdline_get_int("-intdecim", 0, "")); + ASSERT_EQ(10, cmdline_get_int("-nonexistent", 10, "")); + PASS(); +} + +TEST test_cmdline_get_double(void) +{ + ASSERT_EQ(2, cmdline_get_double("-simple_double", 0, "")); + ASSERT_EQ(5.2, cmdline_get_double("-double", 0, "")); + ASSERT_EQ(3.14, cmdline_get_double("-nonexistent", 3.14, "")); + PASS(); +} + +TEST test_cmdline_get_bool(void) +{ + ASSERT(cmdline_get_bool("-bool", false, "")); + ASSERT(cmdline_get_bool("-shortbool/-b", false, "")); + ASSERT(cmdline_get_bool("-boolnd/-n", true, "")); + ASSERT_FALSE(cmdline_get_bool("-boolnd/-n", false, "")); + PASS(); +} + +TEST test_cmdline_create_usage(void) +{ + cmdline_get_string("-msgstring/-ms", "", "A string to test usage creation"); + cmdline_get_int("-msgint/-mi", 0, "An int to test usage creation"); + cmdline_get_double("-msgdouble/-md", 0, "A double to test usage creation"); + cmdline_get_bool("-msgbool/-mb", false, "A bool to test usage creation"); + char *usage = cmdline_create_usage(); + ASSERT_FALSE(strstr(usage, "-msgstring/-ms") == NULL); + ASSERT_FALSE(strstr(usage, "A string to test usage creation") == NULL); + ASSERT_FALSE(strstr(usage, "-msgint/-mi") == NULL); + ASSERT_FALSE(strstr(usage, "An int to test usage creation") == NULL); + ASSERT_FALSE(strstr(usage, "-msgdouble/-md") == NULL); + ASSERT_FALSE(strstr(usage, "A double to test usage creation") == NULL); + ASSERT_FALSE(strstr(usage, "-msgbool/-mb") == NULL); + ASSERT_FALSE(strstr(usage, "A bool to test usage creation") == NULL); + free(usage); + PASS(); +} + +TEST test_option_get_string(void) +{ + char *string_section = "string"; + + ASSERT_STR_EQ("A simple string", option_get_string(string_section, "simple", "-nonexistent", "", "")); + ASSERT_STR_EQ("Single_word_string", option_get_string(string_section, "simple", "-str/-s", "", "")); + ASSERT_STR_EQ("A simple string from the cmdline", option_get_string(string_section, "simple", "-string", "", "")); + ASSERT_STR_EQ("A simple string from the cmdline", option_get_string(string_section, "simple", "-string/-s", "", "")); + ASSERT_STR_EQ("Single_word_string", option_get_string(string_section, "simple", "-s", "", "")); + ASSERT_STR_EQ("Default", option_get_string(string_section, "nonexistent", "-nonexistent", "Default", "")); + PASS(); +} + +TEST test_option_get_int(void) +{ + char *int_section = "int"; + ASSERT_EQ(3, option_get_int(int_section, "negative", "-int", 0, "")); + ASSERT_EQ(2, option_get_int(int_section, "simple", "-int2/-i", 0, "")); + ASSERT_EQ(-7, option_get_int(int_section, "decimal", "-negative", 0, "")); + ASSERT_EQ(4, option_get_int(int_section, "simple", "-zeroes", 0, "")); + ASSERT_EQ(2, option_get_int(int_section, "simple", "-intdecim", 0, "")); + + ASSERT_EQ(5, option_get_int(int_section, "simple", "-nonexistent", 0, "")); + ASSERT_EQ(-10, option_get_int(int_section, "negative", "-nonexistent", 0, "")); + ASSERT_EQ(2, option_get_int(int_section, "decimal", "-nonexistent", 0, "")); + ASSERT_EQ(7, option_get_int(int_section, "leading_zeroes", "-nonexistent", 0, "")); + ASSERT_EQ(1024, option_get_int(int_section, "multi_char", "-nonexistent", 0, "")); + + ASSERT_EQ(3, option_get_int(int_section, "nonexistent", "-nonexistent", 3, "")); + PASS(); +} + +TEST test_option_get_double(void) +{ + char *double_section = "double"; + ASSERT_EQ(2, option_get_double(double_section, "simple", "-simple_double", 0, "")); + ASSERT_EQ(5.2, option_get_double(double_section, "simple", "-double", 0, "")); + ASSERT_EQ(0.005, option_get_double(double_section, "zeroes", "-nonexistent", 0, "")); + ASSERT_EQ(10.5, option_get_double(double_section, "nonexistent", "-nonexistent", 10.5, "")); + PASS(); +} + +TEST test_option_get_bool(void) +{ + char *bool_section = "bool"; + ASSERT(option_get_bool(bool_section, "boolfalse", "-bool/-b", false, "")); + ASSERT(option_get_bool(bool_section, "boolbin1", "-nonexistent", false, "")); + ASSERT_FALSE(option_get_bool(bool_section, "boolbin0", "-nonexistent", false, "")); + ASSERT_FALSE(option_get_bool(bool_section, "nonexistent", "-nonexistent", false, "")); + PASS(); +} + +SUITE(suite_option_parser) +{ + FILE *config_file = fopen("data/test-ini", "r"); + if (config_file == NULL) { + fputs("\nTest config file 'data/test-ini' couldn't be opened, failing.\n", stderr); + exit(1); + } + load_ini_file(config_file); + RUN_TEST(test_next_section); + RUN_TEST(test_ini_get_bool); + RUN_TEST(test_ini_get_string); + 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 " + "-int 3 -i 2 -negative -7 -zeroes 04 -intdecim 2.5 " + "-simple_double 2 -double 5.2" + ; + int argc; + char **argv; + g_shell_parse_argv(&cmdline[0], &argc, &argv, NULL); + cmdline_load(argc, argv); + RUN_TEST(test_cmdline_get_string); + RUN_TEST(test_cmdline_get_int); + RUN_TEST(test_cmdline_get_double); + RUN_TEST(test_cmdline_get_bool); + RUN_TEST(test_cmdline_create_usage); + + RUN_TEST(test_option_get_string); + RUN_TEST(test_option_get_int); + RUN_TEST(test_option_get_double); + RUN_TEST(test_option_get_bool); + free_ini(); + g_strfreev(argv); + fclose(config_file); +} +/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/test/test.c b/test/test.c index 0e22f25..6de63cd 100644 --- a/test/test.c +++ b/test/test.c @@ -1,12 +1,14 @@ #include "greatest.h" SUITE_EXTERN(suite_utils); +SUITE_EXTERN(suite_option_parser); GREATEST_MAIN_DEFS(); int main(int argc, char *argv[]) { GREATEST_MAIN_BEGIN(); RUN_SUITE(suite_utils); + RUN_SUITE(suite_option_parser); GREATEST_MAIN_END(); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */