test_integration_help.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2021 - 2024 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_help.cpp
  10. * @brief integration test implementation for the help command of the shellmatta
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. #include "test/framework/catch.hpp"
  14. extern "C" {
  15. #include "test/framework/fff.h"
  16. #include "shellmatta.h"
  17. }
  18. #include <string.h>
  19. FAKE_VALUE_FUNC(shellmatta_retCode_t, writeFct, const char *, uint32_t)
  20. FAKE_VALUE_FUNC(shellmatta_retCode_t, cmdFct1, shellmatta_handle_t, const char *, uint32_t)
  21. FAKE_VALUE_FUNC(shellmatta_retCode_t, cmdFct2, shellmatta_handle_t, const char *, uint32_t)
  22. FAKE_VALUE_FUNC(shellmatta_retCode_t, cmdFct3, shellmatta_handle_t, const char *, uint32_t)
  23. static char fakeWriteData[1024];
  24. static uint32_t fakeWriteLength;
  25. static shellmatta_retCode_t writeFct_customFake(const char* data, uint32_t length)
  26. {
  27. while((length > 0) && (fakeWriteLength < sizeof(fakeWriteData)))
  28. {
  29. fakeWriteData[fakeWriteLength] = *data;
  30. data ++;
  31. length --;
  32. fakeWriteLength ++;
  33. }
  34. return SHELLMATTA_OK;
  35. }
  36. /* List of fakes */
  37. #define FFF_FAKES_LIST(FAKE) \
  38. FAKE(writeFct) \
  39. FAKE(cmdFct1) \
  40. FAKE(cmdFct2) \
  41. FAKE(cmdFct3)
  42. #define PROCESS_INPUT(input) \
  43. CHECK(SHELLMATTA_OK == shellmatta_processData(handle, (char*)(input), sizeof((input)) - 1u));
  44. static shellmatta_cmd_t cmd1 = {(char*)"cmd1", (char*)"1", (char*)"cmd1 [options]", (char*)"cmd1 usage\r\n--option, -o: option", cmdFct1, NULL};
  45. static shellmatta_cmd_t cmd2 = {(char*)"cmd2", NULL, NULL, NULL, cmdFct2, NULL};
  46. static shellmatta_cmd_t cmd3 = {(char*)"cmd3", (char*)"", (char*)"", (char*)"", cmdFct3, NULL};
  47. SCENARIO("Test the help function")
  48. {
  49. GIVEN("An initialized and empty Shellmatta instance")
  50. {
  51. shellmatta_instance_t inst;
  52. shellmatta_handle_t handle;
  53. char buffer[1024u];
  54. char historyBuffer[1024u];
  55. CHECK(SHELLMATTA_OK == shellmatta_doInit( &inst,
  56. &handle,
  57. buffer,
  58. sizeof(buffer),
  59. historyBuffer,
  60. sizeof(historyBuffer),
  61. "shellmatta->",
  62. NULL,
  63. writeFct));
  64. CHECK(SHELLMATTA_OK == shellmatta_addCmd(handle, &cmd1));
  65. CHECK(SHELLMATTA_OK == shellmatta_addCmd(handle, &cmd2));
  66. CHECK(SHELLMATTA_OK == shellmatta_addCmd(handle, &cmd3));
  67. WHEN("The user hits help")
  68. {
  69. FFF_FAKES_LIST(RESET_FAKE)
  70. fakeWriteLength = 0u;
  71. memset(fakeWriteData, 0, sizeof(fakeWriteData));
  72. shellmatta_write_t writeCustomFakeSeq[1] = {writeFct_customFake};
  73. SET_CUSTOM_FAKE_SEQ(writeFct, writeCustomFakeSeq, 1)
  74. PROCESS_INPUT("help\r\n")
  75. THEN("The shellmatta prints the help text")
  76. {
  77. static const char * response = (char*) "help\r\n"
  78. "cmd1 1 cmd1 [options]\r\n"
  79. "cmd2\r\n"
  80. "cmd3\r\n"
  81. "help ? help [command] - print help or usage information\r\n\r\n"
  82. "shellmatta->";
  83. CHECK(writeFct_fake.call_count == 23);
  84. CHECK(strlen(response) == fakeWriteLength);
  85. CHECK_THAT(response, Catch::Matchers::Equals(fakeWriteData));
  86. }
  87. }
  88. WHEN("The user hits ?")
  89. {
  90. FFF_FAKES_LIST(RESET_FAKE)
  91. fakeWriteLength = 0u;
  92. memset(fakeWriteData, 0, sizeof(fakeWriteData));
  93. shellmatta_write_t writeCustomFakeSeq[1] = {writeFct_customFake};
  94. SET_CUSTOM_FAKE_SEQ(writeFct, writeCustomFakeSeq, 1)
  95. PROCESS_INPUT("?\r\n")
  96. THEN("The shellmatta prints the help text")
  97. {
  98. static const char * response = (char*) "?\r\n"
  99. "cmd1 1 cmd1 [options]\r\n"
  100. "cmd2\r\n"
  101. "cmd3\r\n"
  102. "help ? help [command] - print help or usage information\r\n\r\n"
  103. "shellmatta->";
  104. CHECK(writeFct_fake.call_count == 20);
  105. CHECK(strlen(response) == fakeWriteLength);
  106. CHECK_THAT(response, Catch::Matchers::Equals(fakeWriteData));
  107. }
  108. }
  109. }
  110. }
  111. SCENARIO("Test if the help command prints the usage correctly")
  112. {
  113. GIVEN("An initialized and empty Shellmatta instance")
  114. {
  115. shellmatta_instance_t inst;
  116. shellmatta_handle_t handle;
  117. char buffer[1024u];
  118. char historyBuffer[1024u];
  119. CHECK(SHELLMATTA_OK == shellmatta_doInit( &inst,
  120. &handle,
  121. buffer,
  122. sizeof(buffer),
  123. historyBuffer,
  124. sizeof(historyBuffer),
  125. "shellmatta->",
  126. NULL,
  127. writeFct));
  128. CHECK(SHELLMATTA_OK == shellmatta_addCmd(handle, &cmd1));
  129. CHECK(SHELLMATTA_OK == shellmatta_addCmd(handle, &cmd2));
  130. CHECK(SHELLMATTA_OK == shellmatta_addCmd(handle, &cmd3));
  131. WHEN("The user requests usage information from a valid command")
  132. {
  133. FFF_FAKES_LIST(RESET_FAKE)
  134. fakeWriteLength = 0u;
  135. memset(fakeWriteData, 0, sizeof(fakeWriteData));
  136. shellmatta_write_t writeCustomFakeSeq[1] = {writeFct_customFake};
  137. SET_CUSTOM_FAKE_SEQ(writeFct, writeCustomFakeSeq, 1)
  138. PROCESS_INPUT("help cmd1\r\n")
  139. THEN("The shellmatta prints the help text")
  140. {
  141. static const char * response = (char*) "help cmd1\r\n"
  142. "Help for command: cmd1 (1)\r\n\r\n"
  143. "cmd1 [options]\r\n\r\n"
  144. "Usage: \r\n"
  145. "cmd1 usage\r\n--option, -o: option\r\n"
  146. "\r\nshellmatta->";
  147. CHECK(writeFct_fake.call_count == 22);
  148. CHECK(strlen(response) == fakeWriteLength);
  149. CHECK_THAT(response, Catch::Matchers::Equals(fakeWriteData));
  150. }
  151. }
  152. WHEN("The user requests usage information from a valid command using its alias")
  153. {
  154. FFF_FAKES_LIST(RESET_FAKE)
  155. fakeWriteLength = 0u;
  156. memset(fakeWriteData, 0, sizeof(fakeWriteData));
  157. shellmatta_write_t writeCustomFakeSeq[1] = {writeFct_customFake};
  158. SET_CUSTOM_FAKE_SEQ(writeFct, writeCustomFakeSeq, 1)
  159. PROCESS_INPUT("help 1\r\n")
  160. THEN("The shellmatta prints the help text")
  161. {
  162. static const char * response = (char*) "help 1\r\n"
  163. "Help for command: cmd1 (1)\r\n\r\n"
  164. "cmd1 [options]\r\n\r\n"
  165. "Usage: \r\n"
  166. "cmd1 usage\r\n--option, -o: option\r\n"
  167. "\r\nshellmatta->";
  168. CHECK(writeFct_fake.call_count == 19);
  169. CHECK(strlen(response) == fakeWriteLength);
  170. CHECK_THAT(response, Catch::Matchers::Equals(fakeWriteData));
  171. }
  172. }
  173. WHEN("The user requests usage information from an empty command")
  174. {
  175. FFF_FAKES_LIST(RESET_FAKE)
  176. fakeWriteLength = 0u;
  177. memset(fakeWriteData, 0, sizeof(fakeWriteData));
  178. shellmatta_write_t writeCustomFakeSeq[1] = {writeFct_customFake};
  179. SET_CUSTOM_FAKE_SEQ(writeFct, writeCustomFakeSeq, 1)
  180. PROCESS_INPUT("help cmd2\r\n")
  181. THEN("The shellmatta prints the help text - without alias, help and usage text")
  182. {
  183. static const char * response = (char*) "help cmd2\r\n"
  184. "Help for command: cmd2\r\n"
  185. "\r\nshellmatta->";
  186. CHECK(writeFct_fake.call_count == 15);
  187. CHECK(strlen(response) == fakeWriteLength);
  188. CHECK_THAT(response, Catch::Matchers::Equals(fakeWriteData));
  189. }
  190. }
  191. WHEN("The user requests usage information from an empty stringed command")
  192. {
  193. FFF_FAKES_LIST(RESET_FAKE)
  194. fakeWriteLength = 0u;
  195. memset(fakeWriteData, 0, sizeof(fakeWriteData));
  196. shellmatta_write_t writeCustomFakeSeq[1] = {writeFct_customFake};
  197. SET_CUSTOM_FAKE_SEQ(writeFct, writeCustomFakeSeq, 1)
  198. PROCESS_INPUT("help cmd3\r\n")
  199. THEN("The shellmatta prints the help text - without alias, help and usage text")
  200. {
  201. static const char * response = (char*) "help cmd3\r\n"
  202. "Help for command: cmd3\r\n"
  203. "\r\nshellmatta->";
  204. CHECK(writeFct_fake.call_count == 15);
  205. CHECK(strlen(response) == fakeWriteLength);
  206. CHECK_THAT(response, Catch::Matchers::Equals(fakeWriteData));
  207. }
  208. }
  209. WHEN("The user adds additional arguments to the help command")
  210. {
  211. FFF_FAKES_LIST(RESET_FAKE)
  212. fakeWriteLength = 0u;
  213. memset(fakeWriteData, 0, sizeof(fakeWriteData));
  214. shellmatta_write_t writeCustomFakeSeq[1] = {writeFct_customFake};
  215. SET_CUSTOM_FAKE_SEQ(writeFct, writeCustomFakeSeq, 1)
  216. PROCESS_INPUT("help cmd2 foo bar meow this is nonsense\r\n")
  217. THEN("The shellmatta ignores the superflous arguments")
  218. {
  219. static const char * response = (char*) "help cmd2 foo bar meow this is nonsense\r\n"
  220. "Help for command: cmd2\r\n"
  221. "\r\nshellmatta->";
  222. CHECK(writeFct_fake.call_count == 45);
  223. CHECK(strlen(response) == fakeWriteLength);
  224. CHECK_THAT(response, Catch::Matchers::Equals(fakeWriteData));
  225. }
  226. }
  227. WHEN("The user requests help of a nonexisting command")
  228. {
  229. FFF_FAKES_LIST(RESET_FAKE)
  230. fakeWriteLength = 0u;
  231. memset(fakeWriteData, 0, sizeof(fakeWriteData));
  232. shellmatta_write_t writeCustomFakeSeq[1] = {writeFct_customFake};
  233. SET_CUSTOM_FAKE_SEQ(writeFct, writeCustomFakeSeq, 1)
  234. PROCESS_INPUT("help cmd4\r\n")
  235. THEN("The shellmatta prints the help list")
  236. {
  237. static const char * response = (char*) "help cmd4\r\n"
  238. "cmd1 1 cmd1 [options]\r\n"
  239. "cmd2\r\n"
  240. "cmd3\r\n"
  241. "help ? help [command] - print help or usage information\r\n\r\n"
  242. "shellmatta->";
  243. CHECK(writeFct_fake.call_count == 28);
  244. CHECK(strlen(response) == fakeWriteLength);
  245. CHECK_THAT(response, Catch::Matchers::Equals(fakeWriteData));
  246. }
  247. }
  248. }
  249. }