(filevercmp): make inline.
[midnight-commander.git] / tests / lib / vfs / relative_cd.c
blob93ec93322a8c4a01c7b913f86911367942c07397
1 /* lib/vfs - test vfs_path_t manipulation functions
3 Copyright (C) 2011-2024
4 Free Software Foundation, Inc.
6 Written by:
7 Slava Zanko <slavazanko@gmail.com>, 2011, 2013
9 This file is part of the Midnight Commander.
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.
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.
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/>.
25 #define TEST_SUITE_NAME "/lib/vfs"
27 #include "tests/mctest.h"
29 #include <string.h> /* memset() */
31 #include "lib/strutil.h"
32 #include "lib/vfs/xdirentry.h"
33 #include "lib/vfs/path.h"
35 #include "src/vfs/local/local.c"
38 static struct vfs_s_subclass vfs_test_subclass1;
39 static struct vfs_class *vfs_test_ops1 = VFS_CLASS (&vfs_test_subclass1);
41 static int test_chdir (const vfs_path_t * vpath);
43 /* --------------------------------------------------------------------------------------------- */
45 /* @CapturedValue */
46 static vfs_path_t *test_chdir__vpath__captured;
47 /* @ThenReturnValue */
48 static int test_chdir__return_value;
50 /* @Mock */
51 static int
52 test_chdir (const vfs_path_t *vpath)
54 test_chdir__vpath__captured = vfs_path_clone (vpath);
55 return test_chdir__return_value;
58 static void
59 test_chdir__init (void)
61 test_chdir__vpath__captured = NULL;
64 static void
65 test_chdir__deinit (void)
67 vfs_path_free (test_chdir__vpath__captured, TRUE);
70 /* --------------------------------------------------------------------------------------------- */
72 /* @Before */
73 static void
74 setup (void)
76 str_init_strings (NULL);
78 vfs_init ();
79 vfs_init_localfs ();
80 vfs_setup_work_dir ();
82 memset (&vfs_test_subclass1, 0, sizeof (vfs_test_subclass1));
83 vfs_init_class (vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
84 vfs_test_ops1->chdir = test_chdir;
85 vfs_register_class (vfs_test_ops1);
87 vfs_local_ops->chdir = test_chdir;
89 test_chdir__init ();
92 /* --------------------------------------------------------------------------------------------- */
94 /* @After */
95 static void
96 teardown (void)
98 test_chdir__deinit ();
100 vfs_shut ();
101 str_uninit_strings ();
104 /* --------------------------------------------------------------------------------------------- */
105 /* @DataSource("test_relative_cd_ds") */
106 /* *INDENT-OFF* */
107 static const struct test_relative_cd_ds
109 const char *input_string;
110 const vfs_path_flag_t input_flags;
111 const char *expected_element_path;
112 } test_relative_cd_ds[] =
114 { /* 0. */
115 "/test1://user:pass@some.host:12345/path/to/dir",
116 VPF_NONE,
117 "path/to/dir"
119 { /* 1. */
120 "some-non-exists-dir",
121 VPF_NO_CANON,
122 "some-non-exists-dir"
125 /* *INDENT-ON* */
127 /* @Test(dataSource = "test_relative_cd_ds") */
128 /* *INDENT-OFF* */
129 START_PARAMETRIZED_TEST (test_relative_cd, test_relative_cd_ds)
130 /* *INDENT-ON* */
132 /* given */
133 vfs_path_t *vpath;
134 int actual_result;
136 test_chdir__return_value = 0;
138 vpath = vfs_path_from_str_flags (data->input_string, data->input_flags);
140 /* when */
141 actual_result = mc_chdir (vpath);
143 /* then */
145 const char *element_path;
147 ck_assert_int_eq (actual_result, 0);
148 element_path = vfs_path_get_last_path_str (vpath);
149 mctest_assert_str_eq (element_path, data->expected_element_path);
150 vfs_path_free (vpath, TRUE);
153 /* *INDENT-OFF* */
154 END_PARAMETRIZED_TEST
155 /* *INDENT-ON* */
157 /* --------------------------------------------------------------------------------------------- */
159 /* Relative to panel_correct_path_to_show() */
161 /* @Test */
162 /* *INDENT-OFF* */
163 START_TEST (test_vpath_to_str_filter)
164 /* *INDENT-ON* */
166 /* given */
167 vfs_path_t *vpath, *last_vpath;
168 char *filtered_path;
169 const vfs_path_element_t *path_element;
171 /* when */
172 vpath = vfs_path_from_str ("/test1://some.host/dir");
173 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, -1));
174 vfs_path_free (vpath, TRUE);
176 last_vpath = vfs_path_new (TRUE);
178 vfs_path_add_element (last_vpath, path_element);
180 filtered_path = vfs_path_to_str_flags (last_vpath, 0,
181 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
183 /* then */
184 mctest_assert_str_eq (filtered_path, "test1://some.host/dir");
186 vfs_path_free (last_vpath, TRUE);
187 g_free (filtered_path);
189 /* *INDENT-OFF* */
190 END_TEST
191 /* *INDENT-ON* */
193 /* --------------------------------------------------------------------------------------------- */
196 main (void)
198 TCase *tc_core;
199 char *cwd;
201 tc_core = tcase_create ("Core");
203 /* writable directory where check creates temporary files */
204 cwd = g_get_current_dir ();
205 g_setenv ("TEMP", cwd, TRUE);
206 g_free (cwd);
208 tcase_add_checked_fixture (tc_core, setup, teardown);
210 /* Add new tests here: *************** */
211 mctest_add_parameterized_test (tc_core, test_relative_cd, test_relative_cd_ds);
212 tcase_add_test (tc_core, test_vpath_to_str_filter);
213 /* *********************************** */
215 return mctest_run_all (tc_core);
218 /* --------------------------------------------------------------------------------------------- */