From: Daniel Carl Date: Sat, 7 Jun 2014 23:49:12 +0000 (+0200) Subject: Added test for key mapping. X-Git-Url: https://git.owens.tech/git.owens.tech/git.owens.tech/git?a=commitdiff_plain;h=06ea1d658224545883b87ec903fbfac2653c45fa;p=vimb.git Added test for key mapping. --- diff --git a/Makefile b/Makefile index 7c4ab04..ff2cdc5 100644 --- a/Makefile +++ b/Makefile @@ -63,7 +63,7 @@ $(DTARGET): $(DOBJ) $(LIBTARGET): $(LOBJ) @echo "$(CC) tests/$@" - @$(CC) -shared ${LOBJ} -o ./tests/$(LIBTARGET) + @$(CC) -shared ${LOBJ} -o ./tests/$(LIBTARGET) $(LDFLAGS) src/config.h: @echo create $@ from src/config.def.h @@ -79,6 +79,6 @@ src/config.h: %.lo: %.c %.h @echo "${CC} $@" - @$(CC) $(CFLAGS) -fPIC -c -o $@ $< + @$(CC) -DTESTLIB $(DFLAGS) -fPIC -c -o $@ $< .PHONY: clean debug all install uninstall options dist test diff --git a/src/map.c b/src/map.c index e066d7d..dfaa96c 100644 --- a/src/map.c +++ b/src/map.c @@ -220,7 +220,9 @@ MapState map_handle_keys(const guchar *keys, int keylen, gboolean use_map) /* send the key to the parser */ if (RESULT_MORE != mode_handle_key((int)qk)) { +#ifndef TESTLIB normal_showcmd(0); +#endif } } @@ -243,6 +245,7 @@ MapState map_handle_keys(const guchar *keys, int keylen, gboolean use_map) /* find ambiguous matches */ if (!timeout && m->inlen > map.qlen && !strncmp(m->in, map.queue, map.qlen)) { +#ifndef TESTLIB if (ambiguous == 0) { /* show command chars for the ambiguous commands */ int i = map.qlen > SHOWCMD_LEN ? map.qlen - SHOWCMD_LEN : 0; @@ -254,6 +257,7 @@ MapState map_handle_keys(const guchar *keys, int keylen, gboolean use_map) normal_showcmd(map.queue[i++]); } } +#endif ambiguous++; } /* complete match or better/longer match than previous found */ @@ -280,7 +284,9 @@ MapState map_handle_keys(const guchar *keys, int keylen, gboolean use_map) /* flush ths show command to make room for possible mapped command * chars to show for example if :nmap foo 12g is use we want to * display the incomplete 12g command */ +#ifndef TESTLIB normal_showcmd(0); +#endif if (match->inlen < match->mappedlen) { /* make some space within the queue */ for (i = map.qlen + match->mappedlen - match->inlen, j = map.qlen; j > match->inlen; ) { diff --git a/src/mode.c b/src/mode.c index ed60ff8..7af00b6 100644 --- a/src/mode.c +++ b/src/mode.c @@ -88,7 +88,9 @@ void mode_enter(char id) new->enter(); } +#ifndef TESTLIB vb_update_statusbar(); +#endif } /** diff --git a/tests/Makefile b/tests/Makefile index 841cb1f..abe6b5b 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,9 +1,12 @@ include ../config.mk CPPFLAGS += -I ../ -CFLAGS += -fPIC +CFLAGS += -fPIC -Wpedantic -TEST_PROGS = test-util test-handlers test-shortcut +TEST_PROGS = test-handlers \ + test-map \ + test-shortcut \ + test-util all: $(TEST_PROGS) LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):." gtester --verbose $(TEST_PROGS) diff --git a/tests/test-map.c b/tests/test-map.c new file mode 100644 index 0000000..f34c680 --- /dev/null +++ b/tests/test-map.c @@ -0,0 +1,168 @@ +/** + * 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 +#include +#include +#include +#include + +static char queue[10]; /* receives the keypresses */ +static int qpos = 0; /* points to the queue entry for the next keypress */ + +#define QUEUE_APPEND(c) { \ + queue[qpos++] = (char)c; \ + queue[qpos] = '\0'; \ +} +#define QUEUE_CLEAR() {queue[(qpos = 0)] = '\0';} +#define TEST_HANDLE_STRING(in, expected) { \ + QUEUE_CLEAR(); \ + map_handle_string(in, true); \ + g_assert_cmpstr(queue, ==, expected); \ +} + +typedef struct { + guint state; + guint keyval; +} TestKeypress; + +VbResult keypress(int key) +{ + /* put the key into the queue */ + QUEUE_APPEND(key); + + return RESULT_COMPLETE; +} + +static void test_handle_string_simple(void) +{ + /* test simple mappings */ + TEST_HANDLE_STRING("a", "[a]"); + TEST_HANDLE_STRING("b", "[b]"); + TEST_HANDLE_STRING("", "[tab]"); + TEST_HANDLE_STRING("", "[shift-tab]"); + TEST_HANDLE_STRING("", "[ctrl-f]"); + TEST_HANDLE_STRING("", "[ctrl-f]"); + TEST_HANDLE_STRING("", "[cr]"); + TEST_HANDLE_STRING("foobar", "[baz]"); +} + +static void test_handle_string_alias(void) +{ + /* CTRL-I is the same like and CTRL-M like */ + TEST_HANDLE_STRING("", "[tab]"); + TEST_HANDLE_STRING("", "[cr]"); +} + +static void test_handle_string_multiple(void) +{ + /* test multiple mappings together */ + TEST_HANDLE_STRING("ba", "[b][a]"); + + /* incomplete ambiguous sequences are not matched jet */ + TEST_HANDLE_STRING("foob", ""); + TEST_HANDLE_STRING("ar", "[baz]"); +} + +static void test_handle_string_remapped(void) +{ + /* test multiple mappings together */ + TEST_HANDLE_STRING("ba", "[b][a]"); + + /* incomplete ambiguous sequences are not matched jet */ + TEST_HANDLE_STRING("foob", ""); + TEST_HANDLE_STRING("ar", "[baz]"); + + /* test remapping */ + map_insert("c", "baza", 't', true); + TEST_HANDLE_STRING("c", "[b][a]z[a]"); + map_insert("d", "cki", 't', true); + TEST_HANDLE_STRING("d", "[b][a]z[a]ki"); +} + +static void test_remove(void) +{ + map_insert("x", "[x]", 't', false); + /* make sure that the mapping works */ + TEST_HANDLE_STRING("x", "[x]"); + + map_delete("x", 't'); + + /* make sure the mapping removed */ + TEST_HANDLE_STRING("x", "x"); +} + +static void test_keypress_single(void) +{ + QUEUE_CLEAR(); + + map_keypress(NULL, &((GdkEventKey){.keyval = GDK_Tab, .state = GDK_SHIFT_MASK}), NULL); + g_assert_cmpstr(queue, ==, "[shift-tab]"); +} + +static void test_keypress_sequence(void) +{ + QUEUE_CLEAR(); + + map_keypress(NULL, &((GdkEventKey){.keyval = GDK_f}), NULL); + g_assert_cmpstr(queue, ==, ""); + map_keypress(NULL, &((GdkEventKey){.keyval = GDK_o}), NULL); + g_assert_cmpstr(queue, ==, ""); + map_keypress(NULL, &((GdkEventKey){.keyval = GDK_o}), NULL); + g_assert_cmpstr(queue, ==, ""); + map_keypress(NULL, &((GdkEventKey){.keyval = GDK_b}), NULL); + g_assert_cmpstr(queue, ==, ""); + map_keypress(NULL, &((GdkEventKey){.keyval = GDK_a}), NULL); + g_assert_cmpstr(queue, ==, ""); + map_keypress(NULL, &((GdkEventKey){.keyval = GDK_r}), NULL); + g_assert_cmpstr(queue, ==, "[baz]"); +} + +int main(int argc, char *argv[]) +{ + int result; + g_test_init(&argc, &argv, NULL); + + /* add a test mode to handle the maped sequences */ + mode_init(); + mode_add('t', NULL, NULL, keypress, NULL); + mode_enter('t'); + + g_test_add_func("/test-map/handle_string/simple", test_handle_string_simple); + g_test_add_func("/test-map/handle_string/alias", test_handle_string_alias); + g_test_add_func("/test-map/handle_string/multi", test_handle_string_multiple); + g_test_add_func("/test-map/handle_string/remapped", test_handle_string_remapped); + g_test_add_func("/test-map/remove", test_remove); + g_test_add_func("/test-map/keypress/single-char", test_keypress_single); + g_test_add_func("/test-map/keypress/sequence", test_keypress_sequence); + + /* add some mappings to test */ + map_insert("a", "[a]", 't', false); + map_insert("b", "[b]", 't', false); + map_insert("", "[tab]", 't', false); + map_insert("", "[shift-tab]", 't', false); + map_insert("", "[ctrl-f]", 't', false); + map_insert("", "[cr]", 't', false); + /* map long sequence to shorter result */ + map_insert("foobar", "[baz]", 't', false); + + result = g_test_run(); + map_cleanup(); + return result; +}