Merge pull request #555 from anbenson/master

Add tests for extract_urls
This commit is contained in:
Benedikt Heine 2018-12-29 13:54:09 +01:00 committed by GitHub
commit a359b74901
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 18 deletions

View File

@ -63,41 +63,32 @@ void regex_teardown(void)
} }
} }
/* /* see menu.h */
* Extract all urls from a given string.
*
* Return: a string of urls separated by \n
*
*/
char *extract_urls(const char *to_match) char *extract_urls(const char *to_match)
{ {
char *urls = NULL; if (!to_match)
return NULL;
if (!regex_init()) if (!regex_init())
return NULL; return NULL;
char *urls = NULL;
const char *p = to_match; const char *p = to_match;
regmatch_t m; regmatch_t m;
while (1) { while (1) {
int nomatch = regexec(&url_regex, p, 1, &m, 0); int nomatch = regexec(&url_regex, p, 1, &m, 0);
if (nomatch) {
return urls; if (nomatch || m.rm_so == -1)
}
int start;
int finish;
if (m.rm_so == -1) {
break; break;
}
start = m.rm_so + (p - to_match); int start = m.rm_so + (p - to_match);
finish = m.rm_eo + (p - to_match); int finish = m.rm_eo + (p - to_match);
char *match = g_strndup(to_match + start, finish - start); char *match = g_strndup(to_match + start, finish - start);
urls = string_append(urls, match, "\n"); urls = string_append(urls, match, "\n");
g_free(match); g_free(match);
p += m.rm_eo; p += m.rm_eo;
} }
return urls; return urls;

View File

@ -2,7 +2,14 @@
#ifndef DUNST_MENU_H #ifndef DUNST_MENU_H
#define 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); char *extract_urls(const char *to_match);
void open_browser(const char *in); void open_browser(const char *in);
void invoke_action(const char *action); void invoke_action(const char *action);
void regex_teardown(void); void regex_teardown(void);

74
test/menu.c Normal file
View File

@ -0,0 +1,74 @@
#include "../src/menu.c"
#include "greatest.h"
#include <glib.h>
TEST test_extract_urls_from_empty_string(void)
{
char *urls = extract_urls("");
ASSERT_EQ_FMT(NULL, (void*)urls, "%p");
urls = extract_urls(NULL);
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(!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();
}
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)
{
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);
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: */

View File

@ -18,6 +18,7 @@ SUITE_EXTERN(suite_icon);
SUITE_EXTERN(suite_queues); SUITE_EXTERN(suite_queues);
SUITE_EXTERN(suite_dunst); SUITE_EXTERN(suite_dunst);
SUITE_EXTERN(suite_log); SUITE_EXTERN(suite_log);
SUITE_EXTERN(suite_menu);
GREATEST_MAIN_DEFS(); GREATEST_MAIN_DEFS();
@ -42,6 +43,7 @@ int main(int argc, char *argv[]) {
RUN_SUITE(suite_queues); RUN_SUITE(suite_queues);
RUN_SUITE(suite_dunst); RUN_SUITE(suite_dunst);
RUN_SUITE(suite_log); RUN_SUITE(suite_log);
RUN_SUITE(suite_menu);
GREATEST_MAIN_END(); GREATEST_MAIN_END();
base = NULL; base = NULL;