test_utils_restoreCursorPos.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_restoreCursorPos.c
  10. * @brief unittest for shellmatta utils_restoreCursorPos
  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. memcpy(write_data, data, length);
  23. write_length = length;
  24. return SHELLMATTA_OK;
  25. }
  26. TEST_CASE( "shellmatta_utils_restoreCursorPos" ) {
  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_restoreCursorPos(&inst);
  39. CHECK( write_callCnt == 1u);
  40. CHECK( write_length == 3u);
  41. REQUIRE( strncmp("\x1b[u", write_data, 3u) == 0);
  42. }