root/tests/src/execute__execute_external_editor_or_viewer.c

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

DEFINITIONS

This source file includes following definitions.
  1. execute_get_external_cmd_opts_from_config
  2. execute_get_external_cmd_opts_from_config__init
  3. execute_get_external_cmd_opts_from_config__deinit
  4. do_executev
  5. do_executev__init
  6. do_executev__deinit
  7. my_setup
  8. my_teardown
  9. START_TEST
  10. 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 "execute__common.c"
  31 
  32 /* --------------------------------------------------------------------------------------------- */
  33 
  34 char *execute_get_external_cmd_opts_from_config (const char *command,
  35                                                  const vfs_path_t * filename_vpath,
  36                                                  long start_line);
  37 
  38 /* @CapturedValue */
  39 static char *execute_external_cmd_opts__command__captured;
  40 /* @CapturedValue */
  41 static vfs_path_t *execute_external_cmd_opts__filename_vpath__captured;
  42 /* @CapturedValue */
  43 static int execute_external_cmd_opts__start_line__captured;
  44 
  45 /* @ThenReturnValue */
  46 static char *execute_external_cmd_opts__return_value;
  47 
  48 /* @Mock */
  49 char *
  50 execute_get_external_cmd_opts_from_config (const char *command, const vfs_path_t * filename_vpath,
     /* [previous][next][first][last][top][bottom][index][help]  */
  51                                            long start_line)
  52 {
  53     execute_external_cmd_opts__command__captured = g_strdup (command);
  54     execute_external_cmd_opts__filename_vpath__captured = vfs_path_clone (filename_vpath);
  55     execute_external_cmd_opts__start_line__captured = start_line;
  56 
  57     return execute_external_cmd_opts__return_value;
  58 }
  59 
  60 static void
  61 execute_get_external_cmd_opts_from_config__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  62 {
  63     execute_external_cmd_opts__command__captured = NULL;
  64     execute_external_cmd_opts__filename_vpath__captured = NULL;
  65     execute_external_cmd_opts__start_line__captured = 0;
  66 }
  67 
  68 static void
  69 execute_get_external_cmd_opts_from_config__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  70 {
  71     g_free (execute_external_cmd_opts__command__captured);
  72     vfs_path_free (execute_external_cmd_opts__filename_vpath__captured, TRUE);
  73 }
  74 
  75 /* --------------------------------------------------------------------------------------------- */
  76 void do_executev (const char *lc_shell, int flags, char *const argv[]);
  77 
  78 /* @CapturedValue */
  79 static char *do_executev__lc_shell__captured;
  80 /* @CapturedValue */
  81 static int do_executev__flags__captured;
  82 /* @CapturedValue */
  83 static GPtrArray *do_executev__argv__captured;
  84 
  85 /* @Mock */
  86 void
  87 do_executev (const char *lc_shell, int flags, char *const argv[])
     /* [previous][next][first][last][top][bottom][index][help]  */
  88 {
  89     do_executev__lc_shell__captured = g_strdup (lc_shell);
  90     do_executev__flags__captured = flags;
  91 
  92     for (; argv != NULL && *argv != NULL; argv++)
  93         g_ptr_array_add (do_executev__argv__captured, g_strdup (*argv));
  94 }
  95 
  96 static void
  97 do_executev__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  98 {
  99     do_executev__lc_shell__captured = NULL;
 100     do_executev__argv__captured = g_ptr_array_new ();
 101     do_executev__flags__captured = 0;
 102 }
 103 
 104 static void
 105 do_executev__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 106 {
 107     g_free (do_executev__lc_shell__captured);
 108     g_ptr_array_foreach (do_executev__argv__captured, (GFunc) g_free, NULL);
 109     g_ptr_array_free (do_executev__argv__captured, TRUE);
 110 }
 111 
 112 /* --------------------------------------------------------------------------------------------- */
 113 
 114 /* @Before */
 115 static void
 116 my_setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 117 {
 118     setup ();
 119 
 120     execute_get_external_cmd_opts_from_config__init ();
 121     do_executev__init ();
 122 }
 123 
 124 /* --------------------------------------------------------------------------------------------- */
 125 
 126 /* @After */
 127 static void
 128 my_teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 129 {
 130     do_executev__deinit ();
 131     execute_get_external_cmd_opts_from_config__deinit ();
 132 
 133     teardown ();
 134 }
 135 
 136 /* --------------------------------------------------------------------------------------------- */
 137 
 138 /* @Test */
 139 /* *INDENT-OFF* */
 140 START_TEST (do_open_external_editor_or_viewer)
     /* [previous][next][first][last][top][bottom][index][help]  */
 141 /* *INDENT-ON* */
 142 {
 143     /* given */
 144     vfs_path_t *filename_vpath;
 145     filename_vpath = vfs_path_from_str ("/path/to/file.txt");
 146 
 147     vfs_file_is_local__return_value = TRUE;
 148     execute_external_cmd_opts__return_value =
 149         g_strdup
 150         (" 'param 1 with spaces' \"param 2\"           -a -b -cdef /path/to/file.txt +123");
 151 
 152     /* when */
 153     execute_external_editor_or_viewer ("editor_or_viewer", filename_vpath, 123);
 154 
 155     /* then */
 156 
 157     /* check call to execute_get_external_cmd_opts_from_config() */
 158     mctest_assert_str_eq (execute_external_cmd_opts__command__captured, "editor_or_viewer");
 159     mctest_assert_int_eq (vfs_path_equal
 160                           (execute_external_cmd_opts__filename_vpath__captured, filename_vpath),
 161                           TRUE);
 162     mctest_assert_int_eq (execute_external_cmd_opts__start_line__captured, 123);
 163 
 164     /* check call to do_executev() */
 165     mctest_assert_str_eq (do_executev__lc_shell__captured, "editor_or_viewer");
 166     mctest_assert_int_eq (do_executev__flags__captured, EXECUTE_INTERNAL);
 167     mctest_assert_int_eq (do_executev__argv__captured->len, 7);
 168 
 169     mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 0),
 170                           "param 1 with spaces");
 171     mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 1), "param 2");
 172     mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 2), "-a");
 173     mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 3), "-b");
 174     mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 4), "-cdef");
 175     mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 5), "/path/to/file.txt");
 176     mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 6), "+123");
 177 
 178     vfs_path_free (filename_vpath, TRUE);
 179 }
 180 /* *INDENT-OFF* */
 181 END_TEST
 182 /* *INDENT-ON* */
 183 
 184 /* --------------------------------------------------------------------------------------------- */
 185 
 186 int
 187 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 188 {
 189     TCase *tc_core;
 190 
 191     tc_core = tcase_create ("Core");
 192 
 193     tcase_add_checked_fixture (tc_core, my_setup, my_teardown);
 194 
 195     /* Add new tests here: *************** */
 196     tcase_add_test (tc_core, do_open_external_editor_or_viewer);
 197     /* *********************************** */
 198 
 199     return mctest_run_all (tc_core);
 200 }
 201 
 202 /* --------------------------------------------------------------------------------------------- */

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