Browse Source

added unit tests for several util functions

prozessorkern 5 years ago
parent
commit
d65765371b

+ 2 - 2
example/main.c

@@ -88,7 +88,7 @@ shellmatta_retCode_t writeFct(const char* data, uint32_t length)
 
 
 int main(void)
 int main(void)
 {
 {
-   static char buffer[1024];
+    static char buffer[1024];
     static char historyBuffer[4096];
     static char historyBuffer[4096];
     static shellmatta_instance_t instance;
     static shellmatta_instance_t instance;
 
 
@@ -96,7 +96,7 @@ int main(void)
 //    raw();
 //    raw();
 //    keypad(stdscr, TRUE);
 //    keypad(stdscr, TRUE);
 //    noecho();
 //    noecho();
-    f = open("/dev/pts/1", O_RDWR | O_SYNC);
+    f = open("/dev/pts/2", O_RDWR | O_SYNC);
 
 
     if (f < 0)
     if (f < 0)
     {
     {

+ 10 - 4
makefile

@@ -20,10 +20,16 @@ SOURCES :=  src/shellmatta.c                \
             
             
 INCLUDES    := api .
 INCLUDES    := api .
 
 
-TEST_SOURCES := test/test_main.cpp                      \
-                test/shellmatta_utils/itoa.cpp          \
-                test/shellmatta_utils/clearInput.cpp    \
-                test/shellmatta_utils/insertChars.cpp
+TEST_SOURCES := test/test_main.cpp                                      \
+                test/shellmatta_utils/test_utils_writeEcho.cpp          \
+                test/shellmatta_utils/test_utils_shellItoa.cpp          \
+                test/shellmatta_utils/test_utils_saveCursorPos.cpp      \
+                test/shellmatta_utils/test_utils_restoreCursorPos.cpp   \
+                test/shellmatta_utils/test_utils_eraseLine.cpp          \
+                test/shellmatta_utils/test_utils_rewindCursor.cpp       \
+                test/shellmatta_utils/test_utils_forwardCursor.cpp      \
+                test/shellmatta_utils/test_utils_clearInput.cpp         \
+                test/shellmatta_utils/test_utils_insertChars.cpp
 
 
 TEST_CPPOBJ  := $(patsubst %.cpp,$(OBJ_DIR)%.o,$(TEST_SOURCES))
 TEST_CPPOBJ  := $(patsubst %.cpp,$(OBJ_DIR)%.o,$(TEST_SOURCES))
 
 

test/shellmatta_utils/clearInput.cpp → test/shellmatta_utils/test_utils_clearInput.cpp


+ 41 - 0
test/shellmatta_utils/test_utils_eraseLine.cpp

@@ -0,0 +1,41 @@
+#include "test/framework/catch.hpp"
+#include "src/shellmatta_utils.c"
+#include <string.h>
+
+static uint32_t write_callCnt = 0u;
+static char write_data[10];
+static uint32_t write_length;
+
+static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
+{
+    write_callCnt ++;
+    strncpy(write_data, data, length);
+    write_length = length;
+    return SHELLMATTA_OK;
+}
+
+
+TEST_CASE( "shellmatta_utils_eraseLine" ) {
+
+    shellmatta_instance_t inst;
+    char buffer[20];
+    char dummyData[29];
+
+    inst.buffer = buffer;
+    inst.bufferSize = 20;
+    inst.cursor = 10;
+    inst.inputCount = 10;
+    inst.echoEnabled = true;
+
+    inst.write = writeFct;
+
+    write_callCnt = 0u;
+    memset(write_data, 0, sizeof(write_data));
+    write_length = 0u;
+
+    utils_eraseLine(&inst);
+
+    CHECK( write_callCnt == 1u);
+    CHECK( write_length == 3u);
+    REQUIRE( strncmp("\e[K", write_data, 3u) == 0);
+}

+ 117 - 0
test/shellmatta_utils/test_utils_forwardCursor.cpp

@@ -0,0 +1,117 @@
+#include "test/framework/catch.hpp"
+#include "src/shellmatta_utils.c"
+#include <string.h>
+
+static uint32_t write_callCnt = 0u;
+static char write_data[10];
+static uint32_t write_length;
+
+static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
+{
+    write_callCnt ++;
+    strncpy(write_data, data, length);
+    write_length = length;
+    return SHELLMATTA_OK;
+}
+
+
+TEST_CASE( "shellmatta_utils_forwardCursor normal" ) {
+
+    shellmatta_instance_t inst;
+    char buffer[20];
+    char dummyData[29];
+
+    inst.buffer = buffer;
+    inst.bufferSize = 20;
+    inst.cursor = 10;
+    inst.inputCount = 20;
+    inst.echoEnabled = true;
+
+    inst.write = writeFct;
+
+    write_callCnt = 0u;
+    memset(write_data, 0, sizeof(write_data));
+    write_length = 0u;
+
+    utils_forwardCursor(&inst, 5u);
+
+    CHECK( write_callCnt == 1u);
+    CHECK( write_length == 4u);
+    REQUIRE( strncmp("\e[5C", write_data, 4u) == 0);
+}
+
+TEST_CASE( "shellmatta_utils_forwardCursor normal echo off" ) {
+
+    shellmatta_instance_t inst;
+    char buffer[20];
+    char dummyData[29];
+
+    inst.buffer = buffer;
+    inst.bufferSize = 20;
+    inst.cursor = 10;
+    inst.inputCount = 20;
+    inst.echoEnabled = false;
+
+    inst.write = writeFct;
+
+    write_callCnt = 0u;
+    memset(write_data, 0, sizeof(write_data));
+    write_length = 0u;
+
+    utils_forwardCursor(&inst, 5u);
+
+    CHECK( write_callCnt == 0u);
+    CHECK( write_length == 0u);
+    REQUIRE( strncmp("\0\0\0\0", write_data, 4u) == 0);
+}
+
+
+TEST_CASE( "shellmatta_utils_forwardCursor forward by 12 with cursor at 5 and input count at 10" ) {
+
+    shellmatta_instance_t inst;
+    char buffer[20];
+    char dummyData[29];
+
+    inst.buffer = buffer;
+    inst.bufferSize = 20;
+    inst.cursor = 5;
+    inst.inputCount = 10;
+    inst.echoEnabled = true;
+
+    inst.write = writeFct;
+
+    write_callCnt = 0u;
+    memset(write_data, 0, sizeof(write_data));
+    write_length = 0u;
+
+    utils_forwardCursor(&inst, 12u);
+
+    CHECK( write_callCnt == 1u);
+    CHECK( write_length == 4u);
+    REQUIRE( strncmp("\e[5C", write_data, 4u) == 0);
+}
+
+TEST_CASE( "shellmatta_utils_forwardCursor forward by 0" ) {
+
+    shellmatta_instance_t inst;
+    char buffer[20];
+    char dummyData[29];
+
+    inst.buffer = buffer;
+    inst.bufferSize = 20;
+    inst.cursor = 10;
+    inst.inputCount = 10;
+    inst.echoEnabled = true;
+
+    inst.write = writeFct;
+
+    write_callCnt = 0u;
+    memset(write_data, 0, sizeof(write_data));
+    write_length = 0u;
+
+    utils_forwardCursor(&inst, 0u);
+
+    CHECK( write_callCnt == 0u);
+    CHECK( write_length == 0u);
+    REQUIRE( strncmp("\0\0\0\0\0", write_data, 4u) == 0);
+}

test/shellmatta_utils/insertChars.cpp → test/shellmatta_utils/test_utils_insertChars.cpp


+ 41 - 0
test/shellmatta_utils/test_utils_restoreCursorPos.cpp

@@ -0,0 +1,41 @@
+#include "test/framework/catch.hpp"
+#include "src/shellmatta_utils.c"
+#include <string.h>
+
+static uint32_t write_callCnt = 0u;
+static char write_data[10];
+static uint32_t write_length;
+
+static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
+{
+    write_callCnt ++;
+    strncpy(write_data, data, length);
+    write_length = length;
+    return SHELLMATTA_OK;
+}
+
+
+TEST_CASE( "shellmatta_utils_restoreCursorPos" ) {
+
+    shellmatta_instance_t inst;
+    char buffer[20];
+    char dummyData[29];
+
+    inst.buffer = buffer;
+    inst.bufferSize = 20;
+    inst.cursor = 10;
+    inst.inputCount = 10;
+    inst.echoEnabled = true;
+
+    inst.write = writeFct;
+
+    write_callCnt = 0u;
+    memset(write_data, 0, sizeof(write_data));
+    write_length = 0u;
+
+    utils_restoreCursorPos(&inst);
+
+    CHECK( write_callCnt == 1u);
+    CHECK( write_length == 3u);
+    REQUIRE( strncmp("\e[u", write_data, 3u) == 0);
+}

+ 92 - 0
test/shellmatta_utils/test_utils_rewindCursor.cpp

@@ -0,0 +1,92 @@
+#include "test/framework/catch.hpp"
+#include "src/shellmatta_utils.c"
+#include <string.h>
+
+static uint32_t write_callCnt = 0u;
+static char write_data[10];
+static uint32_t write_length;
+
+static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
+{
+    write_callCnt ++;
+    strncpy(write_data, data, length);
+    write_length = length;
+    return SHELLMATTA_OK;
+}
+
+
+TEST_CASE( "shellmatta_utils_rewindCursor normal" ) {
+
+    shellmatta_instance_t inst;
+    char buffer[20];
+    char dummyData[29];
+
+    inst.buffer = buffer;
+    inst.bufferSize = 20;
+    inst.cursor = 10;
+    inst.inputCount = 10;
+    inst.echoEnabled = true;
+
+    inst.write = writeFct;
+
+    write_callCnt = 0u;
+    memset(write_data, 0, sizeof(write_data));
+    write_length = 0u;
+
+    utils_rewindCursor(&inst, 5u);
+
+    CHECK( write_callCnt == 1u);
+    CHECK( write_length == 4u);
+    REQUIRE( strncmp("\e[5D", write_data, 4u) == 0);
+}
+
+
+TEST_CASE( "shellmatta_utils_rewindCursor rewind by 12 with cursor at 10" ) {
+
+    shellmatta_instance_t inst;
+    char buffer[20];
+    char dummyData[29];
+
+    inst.buffer = buffer;
+    inst.bufferSize = 20;
+    inst.cursor = 10;
+    inst.inputCount = 10;
+    inst.echoEnabled = true;
+
+    inst.write = writeFct;
+
+    write_callCnt = 0u;
+    memset(write_data, 0, sizeof(write_data));
+    write_length = 0u;
+
+    utils_rewindCursor(&inst, 12u);
+
+    CHECK( write_callCnt == 1u);
+    CHECK( write_length == 5u);
+    REQUIRE( strncmp("\e[10D", write_data, 5u) == 0);
+}
+
+TEST_CASE( "shellmatta_utils_rewindCursor rewind by 0" ) {
+
+    shellmatta_instance_t inst;
+    char buffer[20];
+    char dummyData[29];
+
+    inst.buffer = buffer;
+    inst.bufferSize = 20;
+    inst.cursor = 10;
+    inst.inputCount = 10;
+    inst.echoEnabled = true;
+
+    inst.write = writeFct;
+
+    write_callCnt = 0u;
+    memset(write_data, 0, sizeof(write_data));
+    write_length = 0u;
+
+    utils_rewindCursor(&inst, 0u);
+
+    CHECK( write_callCnt == 0u);
+    CHECK( write_length == 0u);
+    REQUIRE( strncmp("\0\0\0\0\0", write_data, 4u) == 0);
+}

+ 41 - 0
test/shellmatta_utils/test_utils_saveCursorPos.cpp

@@ -0,0 +1,41 @@
+#include "test/framework/catch.hpp"
+#include "src/shellmatta_utils.c"
+#include <string.h>
+
+static uint32_t write_callCnt = 0u;
+static char write_data[10];
+static uint32_t write_length;
+
+static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
+{
+    write_callCnt ++;
+    strncpy(write_data, data, length);
+    write_length = length;
+    return SHELLMATTA_OK;
+}
+
+
+TEST_CASE( "shellmatta_utils_saveCursorPos" ) {
+
+    shellmatta_instance_t inst;
+    char buffer[20];
+    char dummyData[29];
+
+    inst.buffer = buffer;
+    inst.bufferSize = 20;
+    inst.cursor = 10;
+    inst.inputCount = 10;
+    inst.echoEnabled = true;
+
+    inst.write = writeFct;
+
+    write_callCnt = 0u;
+    memset(write_data, 0, sizeof(write_data));
+    write_length = 0u;
+
+    utils_saveCursorPos(&inst);
+
+    CHECK( write_callCnt == 1u);
+    CHECK( write_length == 3u);
+    REQUIRE( strncmp("\e[s", write_data, 3u) == 0);
+}

test/shellmatta_utils/itoa.cpp → test/shellmatta_utils/test_utils_shellItoa.cpp


+ 65 - 0
test/shellmatta_utils/test_utils_writeEcho.cpp

@@ -0,0 +1,65 @@
+#include "test/framework/catch.hpp"
+#include "src/shellmatta_utils.c"
+#include <string.h>
+
+static uint32_t write_callCnt = 0u;
+static const char *write_data;
+static uint32_t write_length;
+
+static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
+{
+    write_callCnt ++;
+    write_data = data;
+    write_length = length;
+    return SHELLMATTA_OK;
+}
+
+TEST_CASE( "shellmatta_writeEcho echo enabled" ) {
+
+    shellmatta_instance_t inst;
+    char buffer[20];
+    char dummyData[29];
+
+    inst.buffer = buffer;
+    inst.bufferSize = 20;
+    inst.cursor = 10;
+    inst.inputCount = 10;
+    inst.echoEnabled = true;
+
+    inst.write = writeFct;
+
+    write_callCnt = 0u;
+    write_data = 0u;
+    write_length = 0u;
+
+    utils_writeEcho(&inst, (char*)&dummyData, sizeof(dummyData));
+
+    CHECK( write_callCnt == 1u );
+    CHECK( write_data == (char*)&dummyData );
+    REQUIRE( write_length == sizeof(dummyData));
+}
+
+TEST_CASE( "shellmatta_writeEcho echo disabled" ) {
+
+    shellmatta_instance_t inst;
+    char buffer[20];
+    char dummyData[29];
+
+    inst.buffer = buffer;
+    inst.bufferSize = 20;
+    inst.cursor = 10;
+    inst.inputCount = 10;
+    inst.echoEnabled = false;
+
+    inst.write = writeFct;
+
+    write_callCnt = 0u;
+    write_data = 0u;
+    write_length = 0u;
+
+    utils_writeEcho(&inst, (char*)&dummyData, sizeof(dummyData));
+
+    CHECK( write_callCnt == 0u );
+    CHECK( write_data == (char*)0u );
+    REQUIRE( write_length == 0u );
+}