complete_engine.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. lib/widget - tests for autocomplete feature
  3. Copyright (C) 2013-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2013
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "/lib/widget"
  20. #include "tests/mctest.h"
  21. #include "lib/strutil.h"
  22. #include "lib/widget.h"
  23. /* --------------------------------------------------------------------------------------------- */
  24. void complete_engine_fill_completions (WInput *in);
  25. GPtrArray *try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags);
  26. /* --------------------------------------------------------------------------------------------- */
  27. /* @CapturedValue */
  28. static char *try_complete__text__captured;
  29. /* @CapturedValue */
  30. static int try_complete__lc_start__captured;
  31. /* @CapturedValue */
  32. static int try_complete__lc_end__captured;
  33. /* @CapturedValue */
  34. static input_complete_t try_complete__flags__captured;
  35. /* @ThenReturnValue */
  36. static GPtrArray *try_complete__return_value;
  37. /* @Mock */
  38. GPtrArray *
  39. try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags)
  40. {
  41. try_complete__text__captured = g_strdup (text);
  42. try_complete__lc_start__captured = *lc_start;
  43. try_complete__lc_end__captured = *lc_end;
  44. try_complete__flags__captured = flags;
  45. return try_complete__return_value;
  46. }
  47. static void
  48. try_complete__init (void)
  49. {
  50. try_complete__text__captured = NULL;
  51. try_complete__lc_start__captured = 0;
  52. try_complete__lc_end__captured = 0;
  53. try_complete__flags__captured = 0;
  54. }
  55. static void
  56. try_complete__deinit (void)
  57. {
  58. g_free (try_complete__text__captured);
  59. }
  60. /* --------------------------------------------------------------------------------------------- */
  61. /* @Before */
  62. static void
  63. setup (void)
  64. {
  65. str_init_strings (NULL);
  66. try_complete__init ();
  67. }
  68. /* --------------------------------------------------------------------------------------------- */
  69. /* @After */
  70. static void
  71. teardown (void)
  72. {
  73. try_complete__deinit ();
  74. str_uninit_strings ();
  75. }
  76. /* --------------------------------------------------------------------------------------------- */
  77. /* @DataSource("test_complete_engine_fill_completions_ds") */
  78. /* *INDENT-OFF* */
  79. static const struct test_complete_engine_fill_completions_ds
  80. {
  81. const char *input_buffer;
  82. const int input_point;
  83. const input_complete_t input_completion_flags;
  84. int expected_start;
  85. int expected_end;
  86. } test_complete_engine_fill_completions_ds[] = {
  87. { "string", 3, INPUT_COMPLETE_NONE, 0, 3 },
  88. { "some string", 7, INPUT_COMPLETE_NONE, 0, 7 },
  89. { "some string", 7, INPUT_COMPLETE_SHELL_ESC, 5, 7 },
  90. { "some\\ string111", 9, INPUT_COMPLETE_SHELL_ESC, 0, 9 },
  91. { "some\\\tstring111", 9, INPUT_COMPLETE_SHELL_ESC, 0, 9 },
  92. { "some\tstring", 7, INPUT_COMPLETE_NONE, 5, 7 },
  93. { "some;string", 7, INPUT_COMPLETE_NONE, 5, 7 },
  94. { "some|string", 7, INPUT_COMPLETE_NONE, 5, 7 },
  95. { "some<string", 7, INPUT_COMPLETE_NONE, 5, 7 },
  96. { "some>string", 7, INPUT_COMPLETE_NONE, 5, 7 },
  97. { "some!@#$%^&*()_\\+~`\"',./?:string", 30, INPUT_COMPLETE_NONE, 0, 30 },
  98. };
  99. /* *INDENT-ON* */
  100. /* @Test(dataSource = "test_complete_engine_fill_completions_ds") */
  101. /* *INDENT-OFF* */
  102. START_PARAMETRIZED_TEST (test_complete_engine_fill_completions,
  103. test_complete_engine_fill_completions_ds)
  104. /* *INDENT-ON* */
  105. {
  106. /* given */
  107. WInput *w_input;
  108. w_input = input_new (1, 1, NULL, 100, data->input_buffer, NULL, data->input_completion_flags);
  109. w_input->point = data->input_point;
  110. /* when */
  111. complete_engine_fill_completions (w_input);
  112. /* then */
  113. mctest_assert_str_eq (try_complete__text__captured, data->input_buffer);
  114. ck_assert_int_eq (try_complete__lc_start__captured, data->expected_start);
  115. ck_assert_int_eq (try_complete__lc_end__captured, data->expected_end);
  116. ck_assert_int_eq (try_complete__flags__captured, data->input_completion_flags);
  117. }
  118. /* *INDENT-OFF* */
  119. END_PARAMETRIZED_TEST
  120. /* *INDENT-ON* */
  121. /* --------------------------------------------------------------------------------------------- */
  122. int
  123. main (void)
  124. {
  125. TCase *tc_core;
  126. tc_core = tcase_create ("Core");
  127. tcase_add_checked_fixture (tc_core, setup, teardown);
  128. /* Add new tests here: *************** */
  129. mctest_add_parameterized_test (tc_core, test_complete_engine_fill_completions,
  130. test_complete_engine_fill_completions_ds);
  131. /* *********************************** */
  132. return mctest_run_all (tc_core);
  133. }
  134. /* --------------------------------------------------------------------------------------------- */