test_integration_opt.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_opt.cpp
  10. * @brief integration test implementation for the 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 parseOpts(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. while(SHELLMATTA_OK == shellmatta_opt(handle, (char*)"abcde:f::", &option, &argumentString, &argumentLength))
  87. {
  88. optionCount ++;
  89. switch(option)
  90. {
  91. case 'a':
  92. cntA ++;
  93. argA = argumentString;
  94. lenA = argumentLength;
  95. break;
  96. case 'b':
  97. cntB ++;
  98. argB = argumentString;
  99. lenB = argumentLength;
  100. break;
  101. case 'c':
  102. cntC ++;
  103. argC = argumentString;
  104. lenC = argumentLength;
  105. break;
  106. case 'd':
  107. cntD ++;
  108. argD = argumentString;
  109. lenD = argumentLength;
  110. break;
  111. case 'e':
  112. cntE ++;
  113. argE = argumentString;
  114. lenE = argumentLength;
  115. break;
  116. case 'f':
  117. cntF ++;
  118. argF = argumentString;
  119. lenF = argumentLength;
  120. break;
  121. default:
  122. cntDef ++;
  123. argDef = argumentString;
  124. lenDef = argumentLength;
  125. break;
  126. }
  127. }
  128. shellmatta_printf(handle, "parseOpts - cnt: %u\r\n", optionCount);
  129. return SHELLMATTA_OK;
  130. }
  131. shellmatta_cmd_t parseOptsCmd = {(char*)"parseOpts", (char*)"opt", NULL, NULL, parseOpts, NULL};
  132. TEST_CASE( "shellmatta option parser 1" ) {
  133. shellmatta_instance_t inst;
  134. shellmatta_handle_t handle;
  135. char buffer[1024];
  136. char historyBuffer[1024];
  137. char *dummyData = (char*)"parseOpts -a -e meow\r\nparseOpts - cnt: 2\r\n\r\nshellmatta->";
  138. shellmatta_doInit( &inst,
  139. &handle,
  140. buffer,
  141. sizeof(buffer),
  142. historyBuffer,
  143. sizeof(historyBuffer),
  144. "shellmatta->",
  145. NULL,
  146. writeFct);
  147. initTestcase();
  148. write_callCnt = 0u;
  149. memset(write_data, 0, sizeof(write_data));
  150. write_length = 0u;
  151. shellmatta_addCmd(handle, &parseOptsCmd);
  152. shellmatta_processData(handle, (char*)"parseOpts -a -e meow\r", 21);
  153. CHECK( cntA == 1u );
  154. CHECK( NULL == argA);
  155. CHECK( 0u == lenA );
  156. CHECK( cntE == 1u );
  157. CHECK(((NULL != argE) && (0u == memcmp(argE, "meow", 4))));
  158. CHECK( lenE == 4u );
  159. CHECK( (cntB == 0u && cntC == 0u && cntD == 0u && cntF == 0u && cntDef == 0u) );
  160. CHECK( write_length == strlen(dummyData));
  161. REQUIRE( strcmp(dummyData, write_data) == 0);
  162. }
  163. TEST_CASE( "shellmatta option parser 2 - ignore \"--\"" ) {
  164. shellmatta_instance_t inst;
  165. shellmatta_handle_t handle;
  166. char buffer[1024];
  167. char historyBuffer[1024];
  168. char *dummyData = (char*)"parseOpts -a -e meow --\r\nparseOpts - cnt: 2\r\n\r\nshellmatta->";
  169. shellmatta_doInit( &inst,
  170. &handle,
  171. buffer,
  172. sizeof(buffer),
  173. historyBuffer,
  174. sizeof(historyBuffer),
  175. "shellmatta->",
  176. NULL,
  177. writeFct);
  178. initTestcase();
  179. write_callCnt = 0u;
  180. memset(write_data, 0, sizeof(write_data));
  181. write_length = 0u;
  182. shellmatta_addCmd(handle, &parseOptsCmd);
  183. shellmatta_processData(handle, (char*)"parseOpts -a -e meow --\r", 24);
  184. CHECK( cntA == 1u );
  185. CHECK( NULL == argA);
  186. CHECK( 0u == lenA );
  187. CHECK( cntE == 1u );
  188. CHECK(((NULL != argE) && (0u == memcmp(argE, "meow", 4))));
  189. CHECK( lenE == 4u );
  190. CHECK( (cntB == 0u && cntC == 0u && cntD == 0u && cntF == 0u && cntDef == 0u) );
  191. CHECK( write_length == strlen(dummyData));
  192. REQUIRE( strcmp(dummyData, write_data) == 0);
  193. }