Added test for shortcut feature.
authorDaniel Carl <danielcarl@gmx.de>
Sat, 7 Jun 2014 13:16:20 +0000 (15:16 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Sat, 7 Jun 2014 13:16:20 +0000 (15:16 +0200)
src/shortcut.c
tests/Makefile
tests/test-shortcut.c [new file with mode: 0644]

index 0ba1f8c..240b0b5 100644 (file)
@@ -134,24 +134,21 @@ static int get_max_placeholder(const char *str)
 }
 
 /**
- * Retrieves the shortcut uri template for given string.
- * If the string contains the shortcut key the shortcut for this wee be
- * returned, else the default shortcur uri well be returned.
- * In given query pointer will be filled with the query part of the string,
- * thats the string without a possible shortcut key.
+ * Retrieves the shortcut uri template for given string. And fills given query
+ * pointer with the query part of the given string (everithing except of the
+ * shortcut identifier).
  */
 static const char *shortcut_lookup(const char *string, const char **query)
 {
     char *p, *uri = NULL;
 
     if ((p = strchr(string, ' '))) {
-        *p = '\0';
-        /* is the first word the key? */
-        if ((uri = g_hash_table_lookup(shortcuts, string))) {
+        char *key  = g_strndup(string, p - string);
+        /* is the first word might be a shortcut */
+        if ((uri = g_hash_table_lookup(shortcuts, key))) {
             *query = p + 1;
-        } else {
-            *p = ' ';
         }
+        g_free(key);
     }
 
     if (!uri && default_key && (uri = g_hash_table_lookup(shortcuts, default_key))) {
index 7b26bb2..841cb1f 100644 (file)
@@ -3,7 +3,7 @@ include ../config.mk
 CPPFLAGS += -I ../
 CFLAGS   += -fPIC
 
-TEST_PROGS = test-util test-handlers
+TEST_PROGS = test-util test-handlers test-shortcut
 
 all: $(TEST_PROGS)
        LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):." gtester --verbose $(TEST_PROGS)
diff --git a/tests/test-shortcut.c b/tests/test-shortcut.c
new file mode 100644 (file)
index 0000000..159c9a4
--- /dev/null
@@ -0,0 +1,99 @@
+/**
+ * vimb - a webkit based vim like browser.
+ *
+ * Copyright (C) 2012-2014 Daniel Carl
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/.
+ */
+
+#include <gtk/gtk.h>
+#include <src/shortcut.h>
+
+static void test_shortcut_single(void)
+{
+    char *uri;
+
+    /* call with shortcut identifier */
+    uri = shortcut_get_uri("_vimb1_ zero one");
+    g_assert_cmpstr(uri, ==, "only-zero:zero%20one");
+    g_free(uri);
+}
+
+static void test_shortcut_default(void)
+{
+    char *uri;
+
+    /* call without shortcut identifier and if the last placeholder become the
+     * none matched query words */
+    uri = shortcut_get_uri("zero one two three");
+    g_assert_cmpstr(uri, ==, "default:zero-two%20three");
+    g_free(uri);
+}
+
+static void test_shortcut_keep_unmatched(void)
+{
+    char *uri;
+
+    /* don't remove non matched placeholders */
+    uri = shortcut_get_uri("zero");
+    g_assert_cmpstr(uri, ==, "default:zero-$2");
+    g_free(uri);
+}
+
+static void test_shortcut_fullrange(void)
+{
+    char *uri;
+
+    /* check if all placeholders $0-$9 are replaced */
+    uri = shortcut_get_uri("_vimb3_ zero one two three four five six seven eight nine");
+    g_assert_cmpstr(uri, ==, "fullrange:zero-one-nine");
+    g_free(uri);
+}
+
+static void test_shortcut_remove(void)
+{
+    char *uri;
+
+    g_assert_true(shortcut_remove("_vimb4_"));
+
+    /* check if the shortcut is really no used */
+    uri = shortcut_get_uri("_vimb4_ test");
+    g_assert_cmpstr(uri, ==, "default:_vimb4_-$2");
+}
+
+int main(int argc, char *argv[])
+{
+    int result;
+    shortcut_init();
+
+    g_assert_true(shortcut_add("_vimb1_", "only-zero:$0"));
+    g_assert_true(shortcut_add("_vimb2_", "default:$0-$2"));
+    g_assert_true(shortcut_add("_vimb3_", "fullrange:$0-$1-$9"));
+    g_assert_true(shortcut_add("_vimb4_", "for-remove:$0"));
+    g_assert_true(shortcut_set_default("_vimb2_"));
+
+    g_test_init(&argc, &argv, NULL);
+
+    g_test_add_func("/test-shortcut/get_uri/single", test_shortcut_single);
+    g_test_add_func("/test-shortcut/get_uri/default", test_shortcut_default);
+    g_test_add_func("/test-shortcut/get_uri/keep-unmatched", test_shortcut_keep_unmatched);
+    g_test_add_func("/test-shortcut/get_uri/fullrange", test_shortcut_fullrange);
+    g_test_add_func("/test-shortcut/remove", test_shortcut_remove);
+
+    result = g_test_run();
+
+    shortcut_cleanup();
+
+    return result;
+}