1 /*
2    lib - Quote file names
3 
4    Copyright (C) 2011-2021
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 <http://www.gnu.org/licenses/>.
24  */
25 
26 #define TEST_SUITE_NAME "/lib/util"
27 
28 #include "tests/mctest.h"
29 
30 #include "lib/util.h"
31 
32 /* --------------------------------------------------------------------------------------------- */
33 
34 /* @Before */
35 static void
setup(void)36 setup (void)
37 {
38 }
39 
40 /* @After */
41 static void
teardown(void)42 teardown (void)
43 {
44 }
45 
46 /* --------------------------------------------------------------------------------------------- */
47 
48 /* @DataSource("data_source1") */
49 /* *INDENT-OFF* */
50 static const struct data_source1
51 {
52     gboolean input_quote_percent;
53     const char *input_string;
54 
55     const char *expected_string;
56 } data_source1[] =
57 {
58     { TRUE, "%%", "%%%%"},
59     { FALSE, "%%", "%%"},
60 };
61 /* *INDENT-ON* */
62 
63 /* @Test(dataSource = "data_source1") */
64 /* *INDENT-OFF* */
START_PARAMETRIZED_TEST(quote_percent_test,data_source1)65 START_PARAMETRIZED_TEST (quote_percent_test, data_source1)
66 /* *INDENT-ON* */
67 {
68     /* given */
69     char *actual_string;
70 
71     /* when */
72     actual_string = name_quote (data->input_string, data->input_quote_percent);
73 
74     /* then */
75     mctest_assert_str_eq (actual_string, data->expected_string);
76 
77     g_free (actual_string);
78 }
79 /* *INDENT-OFF* */
80 END_PARAMETRIZED_TEST
81 /* *INDENT-ON* */
82 
83 /* --------------------------------------------------------------------------------------------- */
84 
85 /* @DataSource("data_source2") */
86 /* *INDENT-OFF* */
87 static const struct data_source2
88 {
89     const char *input_string;
90 
91     const char *expected_string;
92 } data_source2[] =
93 {
94     {"-", "./-"},
95     {"blabla-", "blabla-"},
96     {"\r\n\t", "\\\r\\\n\\\t"},
97     {"'\\\";?|[]{}<>`!$&*()", "\\'\\\\\\\"\\;\\?\\|\\[\\]\\{\\}\\<\\>\\`\\!\\$\\&\\*\\(\\)"},
98     {"a b c ", "a\\ b\\ c\\ "},
99     {"#", "\\#"},
100     {"blabla#", "blabla#"},
101     {"~", "\\~"},
102     {"blabla~", "blabla~"},
103 };
104 /* *INDENT-ON* */
105 
106 /* @Test(dataSource = "data_source2") */
107 /* *INDENT-OFF* */
START_PARAMETRIZED_TEST(name_quote_test,data_source2)108 START_PARAMETRIZED_TEST (name_quote_test, data_source2)
109 /* *INDENT-ON* */
110 {
111     /* given */
112     char *actual_string;
113 
114     /* when */
115     actual_string = name_quote (data->input_string, FALSE);
116 
117     /* then */
118     mctest_assert_str_eq (actual_string, data->expected_string);
119 
120     g_free (actual_string);
121 }
122 /* *INDENT-OFF* */
123 END_PARAMETRIZED_TEST
124 /* *INDENT-ON* */
125 
126 /* --------------------------------------------------------------------------------------------- */
127 
128 int
main(void)129 main (void)
130 {
131     TCase *tc_core;
132 
133     tc_core = tcase_create ("Core");
134 
135     tcase_add_checked_fixture (tc_core, setup, teardown);
136 
137     /* Add new tests here: *************** */
138     mctest_add_parameterized_test (tc_core, quote_percent_test, data_source1);
139     mctest_add_parameterized_test (tc_core, name_quote_test, data_source2);
140     /* *********************************** */
141 
142     return mctest_run_all (tc_core);
143 }
144 
145 /* --------------------------------------------------------------------------------------------- */
146