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

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