root/tests/lib/widget/complete_engine.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. try_complete
  2. try_complete__init
  3. try_complete__deinit
  4. setup
  5. teardown
  6. START_PARAMETRIZED_TEST
  7. main

   1 /*
   2    lib/widget - tests for autocomplete feature
   3 
   4    Copyright (C) 2013-2019
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2013
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  17    The Midnight Commander is distributed in the hope that it will be useful,
  18    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20    GNU General Public License for more details.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24  */
  25 
  26 #define TEST_SUITE_NAME "/lib/widget"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include "lib/strutil.h"
  31 #include "lib/widget.h"
  32 
  33 /* --------------------------------------------------------------------------------------------- */
  34 
  35 void complete_engine_fill_completions (WInput * in);
  36 char **try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags);
  37 
  38 /* --------------------------------------------------------------------------------------------- */
  39 
  40 /* @CapturedValue */
  41 static char *try_complete__text__captured;
  42 /* @CapturedValue */
  43 static int try_complete__lc_start__captured;
  44 /* @CapturedValue */
  45 static int try_complete__lc_end__captured;
  46 /* @CapturedValue */
  47 static input_complete_t try_complete__flags__captured;
  48 
  49 /* @ThenReturnValue */
  50 static char **try_complete__return_value;
  51 
  52 /* @Mock */
  53 char **
  54 try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
  55 {
  56     try_complete__text__captured = g_strdup (text);
  57     try_complete__lc_start__captured = *lc_start;
  58     try_complete__lc_end__captured = *lc_end;
  59     try_complete__flags__captured = flags;
  60 
  61     return try_complete__return_value;
  62 }
  63 
  64 static void
  65 try_complete__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  66 {
  67     try_complete__text__captured = NULL;
  68     try_complete__lc_start__captured = 0;
  69     try_complete__lc_end__captured = 0;
  70     try_complete__flags__captured = 0;
  71 }
  72 
  73 static void
  74 try_complete__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  75 {
  76     g_free (try_complete__text__captured);
  77 }
  78 
  79 /* --------------------------------------------------------------------------------------------- */
  80 
  81 /* @Before */
  82 static void
  83 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  84 {
  85     str_init_strings (NULL);
  86     try_complete__init ();
  87 }
  88 
  89 /* --------------------------------------------------------------------------------------------- */
  90 
  91 /* @After */
  92 static void
  93 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  94 {
  95     try_complete__deinit ();
  96     str_uninit_strings ();
  97 }
  98 
  99 /* --------------------------------------------------------------------------------------------- */
 100 
 101 /* @DataSource("test_complete_engine_fill_completions_ds") */
 102 /* *INDENT-OFF* */
 103 static const struct test_complete_engine_fill_completions_ds
 104 {
 105     const char *input_buffer;
 106     const int input_point;
 107     const input_complete_t input_completion_flags;
 108     int expected_start;
 109     int expected_end;
 110 } test_complete_engine_fill_completions_ds[] =
 111 {
 112     {
 113         "string",
 114         3,
 115         INPUT_COMPLETE_NONE,
 116         0,
 117         3
 118     },
 119     {
 120         "some string",
 121         7,
 122         INPUT_COMPLETE_NONE,
 123         0,
 124         7
 125     },
 126     {
 127         "some string",
 128         7,
 129         INPUT_COMPLETE_SHELL_ESC,
 130         5,
 131         7
 132     },
 133     {
 134         "some\\ string111",
 135         9,
 136         INPUT_COMPLETE_SHELL_ESC,
 137         0,
 138         9
 139     },
 140     {
 141         "some\\\tstring111",
 142         9,
 143         INPUT_COMPLETE_SHELL_ESC,
 144         0,
 145         9
 146     },
 147     {
 148         "some\tstring",
 149         7,
 150         INPUT_COMPLETE_NONE,
 151         5,
 152         7
 153     },
 154     {
 155         "some;string",
 156         7,
 157         INPUT_COMPLETE_NONE,
 158         5,
 159         7
 160     },
 161     {
 162         "some|string",
 163         7,
 164         INPUT_COMPLETE_NONE,
 165         5,
 166         7
 167     },
 168     {
 169         "some<string",
 170         7,
 171         INPUT_COMPLETE_NONE,
 172         5,
 173         7
 174     },
 175     {
 176         "some>string",
 177         7,
 178         INPUT_COMPLETE_NONE,
 179         5,
 180         7
 181     },
 182     {
 183         "some!@#$%^&*()_\\+~`\"',./?:string",
 184         30,
 185         INPUT_COMPLETE_NONE,
 186         0,
 187         30
 188     },
 189 };
 190 /* *INDENT-ON* */
 191 
 192 /* @Test(dataSource = "test_complete_engine_fill_completions_ds") */
 193 /* *INDENT-OFF* */
 194 START_PARAMETRIZED_TEST (test_complete_engine_fill_completions,
     /* [previous][next][first][last][top][bottom][index][help]  */
 195                          test_complete_engine_fill_completions_ds)
 196 /* *INDENT-ON* */
 197 {
 198     /* given */
 199     WInput *w_input;
 200 
 201     w_input = g_new (WInput, 1);
 202     w_input->buffer = g_strdup (data->input_buffer);
 203     w_input->point = data->input_point;
 204     w_input->completion_flags = data->input_completion_flags;
 205 
 206     /* when */
 207     complete_engine_fill_completions (w_input);
 208 
 209     /* then */
 210     mctest_assert_str_eq (try_complete__text__captured, data->input_buffer);
 211     mctest_assert_int_eq (try_complete__lc_start__captured, data->expected_start);
 212     mctest_assert_int_eq (try_complete__lc_end__captured, data->expected_end);
 213     mctest_assert_int_eq (try_complete__flags__captured, data->input_completion_flags);
 214 }
 215 /* *INDENT-OFF* */
 216 END_PARAMETRIZED_TEST
 217 /* *INDENT-ON* */
 218 
 219 /* --------------------------------------------------------------------------------------------- */
 220 
 221 int
 222 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 223 {
 224     int number_failed;
 225 
 226     Suite *s = suite_create (TEST_SUITE_NAME);
 227     TCase *tc_core = tcase_create ("Core");
 228     SRunner *sr;
 229 
 230     tcase_add_checked_fixture (tc_core, setup, teardown);
 231 
 232     /* Add new tests here: *************** */
 233     mctest_add_parameterized_test (tc_core, test_complete_engine_fill_completions,
 234                                    test_complete_engine_fill_completions_ds);
 235     /* *********************************** */
 236 
 237     suite_add_tcase (s, tc_core);
 238     sr = srunner_create (s);
 239     srunner_set_log (sr, "complete_engine.log");
 240     srunner_run_all (sr, CK_ENV);
 241     number_failed = srunner_ntests_failed (sr);
 242     srunner_free (sr);
 243     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 244 }
 245 
 246 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */