]> git.mdlowis.com Git - archive/gapbuf.git/commitdiff
Added more unit tests
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 5 Mar 2018 21:20:30 +0000 (16:20 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 5 Mar 2018 21:20:30 +0000 (16:20 -0500)
.gitignore [new file with mode: 0644]
buf.c
testbuf.c

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..cba7efc
--- /dev/null
@@ -0,0 +1 @@
+a.out
diff --git a/buf.c b/buf.c
index 973a8c03758b44e5c96262d845b5188398bed7ad..5c453663b7f97e5b935d5c99b0f7065faaf5d93a 100644 (file)
--- a/buf.c
+++ b/buf.c
@@ -56,12 +56,15 @@ void buf_init(Buf* buf, void (*errfn)(char*)) {
     buf->undo        = NULL;
     buf->redo        = NULL;
     buf->errfn       = errfn;
+    buf->path        = NULL;
     assert(buf->bufstart);
 }
 
 void buf_load(Buf* buf, Sel* sel, char* path) {
     /* process the file path and address */
-    if (path && path[0] == '.' && path[1] == '/')
+    if (sel) *sel = (Sel){0};
+    if (!path) return;
+    if (path[0] == '.' && path[1] == '/')
         path += 2;
     buf->path = strdup(path);
     char* addr = strrchr(buf->path, ':');
@@ -84,7 +87,6 @@ void buf_load(Buf* buf, Sel* sel, char* path) {
         if (nread < 0) die("read() :");
     }
     if (fd > 0) close(fd);
-    if (sel) *sel = (Sel){0};
 }
 
 void buf_reload(Buf* buf) {
index 54b00d459b01bf695258c2b046492f994faf3c92..6788c65424b629cf047c2b3594cdf633a4dd05c7 100644 (file)
--- a/testbuf.c
+++ b/testbuf.c
@@ -47,6 +47,7 @@ TEST_SUITE(BufferTests) {
     TEST(buf_init should initialize an empty buffer) {
         Buf buf = {0};
         buf_init(&buf, (void*)0x12345678);
+        CHECK(buf.path        == NULL);
         CHECK(buf.modified    == false);
         CHECK(buf.expand_tabs == CfgExpandTabs);
         CHECK(buf.crlf        == 0);
@@ -65,6 +66,7 @@ TEST_SUITE(BufferTests) {
         buf_init(&buf, onerror);
         buf_putc(&buf, false, 0, 'a');
         buf_init(&buf, (void*)0x12345678);
+        CHECK(buf.path        == NULL);
         CHECK(buf.modified    == false);
         CHECK(buf.expand_tabs == CfgExpandTabs);
         CHECK(buf.crlf        == 0);
@@ -126,28 +128,25 @@ TEST_SUITE(BufferTests) {
         CHECK(!strcmp(TestBuf.path, "testdocs/lorem.txt"));
     }
 
-    TEST(buf_reload should reload the file from disk) {
+    TEST(buf_load should handle non-existent paths) {
         Sel sel;
         buf_init(&TestBuf, NULL);
-        buf_load(&TestBuf, &sel, "testdocs/waf");
-        TestBuf.path = "testdocs/lorem.txt";
-        buf_reload(&TestBuf);
+        buf_load(&TestBuf, &sel, "nonexistent.txt");
         CHECK(sel.end             == 0);
         CHECK(TestBuf.modified    == false);
         CHECK(TestBuf.expand_tabs == true);
         CHECK(TestBuf.crlf        == 0);
-        CHECK(TestBuf.bufsize     == 61440);
+        CHECK(TestBuf.bufsize     == sysconf(_SC_PAGE_SIZE));
         CHECK(TestBuf.undo        == NULL);
         CHECK(TestBuf.redo        == NULL);
         CHECK(TestBuf.errfn       == NULL);
-        CHECK(!strcmp(TestBuf.path, "testdocs/lorem.txt"));
+        CHECK(!strcmp(TestBuf.path, "nonexistent.txt"));
     }
 
-    TEST(buf_load should handle non-existent paths) {
+    TEST(buf_load should handle NULL for selection) {
         Sel sel;
         buf_init(&TestBuf, NULL);
-        buf_load(&TestBuf, &sel, "nonexistent.txt");
-        buf_reload(&TestBuf);
+        buf_load(&TestBuf, NULL, "nonexistent.txt");
         CHECK(sel.end             == 0);
         CHECK(TestBuf.modified    == false);
         CHECK(TestBuf.expand_tabs == true);
@@ -159,6 +158,38 @@ TEST_SUITE(BufferTests) {
         CHECK(!strcmp(TestBuf.path, "nonexistent.txt"));
     }
 
+    TEST(buf_load should handle NULL for path) {
+        Sel sel;
+        buf_init(&TestBuf, NULL);
+        buf_load(&TestBuf, NULL, NULL);
+        CHECK(sel.end             == 0);
+        CHECK(TestBuf.modified    == false);
+        CHECK(TestBuf.expand_tabs == true);
+        CHECK(TestBuf.crlf        == 0);
+        CHECK(TestBuf.bufsize     == sysconf(_SC_PAGE_SIZE));
+        CHECK(TestBuf.undo        == NULL);
+        CHECK(TestBuf.redo        == NULL);
+        CHECK(TestBuf.errfn       == NULL);
+        CHECK(TestBuf.path        == NULL);
+    }
+
+    TEST(buf_reload should reload the file from disk) {
+        Sel sel;
+        buf_init(&TestBuf, NULL);
+        buf_load(&TestBuf, &sel, "testdocs/waf");
+        TestBuf.path = "testdocs/lorem.txt";
+        buf_reload(&TestBuf);
+        CHECK(sel.end             == 0);
+        CHECK(TestBuf.modified    == false);
+        CHECK(TestBuf.expand_tabs == true);
+        CHECK(TestBuf.crlf        == 0);
+        CHECK(TestBuf.bufsize     == 61440);
+        CHECK(TestBuf.undo        == NULL);
+        CHECK(TestBuf.redo        == NULL);
+        CHECK(TestBuf.errfn       == NULL);
+        CHECK(!strcmp(TestBuf.path, "testdocs/lorem.txt"));
+    }
+
 #if 0
     /* Saving
      *************************************************************************/