root/tests/lib/vfs/relative_cd.c

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

DEFINITIONS

This source file includes following definitions.
  1. test_chdir
  2. test_chdir__init
  3. test_chdir__deinit
  4. setup
  5. teardown
  6. START_PARAMETRIZED_TEST
  7. START_TEST
  8. main

   1 /* lib/vfs - test vfs_path_t manipulation functions
   2 
   3    Copyright (C) 2011-2018
   4    Free Software Foundation, Inc.
   5 
   6    Written by:
   7    Slava Zanko <slavazanko@gmail.com>, 2011, 2013
   8 
   9    This file is part of the Midnight Commander.
  10 
  11    The Midnight Commander is free software: you can redistribute it
  12    and/or modify it under the terms of the GNU General Public License as
  13    published by the Free Software Foundation, either version 3 of the License,
  14    or (at your option) any later version.
  15 
  16    The Midnight Commander is distributed in the hope that it will be useful,
  17    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19    GNU General Public License for more details.
  20 
  21    You should have received a copy of the GNU General Public License
  22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23  */
  24 
  25 #define TEST_SUITE_NAME "/lib/vfs"
  26 
  27 #include "tests/mctest.h"
  28 
  29 #include "lib/strutil.h"
  30 #include "lib/vfs/xdirentry.h"
  31 #include "lib/vfs/path.h"
  32 
  33 #include "src/vfs/local/local.c"
  34 
  35 
  36 struct vfs_s_subclass test_subclass1;
  37 struct vfs_class vfs_test_ops1;
  38 
  39 static int test_chdir (const vfs_path_t * vpath);
  40 
  41 /* --------------------------------------------------------------------------------------------- */
  42 
  43 /* @CapturedValue */
  44 static vfs_path_t *test_chdir__vpath__captured;
  45 /* @ThenReturnValue */
  46 static int test_chdir__return_value;
  47 
  48 /* @Mock */
  49 static int
  50 test_chdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
  51 {
  52     test_chdir__vpath__captured = vfs_path_clone (vpath);
  53     return test_chdir__return_value;
  54 }
  55 
  56 static void
  57 test_chdir__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  58 {
  59     test_chdir__vpath__captured = NULL;
  60 }
  61 
  62 static void
  63 test_chdir__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  64 {
  65     vfs_path_free (test_chdir__vpath__captured);
  66 }
  67 
  68 /* --------------------------------------------------------------------------------------------- */
  69 
  70 /* @Before */
  71 static void
  72 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  73 {
  74 
  75     str_init_strings (NULL);
  76 
  77     vfs_init ();
  78     init_localfs ();
  79     vfs_setup_work_dir ();
  80 
  81     test_subclass1.flags = VFS_S_REMOTE;
  82     vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  83 
  84     vfs_test_ops1.name = "testfs1";
  85     vfs_test_ops1.flags = VFSF_NOLINKS;
  86     vfs_test_ops1.prefix = "test1";
  87     vfs_test_ops1.chdir = test_chdir;
  88     vfs_register_class (&vfs_test_ops1);
  89 
  90     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  91 
  92     vfs_local_ops.chdir = test_chdir;
  93 
  94     test_chdir__init ();
  95 }
  96 
  97 /* --------------------------------------------------------------------------------------------- */
  98 
  99 /* @After */
 100 static void
 101 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 102 {
 103     test_chdir__deinit ();
 104 
 105     vfs_shut ();
 106     str_uninit_strings ();
 107 }
 108 
 109 /* --------------------------------------------------------------------------------------------- */
 110 /* @DataSource("test_relative_cd_ds") */
 111 /* *INDENT-OFF* */
 112 static const struct test_relative_cd_ds
 113 {
 114     const char *input_string;
 115     const vfs_path_flag_t input_flags;
 116     const char *expected_element_path;
 117 } test_relative_cd_ds[] =
 118 {
 119     { /* 0. */
 120         "/test1://user:pass@some.host:12345/path/to/dir",
 121         VPF_NONE,
 122         "path/to/dir"
 123     },
 124     { /* 1. */
 125         "some-non-exists-dir",
 126         VPF_NO_CANON,
 127         "some-non-exists-dir"
 128     },
 129 };
 130 /* *INDENT-ON* */
 131 
 132 /* @Test(dataSource = "test_relative_cd_ds") */
 133 /* *INDENT-OFF* */
 134 START_PARAMETRIZED_TEST (test_relative_cd, test_relative_cd_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 135 /* *INDENT-ON* */
 136 {
 137     /* given */
 138     vfs_path_t *vpath;
 139     int actual_result;
 140 
 141     test_chdir__return_value = 0;
 142 
 143     vpath = vfs_path_from_str_flags (data->input_string, data->input_flags);
 144 
 145     /* when */
 146     actual_result = mc_chdir (vpath);
 147 
 148     /* then */
 149     {
 150         const vfs_path_element_t *element;
 151 
 152         mctest_assert_int_eq (actual_result, 0);
 153         element = vfs_path_get_by_index (vpath, -1);
 154         mctest_assert_str_eq (element->path, data->expected_element_path);
 155         vfs_path_free (vpath);
 156     }
 157 }
 158 /* *INDENT-OFF* */
 159 END_PARAMETRIZED_TEST
 160 /* *INDENT-ON* */
 161 
 162 /* --------------------------------------------------------------------------------------------- */
 163 
 164 /* Relative to panel_correct_path_to_show()  */
 165 
 166 /* @Test */
 167 /* *INDENT-OFF* */
 168 START_TEST (test_vpath_to_str_filter)
     /* [previous][next][first][last][top][bottom][index][help]  */
 169 /* *INDENT-ON* */
 170 {
 171     /* given */
 172     vfs_path_t *vpath, *last_vpath;
 173     char *filtered_path;
 174     const vfs_path_element_t *path_element;
 175 
 176     /* when */
 177     vpath = vfs_path_from_str ("/test1://some.host/dir");
 178     path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, -1));
 179     vfs_path_free (vpath);
 180 
 181     last_vpath = vfs_path_new ();
 182     last_vpath->relative = TRUE;
 183 
 184     vfs_path_add_element (last_vpath, path_element);
 185 
 186     filtered_path = vfs_path_to_str_flags (last_vpath, 0,
 187                                            VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
 188 
 189     /* then */
 190     mctest_assert_str_eq (filtered_path, "test1://some.host/dir");
 191 
 192     vfs_path_free (last_vpath);
 193     g_free (filtered_path);
 194 }
 195 /* *INDENT-OFF* */
 196 END_TEST
 197 /* *INDENT-ON* */
 198 
 199 /* --------------------------------------------------------------------------------------------- */
 200 
 201 int
 202 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 203 {
 204     int number_failed;
 205 
 206     Suite *s = suite_create (TEST_SUITE_NAME);
 207     TCase *tc_core = tcase_create ("Core");
 208     SRunner *sr;
 209 
 210     tcase_add_checked_fixture (tc_core, setup, teardown);
 211 
 212     /* Add new tests here: *************** */
 213     mctest_add_parameterized_test (tc_core, test_relative_cd, test_relative_cd_ds);
 214     tcase_add_test (tc_core, test_vpath_to_str_filter);
 215     /* *********************************** */
 216 
 217     suite_add_tcase (s, tc_core);
 218     sr = srunner_create (s);
 219     srunner_set_log (sr, "relative_cd.log");
 220     srunner_run_all (sr, CK_ENV);
 221     number_failed = srunner_ntests_failed (sr);
 222     srunner_free (sr);
 223     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 224 }
 225 
 226 /* --------------------------------------------------------------------------------------------- */

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