Debian Codegraph @

utest/logging_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 'logging' module
18 */
19
20 #include "config.h"
21
22 #include <setjmp.h>
23 #include <stdarg.h>
24 #include <stddef.h>
25
26 #include <cmocka.h>
27
28 #include "boxes.h"
29 #include "tools.h"
30 #include "logging.h"
31 #include "logging_test.h"
32 #include "global_mock.h"
33 #include "utest_tools.h"
34
35
36 int logging_setup(void **state)
37 {
38 UNUSED(state);
39
40 int *areas = (int *) calloc(NUM_LOG_AREAS, sizeof(int));
41 areas[MAIN - 2] = 1;
42 activate_debug_logging(areas);
43 BFREE(areas);
44 return 0;
45 }
46
47
48
49 int logging_teardown(void **state)
50 {
51 UNUSED(state);
52
53 activate_debug_logging(NULL); /* turn off debug logging */
54 return 0;
55 }
56
57
58
59 void test_debug_shorten_nopath(void **state)
60 {
61 UNUSED(state);
62
63 log_debug("nopath.c", MAIN, "foo\n");
64
65 assert_int_equal(1, collect_err_size);
66 assert_string_equal("[nopath ] foo\n", collect_err[0]);
67 }
68
69
70
71 void test_debug_shorten_minimal(void **state)
72 {
73 UNUSED(state);
74
75 log_debug("minimal", MAIN, "foo\n");
76
77 assert_int_equal(1, collect_err_size);
78 assert_string_equal("[minimal ] foo\n", collect_err[0]);
79 }
80
81
82
83 void test_debug_shorten_null(void **state)
84 {
85 UNUSED(state);
86
87 log_debug(NULL, MAIN, "foo\n");
88
89 assert_int_equal(1, collect_err_size);
90 assert_string_equal("[NULL ] foo\n", collect_err[0]);
91 }
92
93
94
95 void test_debug_shorten_backslash(void **state)
96 {
97 UNUSED(state);
98
99 log_debug("..\\path\\to\\module.c", MAIN, "foo\n");
100
101 assert_int_equal(1, collect_err_size);
102 assert_string_equal("[module ] foo\n", collect_err[0]);
103 }
104
105
106
107 void test_debug_shorten_slash(void **state)
108 {
109 UNUSED(state);
110
111 log_debug("path/to/module.c", MAIN, "foo\n");
112
113 assert_int_equal(1, collect_err_size);
114 assert_string_equal("[module ] foo\n", collect_err[0]);
115 }
116
117
118
119 void test_debug_shorten_nosuffix(void **state)
120 {
121 UNUSED(state);
122
123 log_debug("path/to/module", MAIN, "foo\n");
124
125 assert_int_equal(1, collect_err_size);
126 assert_string_equal("[module ] foo\n", collect_err[0]);
127 }
128
129
130
131 void test_debug_shorten_wrongdot(void **state)
132 {
133 UNUSED(state);
134
135 log_debug("path/to.dot/module", MAIN, "foo\n");
136
137 assert_int_equal(1, collect_err_size);
138 assert_string_equal("[module ] foo\n", collect_err[0]);
139 }
140
141
142
143 void test_debug_shorten_empty(void **state)
144 {
145 UNUSED(state);
146
147 log_debug("", MAIN, "foo\n");
148
149 assert_int_equal(1, collect_err_size);
150 assert_string_equal("[ ] foo\n", collect_err[0]);
151 }
152
153
154
155 void test_debug_inactive_area(void **state)
156 {
157 UNUSED(state);
158
159 log_debug("module.c", LEXER, "foo\n");
160
161 assert_int_equal(0, collect_err_size);
162 }
163
164
165
166 void test_debug_continue(void **state)
167 {
168 UNUSED(state);
169
170 log_debug_cont(MAIN, "foo");
171
172 assert_int_equal(1, collect_err_size);
173 assert_string_equal("foo", collect_err[0]);
174 }
175
176
177
178 void test_debug_continue_inactive(void **state)
179 {
180 UNUSED(state);
181
182 log_debug_cont(LEXER, "foo");
183
184 assert_int_equal(0, collect_err_size);
185 }
186
187
188
189 void test_debug_area_too_big(void **state)
190 {
191 UNUSED(state);
192
193 log_debug("module.c", 100, "foo\n"); /* 100 is way too large and does not exist */
194
195 assert_int_equal(0, collect_err_size);
196 }
197
198
199
200 void test_debug_area_reserved(void **state)
201 {
202 UNUSED(state);
203
204 log_debug("module.c", RESERVED, "foo\n"); /* RESERVED may never be used and is always "inactive" */
205
206 assert_int_equal(0, collect_err_size);
207 }
208
209
210
211 void test_debug_area_all(void **state)
212 {
213 UNUSED(state);
214
215 log_debug("module.c", ALL, "foo\n"); /* only MAIN is active, but not all of them */
216
217 assert_int_equal(0, collect_err_size);
218 }
219
220
221
222 void test_debug_deactivated(void **state)
223 {
224 UNUSED(state);
225
226 activate_debug_logging(NULL);
227 log_debug("module.c", MAIN, "foo\n");
228
229 assert_int_equal(0, collect_err_size);
230 }
231
232
233
234 void test_debug_all_active(void **state)
235 {
236 UNUSED(state);
237
238 int *areas = (int *) calloc(NUM_LOG_AREAS, sizeof(int));
239 for(size_t i = 0; i < NUM_LOG_AREAS; i++) {
240 areas[i] = 1;
241 }
242 activate_debug_logging(areas);
243 BFREE(areas);
244
245 log_debug("module.c", ALL, "foo\n");
246
247 assert_int_equal(1, collect_err_size);
248 assert_string_equal("[module ] foo\n", collect_err[0]);
249 }
250
251
252 /* vim: set cindent sw=4: */