test_utils_forwardCursor.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "test/framework/catch.hpp"
  2. #include "src/shellmatta_utils.c"
  3. #include <string.h>
  4. static uint32_t write_callCnt = 0u;
  5. static char write_data[10];
  6. static uint32_t write_length;
  7. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  8. {
  9. write_callCnt ++;
  10. strncpy(write_data, data, length);
  11. write_length = length;
  12. return SHELLMATTA_OK;
  13. }
  14. TEST_CASE( "shellmatta_utils_forwardCursor normal" ) {
  15. shellmatta_instance_t inst;
  16. char buffer[20];
  17. char dummyData[29];
  18. inst.buffer = buffer;
  19. inst.bufferSize = 20;
  20. inst.cursor = 10;
  21. inst.inputCount = 20;
  22. inst.echoEnabled = true;
  23. inst.write = writeFct;
  24. write_callCnt = 0u;
  25. memset(write_data, 0, sizeof(write_data));
  26. write_length = 0u;
  27. utils_forwardCursor(&inst, 5u);
  28. CHECK( write_callCnt == 1u);
  29. CHECK( write_length == 4u);
  30. REQUIRE( strncmp("\e[5C", write_data, 4u) == 0);
  31. }
  32. TEST_CASE( "shellmatta_utils_forwardCursor normal echo off" ) {
  33. shellmatta_instance_t inst;
  34. char buffer[20];
  35. char dummyData[29];
  36. inst.buffer = buffer;
  37. inst.bufferSize = 20;
  38. inst.cursor = 10;
  39. inst.inputCount = 20;
  40. inst.echoEnabled = false;
  41. inst.write = writeFct;
  42. write_callCnt = 0u;
  43. memset(write_data, 0, sizeof(write_data));
  44. write_length = 0u;
  45. utils_forwardCursor(&inst, 5u);
  46. CHECK( write_callCnt == 0u);
  47. CHECK( write_length == 0u);
  48. REQUIRE( strncmp("\0\0\0\0", write_data, 4u) == 0);
  49. }
  50. TEST_CASE( "shellmatta_utils_forwardCursor forward by 12 with cursor at 5 and input count at 10" ) {
  51. shellmatta_instance_t inst;
  52. char buffer[20];
  53. char dummyData[29];
  54. inst.buffer = buffer;
  55. inst.bufferSize = 20;
  56. inst.cursor = 5;
  57. inst.inputCount = 10;
  58. inst.echoEnabled = true;
  59. inst.write = writeFct;
  60. write_callCnt = 0u;
  61. memset(write_data, 0, sizeof(write_data));
  62. write_length = 0u;
  63. utils_forwardCursor(&inst, 12u);
  64. CHECK( write_callCnt == 1u);
  65. CHECK( write_length == 4u);
  66. REQUIRE( strncmp("\e[5C", write_data, 4u) == 0);
  67. }
  68. TEST_CASE( "shellmatta_utils_forwardCursor forward by 0" ) {
  69. shellmatta_instance_t inst;
  70. char buffer[20];
  71. char dummyData[29];
  72. inst.buffer = buffer;
  73. inst.bufferSize = 20;
  74. inst.cursor = 10;
  75. inst.inputCount = 10;
  76. inst.echoEnabled = true;
  77. inst.write = writeFct;
  78. write_callCnt = 0u;
  79. memset(write_data, 0, sizeof(write_data));
  80. write_length = 0u;
  81. utils_forwardCursor(&inst, 0u);
  82. CHECK( write_callCnt == 0u);
  83. CHECK( write_length == 0u);
  84. REQUIRE( strncmp("\0\0\0\0\0", write_data, 4u) == 0);
  85. }