summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDaniel Carl <danielcarl@gmx.de>2014-09-01 00:06:45 +0200
committerDaniel Carl <danielcarl@gmx.de>2014-09-01 00:41:52 +0200
commitef4c14cc9b1121d591e3c88cfb57a1e7b1688900 (patch)
tree85164864476b99c24badd2cc7e542b11edcc1eb6 /tests
parent84e8e88d1374291702fba050533de855a8426e32 (diff)
Added utility function for wildcard matching.
Diffstat (limited to 'tests')
-rw-r--r--tests/test-util.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/test-util.c b/tests/test-util.c
index 6dc26b4..cd80420 100644
--- a/tests/test-util.c
+++ b/tests/test-util.c
@@ -137,6 +137,54 @@ static void test_str_replace(void)
g_free(value);
}
+static void test_wildmatch_simple(void)
+{
+ g_assert_true(util_wildmatch("", ""));
+ g_assert_true(util_wildmatch("w", "w"));
+ g_assert_true(util_wildmatch(".", "."));
+ g_assert_true(util_wildmatch("~", "~"));
+ g_assert_true(util_wildmatch("wildmatch", "WildMatch"));
+ g_assert_true(util_wildmatch("wild\\match", "wild\\match"));
+
+ /* no special meaning of . and ~ in pattern */
+ g_assert_false(util_wildmatch(".", "w"));
+ g_assert_false(util_wildmatch("~", "w"));
+ g_assert_false(util_wildmatch("wild", "wild "));
+ g_assert_false(util_wildmatch("wild", " wild"));
+ g_assert_false(util_wildmatch("wild", "\\ wild"));
+ g_assert_false(util_wildmatch("wild", "\\wild"));
+ g_assert_false(util_wildmatch("wild", "wild\\"));
+ g_assert_false(util_wildmatch("wild\\1", "wild\\2"));
+}
+
+static void test_wildmatch_questionmark(void)
+{
+ g_assert_true(util_wildmatch("wild?atch", "wildmatch"));
+ g_assert_true(util_wildmatch("wild?atch", "wildBatch"));
+ g_assert_true(util_wildmatch("wild?atch", "wild?atch"));
+ g_assert_true(util_wildmatch("?ild?atch", "MildBatch"));
+ g_assert_true(util_wildmatch("foo\\?bar", "foo?bar"));
+ g_assert_true(util_wildmatch("???", "foo"));
+ g_assert_true(util_wildmatch("???", "bar"));
+
+ g_assert_false(util_wildmatch("foo\\?bar", "foorbar"));
+ g_assert_false(util_wildmatch("?", ""));
+}
+
+static void test_wildmatch_wildcard(void)
+{
+ g_assert_true(util_wildmatch("*", ""));
+ g_assert_true(util_wildmatch("*", "Match as much as possible"));
+ g_assert_true(util_wildmatch("*match", "prefix match"));
+ g_assert_true(util_wildmatch("match*", "match suffix"));
+ g_assert_true(util_wildmatch("match*", "match*"));
+ g_assert_true(util_wildmatch("match\\*", "match*"));
+ g_assert_true(util_wildmatch("do * match", "do a infix match"));
+ g_assert_true(util_wildmatch("*://*.io/*", "http://fanglingsu.github.io/vimb/"));
+
+ g_assert_false(util_wildmatch("match\\*", "match fail"));
+}
+
int main(int argc, char *argv[])
{
g_test_init(&argc, &argv, NULL);
@@ -148,6 +196,9 @@ int main(int argc, char *argv[])
g_test_add_func("/test-util/expand-spacial", test_expand_speacial);
g_test_add_func("/test-util/strcasestr", test_strcasestr);
g_test_add_func("/test-util/str_replace", test_str_replace);
+ g_test_add_func("/test-util/wildmatch-simple", test_wildmatch_simple);
+ g_test_add_func("/test-util/wildmatch-questionmark", test_wildmatch_questionmark);
+ g_test_add_func("/test-util/wildmatch-wildcard", test_wildmatch_wildcard);
return g_test_run();
}