test_utils_rewindCursor.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2019 - 2024 Stefan Strobel <stefan.strobel@shimatta.net>
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file test_utils_rewindCursor.c
  10. * @brief unittest for shellmatta utils_rewindCursor
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. #include "test/framework/catch.hpp"
  14. #include "src/shellmatta_utils.c"
  15. #include <string.h>
  16. static uint32_t write_callCnt = 0u;
  17. static char write_data[10];
  18. static uint32_t write_length;
  19. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  20. {
  21. write_callCnt ++;
  22. strncpy(write_data, data, length);
  23. write_length = length;
  24. return SHELLMATTA_OK;
  25. }
  26. TEST_CASE( "shellmatta_utils_rewindCursor normal" ) {
  27. shellmatta_instance_t inst;
  28. char buffer[20];
  29. inst.buffer = buffer;
  30. inst.bufferSize = 20;
  31. inst.cursor = 10;
  32. inst.inputCount = 10;
  33. inst.echoEnabled = true;
  34. inst.write = writeFct;
  35. write_callCnt = 0u;
  36. memset(write_data, 0, sizeof(write_data));
  37. write_length = 0u;
  38. utils_rewindCursor(&inst, 5u);
  39. CHECK( write_callCnt == 1u);
  40. CHECK( write_length == 4u);
  41. REQUIRE( strncmp("\x1b[5D", write_data, 4u) == 0);
  42. }
  43. TEST_CASE( "shellmatta_utils_rewindCursor rewind by 12 with cursor at 10" ) {
  44. shellmatta_instance_t inst;
  45. char buffer[20];
  46. inst.buffer = buffer;
  47. inst.bufferSize = 20;
  48. inst.cursor = 10;
  49. inst.inputCount = 10;
  50. inst.echoEnabled = true;
  51. inst.write = writeFct;
  52. write_callCnt = 0u;
  53. memset(write_data, 0, sizeof(write_data));
  54. write_length = 0u;
  55. utils_rewindCursor(&inst, 12u);
  56. CHECK( write_callCnt == 1u);
  57. CHECK( write_length == 5u);
  58. REQUIRE( strncmp("\x1b[10D", write_data, 5u) == 0);
  59. }
  60. TEST_CASE( "shellmatta_utils_rewindCursor rewind by 0" ) {
  61. shellmatta_instance_t inst;
  62. char buffer[20];
  63. inst.buffer = buffer;
  64. inst.bufferSize = 20;
  65. inst.cursor = 10;
  66. inst.inputCount = 10;
  67. inst.echoEnabled = true;
  68. inst.write = writeFct;
  69. write_callCnt = 0u;
  70. memset(write_data, 0, sizeof(write_data));
  71. write_length = 0u;
  72. utils_rewindCursor(&inst, 0u);
  73. CHECK( write_callCnt == 0u);
  74. CHECK( write_length == 0u);
  75. REQUIRE( strncmp("\0\0\0\0\0", write_data, 4u) == 0);
  76. }