From 0ab08602945bbead4ae83da61c334af8b370d4c0 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 6 Dec 2016 22:21:49 -0500 Subject: [PATCH] started writing unit integration tests --- Makefile | 7 ++- TODO.md | 1 + config.mk | 4 ++ libedit/view.c | 3 +- tests/xedit.c | 126 +++++++++++++++++++++++++++++++++++++++++-------- xedit.c | 8 ++-- 6 files changed, 123 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 60e5f1c..629cc91 100644 --- a/Makefile +++ b/Makefile @@ -24,11 +24,14 @@ all: xedit xpick clean: $(RM) *.o lib*/*.o tests/*.o *.a xpick xedit unittests + $(RM) *.gcno lib*/*.gcno tests/*.gcno + $(RM) *.gcda lib*/*.gcda tests/*.gcda + $(RM) *.gcov lib*/*.gcov tests/*.gcov install: all mkdir -p $(PREFIX)/bin - cp xedit $(PREFIX)/bin - cp xpick $(PREFIX)/bin + cp -f xedit $(PREFIX)/bin + cp -f xpick $(PREFIX)/bin cp xfilepick $(PREFIX)/bin cp xtagpick $(PREFIX)/bin cp xman $(PREFIX)/bin diff --git a/TODO.md b/TODO.md index f7b9bdf..ae3df7d 100644 --- a/TODO.md +++ b/TODO.md @@ -13,6 +13,7 @@ * Implement fuzzy file/buffer/tag picker * check for file changes when window regains focus * check for file changes on save +* backspace should delete indent if preceded by whitespace # Auxillary Programs diff --git a/config.mk b/config.mk index 33775f1..85d96ed 100644 --- a/config.mk +++ b/config.mk @@ -22,3 +22,7 @@ LIBS += -L/usr/X11/lib # Linux Freetype2 Flags INCS += -I/usr/include/freetype2 + +# Gcov Coverage +#CFLAGS += --coverage +#LDFLAGS += --coverage diff --git a/libedit/view.c b/libedit/view.c index fde73de..97b2324 100644 --- a/libedit/view.c +++ b/libedit/view.c @@ -183,6 +183,7 @@ void view_init(View* view, char* file) { free(view->rows); } buf_init(&(view->buffer)); + view->selection = (Sel){ 0 }; if (file) { view->selection.end = buf_load(&(view->buffer), file); view->selection.beg = view->selection.end; @@ -596,4 +597,4 @@ void view_indent(View* view, int dir) { } off = buf_byline(buf, off, UP); } -} \ No newline at end of file +} diff --git a/tests/xedit.c b/tests/xedit.c index d9015a6..8abfffe 100644 --- a/tests/xedit.c +++ b/tests/xedit.c @@ -5,10 +5,13 @@ int Mods = 0; /* Helper Functions *****************************************************************************/ -void setup_view(enum RegionId id, char* text) { +void setup_view(enum RegionId id, char* text, unsigned cursor) { Focused = id; view_init(getview(id), NULL); view_putstr(getview(id), text); + getsel(id)->beg = cursor; + getsel(id)->end = cursor; + getsel(id)->col = buf_getcol(getbuf(id), getsel(id)->end); } /* Stubbed Functions @@ -32,53 +35,138 @@ static void redraw(int width, int height) { /* Unit Tests *****************************************************************************/ TEST_SUITE(XeditTests) { - /* Key Handling - Cursor Movement + /* Key Handling - Cursor Movement - Basic *************************************************************************/ - TEST(left arrow should do nothing for empty buffer) { - setup_view(EDIT, ""); + TEST(left should do nothing for empty buffer) { + setup_view(EDIT, "", 0); key_handler(ModNone, KEY_LEFT); CHECK(getsel(EDIT)->beg == 0); CHECK(getsel(EDIT)->end == 0); - CHECK(getsel(EDIT)->col == 0); } - TEST(right arrow should do nothing for empty buffer) { - setup_view(EDIT, ""); + TEST(left should do nothing at beginning of buffer) { + setup_view(EDIT, "AB", 0); + key_handler(ModNone, KEY_LEFT); + CHECK(getsel(EDIT)->beg == 0); + CHECK(getsel(EDIT)->end == 0); + } + + TEST(left should move left by one rune) { + setup_view(EDIT, "AB", 1); + key_handler(ModNone, KEY_LEFT); + CHECK(getsel(EDIT)->beg == 0); + CHECK(getsel(EDIT)->end == 0); + } + + TEST(right should do nothing for empty buffer) { + setup_view(EDIT, "", 0); key_handler(ModNone, KEY_RIGHT); CHECK(getsel(EDIT)->beg == 0); CHECK(getsel(EDIT)->end == 0); - CHECK(getsel(EDIT)->col == 0); } - TEST(up arrow should do nothing for empty buffer) { - setup_view(EDIT, ""); + TEST(right should do nothing at end of buffer) { + setup_view(EDIT, "AB", 2); + key_handler(ModNone, KEY_RIGHT); + CHECK(getsel(EDIT)->beg == 2); + CHECK(getsel(EDIT)->end == 2); + } + + TEST(right should move right by one rune) { + setup_view(EDIT, "AB", 1); + key_handler(ModNone, KEY_RIGHT); + CHECK(getsel(EDIT)->beg == 2); + CHECK(getsel(EDIT)->end == 2); + } + + TEST(up should do nothing for empty buffer) { + setup_view(EDIT, "", 0); key_handler(ModNone, KEY_UP); CHECK(getsel(EDIT)->beg == 0); CHECK(getsel(EDIT)->end == 0); - CHECK(getsel(EDIT)->col == 0); } - TEST(down arrow should do nothing for empty buffer) { - setup_view(EDIT, ""); + TEST(up should move cursor up one line) { + setup_view(EDIT, "AB\nCD", 4); + key_handler(ModNone, KEY_UP); + CHECK(getsel(EDIT)->beg == 1); + CHECK(getsel(EDIT)->end == 1); + } + + TEST(up should do nothing for first line) { + setup_view(EDIT, "AB\nCD", 1); + key_handler(ModNone, KEY_UP); + CHECK(getsel(EDIT)->beg == 1); + CHECK(getsel(EDIT)->end == 1); + } + + TEST(down should do nothing for empty buffer) { + setup_view(EDIT, "", 0); key_handler(ModNone, KEY_DOWN); CHECK(getsel(EDIT)->beg == 0); CHECK(getsel(EDIT)->end == 0); - CHECK(getsel(EDIT)->col == 0); + } + + TEST(down should move down one line) { + setup_view(EDIT, "AB\nCD", 1); + key_handler(ModNone, KEY_DOWN); + CHECK(getsel(EDIT)->beg == 4); + CHECK(getsel(EDIT)->end == 4); + } + + TEST(down should do nothing on last line) { + setup_view(EDIT, "AB\nCD", 4); + key_handler(ModNone, KEY_DOWN); + CHECK(getsel(EDIT)->beg == 4); + CHECK(getsel(EDIT)->end == 4); } TEST(home should do nothing for empty buffer) { - setup_view(EDIT, ""); + setup_view(EDIT, "", 0); key_handler(ModNone, KEY_HOME); CHECK(getsel(EDIT)->beg == 0); CHECK(getsel(EDIT)->end == 0); - CHECK(getsel(EDIT)->col == 0); + } + + TEST(home should move to beginning of indented content) { + setup_view(EDIT, " ABCD", 7); + key_handler(ModNone, KEY_HOME); + CHECK(getsel(EDIT)->beg == 4); + CHECK(getsel(EDIT)->end == 4); + } + + TEST(home should move to beginning of line if at beginning of indented content) { + setup_view(EDIT, " ABCD", 7); + key_handler(ModNone, KEY_HOME); + CHECK(getsel(EDIT)->beg == 4); + CHECK(getsel(EDIT)->end == 4); + } + + TEST(home should move to beginning of indented content when at beginning of line) { + setup_view(EDIT, " ABCD", 0); + key_handler(ModNone, KEY_HOME); + CHECK(getsel(EDIT)->beg == 4); + CHECK(getsel(EDIT)->end == 4); } TEST(end should do nothing for empty buffer) { - setup_view(EDIT, ""); + setup_view(EDIT, "", 0); key_handler(ModNone, KEY_END); CHECK(getsel(EDIT)->beg == 0); CHECK(getsel(EDIT)->end == 0); - CHECK(getsel(EDIT)->col == 0); } -} \ No newline at end of file + + TEST(end should do nothing at end of line) { + setup_view(EDIT, "AB\nCD", 2); + key_handler(ModNone, KEY_END); + CHECK(getsel(EDIT)->beg == 2); + CHECK(getsel(EDIT)->end == 2); + } + + TEST(end should move to end of line) { + setup_view(EDIT, "AB\nCD", 0); + key_handler(ModNone, KEY_END); + CHECK(getsel(EDIT)->beg == 2); + CHECK(getsel(EDIT)->end == 2); + } +} diff --git a/xedit.c b/xedit.c index 2161177..e7b7648 100644 --- a/xedit.c +++ b/xedit.c @@ -142,10 +142,10 @@ static KeyBinding Bindings[] = { { ModCtrl, ']', add_indent }, /* Common Special Keys */ - { ModNone, KEY_PGUP, page_up }, - { ModNone, KEY_PGDN, page_dn }, - { ModAny, KEY_DELETE, delete }, - { ModAny, KEY_BACKSPACE, backspace }, + { ModNone, KEY_PGUP, page_up }, + { ModNone, KEY_PGDN, page_dn }, + { ModAny, KEY_DELETE, delete }, + { ModAny, KEY_BACKSPACE, backspace }, /* Cursor Movements */ { ModAny, KEY_HOME, cursor_home }, -- 2.49.0