]> git.mdlowis.com Git - projs/tide.git/commitdiff
Restructured code to better facilitate unit testing
authorMichael D. Lowis <mike.lowis@gentex.com>
Fri, 3 Feb 2017 21:23:49 +0000 (16:23 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Fri, 3 Feb 2017 21:23:49 +0000 (16:23 -0500)
Makefile
inc/x11.h
lib/win.c
lib/x11.c
term.c
tests/term.c [new file with mode: 0644]
tests/xedit.c
xedit.c

index 31a3bbcdee7ab245843c583213bf085f8ed2d61a..0fb7c1aec9e460dd130786fadc1691c995caf22a 100644 (file)
--- 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
 
index 604d2153c0554196d7480038d5df6d94bc37f5a6..449ae2fd69de8bf763d69d4f9be49cd46cbcda10 100644 (file)
--- 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);
index 2d0170f124188185c755122531c7ad5690e133ee..a3d642fbd2e96a2b2135b105eaf8473afe50b15d 100644 (file)
--- a/lib/win.c
+++ b/lib/win.c
@@ -49,10 +49,10 @@ void win_init(char* name) {
     x11_init(&Config);\r
     Font = x11_font_load(FONTNAME);\r
     x11_window(name, Width, Height);\r
-    x11_show();\r
 }\r
 \r
 void win_loop(void) {\r
+    x11_show();\r
     x11_loop();\r
 }\r
 \r
index b0b20f04a3c6a5d7bb2055354af02d4645417df2..6e57eee66f3b210fbc5f3720326627c6c4900149 100644 (file)
--- 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 8c5a877c45b46f576e0a854e7d6a3dd7e48d552b..3b96313ead625125d1dffb2acc09503d5494218f 100644 (file)
--- 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 (file)
index 0000000..43bfabe
--- /dev/null
@@ -0,0 +1,15 @@
+#define INCLUDE_DEFS
+#include <atf.h>
+#include <time.h>
+
+// 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();
+}
index 4158741713cc7274d8aa8c07990f6a9f1f1f83dc..fe0a2a171c1d04f8e808422286bdc5669a3be1ab 100644 (file)
@@ -1,6 +1,7 @@
 #define INCLUDE_DEFS
 #include <atf.h>
 #include <time.h>
+#include <setjmp.h>
 
 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 7e2311934a044a8ac5fcbf7f8e810acb973fb2ae..8c09ccdb1d4716703c34c93592cdc666dabd9c57 100644 (file)
--- a/xedit.c
+++ b/xedit.c
@@ -5,10 +5,6 @@
 #include <ctype.h>
 #include <win.h>
 
-#ifdef TEST
-#define exit mockexit
-#endif
-
 typedef struct {
     char* tag;
     union {