test_utils_forwardCursor.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_forwardCursor.c
  10. * @brief unittest for shellmatta utils_forwardCursor
  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_forwardCursor 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 = 20;
  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_forwardCursor(&inst, 5u);
  39. CHECK( write_callCnt == 1u);
  40. CHECK( write_length == 4u);
  41. REQUIRE( strncmp("\x1b[5C", write_data, 4u) == 0);
  42. }
  43. TEST_CASE( "shellmatta_utils_forwardCursor normal echo off" ) {
  44. shellmatta_instance_t inst;
  45. char buffer[20];
  46. inst.buffer = buffer;
  47. inst.bufferSize = 20;
  48. inst.cursor = 10;
  49. inst.inputCount = 20;
  50. inst.echoEnabled = false;
  51. inst.write = writeFct;
  52. write_callCnt = 0u;
  53. memset(write_data, 0, sizeof(write_data));
  54. write_length = 0u;
  55. utils_forwardCursor(&inst, 5u);
  56. CHECK( write_callCnt == 0u);
  57. CHECK( write_length == 0u);
  58. REQUIRE( strncmp("\0\0\0\0", write_data, 4u) == 0);
  59. }
  60. TEST_CASE( "shellmatta_utils_forwardCursor forward by 12 with cursor at 5 and input count at 10" ) {
  61. shellmatta_instance_t inst;
  62. char buffer[20];
  63. inst.buffer = buffer;
  64. inst.bufferSize = 20;
  65. inst.cursor = 5;
  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_forwardCursor(&inst, 12u);
  73. CHECK( write_callCnt == 1u);
  74. CHECK( write_length == 4u);
  75. REQUIRE( strncmp("\x1b[5C", write_data, 4u) == 0);
  76. }
  77. TEST_CASE( "shellmatta_utils_forwardCursor forward by 0" ) {
  78. shellmatta_instance_t inst;
  79. char buffer[20];
  80. inst.buffer = buffer;
  81. inst.bufferSize = 20;
  82. inst.cursor = 10;
  83. inst.inputCount = 10;
  84. inst.echoEnabled = true;
  85. inst.write = writeFct;
  86. write_callCnt = 0u;
  87. memset(write_data, 0, sizeof(write_data));
  88. write_length = 0u;
  89. utils_forwardCursor(&inst, 0u);
  90. CHECK( write_callCnt == 0u);
  91. CHECK( write_length == 0u);
  92. REQUIRE( strncmp("\0\0\0\0\0", write_data, 4u) == 0);
  93. }