2020-03-08 22:02:51 +01:00
|
|
|
/*
|
2021-01-22 23:13:20 +01:00
|
|
|
* Copyright (c) 2019 - 2021 Stefan Strobel <stefan.strobel@shimatta.net>
|
2020-03-08 22:02:51 +01:00
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file test_integration_opt.cpp
|
|
|
|
* @brief integration test implementation for the option parser of the shellmatta
|
|
|
|
* @author Stefan Strobel <stefan.strobel@shimatta.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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;
|
2020-03-16 22:08:06 +01:00
|
|
|
static uint32_t cntA = 0u;
|
|
|
|
static uint32_t cntB = 0u;
|
|
|
|
static uint32_t cntC = 0u;
|
|
|
|
static uint32_t cntD = 0u;
|
|
|
|
static uint32_t cntE = 0u;
|
|
|
|
static uint32_t cntF = 0u;
|
|
|
|
static uint32_t cntDef = 0u;
|
|
|
|
static char *argA = NULL;
|
|
|
|
static char *argB = NULL;
|
|
|
|
static char *argC = NULL;
|
|
|
|
static char *argD = NULL;
|
|
|
|
static char *argE = NULL;
|
|
|
|
static char *argF = NULL;
|
|
|
|
static char *argDef = NULL;
|
|
|
|
static uint32_t lenA = 0u;
|
|
|
|
static uint32_t lenB = 0u;
|
|
|
|
static uint32_t lenC = 0u;
|
|
|
|
static uint32_t lenD = 0u;
|
|
|
|
static uint32_t lenE = 0u;
|
|
|
|
static uint32_t lenF = 0u;
|
|
|
|
static uint32_t lenDef = 0u;
|
|
|
|
|
|
|
|
static void initTestcase(void)
|
|
|
|
{
|
|
|
|
cntA = 0u;
|
|
|
|
cntB = 0u;
|
|
|
|
cntC = 0u;
|
|
|
|
cntD = 0u;
|
|
|
|
cntE = 0u;
|
|
|
|
cntF = 0u;
|
|
|
|
cntDef = 0u;
|
|
|
|
argA = NULL;
|
|
|
|
argB = NULL;
|
|
|
|
argC = NULL;
|
|
|
|
argD = NULL;
|
|
|
|
argE = NULL;
|
|
|
|
argF = NULL;
|
|
|
|
argDef = NULL;
|
|
|
|
lenA = 0u;
|
|
|
|
lenB = 0u;
|
|
|
|
lenC = 0u;
|
|
|
|
lenD = 0u;
|
|
|
|
lenE = 0u;
|
|
|
|
lenF = 0u;
|
|
|
|
lenDef = 0u;
|
|
|
|
}
|
2020-03-08 22:02:51 +01: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 parseOpts(shellmatta_handle_t handle, const char *arguments, uint32_t length)
|
|
|
|
{
|
|
|
|
(void) arguments;
|
|
|
|
(void) length;
|
|
|
|
char option;
|
|
|
|
char *argumentString;
|
|
|
|
uint32_t argumentLength;
|
2020-03-16 22:08:06 +01:00
|
|
|
uint32_t optionCount = 0u;
|
2020-03-08 22:02:51 +01:00
|
|
|
|
|
|
|
while(SHELLMATTA_OK == shellmatta_opt(handle, (char*)"abcde:f::", &option, &argumentString, &argumentLength))
|
|
|
|
{
|
2020-03-16 22:08:06 +01:00
|
|
|
optionCount ++;
|
2020-03-08 22:02:51 +01:00
|
|
|
switch(option)
|
|
|
|
{
|
|
|
|
case 'a':
|
2020-03-16 22:08:06 +01:00
|
|
|
cntA ++;
|
|
|
|
argA = argumentString;
|
|
|
|
lenA = argumentLength;
|
2020-03-08 22:02:51 +01:00
|
|
|
break;
|
|
|
|
case 'b':
|
2020-03-16 22:08:06 +01:00
|
|
|
cntB ++;
|
|
|
|
argB = argumentString;
|
|
|
|
lenB = argumentLength;
|
2020-03-08 22:02:51 +01:00
|
|
|
break;
|
|
|
|
case 'c':
|
2020-03-16 22:08:06 +01:00
|
|
|
cntC ++;
|
|
|
|
argC = argumentString;
|
|
|
|
lenC = argumentLength;
|
2020-03-08 22:02:51 +01:00
|
|
|
break;
|
|
|
|
case 'd':
|
2020-03-16 22:08:06 +01:00
|
|
|
cntD ++;
|
|
|
|
argD = argumentString;
|
|
|
|
lenD = argumentLength;
|
2020-03-08 22:02:51 +01:00
|
|
|
break;
|
|
|
|
case 'e':
|
2020-03-16 22:08:06 +01:00
|
|
|
cntE ++;
|
|
|
|
argE = argumentString;
|
|
|
|
lenE = argumentLength;
|
2020-03-08 22:02:51 +01:00
|
|
|
break;
|
|
|
|
case 'f':
|
2020-03-16 22:08:06 +01:00
|
|
|
cntF ++;
|
|
|
|
argF = argumentString;
|
|
|
|
lenF = argumentLength;
|
2020-03-08 22:02:51 +01:00
|
|
|
break;
|
|
|
|
default:
|
2020-03-16 22:08:06 +01:00
|
|
|
cntDef ++;
|
|
|
|
argDef = argumentString;
|
|
|
|
lenDef = argumentLength;
|
2020-03-08 22:02:51 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 22:08:06 +01:00
|
|
|
shellmatta_printf(handle, "parseOpts - cnt: %u\r\n", optionCount);
|
|
|
|
|
2020-03-08 22:02:51 +01:00
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
shellmatta_cmd_t parseOptsCmd = {(char*)"parseOpts", (char*)"opt", NULL, NULL, parseOpts, NULL};
|
|
|
|
|
|
|
|
TEST_CASE( "shellmatta option parser 1" ) {
|
|
|
|
|
|
|
|
shellmatta_instance_t inst;
|
|
|
|
shellmatta_handle_t handle;
|
|
|
|
char buffer[1024];
|
|
|
|
char historyBuffer[1024];
|
2020-03-16 22:08:06 +01:00
|
|
|
char *dummyData = (char*)"parseOpts -a -e meow\r\nparseOpts - cnt: 2\r\n\r\nshellmatta->";
|
2020-03-08 22:02:51 +01:00
|
|
|
|
|
|
|
shellmatta_doInit( &inst,
|
|
|
|
&handle,
|
|
|
|
buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
historyBuffer,
|
|
|
|
sizeof(historyBuffer),
|
|
|
|
"shellmatta->",
|
|
|
|
NULL,
|
|
|
|
writeFct);
|
|
|
|
|
2020-03-16 22:08:06 +01:00
|
|
|
initTestcase();
|
2020-03-08 22:02:51 +01:00
|
|
|
write_callCnt = 0u;
|
|
|
|
memset(write_data, 0, sizeof(write_data));
|
|
|
|
write_length = 0u;
|
|
|
|
|
2020-03-16 22:08:06 +01:00
|
|
|
shellmatta_addCmd(handle, &parseOptsCmd);
|
|
|
|
|
|
|
|
shellmatta_processData(handle, (char*)"parseOpts -a -e meow\r", 21);
|
2020-03-08 22:02:51 +01:00
|
|
|
|
2020-03-16 22:08:06 +01:00
|
|
|
CHECK( cntA == 1u );
|
|
|
|
CHECK( NULL == argA);
|
|
|
|
CHECK( 0u == lenA );
|
|
|
|
CHECK( cntE == 1u );
|
|
|
|
CHECK(((NULL != argE) && (0u == memcmp(argE, "meow", 4))));
|
|
|
|
CHECK( lenE == 4u );
|
|
|
|
CHECK( (cntB == 0u && cntC == 0u && cntD == 0u && cntF == 0u && cntDef == 0u) );
|
2020-03-22 18:56:17 +01:00
|
|
|
CHECK( write_length == strlen(dummyData));
|
2020-03-16 22:08:06 +01:00
|
|
|
REQUIRE( strcmp(dummyData, write_data) == 0);
|
2020-03-08 22:02:51 +01:00
|
|
|
}
|
2020-03-22 18:56:17 +01:00
|
|
|
|
|
|
|
TEST_CASE( "shellmatta option parser 2 - ignore \"--\"" ) {
|
|
|
|
|
|
|
|
shellmatta_instance_t inst;
|
|
|
|
shellmatta_handle_t handle;
|
|
|
|
char buffer[1024];
|
|
|
|
char historyBuffer[1024];
|
|
|
|
char *dummyData = (char*)"parseOpts -a -e meow --\r\nparseOpts - cnt: 2\r\n\r\nshellmatta->";
|
|
|
|
|
|
|
|
shellmatta_doInit( &inst,
|
|
|
|
&handle,
|
|
|
|
buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
historyBuffer,
|
|
|
|
sizeof(historyBuffer),
|
|
|
|
"shellmatta->",
|
|
|
|
NULL,
|
|
|
|
writeFct);
|
|
|
|
|
|
|
|
initTestcase();
|
|
|
|
write_callCnt = 0u;
|
|
|
|
memset(write_data, 0, sizeof(write_data));
|
|
|
|
write_length = 0u;
|
|
|
|
|
|
|
|
shellmatta_addCmd(handle, &parseOptsCmd);
|
|
|
|
|
|
|
|
shellmatta_processData(handle, (char*)"parseOpts -a -e meow --\r", 24);
|
|
|
|
|
|
|
|
CHECK( cntA == 1u );
|
|
|
|
CHECK( NULL == argA);
|
|
|
|
CHECK( 0u == lenA );
|
|
|
|
CHECK( cntE == 1u );
|
|
|
|
CHECK(((NULL != argE) && (0u == memcmp(argE, "meow", 4))));
|
|
|
|
CHECK( lenE == 4u );
|
|
|
|
CHECK( (cntB == 0u && cntC == 0u && cntD == 0u && cntF == 0u && cntDef == 0u) );
|
|
|
|
CHECK( write_length == strlen(dummyData));
|
|
|
|
REQUIRE( strcmp(dummyData, write_data) == 0);
|
|
|
|
}
|
|
|
|
|