Debian Codegraph @

utest/global_mock.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 * Mocks of boxes' global variables.
18 */
19
20 #include "config.h"
21
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <locale.h>
26 #include <uniconv.h>
27
28 #include "boxes.h"
29 #include "unicode.h"
30 #include "tools.h"
31 #include "global_mock.h"
32
33
34
35 design_t *designs = NULL;
36
37 int num_designs = 0;
38
39 opt_t opt;
40
41 input_t input;
42
43 int color_output_enabled = 1;
44
45 char **collect_out = NULL;
46 int collect_out_size = 0;
47
48 char **collect_err = NULL;
49 int collect_err_size = 0;
50
51
52 void collect_reset()
53 {
54 for (int i = 0; i < collect_out_size; i++) {
55 BFREE(collect_out[i]);
56 }
57 BFREE(collect_out);
58
59 for (int i = 0; i < collect_err_size; i++) {
60 BFREE(collect_err[i]);
61 }
62 BFREE(collect_err);
63
64 collect_out_size = 0;
65 collect_err_size = 0;
66 }
67
68
69 /**
70 * Mock of the `bx_fprintf()` function which records its output instead of printing it. Assumes that no output string
71 * of our test cases will be longer than 512 characters.
72 * @param stream `stdout` or `stderr`
73 * @param format the format string, followed by the arguments
74 */
75 void __wrap_bx_fprintf(FILE *stream, const char *format, ...)
76 {
77 char **collect = stream == stdout ? collect_out : collect_err;
78 int collect_size = stream == stdout ? collect_out_size : collect_err_size;
79 collect = (char **) realloc(collect, ++collect_size * sizeof(char *));
80
81 char *s = (char *) malloc(512);
82 va_list va;
83 va_start(va, format);
84 vsprintf(s, format, va);
85 va_end(va);
86 collect[collect_size - 1] = s;
87
88 if (stream == stdout) {
89 collect_out = collect;
90 collect_out_size = collect_size;
91 }
92 else {
93 collect_err = collect;
94 collect_err_size = collect_size;
95 }
96 }
97
98
99 void setup_mocks()
100 {
101 setlocale(LC_ALL, "");
102 encoding = check_encoding("UTF-8", locale_charset());
103 collect_reset();
104 set_bx_fprintf(__wrap_bx_fprintf);
105 }
106
107
108 void teardown()
109 {
110 collect_reset();
111 }
112
113
114 /* vim: set cindent sw=4: */