From 65bcce652c5b8670fa93c502ec6d51f7059386a0 Mon Sep 17 00:00:00 2001 From: Andrew Benson Date: Tue, 30 Oct 2018 22:09:32 -0700 Subject: [PATCH 1/4] Add tests for extract_urls --- test/menu.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ test/test.c | 2 ++ 2 files changed, 49 insertions(+) create mode 100644 test/menu.c diff --git a/test/menu.c b/test/menu.c new file mode 100644 index 0000000..7c8f373 --- /dev/null +++ b/test/menu.c @@ -0,0 +1,47 @@ +#include "../src/menu.c" + +#include "greatest.h" + +#include + +TEST test_extract_urls_from_empty_string(void) +{ + char *urls = extract_urls(""); + ASSERT_EQ_FMT(NULL, (void*)urls, "%p"); + g_free(urls); + PASS(); +} + +TEST test_extract_urls_from_no_urls_string(void) +{ + char *urls = extract_urls("You got a new message from your friend"); + ASSERT_EQ_FMT(NULL, (void*)urls, "%p"); + g_free(urls); + PASS(); +} + +TEST test_extract_urls_from_one_url_string(void) +{ + char *urls = extract_urls("Hi from https://www.example.com!"); + ASSERT_STR_EQ("https://www.example.com", urls); + g_free(urls); + PASS(); +} + +TEST test_extract_urls_from_two_url_string(void) +{ + char *urls = extract_urls("Hi from https://www.example.com and ftp://www.example.com!"); + ASSERT_STR_EQ("https://www.example.com\nftp://www.example.com", urls); + g_free(urls); + PASS(); +} + + +SUITE(suite_menu) +{ + RUN_TEST(test_extract_urls_from_empty_string); + RUN_TEST(test_extract_urls_from_no_urls_string); + RUN_TEST(test_extract_urls_from_one_url_string); + RUN_TEST(test_extract_urls_from_two_url_string); +} +/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/test/test.c b/test/test.c index 71645fc..1863070 100644 --- a/test/test.c +++ b/test/test.c @@ -18,6 +18,7 @@ SUITE_EXTERN(suite_icon); SUITE_EXTERN(suite_queues); SUITE_EXTERN(suite_dunst); SUITE_EXTERN(suite_log); +SUITE_EXTERN(suite_menu); GREATEST_MAIN_DEFS(); @@ -42,6 +43,7 @@ int main(int argc, char *argv[]) { RUN_SUITE(suite_queues); RUN_SUITE(suite_dunst); RUN_SUITE(suite_log); + RUN_SUITE(suite_menu); GREATEST_MAIN_END(); base = NULL; From db350883cc2afcbd9774d38731093c5215cc7497 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Wed, 31 Oct 2018 15:03:33 +0100 Subject: [PATCH 2/4] Improve extract_urls --- src/menu.c | 27 +++++++++------------------ src/menu.h | 7 +++++++ test/menu.c | 3 +++ 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/menu.c b/src/menu.c index e427f29..6c97919 100644 --- a/src/menu.c +++ b/src/menu.c @@ -63,41 +63,32 @@ void regex_teardown(void) } } -/* - * Extract all urls from a given string. - * - * Return: a string of urls separated by \n - * - */ +/* see menu.h */ char *extract_urls(const char *to_match) { - char *urls = NULL; + if (!to_match) + return NULL; if (!regex_init()) return NULL; + char *urls = NULL; const char *p = to_match; regmatch_t m; while (1) { int nomatch = regexec(&url_regex, p, 1, &m, 0); - if (nomatch) { - return urls; - } - int start; - int finish; - if (m.rm_so == -1) { + + if (nomatch || m.rm_so == -1) break; - } - start = m.rm_so + (p - to_match); - finish = m.rm_eo + (p - to_match); + + int start = m.rm_so + (p - to_match); + int finish = m.rm_eo + (p - to_match); char *match = g_strndup(to_match + start, finish - start); - urls = string_append(urls, match, "\n"); g_free(match); - p += m.rm_eo; } return urls; diff --git a/src/menu.h b/src/menu.h index b93f78b..c58ed4d 100644 --- a/src/menu.h +++ b/src/menu.h @@ -2,7 +2,14 @@ #ifndef DUNST_MENU_H #define DUNST_MENU_H +/** + * Extract all urls from the given string. + * + * @param to_match (nullable) String to extract URLs + * @return a string of urls separated by '\n' + */ char *extract_urls(const char *to_match); + void open_browser(const char *in); void invoke_action(const char *action); void regex_teardown(void); diff --git a/test/menu.c b/test/menu.c index 7c8f373..93fd4c0 100644 --- a/test/menu.c +++ b/test/menu.c @@ -8,6 +8,9 @@ TEST test_extract_urls_from_empty_string(void) { char *urls = extract_urls(""); ASSERT_EQ_FMT(NULL, (void*)urls, "%p"); + + urls = extract_urls(NULL); + ASSERT_EQ_FMT(NULL, (void*)urls, "%p"); g_free(urls); PASS(); } From d40b42c77d65347871beeee7baf9afa9e138e086 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Thu, 27 Dec 2018 20:44:12 +0100 Subject: [PATCH 3/4] Add additional tests for extract_urls --- test/menu.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/menu.c b/test/menu.c index 93fd4c0..b578ae1 100644 --- a/test/menu.c +++ b/test/menu.c @@ -39,6 +39,29 @@ TEST test_extract_urls_from_two_url_string(void) PASS(); } +TEST test_extract_urls_from_one_url_port(void) +{ + char *urls = extract_urls("Hi from https://www.example.com:8100 and have a nice day!"); + ASSERT_STR_EQ("https://www.example.com:8100", urls); + g_free(urls); + PASS(); +} + +TEST test_extract_urls_from_one_url_path(void) +{ + char *urls = extract_urls("Hi from https://www.example.com:8100/testpath and have a nice day!"); + ASSERT_STR_EQ("https://www.example.com:8100/testpath", urls); + g_free(urls); + PASS(); +} + +TEST test_extract_urls_from_one_url_anchor(void) +{ + char *urls = extract_urls("Hi from https://www.example.com:8100/testpath#anchor and have a nice day!"); + ASSERT_STR_EQ("https://www.example.com:8100/testpath#anchor", urls); + g_free(urls); + PASS(); +} SUITE(suite_menu) { @@ -46,5 +69,8 @@ SUITE(suite_menu) RUN_TEST(test_extract_urls_from_no_urls_string); RUN_TEST(test_extract_urls_from_one_url_string); RUN_TEST(test_extract_urls_from_two_url_string); + RUN_TEST(test_extract_urls_from_one_url_port); + RUN_TEST(test_extract_urls_from_one_url_path); + RUN_TEST(test_extract_urls_from_one_url_anchor); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ From f4b8fff89d9c69c023eed2f42357e6378b025cb9 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Sat, 29 Dec 2018 13:50:35 +0100 Subject: [PATCH 4/4] Simplify pointer test cases --- test/menu.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/menu.c b/test/menu.c index b578ae1..2d1ae00 100644 --- a/test/menu.c +++ b/test/menu.c @@ -10,16 +10,14 @@ TEST test_extract_urls_from_empty_string(void) ASSERT_EQ_FMT(NULL, (void*)urls, "%p"); urls = extract_urls(NULL); - ASSERT_EQ_FMT(NULL, (void*)urls, "%p"); - g_free(urls); + ASSERT(!urls); PASS(); } TEST test_extract_urls_from_no_urls_string(void) { char *urls = extract_urls("You got a new message from your friend"); - ASSERT_EQ_FMT(NULL, (void*)urls, "%p"); - g_free(urls); + ASSERT(!urls); PASS(); }