diff --git a/example/main.c b/example/main.c index c63c5a5..7f72a06 100644 --- a/example/main.c +++ b/example/main.c @@ -88,7 +88,7 @@ shellmatta_retCode_t writeFct(const char* data, uint32_t length) int main(void) { - static char buffer[1024]; + static char buffer[1024]; static char historyBuffer[4096]; static shellmatta_instance_t instance; @@ -96,7 +96,7 @@ int main(void) // raw(); // keypad(stdscr, TRUE); // noecho(); - f = open("/dev/pts/1", O_RDWR | O_SYNC); + f = open("/dev/pts/2", O_RDWR | O_SYNC); if (f < 0) { diff --git a/makefile b/makefile index 3071c36..4a2804a 100644 --- a/makefile +++ b/makefile @@ -20,10 +20,16 @@ SOURCES := src/shellmatta.c \ 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)) diff --git a/test/shellmatta_utils/clearInput.cpp b/test/shellmatta_utils/test_utils_clearInput.cpp similarity index 100% rename from test/shellmatta_utils/clearInput.cpp rename to test/shellmatta_utils/test_utils_clearInput.cpp diff --git a/test/shellmatta_utils/test_utils_eraseLine.cpp b/test/shellmatta_utils/test_utils_eraseLine.cpp new file mode 100644 index 0000000..a4c4543 --- /dev/null +++ b/test/shellmatta_utils/test_utils_eraseLine.cpp @@ -0,0 +1,41 @@ +#include "test/framework/catch.hpp" +#include "src/shellmatta_utils.c" +#include + +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); +} diff --git a/test/shellmatta_utils/test_utils_forwardCursor.cpp b/test/shellmatta_utils/test_utils_forwardCursor.cpp new file mode 100644 index 0000000..31d0672 --- /dev/null +++ b/test/shellmatta_utils/test_utils_forwardCursor.cpp @@ -0,0 +1,117 @@ +#include "test/framework/catch.hpp" +#include "src/shellmatta_utils.c" +#include + +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); +} diff --git a/test/shellmatta_utils/insertChars.cpp b/test/shellmatta_utils/test_utils_insertChars.cpp similarity index 100% rename from test/shellmatta_utils/insertChars.cpp rename to test/shellmatta_utils/test_utils_insertChars.cpp diff --git a/test/shellmatta_utils/test_utils_restoreCursorPos.cpp b/test/shellmatta_utils/test_utils_restoreCursorPos.cpp new file mode 100644 index 0000000..e16f428 --- /dev/null +++ b/test/shellmatta_utils/test_utils_restoreCursorPos.cpp @@ -0,0 +1,41 @@ +#include "test/framework/catch.hpp" +#include "src/shellmatta_utils.c" +#include + +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); +} diff --git a/test/shellmatta_utils/test_utils_rewindCursor.cpp b/test/shellmatta_utils/test_utils_rewindCursor.cpp new file mode 100644 index 0000000..2d46a2b --- /dev/null +++ b/test/shellmatta_utils/test_utils_rewindCursor.cpp @@ -0,0 +1,92 @@ +#include "test/framework/catch.hpp" +#include "src/shellmatta_utils.c" +#include + +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); +} diff --git a/test/shellmatta_utils/test_utils_saveCursorPos.cpp b/test/shellmatta_utils/test_utils_saveCursorPos.cpp new file mode 100644 index 0000000..83d0f8d --- /dev/null +++ b/test/shellmatta_utils/test_utils_saveCursorPos.cpp @@ -0,0 +1,41 @@ +#include "test/framework/catch.hpp" +#include "src/shellmatta_utils.c" +#include + +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); +} diff --git a/test/shellmatta_utils/itoa.cpp b/test/shellmatta_utils/test_utils_shellItoa.cpp similarity index 100% rename from test/shellmatta_utils/itoa.cpp rename to test/shellmatta_utils/test_utils_shellItoa.cpp diff --git a/test/shellmatta_utils/test_utils_writeEcho.cpp b/test/shellmatta_utils/test_utils_writeEcho.cpp new file mode 100644 index 0000000..86ec910 --- /dev/null +++ b/test/shellmatta_utils/test_utils_writeEcho.cpp @@ -0,0 +1,65 @@ +#include "test/framework/catch.hpp" +#include "src/shellmatta_utils.c" +#include + +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 ); +}