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

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