Fix quotes handling.
[midnight-commander.git] / tests / lib / search / hex_translate_to_regex.c
blobe2dae2f42d8a0ed4d8db2b71139932e59b8de934
1 /*
2 libmc - checks for hex pattern parsing
4 Copyright (C) 2016
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #define TEST_SUITE_NAME "lib/search/hex"
25 #include "tests/mctest.h"
27 #include "hex.c" /* for testing static functions */
29 /* --------------------------------------------------------------------------------------------- */
31 /* @DataSource("test_hex_translate_to_regex_ds") */
32 /* *INDENT-OFF* */
33 static const struct test_hex_translate_to_regex_ds
35 const char *input_value;
36 const char *expected_result;
37 mc_search_hex_parse_error_t expected_error;
38 } test_hex_translate_to_regex_ds[] =
41 /* Simplest case */
42 "12 34",
43 "\\x12\\x34",
44 MC_SEARCH_HEX_E_OK
47 /* Prefixes (0x, 0X) */
48 "0x12 0X34",
49 "\\x12\\x34",
50 MC_SEARCH_HEX_E_OK
53 /* Prefix "0" doesn't signify octal! Numbers are always interpreted in hex. */
54 "012",
55 "\\x12",
56 MC_SEARCH_HEX_E_OK
59 /* Extra whitespace */
60 " 12 34 ",
61 "\\x12\\x34",
62 MC_SEARCH_HEX_E_OK
65 /* Min/max values */
66 "0 ff",
67 "\\x00\\xFF",
68 MC_SEARCH_HEX_E_OK
71 /* Error: Number out of range */
72 "100",
73 NULL,
74 MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE
77 /* Error: Number out of range (negative) */
78 "-1",
79 NULL,
80 MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE
83 /* Error: Invalid characters */
84 "1 z 2",
85 NULL,
86 MC_SEARCH_HEX_E_INVALID_CHARACTER
89 * Quotes.
92 " \"abc\" ",
93 "abc",
94 MC_SEARCH_HEX_E_OK
97 /* Preserve upper/lower case */
98 "\"aBc\"",
99 "aBc",
100 MC_SEARCH_HEX_E_OK
103 " 12\"abc\"34 ",
104 "\\x12abc\\x34",
105 MC_SEARCH_HEX_E_OK
108 "\"a\"\"b\"",
109 "ab",
110 MC_SEARCH_HEX_E_OK
112 /* Empty quotes */
114 "\"\"",
116 MC_SEARCH_HEX_E_OK
119 "12 \"\"",
120 "\\x12",
121 MC_SEARCH_HEX_E_OK
123 /* Error: Unmatched quotes */
125 "\"a",
126 NULL,
127 MC_SEARCH_HEX_E_UNMATCHED_QUOTES
130 "\"",
131 NULL,
132 MC_SEARCH_HEX_E_UNMATCHED_QUOTES
134 /* Escaped quotes */
136 "\"a\\\"b\"",
137 "a\"b",
138 MC_SEARCH_HEX_E_OK
141 "\"a\\\\b\"",
142 "a\\b",
143 MC_SEARCH_HEX_E_OK
146 /* *INDENT-ON* */
148 /* @Test(dataSource = "test_hex_translate_to_regex_ds") */
149 /* *INDENT-OFF* */
150 START_PARAMETRIZED_TEST (test_hex_translate_to_regex, test_hex_translate_to_regex_ds)
151 /* *INDENT-ON* */
153 GString *tmp, *dest_str;
154 mc_search_hex_parse_error_t error;
156 /* given */
157 tmp = g_string_new (data->input_value);
159 /* when */
160 dest_str = mc_search__hex_translate_to_regex (tmp, &error, NULL);
162 g_string_free (tmp, TRUE);
164 /* then */
165 if (dest_str != NULL)
167 mctest_assert_str_eq (dest_str->str, data->expected_result);
168 g_string_free (dest_str, TRUE);
170 else
172 mctest_assert_int_eq (error, data->expected_error);
175 /* *INDENT-OFF* */
176 END_PARAMETRIZED_TEST
177 /* *INDENT-ON* */
179 /* --------------------------------------------------------------------------------------------- */
182 main (void)
184 int number_failed;
186 Suite *s = suite_create (TEST_SUITE_NAME);
187 TCase *tc_core = tcase_create ("Core");
188 SRunner *sr;
190 /* Add new tests here: *************** */
191 mctest_add_parameterized_test (tc_core, test_hex_translate_to_regex,
192 test_hex_translate_to_regex_ds);
193 /* *********************************** */
195 suite_add_tcase (s, tc_core);
196 sr = srunner_create (s);
197 srunner_set_log (sr, "hex_translate_to_regex.log");
198 srunner_run_all (sr, CK_ENV);
199 number_failed = srunner_ntests_failed (sr);
200 srunner_free (sr);
201 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
204 /* --------------------------------------------------------------------------------------------- */