Manual pages: mcmcdiffmceditmcview

root/tests/lib/vfs/vfs_path_string_convert.c

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

DEFINITIONS

This source file includes following definitions.
  1. setup
  2. teardown
  3. START_PARAMETRIZED_TEST
  4. START_PARAMETRIZED_TEST
  5. START_TEST
  6. main

   1 /*
   2    lib/vfs - get vfs_path_t from string
   3 
   4    Copyright (C) 2011-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2011, 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 "/lib/vfs"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include "lib/charsets.h"
  31 #include "lib/strutil.h"
  32 #include "lib/vfs/xdirentry.h"
  33 #include "lib/vfs/path.c"  // for testing static methods
  34 
  35 #include "src/vfs/local/local.c"
  36 
  37 static struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  38 
  39 #define ETALON_PATH_STR     "/#test1/bla-bla/some/path/#test2/bla-bla/some/path#test3/111/22/33"
  40 #define ETALON_PATH_URL_STR "/test1://bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
  41 
  42 /* --------------------------------------------------------------------------------------------- */
  43 
  44 /* @Before */
  45 static void
  46 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  47 {
  48     str_init_strings ("UTF-8");
  49 
  50     vfs_init ();
  51     vfs_init_localfs ();
  52     vfs_setup_work_dir ();
  53 
  54     vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS, "test1");
  55     vfs_register_class (&vfs_test_ops1);
  56 
  57     vfs_init_class (&vfs_test_ops2, "testfs2", VFSF_REMOTE, "test2");
  58     vfs_register_class (&vfs_test_ops2);
  59 
  60     vfs_init_class (&vfs_test_ops3, "testfs3", VFSF_UNKNOWN, "test3");
  61     vfs_register_class (&vfs_test_ops3);
  62 
  63     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  64     load_codepages_list ();
  65 }
  66 
  67 /* --------------------------------------------------------------------------------------------- */
  68 
  69 /* @After */
  70 static void
  71 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  72 {
  73     free_codepages_list ();
  74     vfs_shut ();
  75     str_uninit_strings ();
  76 }
  77 
  78 /* --------------------------------------------------------------------------------------------- */
  79 /* @DataSource("test_from_to_string_ds") */
  80 static const struct test_from_to_string_ds
  81 {
  82     const char *input_string;
  83     const char *expected_result;
  84     const char *expected_element_path;
  85     const size_t expected_elements_count;
  86     struct vfs_class *expected_vfs_class;
  87 } test_from_to_string_ds[] = {
  88     {
  89         // 0.
  90         ETALON_PATH_STR,
  91         ETALON_PATH_URL_STR,
  92         "111/22/33",
  93         4,
  94         &vfs_test_ops3,
  95     },
  96     {
  97         // 1.
  98         "/",
  99         "/",
 100         "/",
 101         1,
 102         VFS_CLASS (&local_subclass),
 103     },
 104     {
 105         // 2.
 106         "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/"
 107         "test3://111/22/33",
 108         "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/"
 109         "test3://111/22/33",
 110         "111/22/33",
 111         4,
 112         &vfs_test_ops3,
 113     },
 114     {
 115         // 3.
 116         "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
 117         "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
 118         "111/22/33",
 119         4,
 120         &vfs_test_ops3,
 121     },
 122     {
 123         // 4.
 124         "/#test1/bla-bla1/#enc:CP866/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path"
 125         "#test3/111/22/33",
 126         "/test1://#enc:CP866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/"
 127         "test3://111/22/33",
 128         "111/22/33",
 129         4,
 130         &vfs_test_ops3,
 131     },
 132     {
 133         // 5.
 134         "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:CP866/#enc:KOI8-R/some/path"
 135         "#test3/111/22/33",
 136         "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/"
 137         "test3://111/22/33",
 138         "111/22/33",
 139         4,
 140         &vfs_test_ops3,
 141     },
 142     {
 143         // 6.
 144         "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:CP866/some/#enc:KOI8-R/path"
 145         "#test3/111/22/33",
 146         "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/"
 147         "test3://111/22/33",
 148         "111/22/33",
 149         4,
 150         &vfs_test_ops3,
 151     },
 152     {
 153         // 7.
 154         "/#test1/bla-bla1/some/path/#test2/#enc:CP866/bla-bla2/#enc:KOI8-R/some/path"
 155         "#test3/111/22/33",
 156         "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/"
 157         "test3://111/22/33",
 158         "111/22/33",
 159         4,
 160         &vfs_test_ops3,
 161     },
 162     {
 163         // 8.
 164         "/#test1/bla-bla1/some/path/#enc:CP866/#test2/bla-bla2/#enc:KOI8-R/some/path"
 165         "#test3/111/22/33",
 166         "/test1://#enc:CP866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/"
 167         "test3://111/22/33",
 168         "111/22/33",
 169         4,
 170         &vfs_test_ops3,
 171     },
 172 };
 173 
 174 /* @Test */
 175 START_PARAMETRIZED_TEST (test_from_to_string, test_from_to_string_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 176 {
 177     // given
 178     vfs_path_t *vpath;
 179     size_t vpath_len;
 180     const vfs_path_element_t *path_element;
 181     const char *actual_result;
 182 
 183     vpath = vfs_path_from_str_flags (data->input_string, VPF_USE_DEPRECATED_PARSER);
 184 
 185     // when
 186     vpath_len = vfs_path_elements_count (vpath);
 187     actual_result = vfs_path_as_str (vpath);
 188     path_element = vfs_path_get_by_index (vpath, -1);
 189 
 190     // then
 191     ck_assert_int_eq (vpath_len, data->expected_elements_count);
 192     mctest_assert_str_eq (actual_result, data->expected_result);
 193     mctest_assert_ptr_eq (path_element->class, data->expected_vfs_class);
 194     mctest_assert_str_eq (path_element->path, data->expected_element_path);
 195 
 196     vfs_path_free (vpath, TRUE);
 197 }
 198 END_PARAMETRIZED_TEST
 199 
 200 /* --------------------------------------------------------------------------------------------- */
 201 
 202 /* @DataSource("test_partial_string_by_index_ds") */
 203 static const struct test_partial_string_by_index_ds
 204 {
 205     const char *input_string;
 206     const off_t element_index;
 207     const char *expected_result;
 208 } test_partial_string_by_index_ds[] = {
 209     {
 210         // 0.
 211         ETALON_PATH_STR,
 212         -1,
 213         "/test1://bla-bla/some/path/test2://bla-bla/some/path",
 214     },
 215     {
 216         // 1.
 217         ETALON_PATH_STR,
 218         -2,
 219         "/test1://bla-bla/some/path/",
 220     },
 221     {
 222         // 2.
 223         ETALON_PATH_STR,
 224         -3,
 225         "/",
 226     },
 227     {
 228         // 3. Index out of bound
 229         ETALON_PATH_STR,
 230         -4,
 231         "",
 232     },
 233     {
 234         // 4.
 235         ETALON_PATH_STR,
 236         1,
 237         "/",
 238     },
 239     {
 240         // 5.
 241         ETALON_PATH_STR,
 242         2,
 243         "/test1://bla-bla/some/path/",
 244     },
 245     {
 246         // 6.
 247         ETALON_PATH_STR,
 248         3,
 249         "/test1://bla-bla/some/path/test2://bla-bla/some/path",
 250     },
 251     {
 252         // 6.
 253         ETALON_PATH_STR,
 254         4,
 255         ETALON_PATH_URL_STR,
 256     },
 257     {
 258         // 7. Index out of bound
 259         ETALON_PATH_STR,
 260         5,
 261         ETALON_PATH_URL_STR,
 262     },
 263 };
 264 
 265 /* @Test(dataSource = "test_partial_string_by_index_ds") */
 266 START_PARAMETRIZED_TEST (test_partial_string_by_index, test_partial_string_by_index_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 267 {
 268     // given
 269     vfs_path_t *vpath;
 270     char *actual_result;
 271     vpath = vfs_path_from_str_flags (data->input_string, VPF_USE_DEPRECATED_PARSER);
 272 
 273     // when
 274     actual_result = vfs_path_to_str_elements_count (vpath, data->element_index);
 275 
 276     // then
 277     mctest_assert_str_eq (actual_result, data->expected_result);
 278     g_free (actual_result);
 279 
 280     vfs_path_free (vpath, TRUE);
 281 }
 282 END_PARAMETRIZED_TEST
 283 
 284 #define ETALON_STR "/path/to/file.ext/test1://#enc:KOI8-R"
 285 
 286 /* @Test */
 287 START_TEST (test_vfs_path_encoding_at_end)
     /* [previous][next][first][last][top][bottom][index][help]  */
 288 {
 289     // given
 290     vfs_path_t *vpath;
 291     const char *result;
 292     const vfs_path_element_t *element;
 293 
 294     vpath =
 295         vfs_path_from_str_flags ("/path/to/file.ext#test1:/#enc:KOI8-R", VPF_USE_DEPRECATED_PARSER);
 296 
 297     // when
 298     result = vfs_path_as_str (vpath);
 299     element = vfs_path_get_by_index (vpath, -1);
 300 
 301     // then
 302     mctest_assert_str_eq (element->path, "");
 303     mctest_assert_not_null (element->encoding);
 304     mctest_assert_str_eq (result, ETALON_STR);
 305 
 306     vfs_path_free (vpath, TRUE);
 307 }
 308 
 309 END_TEST
 310 
 311 /* --------------------------------------------------------------------------------------------- */
 312 
 313 int
 314 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 315 {
 316     TCase *tc_core;
 317 
 318     tc_core = tcase_create ("Core");
 319 
 320     tcase_add_checked_fixture (tc_core, setup, teardown);
 321 
 322     // Add new tests here: ***************
 323     mctest_add_parameterized_test (tc_core, test_from_to_string, test_from_to_string_ds);
 324     mctest_add_parameterized_test (tc_core, test_partial_string_by_index,
 325                                    test_partial_string_by_index_ds);
 326     tcase_add_test (tc_core, test_vfs_path_encoding_at_end);
 327     // ***********************************
 328 
 329     return mctest_run_all (tc_core);
 330 }
 331 
 332 /* --------------------------------------------------------------------------------------------- */

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