test_utils_rewindCursor.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_rewindCursor 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 = 10;
  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_rewindCursor(&inst, 5u);
  28. CHECK( write_callCnt == 1u);
  29. CHECK( write_length == 4u);
  30. REQUIRE( strncmp("\e[5D", write_data, 4u) == 0);
  31. }
  32. TEST_CASE( "shellmatta_utils_rewindCursor rewind by 12 with cursor at 10" ) {
  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 = 10;
  40. inst.echoEnabled = true;
  41. inst.write = writeFct;
  42. write_callCnt = 0u;
  43. memset(write_data, 0, sizeof(write_data));
  44. write_length = 0u;
  45. utils_rewindCursor(&inst, 12u);
  46. CHECK( write_callCnt == 1u);
  47. CHECK( write_length == 5u);
  48. REQUIRE( strncmp("\e[10D", write_data, 5u) == 0);
  49. }
  50. TEST_CASE( "shellmatta_utils_rewindCursor rewind by 0" ) {
  51. shellmatta_instance_t inst;
  52. char buffer[20];
  53. char dummyData[29];
  54. inst.buffer = buffer;
  55. inst.bufferSize = 20;
  56. inst.cursor = 10;
  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_rewindCursor(&inst, 0u);
  64. CHECK( write_callCnt == 0u);
  65. CHECK( write_length == 0u);
  66. REQUIRE( strncmp("\0\0\0\0\0", write_data, 4u) == 0);
  67. }