/* * Copyright (c) 2019 - 2021 Stefan Strobel * * 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 */ #include "test/framework/catch.hpp" extern "C" { #include "shellmatta.h" } #include static uint32_t write_callCnt = 0u; static char write_data[1024]; static uint32_t write_length; 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; } 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; uint32_t optionCount = 0u; while(SHELLMATTA_OK == shellmatta_opt(handle, (char*)"abcde:f::", &option, &argumentString, &argumentLength)) { optionCount ++; switch(option) { case 'a': cntA ++; argA = argumentString; lenA = argumentLength; break; case 'b': cntB ++; argB = argumentString; lenB = argumentLength; break; case 'c': cntC ++; argC = argumentString; lenC = argumentLength; break; case 'd': cntD ++; argD = argumentString; lenD = argumentLength; break; case 'e': cntE ++; argE = argumentString; lenE = argumentLength; break; case 'f': cntF ++; argF = argumentString; lenF = argumentLength; break; default: cntDef ++; argDef = argumentString; lenDef = argumentLength; break; } } shellmatta_printf(handle, "parseOpts - cnt: %u\r\n", optionCount); 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]; 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", 21); 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); } 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); }