Merge branch 'feature/add_tests_#5' of shimatta/shellmatta into develop

This commit is contained in:
shimatta 2020-02-03 20:32:03 +01:00 committed by Gogs
commit ea3c90f305
18 changed files with 351 additions and 45 deletions

29
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,29 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "debug unittest",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/output/test/unittest/unittest",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "make test",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}

40
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,40 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "make example",
"type": "shell",
"command": "make example",
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "make clean",
"type": "shell",
"command": "make clean",
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"label": "make unittest",
"type": "shell",
"command": "make unittest",
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "test",
"isDefault": true
}
}
]
}

View File

@ -17,19 +17,24 @@ SOURCES := src/shellmatta.c \
src/shellmatta_history.c \
src/shellmatta_utils.c \
src/shellmatta_escape.c
INCLUDES := api .
UNITTEST_SOURCES := test/unittest/test_main.cpp \
test/unittest/shellmatta_utils/test_utils_writeEcho.cpp \
test/unittest/shellmatta_utils/test_utils_shellItoa.cpp \
test/unittest/shellmatta_utils/test_utils_saveCursorPos.cpp \
test/unittest/shellmatta_utils/test_utils_restoreCursorPos.cpp \
test/unittest/shellmatta_utils/test_utils_eraseLine.cpp \
test/unittest/shellmatta_utils/test_utils_rewindCursor.cpp \
test/unittest/shellmatta_utils/test_utils_forwardCursor.cpp \
test/unittest/shellmatta_utils/test_utils_clearInput.cpp \
test/unittest/shellmatta_utils/test_utils_insertChars.cpp
UNITTEST_SOURCES := test/unittest/test_main.cpp \
test/unittest/shellmatta_utils/test_utils_writeEcho.cpp \
test/unittest/shellmatta_utils/test_utils_shellItoa.cpp \
test/unittest/shellmatta_utils/test_utils_saveCursorPos.cpp \
test/unittest/shellmatta_utils/test_utils_restoreCursorPos.cpp \
test/unittest/shellmatta_utils/test_utils_eraseLine.cpp \
test/unittest/shellmatta_utils/test_utils_rewindCursor.cpp \
test/unittest/shellmatta_utils/test_utils_forwardCursor.cpp \
test/unittest/shellmatta_utils/test_utils_clearInput.cpp \
test/unittest/shellmatta_utils/test_utils_insertChars.cpp \
test/unittest/shellmatta_utils/test_utils_terminateInput.cpp \
test/unittest/shellmatta_autocomplete/test_autocomplete_run.cpp \
test/unittest/shellmatta_escape/test_escape_processArrowKeys.cpp \
test/unittest/shellmatta_history/test_appendHistoryByte.cpp \
test/unittest/shellmatta/test_shellmatta_doInit.cpp
INTEGRATIONTEST_SOURCES := test/integrationtest/test_main.cpp \
test/integrationtest/test_integration.cpp
@ -38,12 +43,12 @@ UNITTEST_CPPOBJ := $(patsubst %.cpp,$(OBJ_DIR)%.o,$(UNITTEST_SOURCES))
INTEGRATIONTEST_CPPOBJ := $(patsubst %.cpp,$(OBJ_DIR)%.o,$(INTEGRATIONTEST_SOURCES))
CFLAGS := $(INCLUDES:%=-I%) -g
TESTFLAGS := $(INCLUDES:%=-I%) -g -fprofile-arcs -ftest-coverage
CFLAGS := $(INCLUDES:%=-I%) -g -Wall -Werror
TESTFLAGS := $(INCLUDES:%=-I%) -g -Wall -Werror -fprofile-arcs -ftest-coverage
TESTLFLAGS := -fprofile-arcs -Wl,--allow-multiple-definition
DEPEND = -MT $@ -MF "$(@:%.o=%.d)" -MG -MM
COBJ := $(patsubst %.c,$(OBJ_DIR)%.o,$(SOURCES))
EXAMPLE_SOURCES := example/main.c
@ -69,20 +74,31 @@ help:
test: unittest integrationtest
cppcheck:
- @mkdir -p output/cppcheck/html
cppcheck --addon=/usr/bin/misra.py --enable=all --template=gcc --cppcheck-build-dir=output/cppcheck $(SOURCES)
cppcheck --addon=/usr/bin/misra.py --enable=all --template=gcc --cppcheck-build-dir=output/cppcheck $(SOURCES) --xml 2>output/cppcheck/cppcheck.xml
cppcheck-htmlreport --file=output/cppcheck/cppcheck.xml --title="Shellmatta" --report-dir=output/cppcheck/html
unittest: $(UNITTEST_TARGET)
- @mkdir -p output/test/unittest/report
@echo running test:
@# remove coverage from former run
@-find . -name "*.gcda" -type f -delete
-$(UNITTEST_TARGET)
#gcov -o output/test $(TEST_CPPOBJ) -r
@#gcov -o output/test/unittest $(UNITTEST_CPPOBJ) -r src
@# remove report from former run
-rm -rf $(OBJ_DIR)test/unittest/report/*
gcovr --html-details --output $(OBJ_DIR)test/unittest/report/report.html output/test/unittest -f src -f api
#-rm *.gcov
@#-rm *.gcov
integrationtest: $(INTEGRATIONTEST_TARGET)
- @mkdir -p output/test/integrationtest/report
@echo running test:
-$(INTEGRATIONTEST_TARGET)
#gcov -o output/test $(TEST_CPPOBJ) -r
gcovr --html-details --output $(OBJ_DIR)test/integrationtest/report/report.html output/test/integrationtest -f src -f api
gcovr --html-details --output $(OBJ_DIR)test/integrationtest/report/report.html output/src -f src -f api
#-rm *.gcov
example: $(EXAMPLE_TARGET)
@ -91,7 +107,7 @@ example: $(EXAMPLE_TARGET)
doc:
- @mkdir -p output/doc/html
- @mkdir -p output/doc/latex
doxygen settings/doxygen/doxyfile
doxygen cfg/doxygen/doxyfile
clean:
- rm -rf $(OBJ_DIR)

View File

@ -69,7 +69,7 @@ uint32_t utils_shellItoa(int32_t value, char *buffer, uint32_t base)
do
{
digitValue = (char) (value % base);
tempBuffer[i] = (digitValue < 10u) ? ('0' + digitValue) : (('A' - 10) + digitValue);
tempBuffer[i] = (digitValue < 10) ? ('0' + digitValue) : (('A' - 10) + digitValue);
value /= base;
i ++;
}while(value > 0);
@ -327,7 +327,7 @@ static shellmatta_retCode_t helpCmdFct(shellmatta_handle_t handle, const char *a
return ret;
}
shellmatta_cmd_t helpCmd = {"help", "h", "Print this help text", "help", helpCmdFct, NULL};
shellmatta_cmd_t helpCmd = {(char*)"help", (char*)"h", (char*)"Print this help text", (char*)"help", helpCmdFct, NULL};
/**
* @brief terminates an input and prints the prompt again

View File

@ -7,6 +7,8 @@ extern "C" {
static uint32_t write_callCnt = 0u;
static char write_data[1024];
static uint32_t write_length;
static const char *doSomethingArguments;
static uint32_t doSomethingLength;
static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
{
@ -24,10 +26,12 @@ static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
{
doSomethingArguments = arguments;
doSomethingLength = length;
shellmatta_printf(handle, "%s - length: %u", arguments, length);
return SHELLMATTA_OK;
}
shellmatta_cmd_t doSomethingCmd = {"doSomething", "do", "Function does something", "use me, please", doSomething, NULL};
shellmatta_cmd_t doSomethingCmd = {(char*)"doSomething", (char*)"do", (char*)"Function does something", (char*)"use me, please", doSomething, NULL};
TEST_CASE( "shellmatta empty function" ) {
@ -36,7 +40,7 @@ TEST_CASE( "shellmatta empty function" ) {
shellmatta_handle_t handle;
char buffer[1024];
char historyBuffer[1024];
char *dummyData = "\r\nshellmatta->";
char *dummyData = (char*)"\r\nshellmatta->";
shellmatta_doInit( &inst,
&handle,
@ -52,7 +56,7 @@ TEST_CASE( "shellmatta empty function" ) {
memset(write_data, 0, sizeof(write_data));
write_length = 0u;
shellmatta_processData(handle, "\r", 1);
shellmatta_processData(handle, (char*)"\r", 1);
CHECK( write_length == 14u);
REQUIRE( strcmp(dummyData, write_data) == 0);
@ -65,7 +69,7 @@ TEST_CASE( "shellmatta help function" ) {
shellmatta_handle_t handle;
char buffer[1024];
char historyBuffer[1024];
char *dummyData = "h\r\n"
char *dummyData = (char*)"h\r\n"
"doSomething do Function does something use me, please\r\n"
"help h Print this help text help\r\n"
"\r\nshellmatta->";
@ -85,7 +89,7 @@ TEST_CASE( "shellmatta help function" ) {
memset(write_data, 0, sizeof(write_data));
write_length = 0u;
shellmatta_processData(handle, "h\r", 2);
shellmatta_processData(handle, (char*)"h\r", 2);
CHECK( write_length == 123u);
CHECK( strcmp(dummyData, write_data) == 0);
@ -95,12 +99,12 @@ TEST_CASE( "shellmatta help function" ) {
memset(write_data, 0, sizeof(write_data));
write_length = 0u;
dummyData = "h 564 321 56 465 46\r\n"
dummyData = (char*)"h 564 321 56 465 46\r\n"
"doSomething do Function does something use me, please\r\n"
"help h Print this help text help\r\n"
"\r\nshellmatta->";
shellmatta_processData(handle, "h 564 321 56 465 46\r", 20);
shellmatta_processData(handle, (char*)"h 564 321 56 465 46\r", 20);
CHECK( write_length == 141u);
CHECK( strcmp(dummyData, write_data) == 0);
@ -110,13 +114,50 @@ TEST_CASE( "shellmatta help function" ) {
memset(write_data, 0, sizeof(write_data));
write_length = 0u;
dummyData = "hr\r\n"
dummyData = (char*)"hr\r\n"
"Command: hr not found"
"\r\nshellmatta->";
shellmatta_processData(handle, "hr\r", 3);
shellmatta_processData(handle, (char*)"hr\r", 3);
CHECK( write_length == 39u);
REQUIRE( strcmp(dummyData, write_data) == 0);
}
TEST_CASE( "shellmatta heredoc test" ) {
shellmatta_instance_t inst;
shellmatta_handle_t handle;
char buffer[1024];
char historyBuffer[1024];
char *dummyData = (char*)"do this \r\n"
"asdf\r\n"
"1234";
shellmatta_doInit( &inst,
&handle,
buffer,
sizeof(buffer),
historyBuffer,
sizeof(historyBuffer),
"shellmatta->",
NULL,
writeFct);
shellmatta_addCmd(handle, &doSomethingCmd);
doSomethingArguments = NULL;
doSomethingLength = 0u;
shellmatta_processData(handle, (char*)"do this << EOF\r\n"
"asdf\r\n"
"1234\r\n"
"EOF\r\n"
, 34);
CHECK( doSomethingLength == 20u);
REQUIRE( strcmp(dummyData, doSomethingArguments) == 0);
}

View File

@ -0,0 +1,14 @@
#include "test/framework/catch.hpp"
#include "src/shellmatta.c"
#include <string.h>
TEST_CASE( "shellmatta dummy" ) {
shellmatta_instance_t inst;
//shellmatta_handle_t handle;
inst.inputCount = 0u;
//shellmatta_doInit(&inst, &handle, )
REQUIRE( inst.inputCount == 0u);
}

View File

@ -0,0 +1,13 @@
#include "test/framework/catch.hpp"
#include "src/shellmatta_autocomplete.c"
#include <string.h>
TEST_CASE( "shellmatta_autocomplete_run dummy" ) {
shellmatta_instance_t inst;
inst.inputCount = 0u;
autocomplete_run(&inst);
REQUIRE( inst.inputCount == 0u);
}

View File

@ -0,0 +1,13 @@
#include "test/framework/catch.hpp"
#include "src/shellmatta_escape.c"
#include <string.h>
TEST_CASE( "shellmatta_escape dummy" ) {
shellmatta_instance_t inst;
inst.inputCount = 0u;
escape_processArrowKeys(&inst);
REQUIRE( inst.inputCount == 0u);
}

View File

@ -0,0 +1,13 @@
#include "test/framework/catch.hpp"
#include "src/shellmatta_history.c"
#include <string.h>
TEST_CASE( "shellmatta_history dummy" ) {
shellmatta_instance_t inst;
inst.inputCount = 0u;
//appendHistoryByte(&inst, 'a');
REQUIRE( inst.inputCount == 0u);
}

View File

@ -2,7 +2,7 @@
#include "src/shellmatta_utils.c"
#include <string.h>
shellmatta_retCode_t writeFct(const char* data, uint32_t length)
static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
{
return SHELLMATTA_OK;
}

View File

@ -19,7 +19,6 @@ TEST_CASE( "shellmatta_utils_eraseLine" ) {
shellmatta_instance_t inst;
char buffer[20];
char dummyData[29];
inst.buffer = buffer;
inst.bufferSize = 20;

View File

@ -19,7 +19,6 @@ TEST_CASE( "shellmatta_utils_forwardCursor normal" ) {
shellmatta_instance_t inst;
char buffer[20];
char dummyData[29];
inst.buffer = buffer;
inst.bufferSize = 20;
@ -44,7 +43,6 @@ TEST_CASE( "shellmatta_utils_forwardCursor normal echo off" ) {
shellmatta_instance_t inst;
char buffer[20];
char dummyData[29];
inst.buffer = buffer;
inst.bufferSize = 20;
@ -70,7 +68,6 @@ TEST_CASE( "shellmatta_utils_forwardCursor forward by 12 with cursor at 5 and in
shellmatta_instance_t inst;
char buffer[20];
char dummyData[29];
inst.buffer = buffer;
inst.bufferSize = 20;
@ -95,7 +92,6 @@ TEST_CASE( "shellmatta_utils_forwardCursor forward by 0" ) {
shellmatta_instance_t inst;
char buffer[20];
char dummyData[29];
inst.buffer = buffer;
inst.bufferSize = 20;

View File

@ -2,26 +2,120 @@
#include "src/shellmatta_utils.c"
#include <string.h>
shellmatta_retCode_t writeFct(const char* data, uint32_t length)
static uint32_t write_callCnt = 0u;
static char write_data[20];
static uint32_t write_idx;
static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
{
write_callCnt ++;
strncpy(&write_data[write_idx], data, length);
write_idx += length;
return SHELLMATTA_OK;
}
TEST_CASE( "shellmatta_insertChars normal call" ) {
shellmatta_instance_t inst;
char buffer[20];
char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
inst.buffer = buffer;
inst.bufferSize = 20;
inst.cursor = 8;
inst.inputCount = 10;
inst.echoEnabled = true;
inst.write = writeFct;
utils_insertChars(&inst, "blksdflsd kfjlk", 4);
write_callCnt = 0u;
memset(write_data, 0, sizeof(write_data));
write_idx = 0u;
utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
CHECK( inst.cursor == 12);
REQUIRE( inst.inputCount == 14);
CHECK( inst.inputCount == 14);
CHECK( write_callCnt == 5u );
CHECK( strncmp("blks\e[K\e[sij\e[u", write_data, 15u) == 0);
REQUIRE( strncmp("abcdefghblksij\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
}
TEST_CASE( "shellmatta_insertChars overwrite" ) {
shellmatta_instance_t inst;
char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
inst.buffer = buffer;
inst.bufferSize = 20;
inst.cursor = 8;
inst.inputCount = 10;
inst.echoEnabled = true;
inst.mode = SHELLMATTA_MODE_OVERWRITE;
inst.write = writeFct;
write_callCnt = 0u;
memset(write_data, 0, sizeof(write_data));
write_idx = 0u;
utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
CHECK( inst.cursor == 12);
CHECK( inst.inputCount == 14);
CHECK( write_callCnt == 1u );
CHECK( strncmp("blks", write_data, 5u) == 0);
REQUIRE( strncmp("abcdefghblks\0\0\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
}
TEST_CASE( "shellmatta_insertChars append" ) {
shellmatta_instance_t inst;
char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
inst.buffer = buffer;
inst.bufferSize = 20;
inst.cursor = 10;
inst.inputCount = 10;
inst.echoEnabled = true;
inst.mode = SHELLMATTA_MODE_INSERT;
inst.write = writeFct;
write_callCnt = 0u;
memset(write_data, 0, sizeof(write_data));
write_idx = 0u;
utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
CHECK( inst.cursor == 14);
CHECK( inst.inputCount == 14);
CHECK( write_callCnt == 1u );
CHECK( strncmp("blks", write_data, 5u) == 0);
REQUIRE( strncmp("abcdefghijblks\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
}
TEST_CASE( "shellmatta_insertChars 0 length" ) {
shellmatta_instance_t inst;
char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
inst.buffer = buffer;
inst.bufferSize = 20;
inst.cursor = 8;
inst.inputCount = 10;
inst.echoEnabled = true;
inst.write = writeFct;
write_callCnt = 0u;
memset(write_data, 0, sizeof(write_data));
write_idx = 0u;
utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 0u);
CHECK( inst.cursor == 8u );
CHECK( inst.inputCount == 10u );
CHECK( write_callCnt == 0u );
CHECK( memcmp("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", write_data, sizeof(write_data) ) == 0u );
REQUIRE( memcmp("abcdefghij\0\0\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
}

View File

@ -19,7 +19,6 @@ TEST_CASE( "shellmatta_utils_restoreCursorPos" ) {
shellmatta_instance_t inst;
char buffer[20];
char dummyData[29];
inst.buffer = buffer;
inst.bufferSize = 20;

View File

@ -19,7 +19,6 @@ TEST_CASE( "shellmatta_utils_rewindCursor normal" ) {
shellmatta_instance_t inst;
char buffer[20];
char dummyData[29];
inst.buffer = buffer;
inst.bufferSize = 20;
@ -45,7 +44,6 @@ 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;
@ -70,7 +68,6 @@ TEST_CASE( "shellmatta_utils_rewindCursor rewind by 0" ) {
shellmatta_instance_t inst;
char buffer[20];
char dummyData[29];
inst.buffer = buffer;
inst.bufferSize = 20;

View File

@ -19,7 +19,6 @@ TEST_CASE( "shellmatta_utils_saveCursorPos" ) {
shellmatta_instance_t inst;
char buffer[20];
char dummyData[29];
inst.buffer = buffer;
inst.bufferSize = 20;

View File

@ -0,0 +1,43 @@
#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_idx;
static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
{
write_callCnt ++;
strncpy(&write_data[write_idx], data, length);
write_idx += length;
return SHELLMATTA_OK;
}
TEST_CASE( "shellmatta_utils_terminateInput" ) {
shellmatta_instance_t inst;
char buffer[20];
inst.buffer = buffer;
inst.bufferSize = 20;
inst.cursor = 10;
inst.inputCount = 10;
inst.echoEnabled = true;
inst.prompt = "->";
inst.write = writeFct;
write_callCnt = 0u;
memset(write_data, 0, sizeof(write_data));
write_idx = 0u;
utils_terminateInput(&inst);
CHECK( inst.cursor == 0u );
CHECK( inst.inputCount == 0u );
CHECK( write_callCnt == 2u );
CHECK( write_idx == 4u );
REQUIRE( strncmp("\r\n->", write_data, 4u) == 0);
}