test_integration_optLong.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2019 - 2021 Stefan Strobel <stefan.strobel@shimatta.net>
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file test_integration_optLong.cpp
  10. * @brief integration test implementation for the long option parser of the shellmatta
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. #include "test/framework/catch.hpp"
  14. extern "C" {
  15. #include "shellmatta.h"
  16. }
  17. #include <string.h>
  18. static uint32_t write_callCnt = 0u;
  19. static char write_data[1024];
  20. static uint32_t write_length;
  21. static uint32_t cntA = 0u;
  22. static uint32_t cntB = 0u;
  23. static uint32_t cntC = 0u;
  24. static uint32_t cntD = 0u;
  25. static uint32_t cntE = 0u;
  26. static uint32_t cntF = 0u;
  27. static uint32_t cntDef = 0u;
  28. static char *argA = NULL;
  29. static char *argB = NULL;
  30. static char *argC = NULL;
  31. static char *argD = NULL;
  32. static char *argE = NULL;
  33. static char *argF = NULL;
  34. static char *argDef = NULL;
  35. static uint32_t lenA = 0u;
  36. static uint32_t lenB = 0u;
  37. static uint32_t lenC = 0u;
  38. static uint32_t lenD = 0u;
  39. static uint32_t lenE = 0u;
  40. static uint32_t lenF = 0u;
  41. static uint32_t lenDef = 0u;
  42. static void initTestcase(void)
  43. {
  44. cntA = 0u;
  45. cntB = 0u;
  46. cntC = 0u;
  47. cntD = 0u;
  48. cntE = 0u;
  49. cntF = 0u;
  50. cntDef = 0u;
  51. argA = NULL;
  52. argB = NULL;
  53. argC = NULL;
  54. argD = NULL;
  55. argE = NULL;
  56. argF = NULL;
  57. argDef = NULL;
  58. lenA = 0u;
  59. lenB = 0u;
  60. lenC = 0u;
  61. lenD = 0u;
  62. lenE = 0u;
  63. lenF = 0u;
  64. lenDef = 0u;
  65. }
  66. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  67. {
  68. write_callCnt ++;
  69. while((length > 0) && (write_length < sizeof(write_data)))
  70. {
  71. write_data[write_length] = *data;
  72. data ++;
  73. length --;
  74. write_length ++;
  75. }
  76. return SHELLMATTA_OK;
  77. }
  78. static shellmatta_retCode_t parseOptsLong(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  79. {
  80. (void) arguments;
  81. (void) length;
  82. char option;
  83. char *argumentString;
  84. uint32_t argumentLength;
  85. uint32_t optionCount = 0u;
  86. shellmatta_opt_long_t longOptions[] = {
  87. {"auto", 'a', SHELLMATTA_OPT_ARG_NONE},
  88. {"build", 'b', SHELLMATTA_OPT_ARG_NONE},
  89. {"cat", 'c', SHELLMATTA_OPT_ARG_NONE},
  90. {"doom", 'd', SHELLMATTA_OPT_ARG_NONE},
  91. {"erase", 'e', SHELLMATTA_OPT_ARG_REQUIRED},
  92. {"fuck", 'f', SHELLMATTA_OPT_ARG_OPTIONAL},
  93. {NULL, '\0', SHELLMATTA_OPT_ARG_NONE}
  94. };
  95. while(SHELLMATTA_OK == shellmatta_opt_long(handle, longOptions, &option, &argumentString, &argumentLength))
  96. {
  97. optionCount ++;
  98. switch(option)
  99. {
  100. case 'a':
  101. cntA ++;
  102. argA = argumentString;
  103. lenA = argumentLength;
  104. break;
  105. case 'b':
  106. cntB ++;
  107. argB = argumentString;
  108. lenB = argumentLength;
  109. break;
  110. case 'c':
  111. cntC ++;
  112. argC = argumentString;
  113. lenC = argumentLength;
  114. break;
  115. case 'd':
  116. cntD ++;
  117. argD = argumentString;
  118. lenD = argumentLength;
  119. break;
  120. case 'e':
  121. cntE ++;
  122. argE = argumentString;
  123. lenE = argumentLength;
  124. break;
  125. case 'f':
  126. cntF ++;
  127. argF = argumentString;
  128. lenF = argumentLength;
  129. break;
  130. default:
  131. cntDef ++;
  132. argDef = argumentString;
  133. lenDef = argumentLength;
  134. break;
  135. }
  136. }
  137. shellmatta_printf(handle, "parseOpts - cnt: %u\r\n", optionCount);
  138. return SHELLMATTA_OK;
  139. }
  140. shellmatta_cmd_t parseOptsLongCmd = {(char*)"parseOpts", (char*)"opt", NULL, NULL, parseOptsLong, NULL};
  141. TEST_CASE( "shellmatta long option parser 1" ) {
  142. shellmatta_instance_t inst;
  143. shellmatta_handle_t handle;
  144. char buffer[1024];
  145. char historyBuffer[1024];
  146. char *dummyData = (char*)"parseOpts -a -e meow\r\nparseOpts - cnt: 2\r\n\r\nshellmatta->";
  147. shellmatta_doInit( &inst,
  148. &handle,
  149. buffer,
  150. sizeof(buffer),
  151. historyBuffer,
  152. sizeof(historyBuffer),
  153. "shellmatta->",
  154. NULL,
  155. writeFct);
  156. initTestcase();
  157. write_callCnt = 0u;
  158. memset(write_data, 0, sizeof(write_data));
  159. write_length = 0u;
  160. shellmatta_addCmd(handle, &parseOptsLongCmd);
  161. shellmatta_processData(handle, (char*)"parseOpts -a -e meow\r", 21);
  162. CHECK( cntA == 1u );
  163. CHECK( NULL == argA);
  164. CHECK( 0u == lenA );
  165. CHECK( cntE == 1u );
  166. CHECK(((NULL != argE) && (0u == memcmp(argE, "meow", 4))));
  167. CHECK( lenE == 4u );
  168. CHECK( (cntB == 0u && cntC == 0u && cntD == 0u && cntF == 0u && cntDef == 0u) );
  169. CHECK( write_length == strlen(dummyData));
  170. REQUIRE( strcmp(dummyData, write_data) == 0);
  171. }
  172. TEST_CASE( "shellmatta long option parser 2" ) {
  173. shellmatta_instance_t inst;
  174. shellmatta_handle_t handle;
  175. char buffer[1024];
  176. char historyBuffer[1024];
  177. char *dummyData = (char*)"parseOpts --auto --erase meow -- --lalelu -u\r\nparseOpts - cnt: 4\r\n\r\nshellmatta->";
  178. shellmatta_doInit( &inst,
  179. &handle,
  180. buffer,
  181. sizeof(buffer),
  182. historyBuffer,
  183. sizeof(historyBuffer),
  184. "shellmatta->",
  185. NULL,
  186. writeFct);
  187. initTestcase();
  188. write_callCnt = 0u;
  189. memset(write_data, 0, sizeof(write_data));
  190. write_length = 0u;
  191. shellmatta_addCmd(handle, &parseOptsLongCmd);
  192. shellmatta_processData(handle, (char*)"parseOpts --auto --erase meow -- --lalelu -u\r", 45);
  193. CHECK( cntA == 1u );
  194. CHECK( NULL == argA);
  195. CHECK( 0u == lenA );
  196. CHECK( cntE == 1u );
  197. CHECK(((NULL != argE) && (0u == memcmp(argE, "meow", 4))));
  198. CHECK( lenE == 4u );
  199. CHECK( (cntB == 0u && cntC == 0u && cntD == 0u && cntF == 0u) );
  200. CHECK( cntDef == 2u );
  201. CHECK( lenDef == 2u );
  202. CHECK( write_length == strlen(dummyData));
  203. REQUIRE( strcmp(dummyData, write_data) == 0);
  204. }