added unit tests for several util functions

This commit is contained in:
prozessorkern 2019-07-27 16:31:19 +02:00
parent f65f86d8e2
commit d65765371b
11 changed files with 409 additions and 6 deletions

View File

@ -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)
{ {

View File

@ -20,10 +20,16 @@ SOURCES := src/shellmatta.c \
INCLUDES := api . INCLUDES := api .
TEST_SOURCES := test/test_main.cpp \ TEST_SOURCES := test/test_main.cpp \
test/shellmatta_utils/itoa.cpp \ test/shellmatta_utils/test_utils_writeEcho.cpp \
test/shellmatta_utils/clearInput.cpp \ test/shellmatta_utils/test_utils_shellItoa.cpp \
test/shellmatta_utils/insertChars.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))

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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 );
}