From 7eb99ea64d6f85db085c8bb0bf4e2197f94bca29 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Sat, 5 Aug 2017 15:16:47 +0200 Subject: [PATCH] add tests for *_get_path functions --- test/data/test-ini | 3 ++ test/option_parser.c | 77 ++++++++++++++++++++++++++++++++++++++++++++ test/utils.c | 29 +++++++++++++++++ 3 files changed, 109 insertions(+) diff --git a/test/data/test-ini b/test/data/test-ini index 4afb1d2..9d2f7cd 100644 --- a/test/data/test-ini +++ b/test/data/test-ini @@ -23,6 +23,9 @@ quoted = "A quoted string" quoted_with_quotes = "A string "with quotes"" +[path] + expand_tilde = ~/.path/to/tilde + [int] simple = 5 negative = -10 diff --git a/test/option_parser.c b/test/option_parser.c index 8147136..45d6451 100644 --- a/test/option_parser.c +++ b/test/option_parser.c @@ -10,6 +10,7 @@ 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("path", (section = next_section(section))); ASSERT_STR_EQ("int", (section = next_section(section))); ASSERT_STR_EQ("double", (section = next_section(section))); PASS(); @@ -59,6 +60,27 @@ TEST test_ini_get_string(void) PASS(); } +TEST test_ini_get_path(void) +{ + char *section = "path"; + char *ptr, *exp; + char *home = getenv("HOME"); + + // return default, if nonexistent key + ASSERT_EQ(NULL, (ptr = ini_get_path(section, "nonexistent", NULL))); + ASSERT_STR_EQ("default", (ptr = ini_get_path(section, "nonexistent", "default"))); + g_free(ptr); + + // return path with replaced home + ASSERT_STR_EQ((exp = g_strconcat(home, "/.path/to/tilde", NULL)), + (ptr = ini_get_path(section, "expand_tilde", NULL))); + g_free(ptr); + g_free(exp); + + PASS(); +} + + TEST test_ini_get_int(void) { char *int_section = "int"; @@ -86,6 +108,25 @@ TEST test_ini_get_double(void) PASS(); } +TEST test_cmdline_get_path(void) +{ + char *ptr, *exp; + char *home = getenv("HOME"); + + // return default, if nonexistent key + ASSERT_EQ(NULL, (ptr = cmdline_get_path("-nonexistent", NULL, "desc"))); + ASSERT_STR_EQ("default", (ptr = cmdline_get_path("-nonexistent", "default", "desc"))); + g_free(ptr); + + // return path with replaced home + ASSERT_STR_EQ((exp = g_strconcat(home, "/path/from/cmdline", NULL)), + (ptr = cmdline_get_path("-path", NULL, "desc"))); + g_free(ptr); + g_free(exp); + + PASS(); +} + TEST test_cmdline_get_string(void) { char *ptr; @@ -165,6 +206,38 @@ TEST test_option_get_string(void) PASS(); } +TEST test_option_get_path(void) +{ + char *section = "path"; + char *ptr, *exp; + char *home = getenv("HOME"); + + // invalid ini, invalid cmdline + ASSERT_EQ(NULL, (ptr = option_get_path(section, "nonexistent", "-nonexistent", NULL, "desc"))); + ASSERT_STR_EQ("default", (ptr = option_get_path(section, "nonexistent", "-nonexistent", "default", "desc"))); + free(ptr); + + // valid ini, invalid cmdline + ASSERT_STR_EQ((exp = g_strconcat(home, "/.path/to/tilde", NULL)), + (ptr = option_get_path(section, "expand_tilde", "-nonexistent", NULL, "desc"))); + g_free(exp); + g_free(ptr); + + // valid ini, valid cmdline + ASSERT_STR_EQ((exp = g_strconcat(home, "/path/from/cmdline", NULL)), + (ptr = option_get_path(section, "expand_tilde", "-path", NULL, "desc"))); + g_free(exp); + g_free(ptr); + + // invalid ini, valid cmdline + ASSERT_STR_EQ((exp = g_strconcat(home, "/path/from/cmdline", NULL)), + (ptr = option_get_path(section, "nonexistent", "-path", NULL, "desc"))); + g_free(exp); + g_free(ptr); + + PASS(); +} + TEST test_option_get_int(void) { char *int_section = "int"; @@ -215,11 +288,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_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 " "-int 3 -i 2 -negative -7 -zeroes 04 -intdecim 2.5 " + "-path ~/path/from/cmdline " "-simple_double 2 -double 5.2" ; int argc; @@ -227,12 +302,14 @@ 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_path); 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_path); RUN_TEST(test_option_get_int); RUN_TEST(test_option_get_double); RUN_TEST(test_option_get_bool); diff --git a/test/utils.c b/test/utils.c index 509bf78..84dc344 100644 --- a/test/utils.c +++ b/test/utils.c @@ -1,6 +1,8 @@ #include "greatest.h" #include "src/utils.h" +#include + TEST test_string_replace_char(void) { char *text = malloc(128 * sizeof(char)); @@ -102,6 +104,32 @@ TEST test_string_strip_delimited(void) PASS(); } +TEST test_string_to_path(void) +{ + char *ptr, *exp; + char *home = getenv("HOME"); + + exp = "/usr/local/bin/script"; + ASSERT_STR_EQ(exp, (ptr = string_to_path(g_strdup(exp)))); + free(ptr); + + exp = "~path/with/wrong/tilde"; + ASSERT_STR_EQ(exp, (ptr = string_to_path(g_strdup(exp)))); + free(ptr); + + ASSERT_STR_EQ((exp = g_strconcat(home, "/.path/with/tilde", NULL)), + (ptr = string_to_path(g_strdup("~/.path/with/tilde")))); + free(exp); + free(ptr); + + ASSERT_STR_EQ((exp = g_strconcat(home, "/.path/with/tilde and some space", NULL)), + (ptr = string_to_path(g_strdup("~/.path/with/tilde and some space")))); + free(exp); + free(ptr); + + PASS(); +} + SUITE(suite_utils) { RUN_TEST(test_string_replace_char); @@ -109,5 +137,6 @@ SUITE(suite_utils) RUN_TEST(test_string_replace); RUN_TEST(test_string_append); RUN_TEST(test_string_strip_delimited); + RUN_TEST(test_string_to_path); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */