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-2025
   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 <https://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 GPtrArray *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 GPtrArray *try_complete__return_value;
  51 
  52 /* @Mock */
  53 GPtrArray *
  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 static const struct test_complete_engine_fill_completions_ds
 103 {
 104     const char *input_buffer;
 105     const int input_point;
 106     const input_complete_t input_completion_flags;
 107     int expected_start;
 108     int expected_end;
 109 } test_complete_engine_fill_completions_ds[] = {
 110     { "string", 3, INPUT_COMPLETE_NONE, 0, 3 },
 111     { "some string", 7, INPUT_COMPLETE_NONE, 0, 7 },
 112     { "some string", 7, INPUT_COMPLETE_SHELL_ESC, 5, 7 },
 113     { "some\\ string111", 9, INPUT_COMPLETE_SHELL_ESC, 0, 9 },
 114     { "some\\\tstring111", 9, INPUT_COMPLETE_SHELL_ESC, 0, 9 },
 115     { "some\tstring", 7, INPUT_COMPLETE_NONE, 5, 7 },
 116     { "some;string", 7, INPUT_COMPLETE_NONE, 5, 7 },
 117     { "some|string", 7, INPUT_COMPLETE_NONE, 5, 7 },
 118     { "some<string", 7, INPUT_COMPLETE_NONE, 5, 7 },
 119     { "some>string", 7, INPUT_COMPLETE_NONE, 5, 7 },
 120     { "some!@#$%^&*()_\\+~`\"',./?:string", 30, INPUT_COMPLETE_NONE, 0, 30 },
 121 };
 122 
 123 /* @Test(dataSource = "test_complete_engine_fill_completions_ds") */
 124 START_PARAMETRIZED_TEST (test_complete_engine_fill_completions,
     /* [previous][next][first][last][top][bottom][index][help]  */
 125                          test_complete_engine_fill_completions_ds)
 126 {
 127     // given
 128     WInput *w_input;
 129 
 130     w_input = input_new (1, 1, NULL, 100, data->input_buffer, NULL, data->input_completion_flags);
 131     w_input->point = data->input_point;
 132 
 133     // when
 134     complete_engine_fill_completions (w_input);
 135 
 136     // then
 137     mctest_assert_str_eq (try_complete__text__captured, data->input_buffer);
 138     ck_assert_int_eq (try_complete__lc_start__captured, data->expected_start);
 139     ck_assert_int_eq (try_complete__lc_end__captured, data->expected_end);
 140     ck_assert_int_eq (try_complete__flags__captured, data->input_completion_flags);
 141 }
 142 END_PARAMETRIZED_TEST
 143 
 144 /* --------------------------------------------------------------------------------------------- */
 145 
 146 int
 147 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 148 {
 149     TCase *tc_core;
 150 
 151     tc_core = tcase_create ("Core");
 152 
 153     tcase_add_checked_fixture (tc_core, setup, teardown);
 154 
 155     // Add new tests here: ***************
 156     mctest_add_parameterized_test (tc_core, test_complete_engine_fill_completions,
 157                                    test_complete_engine_fill_completions_ds);
 158     // ***********************************
 159 
 160     return mctest_run_all (tc_core);
 161 }
 162 
 163 /* --------------------------------------------------------------------------------------------- */

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