Browse Source

added some testcases
+ adapted makefile to delete coverage data from former runs

prozessorkern 4 years ago
parent
commit
11bf2d2671

+ 11 - 5
makefile

@@ -17,7 +17,7 @@ SOURCES :=  src/shellmatta.c                \
             src/shellmatta_history.c        \
             src/shellmatta_history.c        \
             src/shellmatta_utils.c          \
             src/shellmatta_utils.c          \
             src/shellmatta_escape.c
             src/shellmatta_escape.c
-            
+
 INCLUDES    := api .
 INCLUDES    := api .
 
 
 UNITTEST_SOURCES := test/unittest/test_main.cpp                                         \
 UNITTEST_SOURCES := test/unittest/test_main.cpp                                         \
@@ -30,7 +30,8 @@ UNITTEST_SOURCES := test/unittest/test_main.cpp
                     test/unittest/shellmatta_utils/test_utils_forwardCursor.cpp         \
                     test/unittest/shellmatta_utils/test_utils_forwardCursor.cpp         \
                     test/unittest/shellmatta_utils/test_utils_clearInput.cpp            \
                     test/unittest/shellmatta_utils/test_utils_clearInput.cpp            \
                     test/unittest/shellmatta_utils/test_utils_insertChars.cpp           \
                     test/unittest/shellmatta_utils/test_utils_insertChars.cpp           \
-                    test/unittest/shellmatta_autocomplete/test_autocomplete_run.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_escape/test_escape_processArrowKeys.cpp    \
                     test/unittest/shellmatta_history/test_appendHistoryByte.cpp         \
                     test/unittest/shellmatta_history/test_appendHistoryByte.cpp         \
                     test/unittest/shellmatta/test_shellmatta_doInit.cpp
                     test/unittest/shellmatta/test_shellmatta_doInit.cpp
@@ -47,7 +48,7 @@ TESTFLAGS   := $(INCLUDES:%=-I%) -g -Wall -Werror -fprofile-arcs -ftest-coverage
 TESTLFLAGS  := -fprofile-arcs -Wl,--allow-multiple-definition
 TESTLFLAGS  := -fprofile-arcs -Wl,--allow-multiple-definition
 
 
 DEPEND      = -MT $@ -MF "$(@:%.o=%.d)" -MG -MM
 DEPEND      = -MT $@ -MF "$(@:%.o=%.d)" -MG -MM
-            
+			
 COBJ    := $(patsubst %.c,$(OBJ_DIR)%.o,$(SOURCES))
 COBJ    := $(patsubst %.c,$(OBJ_DIR)%.o,$(SOURCES))
 
 
 EXAMPLE_SOURCES := example/main.c
 EXAMPLE_SOURCES := example/main.c
@@ -82,10 +83,15 @@ cppcheck:
 unittest: $(UNITTEST_TARGET)
 unittest: $(UNITTEST_TARGET)
 	- @mkdir -p output/test/unittest/report
 	- @mkdir -p output/test/unittest/report
 	@echo running test:
 	@echo running test:
+	@# remove coverage from former run
+	@-find . -name "*.gcda" -type f -delete
 	-$(UNITTEST_TARGET)
 	-$(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
 	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)
 integrationtest: $(INTEGRATIONTEST_TARGET)
 	- @mkdir -p output/test/integrationtest/report
 	- @mkdir -p output/test/integrationtest/report

+ 1 - 1
test/unittest/shellmatta_utils/test_utils_clearInput.cpp

@@ -2,7 +2,7 @@
 #include "src/shellmatta_utils.c"
 #include "src/shellmatta_utils.c"
 #include <string.h>
 #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;
     return SHELLMATTA_OK;
 }
 }

+ 98 - 4
test/unittest/shellmatta_utils/test_utils_insertChars.cpp

@@ -2,26 +2,120 @@
 #include "src/shellmatta_utils.c"
 #include "src/shellmatta_utils.c"
 #include <string.h>
 #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;
     return SHELLMATTA_OK;
 }
 }
 
 
 TEST_CASE( "shellmatta_insertChars normal call" ) {
 TEST_CASE( "shellmatta_insertChars normal call" ) {
 
 
     shellmatta_instance_t inst;
     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;
+
+    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 == 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.buffer = buffer;
     inst.bufferSize = 20;
     inst.bufferSize = 20;
     inst.cursor = 8;
     inst.cursor = 8;
     inst.inputCount = 10;
     inst.inputCount = 10;
+    inst.echoEnabled = true;
+    inst.mode = SHELLMATTA_MODE_OVERWRITE;
 
 
     inst.write = writeFct;
     inst.write = writeFct;
 
 
-    utils_insertChars(&inst, (char*)"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);
     CHECK( inst.cursor == 12);
-    REQUIRE( inst.inputCount == 14);
+    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);
+}

+ 43 - 0
test/unittest/shellmatta_utils/test_utils_terminateInput.cpp

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