test_utils_forwardCursor.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. inst.buffer = buffer;
  18. inst.bufferSize = 20;
  19. inst.cursor = 10;
  20. inst.inputCount = 20;
  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_forwardCursor(&inst, 5u);
  27. CHECK( write_callCnt == 1u);
  28. CHECK( write_length == 4u);
  29. REQUIRE( strncmp("\e[5C", write_data, 4u) == 0);
  30. }
  31. TEST_CASE( "shellmatta_utils_forwardCursor normal echo off" ) {
  32. shellmatta_instance_t inst;
  33. char buffer[20];
  34. inst.buffer = buffer;
  35. inst.bufferSize = 20;
  36. inst.cursor = 10;
  37. inst.inputCount = 20;
  38. inst.echoEnabled = false;
  39. inst.write = writeFct;
  40. write_callCnt = 0u;
  41. memset(write_data, 0, sizeof(write_data));
  42. write_length = 0u;
  43. utils_forwardCursor(&inst, 5u);
  44. CHECK( write_callCnt == 0u);
  45. CHECK( write_length == 0u);
  46. REQUIRE( strncmp("\0\0\0\0", write_data, 4u) == 0);
  47. }
  48. TEST_CASE( "shellmatta_utils_forwardCursor forward by 12 with cursor at 5 and input count at 10" ) {
  49. shellmatta_instance_t inst;
  50. char buffer[20];
  51. inst.buffer = buffer;
  52. inst.bufferSize = 20;
  53. inst.cursor = 5;
  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_forwardCursor(&inst, 12u);
  61. CHECK( write_callCnt == 1u);
  62. CHECK( write_length == 4u);
  63. REQUIRE( strncmp("\e[5C", write_data, 4u) == 0);
  64. }
  65. TEST_CASE( "shellmatta_utils_forwardCursor forward by 0" ) {
  66. shellmatta_instance_t inst;
  67. char buffer[20];
  68. inst.buffer = buffer;
  69. inst.bufferSize = 20;
  70. inst.cursor = 10;
  71. inst.inputCount = 10;
  72. inst.echoEnabled = true;
  73. inst.write = writeFct;
  74. write_callCnt = 0u;
  75. memset(write_data, 0, sizeof(write_data));
  76. write_length = 0u;
  77. utils_forwardCursor(&inst, 0u);
  78. CHECK( write_callCnt == 0u);
  79. CHECK( write_length == 0u);
  80. REQUIRE( strncmp("\0\0\0\0\0", write_data, 4u) == 0);
  81. }