Manual pages: mcmcdiffmceditmcview

root/tests/src/usermenu__test_expand_format.c

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

DEFINITIONS

This source file includes following definitions.
  1. setup_mock_panels
  2. teardown_mock_panels
  3. START_PARAMETRIZED_TEST
  4. main

   1 /*
   2    src/usermenu - tests for usermenu
   3 
   4    Copyright (C) 2025
   5    Free Software Foundation, Inc.
   6 
   7    This file is part of the Midnight Commander.
   8 
   9    The Midnight Commander is free software: you can redistribute it
  10    and/or modify it under the terms of the GNU General Public License as
  11    published by the Free Software Foundation, either version 3 of the License,
  12    or (at your option) any later version.
  13 
  14    The Midnight Commander is distributed in the hope that it will be useful,
  15    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17    GNU General Public License for more details.
  18 
  19    You should have received a copy of the GNU General Public License
  20    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  21  */
  22 
  23 #define TEST_SUITE_NAME "/src/usermenu"
  24 
  25 #include "tests/mctest.h"
  26 
  27 #include "src/filemanager/panel.h"
  28 #include "src/filemanager/filemanager.h"
  29 
  30 #undef other_panel
  31 static WPanel *other_panel;
  32 
  33 #include "src/usermenu.c"
  34 
  35 #include "execute__common.c"
  36 
  37 #define CURRENT_DIR "/etc/mc"
  38 #define OTHER_DIR   "/usr/lib"
  39 #define CURRENT_PFX "current_"
  40 #define OTHER_PFX   "other_"
  41 #define TAGGED_PFX  "tagged_"
  42 #define FNAME1_BASE "file_without_spaces"
  43 #define FNAME1_EXT  "txt"
  44 #define FNAME1      FNAME1_BASE "." FNAME1_EXT
  45 #define FNAME2_BASE "file with spaces"
  46 #define FNAME2_EXT  "sh"
  47 #define FNAME2      FNAME2_BASE "." FNAME2_EXT
  48 #define FNAME3      "file_utf8_local_chars_ä_ü_ö_ß_"
  49 #define FNAME4      "file_utf8_nonlocal_chars_à_á_"
  50 
  51 /* --------------------------------------------------------------------------------------------- */
  52 
  53 static void
  54 setup_mock_panels (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  55 {
  56     file_entry_t *list;
  57 
  58     setup ();
  59 
  60     current_panel = g_new0 (WPanel, 1);
  61 
  62     current_panel->dir.size = 4;
  63     current_panel->dir.len = current_panel->dir.size;
  64     current_panel->dir.list = g_new0 (file_entry_t, current_panel->dir.size);
  65     current_panel->cwd_vpath = vfs_path_from_str (CURRENT_DIR);
  66 
  67     list = current_panel->dir.list;
  68 
  69     list[0].fname = g_string_new (CURRENT_PFX FNAME1);
  70     list[1].fname = g_string_new (CURRENT_PFX TAGGED_PFX FNAME2);
  71     list[1].f.marked = TRUE;
  72     list[2].fname = g_string_new (CURRENT_PFX FNAME3);
  73     list[3].fname = g_string_new (CURRENT_PFX TAGGED_PFX FNAME4);
  74     list[3].f.marked = TRUE;
  75 
  76     current_panel->current = 0;
  77     current_panel->marked = 2;
  78 
  79     other_panel = g_new0 (WPanel, 1);
  80 
  81     other_panel->dir.size = 4;
  82     other_panel->dir.len = other_panel->dir.size;
  83     other_panel->cwd_vpath = vfs_path_from_str (OTHER_DIR);
  84     other_panel->dir.list = g_new0 (file_entry_t, other_panel->dir.size);
  85 
  86     list = other_panel->dir.list;
  87 
  88     list[0].fname = g_string_new (OTHER_PFX TAGGED_PFX FNAME1);
  89     list[0].f.marked = TRUE;
  90     list[1].fname = g_string_new (OTHER_PFX FNAME2);
  91     list[2].fname = g_string_new (OTHER_PFX TAGGED_PFX FNAME3);
  92     list[2].f.marked = TRUE;
  93     list[3].fname = g_string_new (OTHER_PFX FNAME4);
  94 
  95     other_panel->current = 1;
  96     other_panel->marked = 2;
  97 }
  98 
  99 /* --------------------------------------------------------------------------------------------- */
 100 
 101 static void
 102 teardown_mock_panels (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 103 {
 104     dir_list_free_list (&current_panel->dir);
 105     vfs_path_free (current_panel->cwd_vpath, TRUE);
 106     MC_PTR_FREE (current_panel);
 107     dir_list_free_list (&other_panel->dir);
 108     vfs_path_free (other_panel->cwd_vpath, TRUE);
 109     MC_PTR_FREE (other_panel);
 110     teardown ();
 111 }
 112 
 113 /* --------------------------------------------------------------------------------------------- */
 114 
 115 // clang-format off
 116 #define MOCK_V1_FNAME                                                                              \
 117     CURRENT_DIR PATH_SEP_STR CURRENT_PFX TAGGED_PFX FNAME2 " "                                     \
 118     CURRENT_DIR PATH_SEP_STR CURRENT_PFX TAGGED_PFX FNAME4 " "
 119 
 120 #define MOCK_V2_FNAME                                                                              \
 121     OTHER_DIR PATH_SEP_STR OTHER_PFX TAGGED_PFX FNAME1 " "                                         \
 122     OTHER_DIR PATH_SEP_STR OTHER_PFX TAGGED_PFX FNAME3 " "
 123 // clang-format on
 124 
 125 static const struct check_expand_format_ds
 126 {
 127     const Widget *edit_widget;
 128     char macro;
 129     gboolean do_quote;
 130     int panel_marked;
 131     const char *expected;
 132 } check_expand_format_ds[] = {
 133     // In a fully UTF-8 environment, only the files with real spaces should match
 134     { NULL, '%', FALSE, -1, "%" },
 135     { NULL, 'f', FALSE, -1, CURRENT_PFX FNAME1 },
 136     { NULL, 'p', FALSE, -1, CURRENT_PFX FNAME1 },
 137     { NULL, 'F', FALSE, -1, OTHER_PFX FNAME2 },
 138     { NULL, 'P', FALSE, -1, OTHER_PFX FNAME2 },
 139     { NULL, 'x', FALSE, -1, FNAME1_EXT },
 140     { NULL, 'X', FALSE, -1, FNAME2_EXT },
 141     { NULL, 'n', FALSE, -1, CURRENT_PFX FNAME1_BASE },
 142     { NULL, 'N', FALSE, -1, OTHER_PFX FNAME2_BASE },
 143     { NULL, 'd', FALSE, -1, CURRENT_DIR },
 144     { NULL, 'D', FALSE, -1, OTHER_DIR },
 145     { NULL, 's', FALSE, 0, CURRENT_PFX FNAME1 },
 146     { NULL, 'S', FALSE, 0, OTHER_PFX FNAME2 },
 147     { NULL, 's', FALSE, -1, CURRENT_PFX TAGGED_PFX FNAME2 " " CURRENT_PFX TAGGED_PFX FNAME4 " " },
 148     { NULL, 'S', FALSE, -1, OTHER_PFX TAGGED_PFX FNAME1 " " OTHER_PFX TAGGED_PFX FNAME3 " " },
 149     { NULL, 't', FALSE, -1, CURRENT_PFX TAGGED_PFX FNAME2 " " CURRENT_PFX TAGGED_PFX FNAME4 " " },
 150     { NULL, 'u', FALSE, -1, CURRENT_PFX TAGGED_PFX FNAME2 " " CURRENT_PFX TAGGED_PFX FNAME4 " " },
 151     { NULL, 'T', FALSE, -1, OTHER_PFX TAGGED_PFX FNAME1 " " OTHER_PFX TAGGED_PFX FNAME3 " " },
 152     { NULL, 'U', FALSE, -1, OTHER_PFX TAGGED_PFX FNAME1 " " OTHER_PFX TAGGED_PFX FNAME3 " " },
 153     { NULL, 'v', FALSE, -1, MOCK_V1_FNAME },
 154     { NULL, 'V', FALSE, -1, MOCK_V2_FNAME },
 155 
 156     /* TODO:
 157 #ifdef USE_INTERNAL_EDIT
 158     { NULL, 'c', FALSE, -1, " "},
 159     { NULL, 'i', FALSE, -1, " "},
 160     { NULL, 'y', FALSE, -1, " "},
 161     { NULL, 'b', FALSE, -1, " "},
 162 #endif
 163     { NULL, 'm', FALSE, -1, " "},
 164     */
 165 };
 166 
 167 /* --------------------------------------------------------------------------------------------- */
 168 
 169 START_PARAMETRIZED_TEST (check_expand_format, check_expand_format_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 170 {
 171     const int current_marked = current_panel->marked;
 172     const int other_marked = other_panel->marked;
 173     char *result;
 174 
 175     if (data->panel_marked > -1)
 176     {
 177         if (g_ascii_islower (data->macro))
 178             current_panel->marked = data->panel_marked;
 179         else
 180             other_panel->marked = data->panel_marked;
 181     }
 182 
 183     result = expand_format (data->edit_widget, data->macro, data->do_quote);
 184     ck_assert_str_eq (data->expected, result);
 185 
 186     g_free (result);
 187     current_panel->marked = current_marked;
 188     other_panel->marked = other_marked;
 189 }
 190 END_PARAMETRIZED_TEST
 191 
 192 /* --------------------------------------------------------------------------------------------- */
 193 
 194 int
 195 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 196 {
 197     TCase *tc_core;
 198 
 199     tc_core = tcase_create ("Core");
 200 
 201     tcase_add_checked_fixture (tc_core, setup_mock_panels, teardown_mock_panels);
 202 
 203     // Add new tests here: ***************
 204     mctest_add_parameterized_test (tc_core, check_expand_format, check_expand_format_ds);
 205     // ***********************************
 206 
 207     return mctest_run_all (tc_core);
 208 }
 209 
 210 /* --------------------------------------------------------------------------------------------- */

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