2019-07-28 22:28:54 +02:00
|
|
|
#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;
|
2019-07-30 23:55:46 +02:00
|
|
|
static const char *doSomethingArguments;
|
|
|
|
static uint32_t doSomethingLength;
|
2019-07-28 22:28:54 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2019-07-30 23:55:46 +02:00
|
|
|
doSomethingArguments = arguments;
|
|
|
|
doSomethingLength = length;
|
|
|
|
shellmatta_printf(handle, "%s - length: %u", arguments, length);
|
2019-07-28 22:28:54 +02:00
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
2019-12-05 14:39:26 +01:00
|
|
|
shellmatta_cmd_t doSomethingCmd = {(char*)"doSomething", (char*)"do", (char*)"Function does something", (char*)"use me, please", doSomething, NULL};
|
2019-07-28 22:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE( "shellmatta empty function" ) {
|
|
|
|
|
|
|
|
shellmatta_instance_t inst;
|
|
|
|
shellmatta_handle_t handle;
|
|
|
|
char buffer[1024];
|
|
|
|
char historyBuffer[1024];
|
2019-12-05 14:39:26 +01:00
|
|
|
char *dummyData = (char*)"\r\nshellmatta->";
|
2019-07-28 22:28:54 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-12-05 14:39:26 +01:00
|
|
|
shellmatta_processData(handle, (char*)"\r", 1);
|
2019-07-28 22:28:54 +02:00
|
|
|
|
|
|
|
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];
|
2019-12-05 14:39:26 +01:00
|
|
|
char *dummyData = (char*)"h\r\n"
|
2019-07-28 22:28:54 +02:00
|
|
|
"doSomething do Function does something use me, please\r\n"
|
|
|
|
"help h 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;
|
|
|
|
|
2019-12-05 14:39:26 +01:00
|
|
|
shellmatta_processData(handle, (char*)"h\r", 2);
|
2019-07-28 22:28:54 +02:00
|
|
|
|
|
|
|
CHECK( write_length == 123u);
|
|
|
|
CHECK( strcmp(dummyData, write_data) == 0);
|
|
|
|
|
|
|
|
|
|
|
|
write_callCnt = 0u;
|
|
|
|
memset(write_data, 0, sizeof(write_data));
|
|
|
|
write_length = 0u;
|
|
|
|
|
2019-12-05 14:39:26 +01:00
|
|
|
dummyData = (char*)"h 564 321 56 465 46\r\n"
|
2019-07-28 22:28:54 +02:00
|
|
|
"doSomething do Function does something use me, please\r\n"
|
|
|
|
"help h Print this help text help\r\n"
|
|
|
|
"\r\nshellmatta->";
|
|
|
|
|
2019-12-05 14:39:26 +01:00
|
|
|
shellmatta_processData(handle, (char*)"h 564 321 56 465 46\r", 20);
|
2019-07-28 22:28:54 +02:00
|
|
|
|
|
|
|
CHECK( write_length == 141u);
|
|
|
|
CHECK( strcmp(dummyData, write_data) == 0);
|
|
|
|
|
|
|
|
|
|
|
|
write_callCnt = 0u;
|
|
|
|
memset(write_data, 0, sizeof(write_data));
|
|
|
|
write_length = 0u;
|
|
|
|
|
2019-12-05 14:39:26 +01:00
|
|
|
dummyData = (char*)"hr\r\n"
|
2019-07-28 22:28:54 +02:00
|
|
|
"Command: hr not found"
|
|
|
|
"\r\nshellmatta->";
|
|
|
|
|
2019-12-05 14:39:26 +01:00
|
|
|
shellmatta_processData(handle, (char*)"hr\r", 3);
|
2019-07-28 22:28:54 +02:00
|
|
|
|
|
|
|
CHECK( write_length == 39u);
|
|
|
|
REQUIRE( strcmp(dummyData, write_data) == 0);
|
|
|
|
}
|
|
|
|
|
2019-07-30 23:55:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE( "shellmatta heredoc test" ) {
|
|
|
|
|
|
|
|
shellmatta_instance_t inst;
|
|
|
|
shellmatta_handle_t handle;
|
|
|
|
char buffer[1024];
|
|
|
|
char historyBuffer[1024];
|
2019-12-05 14:39:26 +01:00
|
|
|
char *dummyData = (char*)"do this \r\n"
|
2019-07-30 23:55:46 +02:00
|
|
|
"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;
|
|
|
|
|
2019-12-05 14:39:26 +01:00
|
|
|
shellmatta_processData(handle, (char*)"do this << EOF\r\n"
|
2019-07-30 23:55:46 +02:00
|
|
|
"asdf\r\n"
|
|
|
|
"1234\r\n"
|
|
|
|
"EOF\r\n"
|
|
|
|
, 34);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CHECK( doSomethingLength == 20u);
|
|
|
|
REQUIRE( strcmp(dummyData, doSomethingArguments) == 0);
|
|
|
|
}
|