Debian Codegraph @

utest/regulex_test.c

raw
1 /*
2 * boxes - Command line filter to draw/remove ASCII boxes around text
3 * Copyright (c) 1999-2024 Thomas Jensen and the boxes contributors
4 *
5 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
6 * License, version 3, as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
8 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
9 * details.
10 * You should have received a copy of the GNU General Public License along with this program.
11 * If not, see <https://www.gnu.org/licenses/>.
12 *
13 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
14 */
15
16 /*
17 * Unit tests of the 'regulex' module
18 */
19
20 #include "config.h"
21
22 #include <stdarg.h>
23 #include <stddef.h>
24 #include <setjmp.h>
25 #include <cmocka.h>
26 #include <uniconv.h>
27 #include <string.h>
28
29 #include "boxes.h"
30 #include "global_mock.h"
31 #include "regulex.h"
32 #include "regulex_test.h"
33
34
35
36 void test_compile_pattern_empty(void **state)
37 {
38 UNUSED(state);
39
40 assert_null(compile_pattern(NULL));
41 assert_non_null(compile_pattern(""));
42 }
43
44
45
46 void test_compile_pattern_error(void **state)
47 {
48 UNUSED(state);
49
50 assert_null(compile_pattern("incomplete[x"));
51 assert_int_equal(1, collect_err_size);
52 assert_string_equal("Regular expression pattern \"incomplete[x\" failed to compile at position 12: "
53 "missing terminating ] for character class\n", collect_err[0]);
54
55 collect_reset();
56 assert_null(compile_pattern("incomplete\\"));
57 assert_int_equal(1, collect_err_size);
58 assert_string_equal("Regular expression pattern \"incomplete\\\" failed to compile at position 11: "
59 "\\ at end of pattern\n", collect_err[0]);
60 }
61
62
63
64 void test_regex_replace_invalid_utf(void **state)
65 {
66 UNUSED(state);
67
68 const char *input = "input";
69 assert_null(regex_replace(compile_pattern("search"), NULL, /* NULL is an invalid replacement string*/
70 u32_strconv_from_encoding(input, "ASCII", iconveh_question_mark), strlen(input), 0));
71 assert_int_equal(1, collect_err_size);
72 assert_string_equal("Failed to convert replacement string to UTF-32 - \"(null)\"\n", collect_err[0]);
73 }
74
75
76
77 void test_regex_replace_buffer_resize(void **state)
78 {
79 UNUSED(state);
80
81 const char *input = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
82 uint32_t *actual = regex_replace(compile_pattern("x"), "long_replacement_string_",
83 u32_strconv_from_encoding(input, "ASCII", iconveh_question_mark), strlen(input), 1);
84 char *actual_str = u32_strconv_to_encoding(actual, "ASCII", iconveh_question_mark);
85
86 const char *expected = "long_replacement_string_long_replacement_string_long_replacement_string_"
87 "long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
88 "long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
89 "long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
90 "long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
91 "long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
92 "long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
93 "long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
94 "long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
95 "long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_";
96 assert_string_equal(expected, actual_str);
97 }
98
99
100 void test_regex_replace_error(void **state)
101 {
102 UNUSED(state);
103
104 const char *input = "xxx";
105 uint32_t *actual = regex_replace(compile_pattern("x"), "INVALID $2",
106 u32_strconv_from_encoding(input, "ASCII", iconveh_question_mark), strlen(input), 0);
107 assert_null(actual);
108 assert_int_equal(1, collect_err_size);
109 assert_string_equal("Error substituting \"INVALID $2\": unknown substring\n", collect_err[0]);
110 }
111
112
113 /* vim: set cindent sw=4: */