Reorder VFS parser to work with VFS parameters:
[midnight-commander.git] / lib / tests / vfs / vfs_path_string_convert.c
blob0cd336b61e20f3467dffce21ddb281c53a24232e
1 /* lib/vfs - get vfs_path_t from string
3 Copyright (C) 2011 Free Software Foundation, Inc.
5 Written by:
6 Slava Zanko <slavazanko@gmail.com>, 2011
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License
10 as published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #define TEST_SUITE_NAME "/lib/vfs"
25 #include <check.h>
27 #include "lib/global.c"
29 #ifndef HAVE_CHARSET
30 #define HAVE_CHARSET 1
31 #endif
33 #include "lib/charsets.h"
35 #include "lib/strutil.h"
36 #include "lib/vfs/xdirentry.h"
37 #include "lib/vfs/path.c" /* for testing static methods */
39 #include "src/vfs/local/local.c"
41 struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
42 struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
44 static void
45 setup (void)
48 str_init_strings (NULL);
50 vfs_init ();
51 init_localfs ();
52 vfs_setup_work_dir ();
55 test_subclass1.flags = VFS_S_REMOTE;
56 vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
58 vfs_test_ops1.name = "testfs1";
59 vfs_test_ops1.flags = VFSF_NOLINKS;
60 vfs_test_ops1.prefix = "test1";
61 vfs_register_class (&vfs_test_ops1);
63 vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
64 vfs_test_ops2.name = "testfs2";
65 vfs_test_ops2.prefix = "test2";
66 vfs_register_class (&vfs_test_ops2);
68 vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
69 vfs_test_ops3.name = "testfs3";
70 vfs_test_ops3.prefix = "test3";
71 vfs_register_class (&vfs_test_ops3);
75 static void
76 teardown (void)
78 vfs_shut ();
79 str_uninit_strings ();
82 /* --------------------------------------------------------------------------------------------- */
83 #define ETALON_PATH_STR "/#test1/bla-bla/some/path/#test2/bla-bla/some/path#test3/111/22/33"
84 START_TEST (test_vfs_path_from_to_string)
86 vfs_path_t *vpath;
87 size_t vpath_len;
88 char *result;
89 vpath = vfs_path_from_str (ETALON_PATH_STR);
92 vpath_len = vfs_path_elements_count(vpath);
93 fail_unless(vpath_len == 4, "vpath length should be 4 (actial: %d)",vpath_len);
95 result = vfs_path_to_str(vpath);
96 fail_unless(strcmp(ETALON_PATH_STR, result) == 0, "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_STR, result);
97 g_free(result);
99 vfs_path_free(vpath);
101 END_TEST
103 /* --------------------------------------------------------------------------------------------- */
105 START_TEST (test_vfs_path_from_to_string2)
107 vfs_path_t *vpath;
108 size_t vpath_len;
109 char *result;
110 vfs_path_element_t *path_element;
112 vpath = vfs_path_from_str ("/");
115 vpath_len = vfs_path_elements_count(vpath);
116 fail_unless(vpath_len == 1, "vpath length should be 1 (actial: %d)",vpath_len);
118 result = vfs_path_to_str(vpath);
119 fail_unless(strcmp("/", result) == 0, "expected(%s) doesn't equal to actual(%s)", "/", result);
120 g_free(result);
121 path_element = vfs_path_get_by_index (vpath, -1);
122 fail_unless(strcmp("/", path_element->path) == 0, "expected(%s) doesn't equal to actual(%s)", "/", path_element->path);
124 fail_unless(path_element->class == &vfs_local_ops , "actual vfs-class doesn't equal to localfs");
126 vfs_path_free(vpath);
128 END_TEST
130 /* --------------------------------------------------------------------------------------------- */
131 START_TEST (test_vfs_path_from_to_partial_string_by_class)
133 vfs_path_t *vpath;
134 char *result;
135 vpath = vfs_path_from_str (ETALON_PATH_STR);
138 result = vfs_path_to_str_elements_count(vpath, -1);
139 fail_unless(
140 strcmp("/#test1/bla-bla/some/path/#test2/bla-bla/some/path", result) == 0,
141 "expected(%s) doesn't equal to actual(%s)", "/#test1/bla-bla/some/path/#test2/bla-bla/some/path", result);
142 g_free(result);
144 result = vfs_path_to_str_elements_count(vpath, -2);
145 fail_unless(
146 strcmp("/#test1/bla-bla/some/path/", result) == 0,
147 "expected(%s) doesn't equal to actual(%s)", "/#test1/bla-bla/some/path/", result);
148 g_free(result);
150 result = vfs_path_to_str_elements_count(vpath, -3);
151 fail_unless(
152 strcmp("/", result) == 0,
153 "expected(%s) doesn't equal to actual(%s)", "/", result);
154 g_free(result);
156 /* index out of bound*/
157 result = vfs_path_to_str_elements_count(vpath, -4);
158 fail_unless(
159 strcmp("", result) == 0,
160 "expected(%s) doesn't equal to actual(%s)", "", result);
161 g_free(result);
164 result = vfs_path_to_str_elements_count(vpath, 1);
165 fail_unless(
166 strcmp("/", result) == 0,
167 "expected(%s) doesn't equal to actual(%s)", "/", result);
168 g_free(result);
170 result = vfs_path_to_str_elements_count(vpath, 2);
171 fail_unless(
172 strcmp("/#test1/bla-bla/some/path/", result) == 0,
173 "expected(%s) doesn't equal to actual(%s)", "/#test1/bla-bla/some/path/", result);
174 g_free(result);
176 result = vfs_path_to_str_elements_count(vpath, 3);
177 fail_unless(
178 strcmp("/#test1/bla-bla/some/path/#test2/bla-bla/some/path", result) == 0,
179 "expected(%s) doesn't equal to actual(%s)", "/#test1/bla-bla/some/path/#test2/bla-bla/some/path", result);
180 g_free(result);
182 result = vfs_path_to_str_elements_count(vpath, 4);
183 fail_unless(
184 strcmp(ETALON_PATH_STR, result) == 0,
185 "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_STR, result);
186 g_free(result);
188 /* index out of bound*/
189 result = vfs_path_to_str_elements_count(vpath, 5);
190 fail_unless(
191 strcmp(ETALON_PATH_STR, result) == 0,
192 "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_STR, result);
193 g_free(result);
195 vfs_path_free(vpath);
197 END_TEST
198 /* --------------------------------------------------------------------------------------------- */
200 #define encoding_check( input , etalon ) \
202 vfs_path_t *vpath; \
203 char *result; \
205 vpath = vfs_path_from_str (input); \
206 result = vfs_path_to_str(vpath); \
207 fail_unless( result != NULL && strcmp(result, etalon) ==0, \
208 "\ninput : %s\nactual: %s\netalon: %s", input, result , etalon ); \
210 g_free(result); \
211 vfs_path_free(vpath); \
214 START_TEST (test_vfs_path_from_to_string_encoding)
216 mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
217 load_codepages_list ();
219 encoding_check (
220 "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
221 "/#test1/bla-bla1/some/path/#test2/#enc:KOI8-R/bla-bla2/some/path#test3/111/22/33"
224 encoding_check (
225 "/#test1/bla-bla1/#enc:IBM866/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
226 "/#test1/#enc:IBM866/bla-bla1/some/path/#test2/#enc:KOI8-R/bla-bla2/some/path#test3/111/22/33"
229 encoding_check (
230 "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/#enc:KOI8-R/some/path#test3/111/22/33",
231 "/#test1/bla-bla1/some/path/#test2/#enc:KOI8-R/bla-bla2/some/path#test3/111/22/33"
234 encoding_check (
235 "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/some/#enc:KOI8-R/path#test3/111/22/33",
236 "/#test1/bla-bla1/some/path/#test2/#enc:KOI8-R/bla-bla2/some/path#test3/111/22/33"
239 encoding_check (
240 "/#test1/bla-bla1/some/path/#test2/#enc:IBM866/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
241 "/#test1/bla-bla1/some/path/#test2/#enc:KOI8-R/bla-bla2/some/path#test3/111/22/33"
244 encoding_check (
245 "/#test1/bla-bla1/some/path/#enc:IBM866/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
246 "/#test1/#enc:IBM866/bla-bla1/some/path/#test2/#enc:KOI8-R/bla-bla2/some/path#test3/111/22/33"
249 free_codepages_list ();
252 END_TEST
253 /* --------------------------------------------------------------------------------------------- */
254 #define ETALON_STR "/path/to/file.ext#test1/#enc:KOI8-R"
255 START_TEST (test_vfs_path_encoding_at_end)
257 vfs_path_t *vpath;
258 char *result;
259 vfs_path_element_t *element;
261 mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
262 load_codepages_list ();
264 vpath = vfs_path_from_str ("/path/to/file.ext#test1:/#enc:KOI8-R");
265 result = vfs_path_to_str(vpath);
267 element = vfs_path_get_by_index(vpath, -1);
268 fail_unless(*element->path == '\0', "element->path should be empty, but actual value is '%s'",element->path);
269 fail_unless(element->encoding != NULL && strcmp(element->encoding, "KOI8-R") == 0,
270 "element->encoding should be 'KOI8-R', but actual value is '%s'",element->encoding);
272 fail_unless( result != NULL && strcmp(result, ETALON_STR) ==0,
273 "\nactual: %s\netalon: %s", result , ETALON_STR );
275 g_free(result);
276 vfs_path_free(vpath);
278 free_codepages_list ();
281 END_TEST
282 /* --------------------------------------------------------------------------------------------- */
285 main (void)
287 int number_failed;
289 Suite *s = suite_create (TEST_SUITE_NAME);
290 TCase *tc_core = tcase_create ("Core");
291 SRunner *sr;
293 tcase_add_checked_fixture (tc_core, setup, teardown);
295 /* Add new tests here: *************** */
296 tcase_add_test (tc_core, test_vfs_path_from_to_string);
297 tcase_add_test (tc_core, test_vfs_path_from_to_string2);
298 tcase_add_test (tc_core, test_vfs_path_from_to_partial_string_by_class);
299 tcase_add_test (tc_core, test_vfs_path_from_to_string_encoding);
300 tcase_add_test (tc_core, test_vfs_path_encoding_at_end);
301 /* *********************************** */
303 suite_add_tcase (s, tc_core);
304 sr = srunner_create (s);
305 srunner_set_log (sr, "vfs_path_string_convert.log");
306 srunner_run_all (sr, CK_NORMAL);
307 number_failed = srunner_ntests_failed (sr);
308 srunner_free (sr);
309 return (number_failed == 0) ? 0 : 1;
312 /* --------------------------------------------------------------------------------------------- */