complete_engine.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. {
  88. {
  89. "string",
  90. 3,
  91. INPUT_COMPLETE_NONE,
  92. 0,
  93. 3
  94. },
  95. {
  96. "some string",
  97. 7,
  98. INPUT_COMPLETE_NONE,
  99. 0,
  100. 7
  101. },
  102. {
  103. "some string",
  104. 7,
  105. INPUT_COMPLETE_SHELL_ESC,
  106. 5,
  107. 7
  108. },
  109. {
  110. "some\\ string111",
  111. 9,
  112. INPUT_COMPLETE_SHELL_ESC,
  113. 0,
  114. 9
  115. },
  116. {
  117. "some\\\tstring111",
  118. 9,
  119. INPUT_COMPLETE_SHELL_ESC,
  120. 0,
  121. 9
  122. },
  123. {
  124. "some\tstring",
  125. 7,
  126. INPUT_COMPLETE_NONE,
  127. 5,
  128. 7
  129. },
  130. {
  131. "some;string",
  132. 7,
  133. INPUT_COMPLETE_NONE,
  134. 5,
  135. 7
  136. },
  137. {
  138. "some|string",
  139. 7,
  140. INPUT_COMPLETE_NONE,
  141. 5,
  142. 7
  143. },
  144. {
  145. "some<string",
  146. 7,
  147. INPUT_COMPLETE_NONE,
  148. 5,
  149. 7
  150. },
  151. {
  152. "some>string",
  153. 7,
  154. INPUT_COMPLETE_NONE,
  155. 5,
  156. 7
  157. },
  158. {
  159. "some!@#$%^&*()_\\+~`\"',./?:string",
  160. 30,
  161. INPUT_COMPLETE_NONE,
  162. 0,
  163. 30
  164. },
  165. };
  166. /* *INDENT-ON* */
  167. /* @Test(dataSource = "test_complete_engine_fill_completions_ds") */
  168. /* *INDENT-OFF* */
  169. START_PARAMETRIZED_TEST (test_complete_engine_fill_completions,
  170. test_complete_engine_fill_completions_ds)
  171. /* *INDENT-ON* */
  172. {
  173. /* given */
  174. WInput *w_input;
  175. w_input = input_new (1, 1, NULL, 100, data->input_buffer, NULL, data->input_completion_flags);
  176. w_input->point = data->input_point;
  177. /* when */
  178. complete_engine_fill_completions (w_input);
  179. /* then */
  180. mctest_assert_str_eq (try_complete__text__captured, data->input_buffer);
  181. ck_assert_int_eq (try_complete__lc_start__captured, data->expected_start);
  182. ck_assert_int_eq (try_complete__lc_end__captured, data->expected_end);
  183. ck_assert_int_eq (try_complete__flags__captured, data->input_completion_flags);
  184. }
  185. /* *INDENT-OFF* */
  186. END_PARAMETRIZED_TEST
  187. /* *INDENT-ON* */
  188. /* --------------------------------------------------------------------------------------------- */
  189. int
  190. main (void)
  191. {
  192. TCase *tc_core;
  193. tc_core = tcase_create ("Core");
  194. tcase_add_checked_fixture (tc_core, setup, teardown);
  195. /* Add new tests here: *************** */
  196. mctest_add_parameterized_test (tc_core, test_complete_engine_fill_completions,
  197. test_complete_engine_fill_completions_ds);
  198. /* *********************************** */
  199. return mctest_run_all (tc_core);
  200. }
  201. /* --------------------------------------------------------------------------------------------- */