(mcview_execute_cmd): don't handle CK_Help here.
[midnight-commander.git] / tests / lib / serialize.c
blobf150727caa861d040b5da45112a2b74eee1fb2de
1 /*
2 lib - common serialize/deserialize functions
4 Copyright (C) 2011-2023
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011, 2013
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"
28 #include "tests/mctest.h"
30 #include "lib/strutil.h"
31 #include "lib/serialize.h"
33 static GError *error = NULL;
35 static const char *deserialize_input_value1 =
36 "g6:group1p6:param1v10:some valuep6:param2v11:some value "
37 "g6:group2p6:param1v4:truep6:param2v6:123456"
38 "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n"
39 "g6:group4p6:param1v5:falsep6:param2v6:654321";
41 /* --------------------------------------------------------------------------------------------- */
43 /* @Before */
44 static void
45 setup (void)
47 str_init_strings (NULL);
48 error = NULL;
51 /* --------------------------------------------------------------------------------------------- */
53 /* @After */
54 static void
55 teardown (void)
57 g_clear_error (&error);
58 str_uninit_strings ();
61 /* --------------------------------------------------------------------------------------------- */
63 /* @DataSource("test_serialize_ds") */
64 /* *INDENT-OFF* */
65 static const struct test_serialize_ds
67 const char input_char_prefix;
68 const char *input_string;
69 const char *expected_result;
70 } test_serialize_ds[] =
73 's',
74 "some test string",
75 "s16:some test string"
78 'a',
79 "some test test test string",
80 "a26:some test test test string"
83 /* *INDENT-ON* */
84 /* @Test(dataSource = "test_serialize_ds") */
85 /* *INDENT-OFF* */
86 START_PARAMETRIZED_TEST (test_serialize, test_serialize_ds)
87 /* *INDENT-ON* */
89 /* given */
90 char *actual_result;
92 /* when */
93 actual_result = mc_serialize_str (data->input_char_prefix, data->input_string, &error);
95 /* then */
96 mctest_assert_str_eq (actual_result, data->expected_result);
98 g_free (actual_result);
100 if (error != NULL)
101 g_error_free (error);
103 /* *INDENT-OFF* */
104 END_PARAMETRIZED_TEST
105 /* *INDENT-ON* */
107 /* --------------------------------------------------------------------------------------------- */
109 /* @DataSource("test_deserialize_incorrect_ds") */
110 /* *INDENT-OFF* */
111 static const struct test_deserialize_incorrect_ds
113 const char input_char_prefix;
114 const char *input_string;
115 const int expected_error_code;
116 const char *expected_error_string;
117 } test_deserialize_incorrect_ds[] =
120 's',
121 NULL,
122 0, /* FIXME, TODO */
123 "mc_serialize_str(): Input data is NULL or empty."
126 's',
127 "incorrect string",
128 0, /* FIXME, TODO */
129 "mc_serialize_str(): String prefix doesn't equal to 's'"
132 's',
133 "s12345string without delimiter",
134 0, /* FIXME, TODO */
135 "mc_serialize_str(): Length delimiter ':' doesn't exists"
138 's',
139 "s1234567890123456789012345678901234567890123456789012345678901234567890:too big number",
140 0, /* FIXME, TODO */
141 "mc_serialize_str(): Too big string length"
144 's',
145 "s500:actual string length less that specified length",
146 0, /* FIXME, TODO */
147 "mc_serialize_str(): Specified data length (500) is greater than actual data length (47)"
150 /* *INDENT-ON* */
151 /* @Test(dataSource = "test_deserialize_incorrect_ds") */
152 /* *INDENT-OFF* */
153 START_PARAMETRIZED_TEST (test_deserialize_incorrect, test_deserialize_incorrect_ds)
154 /* *INDENT-ON* */
156 /* given */
157 char *actual_result;
159 /* when */
160 actual_result = mc_deserialize_str (data->input_char_prefix, data->input_string, &error);
162 /* then */
163 mctest_assert_null (actual_result);
165 ck_assert_int_eq (error->code, data->expected_error_code);
166 mctest_assert_str_eq (error->message, data->expected_error_string);
168 /* *INDENT-OFF* */
169 END_PARAMETRIZED_TEST
170 /* *INDENT-ON* */
172 /* --------------------------------------------------------------------------------------------- */
174 /* @DataSource("test_deserialize_ds") */
175 /* *INDENT-OFF* */
176 static const struct test_deserialize_ds
178 const char input_char_prefix;
179 const char *input_string;
180 const char *expected_result;
181 } test_deserialize_ds[] =
184 's',
185 "s10:actual string length great that specified length",
186 "actual str"
189 'r',
190 "r21:The right test string",
191 "The right test string"
194 /* *INDENT-ON* */
195 /* @Test(dataSource = "test_deserialize_ds") */
196 /* *INDENT-OFF* */
197 START_PARAMETRIZED_TEST (test_deserialize, test_deserialize_ds)
198 /* *INDENT-ON* */
200 /* given */
201 char *actual_result;
203 /* when */
204 actual_result = mc_deserialize_str (data->input_char_prefix, data->input_string, &error);
206 /* then */
207 mctest_assert_str_eq (actual_result, data->expected_result);
209 g_free (actual_result);
211 /* *INDENT-OFF* */
212 END_PARAMETRIZED_TEST
213 /* *INDENT-ON* */
215 /* --------------------------------------------------------------------------------------------- */
217 /* *INDENT-OFF* */
218 START_TEST (test_serialize_config)
219 /* *INDENT-ON* */
221 /* given */
222 mc_config_t *test_data;
223 char *actual;
224 const char *expected_result = "g6:group1p6:param1v10:some valuep6:param2v11:some value "
225 "g6:group2p6:param1v4:truep6:param2v6:123456"
226 "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n"
227 "g6:group4p6:param1v5:falsep6:param2v6:654321";
229 test_data = mc_config_init (NULL, FALSE);
231 mc_config_set_string_raw (test_data, "group1", "param1", "some value");
232 mc_config_set_string (test_data, "group1", "param2", "some value ");
234 mc_config_set_bool (test_data, "group2", "param1", TRUE);
235 mc_config_set_int (test_data, "group2", "param2", 123456);
237 mc_config_set_string_raw (test_data, "group3", "param1", "::bla-bla::");
238 mc_config_set_string (test_data, "group3", "param2", "bla-:p1:w:v2:12:g3:123:bla-bla\n");
240 mc_config_set_bool (test_data, "group4", "param1", FALSE);
241 mc_config_set_int (test_data, "group4", "param2", 654321);
243 /* when */
244 actual = mc_serialize_config (test_data, &error);
245 mc_config_deinit (test_data);
247 /* then */
248 mctest_assert_not_null (actual);
249 mctest_assert_str_eq (actual, expected_result);
251 g_free (actual);
253 /* *INDENT-OFF* */
254 END_TEST
255 /* *INDENT-ON* */
257 /* --------------------------------------------------------------------------------------------- */
258 /* @DataSource("test_deserialize_config_incorrect_ds") */
259 /* *INDENT-OFF* */
260 static const struct test_deserialize_config_incorrect_ds
262 const char *input_string;
263 const int expected_error_code;
264 const char *expected_error_string;
265 } test_deserialize_config_incorrect_ds[] =
268 "g123error in group name",
269 0, /* FIXME, TODO */
270 "mc_deserialize_config() at 1: mc_serialize_str(): Length delimiter ':' doesn't exists"
273 "p6:param1v10:some valuep6:param2v11:some value ",
274 0, /* FIXME, TODO */
275 "mc_deserialize_config() at 1: mc_serialize_str(): String prefix doesn't equal to 'g'"
278 "g6:group1v10:some valuep6:param2v11:some value ",
279 0, /* FIXME, TODO */
280 "mc_deserialize_config() at 10: mc_serialize_str(): String prefix doesn't equal to 'p'"
283 "g6:group1p6000:param2v11:some value ",
284 0, /* FIXME, TODO */
285 "mc_deserialize_config() at 10: mc_serialize_str(): Specified data length (6000) is greater than actual data length (21)"
288 /* *INDENT-ON* */
289 /* @Test(dataSource = "test_deserialize_config_incorrect_ds") */
290 /* *INDENT-OFF* */
291 START_PARAMETRIZED_TEST (test_deserialize_config_incorrect, test_deserialize_config_incorrect_ds)
292 /* *INDENT-ON* */
294 /* given */
295 mc_config_t *actual_result;
297 /* when */
298 actual_result = mc_deserialize_config (data->input_string, &error);
300 /* then */
301 mctest_assert_null (actual_result);
303 ck_assert_int_eq (error->code, data->expected_error_code);
304 mctest_assert_str_eq (error->message, data->expected_error_string);
306 /* *INDENT-OFF* */
307 END_PARAMETRIZED_TEST
308 /* *INDENT-ON* */
310 /* --------------------------------------------------------------------------------------------- */
312 /* *INDENT-OFF* */
313 START_TEST (test_deserialize_config)
314 /* *INDENT-ON* */
316 /* given */
317 mc_config_t *actual;
318 char *actual_value;
320 /* when */
321 actual = mc_deserialize_config (deserialize_input_value1, &error);
323 /* then */
324 mctest_assert_not_null (actual);
326 actual_value = mc_config_get_string_raw (actual, "group1", "param1", "");
327 mctest_assert_str_eq (actual_value, "some value");
328 g_free (actual_value);
330 actual_value = mc_config_get_string (actual, "group1", "param2", "");
331 mctest_assert_str_eq (actual_value, "some value ");
332 g_free (actual_value);
334 mctest_assert_true (mc_config_get_bool (actual, "group2", "param1", FALSE));
336 ck_assert_int_eq (mc_config_get_int (actual, "group2", "param2", 0), 123456);
338 actual_value = mc_config_get_string_raw (actual, "group3", "param1", "");
339 mctest_assert_str_eq (actual_value, "::bla-bla::");
340 g_free (actual_value);
342 actual_value = mc_config_get_string (actual, "group3", "param2", "");
343 mctest_assert_str_eq (actual_value, "bla-:p1:w:v2:12:g3:123:bla-bla\n");
344 g_free (actual_value);
346 mctest_assert_false (mc_config_get_bool (actual, "group4", "param1", TRUE));
348 ck_assert_int_eq (mc_config_get_int (actual, "group4", "param2", 0), 654321);
350 mc_config_deinit (actual);
352 /* *INDENT-OFF* */
353 END_TEST
354 /* *INDENT-ON* */
356 #undef input_value
357 /* --------------------------------------------------------------------------------------------- */
360 main (void)
362 TCase *tc_core;
364 tc_core = tcase_create ("Core");
366 tcase_add_checked_fixture (tc_core, setup, teardown);
368 /* Add new tests here: *************** */
369 mctest_add_parameterized_test (tc_core, test_serialize, test_serialize_ds);
371 mctest_add_parameterized_test (tc_core, test_deserialize_incorrect,
372 test_deserialize_incorrect_ds);
374 mctest_add_parameterized_test (tc_core, test_deserialize, test_deserialize_ds);
376 tcase_add_test (tc_core, test_serialize_config);
378 mctest_add_parameterized_test (tc_core, test_deserialize_config_incorrect,
379 test_deserialize_config_incorrect_ds);
381 tcase_add_test (tc_core, test_deserialize_config);
382 /* *********************************** */
384 return mctest_run_all (tc_core);
387 /* --------------------------------------------------------------------------------------------- */