test_utils_rewindCursor.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. inst.buffer = buffer;
  18. inst.bufferSize = 20;
  19. inst.cursor = 10;
  20. inst.inputCount = 10;
  21. inst.echoEnabled = true;
  22. inst.write = writeFct;
  23. write_callCnt = 0u;
  24. memset(write_data, 0, sizeof(write_data));
  25. write_length = 0u;
  26. utils_rewindCursor(&inst, 5u);
  27. CHECK( write_callCnt == 1u);
  28. CHECK( write_length == 4u);
  29. REQUIRE( strncmp("\x1b[5D", write_data, 4u) == 0);
  30. }
  31. TEST_CASE( "shellmatta_utils_rewindCursor rewind by 12 with cursor at 10" ) {
  32. shellmatta_instance_t inst;
  33. char buffer[20];
  34. inst.buffer = buffer;
  35. inst.bufferSize = 20;
  36. inst.cursor = 10;
  37. inst.inputCount = 10;
  38. inst.echoEnabled = true;
  39. inst.write = writeFct;
  40. write_callCnt = 0u;
  41. memset(write_data, 0, sizeof(write_data));
  42. write_length = 0u;
  43. utils_rewindCursor(&inst, 12u);
  44. CHECK( write_callCnt == 1u);
  45. CHECK( write_length == 5u);
  46. REQUIRE( strncmp("\x1b[10D", write_data, 5u) == 0);
  47. }
  48. TEST_CASE( "shellmatta_utils_rewindCursor rewind by 0" ) {
  49. shellmatta_instance_t inst;
  50. char buffer[20];
  51. inst.buffer = buffer;
  52. inst.bufferSize = 20;
  53. inst.cursor = 10;
  54. inst.inputCount = 10;
  55. inst.echoEnabled = true;
  56. inst.write = writeFct;
  57. write_callCnt = 0u;
  58. memset(write_data, 0, sizeof(write_data));
  59. write_length = 0u;
  60. utils_rewindCursor(&inst, 0u);
  61. CHECK( write_callCnt == 0u);
  62. CHECK( write_length == 0u);
  63. REQUIRE( strncmp("\0\0\0\0\0", write_data, 4u) == 0);
  64. }