test_integration.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "test/framework/catch.hpp"
  2. extern "C" {
  3. #include "shellmatta.h"
  4. }
  5. #include <string.h>
  6. static uint32_t write_callCnt = 0u;
  7. static char write_data[1024];
  8. static uint32_t write_length;
  9. static const char *doSomethingArguments;
  10. static uint32_t doSomethingLength;
  11. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  12. {
  13. write_callCnt ++;
  14. while((length > 0) && (write_length < sizeof(write_data)))
  15. {
  16. write_data[write_length] = *data;
  17. data ++;
  18. length --;
  19. write_length ++;
  20. }
  21. return SHELLMATTA_OK;
  22. }
  23. static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  24. {
  25. doSomethingArguments = arguments;
  26. doSomethingLength = length;
  27. shellmatta_printf(handle, "%s - length: %u", arguments, length);
  28. return SHELLMATTA_OK;
  29. }
  30. shellmatta_cmd_t doSomethingCmd = {(char*)"doSomething", (char*)"do", (char*)"Function does something", (char*)"use me, please", doSomething, NULL};
  31. TEST_CASE( "shellmatta empty function" ) {
  32. shellmatta_instance_t inst;
  33. shellmatta_handle_t handle;
  34. char buffer[1024];
  35. char historyBuffer[1024];
  36. char *dummyData = (char*)"\r\nshellmatta->";
  37. shellmatta_doInit( &inst,
  38. &handle,
  39. buffer,
  40. sizeof(buffer),
  41. historyBuffer,
  42. sizeof(historyBuffer),
  43. "shellmatta->",
  44. NULL,
  45. writeFct);
  46. write_callCnt = 0u;
  47. memset(write_data, 0, sizeof(write_data));
  48. write_length = 0u;
  49. shellmatta_processData(handle, (char*)"\r", 1);
  50. CHECK( write_length == 14u);
  51. REQUIRE( strcmp(dummyData, write_data) == 0);
  52. }
  53. TEST_CASE( "shellmatta help function" ) {
  54. shellmatta_instance_t inst;
  55. shellmatta_handle_t handle;
  56. char buffer[1024];
  57. char historyBuffer[1024];
  58. char *dummyData = (char*)"h\r\n"
  59. "doSomething do Function does something use me, please\r\n"
  60. "help h Print this help text help\r\n"
  61. "\r\nshellmatta->";
  62. shellmatta_doInit( &inst,
  63. &handle,
  64. buffer,
  65. sizeof(buffer),
  66. historyBuffer,
  67. sizeof(historyBuffer),
  68. "shellmatta->",
  69. NULL,
  70. writeFct);
  71. shellmatta_addCmd(handle, &doSomethingCmd);
  72. write_callCnt = 0u;
  73. memset(write_data, 0, sizeof(write_data));
  74. write_length = 0u;
  75. shellmatta_processData(handle, (char*)"h\r", 2);
  76. CHECK( write_length == 123u);
  77. CHECK( strcmp(dummyData, write_data) == 0);
  78. write_callCnt = 0u;
  79. memset(write_data, 0, sizeof(write_data));
  80. write_length = 0u;
  81. dummyData = (char*)"h 564 321 56 465 46\r\n"
  82. "doSomething do Function does something use me, please\r\n"
  83. "help h Print this help text help\r\n"
  84. "\r\nshellmatta->";
  85. shellmatta_processData(handle, (char*)"h 564 321 56 465 46\r", 20);
  86. CHECK( write_length == 141u);
  87. CHECK( strcmp(dummyData, write_data) == 0);
  88. write_callCnt = 0u;
  89. memset(write_data, 0, sizeof(write_data));
  90. write_length = 0u;
  91. dummyData = (char*)"hr\r\n"
  92. "Command: hr not found"
  93. "\r\nshellmatta->";
  94. shellmatta_processData(handle, (char*)"hr\r", 3);
  95. CHECK( write_length == 39u);
  96. REQUIRE( strcmp(dummyData, write_data) == 0);
  97. }
  98. TEST_CASE( "shellmatta heredoc test" ) {
  99. shellmatta_instance_t inst;
  100. shellmatta_handle_t handle;
  101. char buffer[1024];
  102. char historyBuffer[1024];
  103. char *dummyData = (char*)"do this \r\n"
  104. "asdf\r\n"
  105. "1234";
  106. shellmatta_doInit( &inst,
  107. &handle,
  108. buffer,
  109. sizeof(buffer),
  110. historyBuffer,
  111. sizeof(historyBuffer),
  112. "shellmatta->",
  113. NULL,
  114. writeFct);
  115. shellmatta_addCmd(handle, &doSomethingCmd);
  116. doSomethingArguments = NULL;
  117. doSomethingLength = 0u;
  118. shellmatta_processData(handle, (char*)"do this << EOF\r\n"
  119. "asdf\r\n"
  120. "1234\r\n"
  121. "EOF\r\n"
  122. , 34);
  123. CHECK( doSomethingLength == 20u);
  124. REQUIRE( strcmp(dummyData, doSomethingArguments) == 0);
  125. }
  126. TEST_CASE( "shellmatta remove function" ) {
  127. shellmatta_instance_t inst;
  128. shellmatta_handle_t handle;
  129. char buffer[1024];
  130. char historyBuffer[1024];
  131. char *dummyData = (char*)"h\r\n"
  132. "doSomething do Function does something use me, please\r\n"
  133. "help h Print this help text help\r\n"
  134. "\r\nshellmatta->";
  135. shellmatta_doInit( &inst,
  136. &handle,
  137. buffer,
  138. sizeof(buffer),
  139. historyBuffer,
  140. sizeof(historyBuffer),
  141. "shellmatta->",
  142. NULL,
  143. writeFct);
  144. shellmatta_addCmd(handle, &doSomethingCmd);
  145. write_callCnt = 0u;
  146. memset(write_data, 0, sizeof(write_data));
  147. write_length = 0u;
  148. shellmatta_processData(handle, (char*)"h\r", 2);
  149. CHECK( write_length == 123u);
  150. CHECK( strcmp(dummyData, write_data) == 0);
  151. write_callCnt = 0u;
  152. memset(write_data, 0, sizeof(write_data));
  153. write_length = 0u;
  154. dummyData = (char*)"h 564 321 56 465 46\r\n"
  155. "doSomething do Function does something use me, please\r\n"
  156. "help h Print this help text help\r\n"
  157. "\r\nshellmatta->";
  158. shellmatta_processData(handle, (char*)"h 564 321 56 465 46\r", 20);
  159. CHECK( write_length == 141u);
  160. CHECK( strcmp(dummyData, write_data) == 0);
  161. write_callCnt = 0u;
  162. memset(write_data, 0, sizeof(write_data));
  163. write_length = 0u;
  164. shellmatta_removeCmd(handle, &doSomethingCmd);
  165. shellmatta_processData(handle, (char*)"h 564 321 56 465 46\r", 20);
  166. dummyData = (char*)"h 564 321 56 465 46\r\n"
  167. "help h Print this help text help\r\n"
  168. "\r\nshellmatta->";
  169. printf("sdfsd sdf sdf sdf sdf sd fds\n%s", write_data);
  170. CHECK( write_length == 72u);
  171. REQUIRE( strcmp(dummyData, write_data) == 0);
  172. }