root/tests/src/execute__execute_get_external_cmd_opts_from_config.c

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

DEFINITIONS

This source file includes following definitions.
  1. mc_config_get_string_raw
  2. mc_config_get_string__init
  3. mc_config_get_string__deinit
  4. setup
  5. teardown
  6. START_PARAMETRIZED_TEST
  7. main

   1 /*
   2    src - tests for execute_external_editor_or_viewer() function
   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 "/src"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include "lib/mcconfig.h"
  31 #include "lib/strutil.h"
  32 #include "lib/vfs/vfs.h"
  33 #include "src/vfs/local/local.c"
  34 
  35 char *execute_get_external_cmd_opts_from_config (const char *command,
  36                                                  const vfs_path_t *filename_vpath, long start_line);
  37 
  38 /* --------------------------------------------------------------------------------------------- */
  39 
  40 /* @CapturedValue */
  41 static GPtrArray *mc_config_get_string__group__captured;
  42 /* @CapturedValue */
  43 static GPtrArray *mc_config_get_string__param__captured;
  44 /* @CapturedValue */
  45 static GPtrArray *mc_config_get_string__default_value__captured;
  46 /* @ThenReturnValue */
  47 static GPtrArray *mc_config_get_string__return_value;
  48 
  49 /* @Mock */
  50 gchar *
  51 mc_config_get_string_raw (mc_config_t *config_ignored, const gchar *group, const gchar *param,
     /* [previous][next][first][last][top][bottom][index][help]  */
  52                           const gchar *default_value)
  53 {
  54     char *return_value;
  55     (void) config_ignored;
  56 
  57     g_ptr_array_add (mc_config_get_string__group__captured, g_strdup (group));
  58     g_ptr_array_add (mc_config_get_string__param__captured, g_strdup (param));
  59     g_ptr_array_add (mc_config_get_string__default_value__captured, g_strdup (default_value));
  60 
  61     return_value = g_ptr_array_index (mc_config_get_string__return_value, 0);
  62     g_ptr_array_remove_index (mc_config_get_string__return_value, 0);
  63     return return_value;
  64 }
  65 
  66 static void
  67 mc_config_get_string__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  68 {
  69     mc_config_get_string__group__captured = g_ptr_array_new_with_free_func (g_free);
  70     mc_config_get_string__param__captured = g_ptr_array_new_with_free_func (g_free);
  71     mc_config_get_string__default_value__captured = g_ptr_array_new_with_free_func (g_free);
  72 
  73     mc_config_get_string__return_value = g_ptr_array_new ();
  74 }
  75 
  76 static void
  77 mc_config_get_string__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  78 {
  79     g_ptr_array_free (mc_config_get_string__group__captured, TRUE);
  80     g_ptr_array_free (mc_config_get_string__param__captured, TRUE);
  81     g_ptr_array_free (mc_config_get_string__default_value__captured, TRUE);
  82 
  83     g_ptr_array_foreach (mc_config_get_string__return_value, (GFunc) g_free, NULL);
  84     g_ptr_array_free (mc_config_get_string__return_value, TRUE);
  85 }
  86 
  87 /* --------------------------------------------------------------------------------------------- */
  88 
  89 /* @Before */
  90 static void
  91 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  92 {
  93     str_init_strings (NULL);
  94     vfs_init ();
  95     vfs_init_localfs ();
  96     vfs_setup_work_dir ();
  97 
  98     mc_config_get_string__init ();
  99 }
 100 
 101 /* --------------------------------------------------------------------------------------------- */
 102 
 103 /* @After */
 104 static void
 105 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 106 {
 107     mc_config_get_string__deinit ();
 108 
 109     vfs_shut ();
 110     str_uninit_strings ();
 111 }
 112 
 113 /* --------------------------------------------------------------------------------------------- */
 114 
 115 /* @DataSource("check_substitute_ds") */
 116 static const struct check_substitute_ds
 117 {
 118     const char *config_opts_string;
 119     const char *app_name;
 120     const char *file_name;
 121     int start_line;
 122     const char *expected_result;
 123 } check_substitute_ds[] = {
 124     {
 125         "-a -b -c %filename \\%filename %filename:%lineno \\%lineno +%lineno",
 126         "some-editor",
 127         "/path/to/file",
 128         1234,
 129         "-a -b -c '/path/to/file' %filename '/path/to/file':1234 %lineno +1234",
 130     },
 131     {
 132         "%filename:\\\\\\\\\\\\%lineno",
 133         "some-editor",
 134         "/path/to/'f i\" l e \t\t\n",
 135         1234,
 136         "'/path/to/'\\''f i\" l e \t\t\n':\\\\\\\\\\\\1234",
 137     },
 138 };
 139 
 140 /* @Test(dataSource = "check_substitute_ds") */
 141 START_PARAMETRIZED_TEST (check_if_filename_and_lineno_will_be_subtituted, check_substitute_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 142 {
 143     // given
 144     char *actual_result;
 145     vfs_path_t *filename_vpath;
 146 
 147     g_ptr_array_add (mc_config_get_string__return_value, g_strdup (data->config_opts_string));
 148     filename_vpath = vfs_path_from_str (data->file_name);
 149 
 150     // when
 151     actual_result = execute_get_external_cmd_opts_from_config (data->app_name, filename_vpath,
 152                                                                data->start_line);
 153 
 154     // then
 155 
 156     // check returned value
 157     mctest_assert_str_eq (actual_result, data->expected_result);
 158 
 159     // check calls to mc_config_get_string() function
 160     mctest_assert_str_eq (g_ptr_array_index (mc_config_get_string__group__captured, 0),
 161                           CONFIG_EXT_EDITOR_VIEWER_SECTION);
 162     mctest_assert_str_eq (g_ptr_array_index (mc_config_get_string__param__captured, 0),
 163                           data->app_name);
 164     mctest_assert_str_eq (g_ptr_array_index (mc_config_get_string__default_value__captured, 0),
 165                           NULL);
 166 
 167     vfs_path_free (filename_vpath, TRUE);
 168 }
 169 END_PARAMETRIZED_TEST
 170 
 171 /* --------------------------------------------------------------------------------------------- */
 172 
 173 int
 174 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 175 {
 176     TCase *tc_core;
 177 
 178     tc_core = tcase_create ("Core");
 179 
 180     tcase_add_checked_fixture (tc_core, setup, teardown);
 181 
 182     // Add new tests here: ***************
 183     mctest_add_parameterized_test (tc_core, check_if_filename_and_lineno_will_be_subtituted,
 184                                    check_substitute_ds);
 185     // ***********************************
 186 
 187     return mctest_run_all (tc_core);
 188 }
 189 
 190 /* --------------------------------------------------------------------------------------------- */

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