complete_engine.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. lib/widget - tests for autocomplete feature
  3. Copyright (C) 2013-2019
  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. char **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 char **try_complete__return_value;
  37. /* @Mock */
  38. char **
  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 = g_new (WInput, 1);
  176. w_input->buffer = g_strdup (data->input_buffer);
  177. w_input->point = data->input_point;
  178. w_input->completion_flags = data->input_completion_flags;
  179. /* when */
  180. complete_engine_fill_completions (w_input);
  181. /* then */
  182. mctest_assert_str_eq (try_complete__text__captured, data->input_buffer);
  183. mctest_assert_int_eq (try_complete__lc_start__captured, data->expected_start);
  184. mctest_assert_int_eq (try_complete__lc_end__captured, data->expected_end);
  185. mctest_assert_int_eq (try_complete__flags__captured, data->input_completion_flags);
  186. }
  187. /* *INDENT-OFF* */
  188. END_PARAMETRIZED_TEST
  189. /* *INDENT-ON* */
  190. /* --------------------------------------------------------------------------------------------- */
  191. int
  192. main (void)
  193. {
  194. int number_failed;
  195. Suite *s = suite_create (TEST_SUITE_NAME);
  196. TCase *tc_core = tcase_create ("Core");
  197. SRunner *sr;
  198. tcase_add_checked_fixture (tc_core, setup, teardown);
  199. /* Add new tests here: *************** */
  200. mctest_add_parameterized_test (tc_core, test_complete_engine_fill_completions,
  201. test_complete_engine_fill_completions_ds);
  202. /* *********************************** */
  203. suite_add_tcase (s, tc_core);
  204. sr = srunner_create (s);
  205. srunner_set_log (sr, "complete_engine.log");
  206. srunner_run_all (sr, CK_ENV);
  207. number_failed = srunner_ntests_failed (sr);
  208. srunner_free (sr);
  209. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  210. }
  211. /* --------------------------------------------------------------------------------------------- */