Debian Codegraph @

src/text_reporter.c

raw symbols SCIP
1 #include <cgreen/reporter.h>
2 #include <cgreen/breadcrumb.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdbool.h>
7
8 #include <cgreen/text_reporter.h>
9 #include "text_reporter_internal.h"
10
11 #ifdef __ANDROID__
12 #include "cgreen/internal/android_headers/androidcompat.h"
13 #endif // #ifdef __ANDROID__
14
15 #define GREEN "\x1b[32m"
16 #define YELLOW "\x1b[33m"
17 #define RED "\x1b[31m"
18 #define MAGENTA "\x1b[35m"
19 #define RESET "\x1b[0m"
20
21
22 static void text_reporter_start_suite(TestReporter *reporter, const char *name,
23 const int number_of_tests);
24 static void text_reporter_start_test(TestReporter *reporter, const char *name);
25 static void text_reporter_finish(TestReporter *reporter, const char *filename,
26 int line, const char *message);
27 static void show_fail(TestReporter *reporter, const char *file, int line,
28 const char *message, va_list arguments);
29 static void show_incomplete(TestReporter *reporter, const char *file, int line,
30 const char *message, va_list arguments);
31 static void show_breadcrumb(const char *name, void *memo);
32 static void text_reporter_finish_suite(TestReporter *reporter, const char *file, int line);
33
34
35 /* To be able to run a reporter as CUT for testing with Cgreen itself
36 we need two reporters simultaneously, so the injected printer needs
37 to be local to the reporter which means we must store it in the
38 memo and use that. This requires a single printer function to be
39 used, often printf() or similar, so any other tricks needs to be
40 performed in char buffers so that memo->printer can do the
41 printing.
42 */
43 typedef struct {
44 TextPrinter *printer;
45 TextVPrinter *vprinter;
46 int depth;
47 } TextMemo;
48
49
50 void set_text_reporter_printer(TestReporter *reporter, TextPrinter *new_printer) {
51 TextMemo *memo = (TextMemo *)reporter->memo;
52 memo->printer = new_printer;
53 }
54
55 void set_text_reporter_vprinter(TestReporter *reporter, TextVPrinter *new_vprinter) {
56 TextMemo *memo = (TextMemo *)reporter->memo;
57 memo->vprinter = new_vprinter;
58 }
59
60 TestReporter *create_text_reporter(void) {
61 TextMemo *memo;
62 TestReporter *reporter = create_reporter();
63
64 if (reporter == NULL) {
65 return NULL;
66 }
67
68 memo = (TextMemo *)malloc(sizeof(TextMemo));
69 if (memo == NULL) {
70 destroy_reporter(reporter);
71 return NULL;
72 }
73 reporter->memo = memo;
74
75 reporter->start_suite = &text_reporter_start_suite;
76 reporter->start_test = &text_reporter_start_test;
77 reporter->show_fail = &show_fail;
78 reporter->show_incomplete = &show_incomplete;
79 reporter->finish_test = &text_reporter_finish;
80 reporter->finish_suite = &text_reporter_finish_suite;
81
82 set_text_reporter_printer(reporter, printf);
83 set_text_reporter_vprinter(reporter, vprintf);
84
85 return reporter;
86 }
87
88
89 static bool have_quiet_mode(TestReporter *reporter) {
90 return reporter->options&&((TextReporterOptions *)reporter->options)->quiet_mode;
91 }
92
93 static bool inhibit_start_suite_message(TestReporter *reporter) {
94 return reporter->options&&((TextReporterOptions *)reporter->options)->inhibit_start_suite_message;
95 }
96
97 static bool inhibit_finish_suite_message(TestReporter *reporter) {
98 return reporter->options&&((TextReporterOptions *)reporter->options)->inhibit_finish_suite_message;
99 }
100
101 static void text_reporter_start_suite(TestReporter *reporter, const char *name,
102 const int number_of_tests) {
103 TextMemo *memo = (TextMemo *)reporter->memo;
104
105 reporter->passes = 0;
106 reporter->failures = 0;
107 reporter->skips = 0;
108 reporter->exceptions = 0;
109
110 reporter_start_test(reporter, name);
111 if (get_breadcrumb_depth((CgreenBreadcrumb *) reporter->breadcrumb) == 1) {
112 if (!have_quiet_mode(reporter) && !inhibit_start_suite_message(reporter))
113 memo->printer("Running \"%s\" (%d test%s)...\n",
114 get_current_from_breadcrumb((CgreenBreadcrumb *) reporter->breadcrumb),
115 number_of_tests,
116 number_of_tests==1?"":"s");
117 fflush(stdout);
118 }
119 }
120
121 static void text_reporter_start_test(TestReporter *reporter, const char *name) {
122 reporter_start_test(reporter, name);
123 }
124
125 static void text_reporter_finish(TestReporter *reporter, const char *filename,
126 int line, const char *message) {
127 reporter_finish_test(reporter, filename, line, message);
128 }
129
130
131 static void format_count(char *buff, int count, const char *name, const char *color, const char *plural, bool use_colors) {
132 const char *color_on = use_colors?color:"";
133 const char *color_off = use_colors?RESET:"";
134 sprintf(buff, "%s%d %s%s%s", count>0?color_on:"", count, name, count==1?"":plural, color_off);
135 }
136
137 static char *format_passes(int passes, bool use_colors) {
138 static char buff[100];
139 format_count(buff, passes, "pass", GREEN, "es", use_colors);
140 return buff;
141 }
142
143 static char *format_skips(int skips, bool use_colors) {
144 static char buff[100];
145 format_count(buff, skips, "skipped", YELLOW, "", use_colors);
146 return buff;
147 }
148
149 static char *format_failures(int failures, bool use_colors) {
150 static char buff[100];
151 format_count(buff, failures, "failure", RED, "s", use_colors);
152 return buff;
153 }
154
155 static char *format_exceptions(int exceptions, bool use_colors) {
156 static char buff[100];
157 format_count(buff, exceptions, "exception", MAGENTA, "s", use_colors);
158 return buff;
159 }
160
161 static char *format_duration(uint32_t duration) {
162 static char buff[100];
163 snprintf(buff, sizeof(buff), " in %dms", duration);
164 return buff;
165 }
166
167 static void insert_comma(char buf[]) {
168 if (buf[strlen(buf)-1] != ' ')
169 strcat(buf, ", ");
170 }
171
172 static void text_reporter_print_results(char *buf, char *prepend,
173 int passes, int failures, int skips, int exceptions, uint32_t duration,
174 bool use_colors) {
175
176 sprintf(buf, "%s", prepend);
177 if (passes || failures || skips || exceptions) {
178 if (passes)
179 strcat(buf, format_passes(passes, use_colors));
180 if (skips) {
181 insert_comma(buf);
182 strcat(buf, format_skips(skips, use_colors));
183 }
184 if (failures) {
185 insert_comma(buf);
186 strcat(buf, format_failures(failures, use_colors));
187 }
188 if (exceptions) {
189 insert_comma(buf);
190 strcat(buf, format_exceptions(exceptions, use_colors));
191 }
192 strcat(buf, format_duration(duration));
193 } else {
194 strcat(buf, "No assertions");
195 }
196 }
197
198 static void text_reporter_finish_suite(TestReporter *reporter, const char *file, int line) {
199 const char *name = get_current_from_breadcrumb((CgreenBreadcrumb *) reporter->breadcrumb);
200 bool use_colors = reporter->options && ((TextReporterOptions *)reporter->options)->use_colours;
201 TextMemo *memo = (TextMemo *)reporter->memo;
202
203 reporter_finish_suite(reporter, file, line);
204
205 reporter->total_passes += reporter->passes;
206 reporter->total_failures += reporter->failures;
207 reporter->total_skips += reporter->skips;
208 reporter->total_exceptions += reporter->exceptions;
209
210 if (have_quiet_mode(reporter)) {
211 if (get_breadcrumb_depth((CgreenBreadcrumb *) reporter->breadcrumb) == 0) {
212 /* Top-level: end the dot stream and print the totals summary, so that
213 failed and suspended (xEnsure) tests are visible even in quiet mode. */
214 char buf[1000];
215 char prepend[100];
216 memo->printer("\n");
217 sprintf(prepend, "Completed \"%s\": ", name);
218 text_reporter_print_results(buf, prepend,
219 reporter->total_passes,
220 reporter->total_failures,
221 reporter->total_skips,
222 reporter->total_exceptions,
223 reporter->total_duration,
224 use_colors);
225 memo->printer("%s.\n", buf);
226 } else {
227 if (use_colors) {
228 memo->printer(GREEN);
229 if (reporter->skips) memo->printer(YELLOW);
230 if (reporter->failures) memo->printer(RED);
231 if (reporter->exceptions) memo->printer(MAGENTA);
232 }
233 if (reporter->exceptions) memo->printer("X");
234 else if (reporter->failures) memo->printer("F");
235 else if (reporter->skips) memo->printer("S");
236 else memo->printer(".");
237 if (use_colors) {
238 memo->printer(RESET);
239 }
240 }
241 } else {
242 char buf[1000];
243 char prepend[100];
244
245 sprintf(prepend, " \"%s\": ", name);
246 text_reporter_print_results(buf, prepend,
247 reporter->passes,
248 reporter->failures,
249 reporter->skips,
250 reporter->exceptions,
251 reporter->duration,
252 use_colors);
253
254 // Don't report top-level (pseudo-suite) if it had no asserts at all
255 if (get_breadcrumb_depth((CgreenBreadcrumb *) reporter->breadcrumb) != 0 ||
256 (reporter->passes || reporter->failures || reporter->skips || reporter->exceptions)) {
257 memo->printer("%s.\n", buf);
258 }
259
260 // Report totals
261 if (get_breadcrumb_depth((CgreenBreadcrumb *) reporter->breadcrumb) == 0) {
262 if (!have_quiet_mode(reporter) && !inhibit_finish_suite_message(reporter)) {
263 sprintf(prepend, "Completed \"%s\": ", name);
264 text_reporter_print_results(buf, prepend,
265 reporter->total_passes,
266 reporter->total_failures,
267 reporter->total_skips,
268 reporter->total_exceptions,
269 reporter->total_duration,
270 use_colors);
271 memo->printer("%s.\n", buf);
272 }
273 }
274 }
275 }
276
277 static void show_fail(TestReporter *reporter, const char *file, int line,
278 const char *message, va_list arguments) {
279 TextMemo *memo = (TextMemo *)reporter->memo;
280 if (have_quiet_mode(reporter)) memo->printer("\n");
281 memo->printer("%s:%d: ", file, line);
282 memo->printer("Failure: ");
283 memo->depth = 0;
284 walk_breadcrumb((CgreenBreadcrumb *) reporter->breadcrumb, &show_breadcrumb, memo);
285 memo->printer("\n\t");
286 // Simplify *printf statements for more robust cross-platform logging
287 if (message == NULL) {
288 memo->printer("<FATAL: NULL for failure message>");
289 } else {
290 memo->vprinter(message, arguments);
291 }
292 memo->printer("\n");
293 memo->printer("\n");
294 fflush(NULL);
295 }
296
297 static void show_incomplete(TestReporter *reporter, const char *file, int line,
298 const char *message, va_list arguments) {
299 TextMemo *memo = (TextMemo *)reporter->memo;
300
301 if (have_quiet_mode(reporter))
302 memo->printer("\n"); /* To break line of "....." for error message parsing */
303 memo->printer("%s:%d: ", file, line);
304 memo->printer("Exception: ");
305
306 memo->depth = 0;
307 walk_breadcrumb((CgreenBreadcrumb *) reporter->breadcrumb, &show_breadcrumb,
308 memo);
309
310 memo->printer("\n\t");
311 if (message == NULL) {
312 memo->printer("Test terminated unexpectedly, "
313 "likely from a non-standard exception or Posix signal");
314 } else {
315 memo->vprinter(message, arguments);
316 }
317 memo->printer("\n");
318 memo->printer("\n");
319 fflush(NULL);
320 }
321
322 static void show_breadcrumb(const char *name, void *memo_ptr) {
323 TextMemo *memo = (TextMemo *)memo_ptr;
324 if (memo->depth > 1) {
325 memo->printer("-> ");
326 }
327 if (memo->depth > 0) {
328 memo->printer("%s ", name);
329 }
330 memo->depth++;
331 }
332
333 /* vim: set ts=4 sw=4 et cindent: */
334 /* Local variables: */
335 /* tab-width: 4 */
336 /* End: */