Added tests for name_quote function.
[midnight-commander.git] / tests / lib / name_quote.c
blob16fce5171e66a2c94518c411e588c72b487d8a31
1 /*
2 lib/vfs - Quote file names
4 Copyright (C) 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011
10 This file is part of the Midnight Commander.
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.
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.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define TEST_SUITE_NAME "/lib/util"
28 #include <config.h>
30 #include <check.h>
32 #include "lib/global.h"
33 #include "lib/util.h"
36 static void
37 setup (void)
41 static void
42 teardown (void)
46 /* --------------------------------------------------------------------------------------------- */
48 static const struct data_source1_struct
50 gboolean input_quote_percent;
51 const char *input_string;
53 const char *expected_string;
54 } data_source1[] =
56 { TRUE, "%%", "%%%%"},
57 { FALSE, "%%", "%%"},
60 START_TEST (quote_percent_test)
62 // given
63 char *actual_string;
64 const struct data_source1_struct test_data = data_source1[_i];
66 // when
67 actual_string = name_quote (test_data.input_string, test_data.input_quote_percent);
69 // then
70 g_assert_cmpstr (actual_string, ==, test_data.expected_string);
72 g_free (actual_string);
74 END_TEST
76 /* --------------------------------------------------------------------------------------------- */
78 static const struct data_source2_struct
80 const char *input_string;
82 const char *expected_string;
83 } data_source2[] =
85 {"-", "./-"},
86 {"blabla-", "blabla-"},
87 {"\r\n\t", "\\\r\\\n\\\t"},
88 {"'\\\";?|[]{}<>`!$&*()", "\\'\\\\\\\"\\;\\?\\|\\[\\]\\{\\}\\<\\>\\`\\!\\$\\&\\*\\(\\)"},
89 {"a b c ", "a\\ b\\ c\\ "},
90 {"#", "\\#"},
91 {"blabla#", "blabla#"},
92 {"~", "\\~"},
93 {"blabla~", "blabla~"},
96 START_TEST (name_quote_test)
98 // given
99 char *actual_string;
100 const struct data_source2_struct test_data = data_source2[_i];
102 // when
103 actual_string = name_quote (test_data.input_string, FALSE);
105 // then
106 g_assert_cmpstr (actual_string, ==, test_data.expected_string);
108 g_free (actual_string);
110 END_TEST
112 /* --------------------------------------------------------------------------------------------- */
115 main (void)
117 int number_failed;
119 Suite *s = suite_create (TEST_SUITE_NAME);
120 TCase *tc_core = tcase_create ("Core");
121 SRunner *sr;
123 tcase_add_checked_fixture (tc_core, setup, teardown);
125 /* Add new tests here: *************** */
126 tcase_add_loop_test (tc_core, quote_percent_test, 0, sizeof(data_source1)/sizeof(data_source1[0]));
128 tcase_add_loop_test (tc_core, name_quote_test, 0, sizeof(data_source2)/sizeof(data_source2[0]));
129 /* *********************************** */
131 suite_add_tcase (s, tc_core);
132 sr = srunner_create (s);
133 srunner_set_log (sr, "serialize.log");
134 srunner_run_all (sr, CK_NORMAL);
135 number_failed = srunner_ntests_failed (sr);
136 srunner_free (sr);
137 return (number_failed == 0) ? 0 : 1;
140 /* --------------------------------------------------------------------------------------------- */