test_integration.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  10. {
  11. write_callCnt ++;
  12. while((length > 0) && (write_length < sizeof(write_data)))
  13. {
  14. write_data[write_length] = *data;
  15. data ++;
  16. length --;
  17. write_length ++;
  18. }
  19. return SHELLMATTA_OK;
  20. }
  21. static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  22. {
  23. return SHELLMATTA_OK;
  24. }
  25. shellmatta_cmd_t doSomethingCmd = {"doSomething", "do", "Function does something", "use me, please", doSomething, NULL};
  26. TEST_CASE( "shellmatta empty function" ) {
  27. shellmatta_instance_t inst;
  28. shellmatta_handle_t handle;
  29. char buffer[1024];
  30. char historyBuffer[1024];
  31. char *dummyData = "\r\nshellmatta->";
  32. shellmatta_doInit( &inst,
  33. &handle,
  34. buffer,
  35. sizeof(buffer),
  36. historyBuffer,
  37. sizeof(historyBuffer),
  38. "shellmatta->",
  39. NULL,
  40. writeFct);
  41. write_callCnt = 0u;
  42. memset(write_data, 0, sizeof(write_data));
  43. write_length = 0u;
  44. shellmatta_processData(handle, "\r", 1);
  45. CHECK( write_length == 14u);
  46. REQUIRE( strcmp(dummyData, write_data) == 0);
  47. }
  48. TEST_CASE( "shellmatta help function" ) {
  49. shellmatta_instance_t inst;
  50. shellmatta_handle_t handle;
  51. char buffer[1024];
  52. char historyBuffer[1024];
  53. char *dummyData = "h\r\n"
  54. "doSomething do Function does something use me, please\r\n"
  55. "help h Print this help text help\r\n"
  56. "\r\nshellmatta->";
  57. shellmatta_doInit( &inst,
  58. &handle,
  59. buffer,
  60. sizeof(buffer),
  61. historyBuffer,
  62. sizeof(historyBuffer),
  63. "shellmatta->",
  64. NULL,
  65. writeFct);
  66. shellmatta_addCmd(handle, &doSomethingCmd);
  67. write_callCnt = 0u;
  68. memset(write_data, 0, sizeof(write_data));
  69. write_length = 0u;
  70. shellmatta_processData(handle, "h\r", 2);
  71. CHECK( write_length == 123u);
  72. CHECK( strcmp(dummyData, write_data) == 0);
  73. write_callCnt = 0u;
  74. memset(write_data, 0, sizeof(write_data));
  75. write_length = 0u;
  76. dummyData = "h 564 321 56 465 46\r\n"
  77. "doSomething do Function does something use me, please\r\n"
  78. "help h Print this help text help\r\n"
  79. "\r\nshellmatta->";
  80. shellmatta_processData(handle, "h 564 321 56 465 46\r", 20);
  81. CHECK( write_length == 141u);
  82. CHECK( strcmp(dummyData, write_data) == 0);
  83. write_callCnt = 0u;
  84. memset(write_data, 0, sizeof(write_data));
  85. write_length = 0u;
  86. dummyData = "hr\r\n"
  87. "Command: hr not found"
  88. "\r\nshellmatta->";
  89. shellmatta_processData(handle, "hr\r", 3);
  90. CHECK( write_length == 39u);
  91. REQUIRE( strcmp(dummyData, write_data) == 0);
  92. }