test_utils_terminateInput.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_terminateInput.c
  10. * @brief unittest for shellmatta utils_terminateInput
  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_idx;
  19. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  20. {
  21. write_callCnt ++;
  22. memcpy(&write_data[write_idx], data, length);
  23. write_idx += length;
  24. return SHELLMATTA_OK;
  25. }
  26. TEST_CASE( "shellmatta_utils_terminateInput" ) {
  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.prompt = "->";
  35. inst.write = writeFct;
  36. write_callCnt = 0u;
  37. memset(write_data, 0, sizeof(write_data));
  38. write_idx = 0u;
  39. utils_terminateInput(&inst);
  40. CHECK( inst.cursor == 0u );
  41. CHECK( inst.inputCount == 0u );
  42. CHECK( write_callCnt == 2u );
  43. CHECK( write_idx == 4u );
  44. REQUIRE( strncmp("\r\n->", write_data, 4u) == 0);
  45. }