diff --git a/src/notification.c b/src/notification.c index 18be083..2b34e10 100644 --- a/src/notification.c +++ b/src/notification.c @@ -522,14 +522,8 @@ int notification_init(notification *n, int id) char *tmp = g_strconcat(n->summary, " ", n->body, NULL); char *tmp_urls = extract_urls(tmp); - if (tmp_urls != NULL) { - if (n->urls != NULL) { - n->urls = string_append(n->urls, tmp_urls, "\n"); - g_free(tmp_urls); - } else { - n->urls = tmp_urls; - } - } + n->urls = string_append(n->urls, tmp_urls, "\n"); + g_free(tmp_urls); if (n->actions) { n->actions->dmenu_str = NULL; diff --git a/src/utils.c b/src/utils.c index d3e5787..d838c53 100644 --- a/src/utils.c +++ b/src/utils.c @@ -76,8 +76,12 @@ char *string_replace_all(const char *needle, const char *replacement, char *string_append(char *a, const char *b, const char *sep) { - if (!a) + if (!a || *a == '\0') { + g_free(a); return g_strdup(b); + } + if (!b || *b == '\0') + return a; char *new; if (!sep) diff --git a/test/utils.c b/test/utils.c index 84dc344..f0b7969 100644 --- a/test/utils.c +++ b/test/utils.c @@ -72,7 +72,35 @@ TEST test_string_replace(void) TEST test_string_append(void) { - SKIP(); //TODO: Implement this + char *exp; + + ASSERT_STR_EQ("text_sep_bit", (exp = string_append(g_strdup("text"), "bit", "_sep_"))); + g_free(exp); + ASSERT_STR_EQ("textbit", (exp = string_append(g_strdup("text"), "bit", NULL))); + g_free(exp); + ASSERT_STR_EQ("textbit", (exp = string_append(g_strdup("text"), "bit", ""))); + g_free(exp); + + ASSERT_STR_EQ("text", (exp = string_append(g_strdup("text"), "", NULL))); + g_free(exp); + ASSERT_STR_EQ("text", (exp = string_append(g_strdup("text"), "", "_sep_"))); + g_free(exp); + + ASSERT_STR_EQ("b", (exp = string_append(g_strdup(""), "b", NULL))); + g_free(exp); + ASSERT_STR_EQ("b", (exp = string_append(NULL, "b", "_sep_"))); + g_free(exp); + + ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), "", NULL))); + g_free(exp); + ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), NULL, "_sep_"))); + g_free(exp); + + ASSERT_STR_EQ("", (exp = string_append(g_strdup(""), "", "_sep_"))); + g_free(exp); + ASSERT_EQ(NULL, (exp = string_append(NULL, NULL, "_sep_"))); + g_free(exp); + PASS(); }