123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- #include "test/framework/catch.hpp"
- extern "C" {
- #include "shellmatta.h"
- }
- #include <string.h>
- 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)
- {
- write_callCnt ++;
- while((length > 0) && (write_length < sizeof(write_data)))
- {
- write_data[write_length] = *data;
- data ++;
- length --;
- write_length ++;
- }
- return SHELLMATTA_OK;
- }
- 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 = {(char*)"doSomething", (char*)"do", (char*)"Function does something", (char*)"use me, please", doSomething, NULL};
- static shellmatta_retCode_t empty(shellmatta_handle_t handle, const char *arguments, uint32_t length)
- {
- shellmatta_printf(handle, "empty - %s - length: %u", arguments, length);
- return SHELLMATTA_OK;
- }
- shellmatta_cmd_t emptyCmd = {(char*)"empty", NULL, NULL, NULL, empty, NULL};
- TEST_CASE( "shellmatta empty function" ) {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- char buffer[1024];
- char historyBuffer[1024];
- char *dummyData = (char*)"\r\nshellmatta->";
- shellmatta_doInit( &inst,
- &handle,
- buffer,
- sizeof(buffer),
- historyBuffer,
- sizeof(historyBuffer),
- "shellmatta->",
- NULL,
- writeFct);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- shellmatta_processData(handle, (char*)"\r", 1);
- CHECK( write_length == 14u);
- REQUIRE( strcmp(dummyData, write_data) == 0);
- }
- TEST_CASE( "shellmatta help function" ) {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- char buffer[1024];
- char historyBuffer[1024];
- char *dummyData = (char*)"?\r\n"
- "doSomething do Function does something use me, please\r\n"
- "help ? Print this help text help\r\n"
- "\r\nshellmatta->";
- shellmatta_doInit( &inst,
- &handle,
- buffer,
- sizeof(buffer),
- historyBuffer,
- sizeof(historyBuffer),
- "shellmatta->",
- NULL,
- writeFct);
- shellmatta_addCmd(handle, &doSomethingCmd);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- shellmatta_processData(handle, (char*)"?\r", 2);
- CHECK( write_length == 123u);
- CHECK( strcmp(dummyData, write_data) == 0);
- shellmatta_addCmd(handle, &emptyCmd);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- dummyData = (char*)"? 564 321 56 465 46\r\n"
- "doSomething do Function does something use me, please\r\n"
- "empty \r\n"
- "help ? Print this help text help\r\n"
- "\r\nshellmatta->";
- shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
- CHECK( write_length == strlen(dummyData));
- CHECK( strcmp(dummyData, write_data) == 0);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- dummyData = (char*)"?r\r\n"
- "Command: ?r not found"
- "\r\nshellmatta->";
- shellmatta_processData(handle, (char*)"?r\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);
- }
- TEST_CASE( "shellmatta remove function" ) {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- char buffer[1024];
- char historyBuffer[1024];
- char *dummyData = (char*)"?\r\n"
- "doSomething do Function does something use me, please\r\n"
- "help ? Print this help text help\r\n"
- "\r\nshellmatta->";
- shellmatta_doInit( &inst,
- &handle,
- buffer,
- sizeof(buffer),
- historyBuffer,
- sizeof(historyBuffer),
- "shellmatta->",
- NULL,
- writeFct);
- shellmatta_addCmd(handle, &doSomethingCmd);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- shellmatta_processData(handle, (char*)"?\r", 2);
- CHECK( write_length == 123u);
- CHECK( strcmp(dummyData, write_data) == 0);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- dummyData = (char*)"? 564 321 56 465 46\r\n"
- "doSomething do Function does something use me, please\r\n"
- "help ? Print this help text help\r\n"
- "\r\nshellmatta->";
- shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
- CHECK( write_length == 141u);
- CHECK( strcmp(dummyData, write_data) == 0);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- shellmatta_removeCmd(handle, &doSomethingCmd);
- shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
- dummyData = (char*)"? 564 321 56 465 46\r\n"
- "help ? Print this help text help\r\n"
- "\r\nshellmatta->";
- printf("sdfsd sdf sdf sdf sdf sd fds\n%s", write_data);
- CHECK( write_length == 72u);
- REQUIRE( strcmp(dummyData, write_data) == 0);
- }
|