From: Michael D. Lowis Date: Fri, 3 Feb 2017 21:23:49 +0000 (-0500) Subject: Restructured code to better facilitate unit testing X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=3256a33c63c09c787f3ce5e47cd57d2909d386d3;p=projs%2Ftide.git Restructured code to better facilitate unit testing --- diff --git a/Makefile b/Makefile index 31a3bbc..0fb7c1a 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ LIBEDIT_OBJS = \ TEST_BINS = \ tests/xedit \ tests/xpick \ + tests/term \ tests/libedit include config.mk @@ -42,7 +43,7 @@ uninstall: rm -f $(PREFIX)/bin/edit test: $(TEST_BINS) - for t in $(TEST_BINS); do ./$$t; done + for t in $(TEST_BINS); do ./$$t || exit 1; done xedit: xedit.o libedit.a $(LD) -o $@ $^ $(LDFLAGS) @@ -61,6 +62,7 @@ libedit.a: $(LIBEDIT_OBJS) tests/libedit: tests/lib/buf.o tests/lib/utf8.o libedit.a tests/xedit: tests/xedit.o libedit.a tests/xpick: tests/xpick.o libedit.a +tests/term: tests/term.o libedit.a --include *.d lib/*.d tests/*.d +-include *.d lib/*.d tests/*.d tests/lib/*.d diff --git a/inc/x11.h b/inc/x11.h index 604d215..449ae2f 100644 --- a/inc/x11.h +++ b/inc/x11.h @@ -135,6 +135,7 @@ void x11_window(char* name, int width, int height); void x11_dialog(char* name, int height, int width); void x11_show(void); void x11_loop(void); +void x11_handle_events(void); XFont x11_font_load(char* name); size_t x11_font_height(XFont fnt); size_t x11_font_width(XFont fnt); diff --git a/lib/win.c b/lib/win.c index 2d0170f..a3d642f 100644 --- a/lib/win.c +++ b/lib/win.c @@ -49,10 +49,10 @@ void win_init(char* name) { x11_init(&Config); Font = x11_font_load(FONTNAME); x11_window(name, Width, Height); - x11_show(); } void win_loop(void) { + x11_show(); x11_loop(); } diff --git a/lib/x11.c b/lib/x11.c index b0b20f0..6e57eee 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -286,40 +286,44 @@ static void handle_mouse(XEvent* e) { Config->handle_mouse(action, button, x, y); } +void x11_handle_events(void) { + XEvent e; + Atom wmDeleteMessage = XInternAtom(X.display, "WM_DELETE_WINDOW", False); + while (XPending(X.display)) { + XNextEvent(X.display, &e); + if (!XFilterEvent(&e, None)) { + switch (e.type) { + case FocusIn: if (X.xic) XSetICFocus(X.xic); break; + case FocusOut: if (X.xic) XUnsetICFocus(X.xic); break; + case KeyPress: handle_key(&e); break; + case ButtonRelease: handle_mouse(&e); break; + case ButtonPress: handle_mouse(&e); break; + case MotionNotify: handle_mouse(&e); break; + case SelectionClear: selclear(&e); break; + case SelectionNotify: selnotify(&e); break; + case SelectionRequest: selrequest(&e); break; + case ClientMessage: + if (e.xclient.data.l[0] == wmDeleteMessage) + Config->shutdown(); + break; + case ConfigureNotify: // Resize the window + if (e.xconfigure.width != X.width || e.xconfigure.height != X.height) { + X.width = e.xconfigure.width; + X.height = e.xconfigure.height; + X.pixmap = XCreatePixmap(X.display, X.window, X.width, X.height, X.depth); + X.xft = XftDrawCreate(X.display, X.pixmap, X.visual, X.colormap); + } + break; + } + } + } +} + void x11_loop(void) { XEvent e; while (Running) { XPeekEvent(X.display,&e); - while (XPending(X.display)) { - XNextEvent(X.display, &e); - if (!XFilterEvent(&e, None)) { - switch (e.type) { - case FocusIn: if (X.xic) XSetICFocus(X.xic); break; - case FocusOut: if (X.xic) XUnsetICFocus(X.xic); break; - case KeyPress: handle_key(&e); break; - case ButtonRelease: handle_mouse(&e); break; - case ButtonPress: handle_mouse(&e); break; - case MotionNotify: handle_mouse(&e); break; - case SelectionClear: selclear(&e); break; - case SelectionNotify: selnotify(&e); break; - case SelectionRequest: selrequest(&e); break; - case ClientMessage: { - Atom wmDeleteMessage = XInternAtom(X.display, "WM_DELETE_WINDOW", False); - if (e.xclient.data.l[0] == wmDeleteMessage) - Config->shutdown(); - } - break; - case ConfigureNotify: // Resize the window - if (e.xconfigure.width != X.width || e.xconfigure.height != X.height) { - X.width = e.xconfigure.width; - X.height = e.xconfigure.height; - X.pixmap = XCreatePixmap(X.display, X.window, X.width, X.height, X.depth); - X.xft = XftDrawCreate(X.display, X.pixmap, X.visual, X.colormap); - } - break; - } - } - } + x11_handle_events(); if (Running) { /* redraw the window */ Config->redraw(X.width, X.height); diff --git a/term.c b/term.c index 8c5a877..3b96313 100644 --- a/term.c +++ b/term.c @@ -36,6 +36,7 @@ MouseConfig* MouseHandlers[NREGIONS] = { void onupdate(void) { } +#ifndef TEST int main(int argc, char** argv) { win_init("term"); //win_setkeys(&Bindings); @@ -43,6 +44,7 @@ int main(int argc, char** argv) { win_loop(); return 0; } +#endif diff --git a/tests/term.c b/tests/term.c new file mode 100644 index 0000000..43bfabe --- /dev/null +++ b/tests/term.c @@ -0,0 +1,15 @@ +#define INCLUDE_DEFS +#include +#include + +// Inculd the source file so we can access everything +#include "../term.c" + +TEST_SUITE(UnitTests) { +} + +int main(int argc, char** argv) { + atf_init(argc,argv); + RUN_TEST_SUITE(UnitTests); + return atf_print_results(); +} diff --git a/tests/xedit.c b/tests/xedit.c index 4158741..fe0a2a1 100644 --- a/tests/xedit.c +++ b/tests/xedit.c @@ -1,6 +1,7 @@ #define INCLUDE_DEFS #include #include +#include enum { LF = 0, @@ -8,19 +9,18 @@ enum { }; // Test Globals -int Mods = 0; int ExitCode = 0; -char* PrimaryText = NULL; -char* ClipboardText = NULL; - -// fake out the exit routine -void mockexit(int code) { - ExitCode = code; -} +jmp_buf ExitPad; // Inculd the source file so we can access everything #include "../xedit.c" +static void initialize(void) { + win_init("edit"); + win_setkeys(Bindings); + //win_setmouse(&MouseHandlers); +} + /* Helper Functions *****************************************************************************/ //void setup_view(enum RegionId id, char* text, int crlf, unsigned cursor) { @@ -52,44 +52,6 @@ void mockexit(int code) { // return result; //} -/* Stubbed Functions - *****************************************************************************/ -//bool x11_keymodsset(int mask) { -// return ((Mods & mask) == mask); -//} -// -//size_t x11_font_height(XFont fnt) { -// return 10; -//} -// -//size_t x11_font_width(XFont fnt) { -// return 10; -//} -// -//static void redraw(int width, int height) { -// /* do nothing for unit testing */ -//} -// -//void x11_deinit(void) { -// mockexit(0); -//} -// -//bool x11_getsel(int selid, void(*cbfn)(char*)) { -// cbfn(selid == PRIMARY ? PrimaryText : ClipboardText); -// return true; -//} -// -//bool x11_setsel(int selid, char* str) { -// if (selid == PRIMARY) { -// free(PrimaryText); -// PrimaryText = str; -// } else { -// free(ClipboardText); -// ClipboardText = str; -// } -// return true; -//} - /* Unit Tests *****************************************************************************/ TEST_SUITE(UnitTests) { @@ -901,7 +863,14 @@ TEST_SUITE(UnitTests) { // } } +// fake out the exit routine +void exit(int code) { + ExitCode = code; + longjmp(ExitPad, 0); +} + int main(int argc, char** argv) { + initialize(); atf_init(argc,argv); RUN_TEST_SUITE(UnitTests); return atf_print_results(); diff --git a/xedit.c b/xedit.c index 7e23119..8c09ccd 100644 --- a/xedit.c +++ b/xedit.c @@ -5,10 +5,6 @@ #include #include -#ifdef TEST -#define exit mockexit -#endif - typedef struct { char* tag; union {