-
Notifications
You must be signed in to change notification settings - Fork 0
/
tric_output.h
540 lines (422 loc) · 14.5 KB
/
tric_output.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
TRIC - Minimalistic unit testing framework for c
Copyright 2024 Philip Colombo
This file is part of TRIC.
TRIC is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
TRIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with TRIC. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef TRIC_H
#error "TRIC is not defined"
#endif
#ifndef TRIC_OUTPUT_H
#define TRIC_OUTPUT_H
#include <string.h>
/**
* \file tric_output.h
*
* \brief Report TRIC test results in various output formats
*
* TRIC itself provides only a simple default reporting. The header tric_output.h defines functions to report the test results of TRIC in other output formats. The header tric.h must be included before the header tric_output.h can be included. Otherwise the compilation of the test suite will fail.
*
* These functions must be called before any test is executed (i.e. in a test suite setup fixture). The following example shows how to report test results in the TAP output format:
*
* \code
#include "tric.h"
#include "tric_output.h"
bool setup(void *data) {
tric_output_tap();
return true;
}
SUITE("with TAP output", setup, NULL, NULL) {
TEST("a failing test", NULL, NULL, NULL) {
ASSERT(0 > 1);
}
}
* \endcode
*
* \author Philip Colombo
* \date 2024
* \copyright GNU Lesser General Public License
*/
#define TRIC_OUTPUT_FORMAT "TRIC_OUTPUT_FORMAT"
/*
internally used
print string representation of execution results
*/
void tric_print_result(enum tric_result result) {
const char *result_strings[] = { "undefined", "ok", "failure", "skipped", "crashed" };
printf("%s", result_strings[result + 1]);
}
/*
internally used
print TAP version
*/
void tric_tap_version(void) {
printf("TAP version 14\n");
}
/*
internally used
print TAP plan
*/
void tric_tap_plan(size_t number_of_tests, const char *description) {
printf("1..%zu # %s\n", number_of_tests, description);
}
/*
internally used
print TAP version and plan
*/
void tric_tap_header(struct tric_suite *suite, struct tric_test *test, void *data) {
tric_tap_version();
tric_tap_plan(suite->number_of_tests, suite->description);
}
/*
internally used
print TAP test status
*/
void tric_tap_status(struct tric_test *test) {
char *status = "not ok";
if (test->result == TRIC_OK
|| (test->before == TRIC_UNDEFINED && test->result == TRIC_SKIPPED)
|| (test->before == TRIC_SKIPPED && test->result == TRIC_SKIPPED)) {
status = "ok";
}
printf("%s", status);
}
/*
internally used
print TAP test point id
*/
void tric_tap_number(size_t id) {
printf(" %zu", id);
}
/*
internally used
print TAP description
*/
void tric_tap_description(const char *description) {
printf(" - %s", description);
}
/*
internally used
print TAP directive
*/
void tric_tap_directive(struct tric_test *test) {
char *directive = "";
if ((test->before == TRIC_UNDEFINED && test->result == TRIC_SKIPPED)
|| (test->before == TRIC_SKIPPED && test->result == TRIC_SKIPPED)) {
directive = " # SKIP";
}
printf("%s\n", directive);
}
/*
internally used
print TAP test point
*/
void tric_tap_test_point(struct tric_suite *suite, struct tric_test *test, void *data) {
tric_tap_status(test);
tric_tap_number(test->id);
tric_tap_description(test->description);
tric_tap_directive(test);
}
/**
* \brief TAP version 14 output
*
* Output test results in the <a href="https://testanything.org/tap-version-14-specification.html">TAP (Test Anything Protocol)</a> format according to version 14 of the TAP specification.
*
* This function must be called before any test in the test suite is executed (i.e. in the test suite setup fixture).
*/
void tric_output_tap(void) {
tric_log(tric_tap_header, tric_tap_test_point, NULL, NULL);
}
/*
internally used
print csv header
*/
void tric_csv_header(bool unix_newline) {
printf("ID,RESULT,LINE,SIGNAL,BEFORE,AFTER,DESCRIPTION%s", unix_newline ? "\n" : "\r\n");
}
/*
internally used
print csv header with newlines according to specification
*/
void tric_csv_header_standard(struct tric_suite *suite, struct tric_test *test, void *data) {
tric_csv_header(false);
}
/*
internally used
print csv header with unix newlines
*/
void tric_csv_header_unix(struct tric_suite *suite, struct tric_test *test, void *data) {
tric_csv_header(true);
}
/*
internally used
print csv record
*/
void tric_csv_record(struct tric_test *test, bool unix_newline) {
printf("%zu,", test->id);
tric_print_result(test->result);
printf(",%zu,%zu,", test->line, test->signal);
tric_print_result(test->before);
printf(",");
tric_print_result(test->after);
printf(",\"%s\"%s", test->description, unix_newline ? "\n" : "\r\n");
}
/*
internally used
print csv record with newlines according to standard
*/
void tric_csv_record_standard(struct tric_suite *suite, struct tric_test *test, void *data) {
tric_csv_record(test, false);
}
/*
internally used
print csv record with unix newlines
*/
void tric_csv_record_unix(struct tric_suite *suite, struct tric_test *test, void *data) {
tric_csv_record(test, true);
}
/**
* \brief CSV output
*
* Output test results in CSV (Comma Separated Values) format according to the specification in <a href="https://www.rfc-editor.org/rfc/rfc4180">RFC 4180</a>. The output of the csv header may be disabled by setting the header parameter to false..
*
* RFC 4180 requires CRLF newlines ("\r\n"). With the parameter unix_newline it is possible to report the test results with unix style LF newlines ("\n").
*
* This function must be called before any test in the test suite is executed (i.e. in the test suite setup fixture).
*
* \param header If set to true, the optional csv header is included in the test reporting.
* \param unix_newline If set to true, unix style newlines ("\n") will be used for the csv output. Otherwise CRLF newlines ("\r\n") will be used.
*/
void tric_output_csv(bool header, bool unix_newline) {
tric_logger_t start = NULL;
if (header) {
start = unix_newline ? tric_csv_header_unix : tric_csv_header_standard;
}
tric_log(start, unix_newline ? tric_csv_record_unix : tric_csv_record_standard, NULL, NULL);
}
/*
internally used
print csv summary header
*/
void tric_csv_summary_header(bool unix_newline) {
printf("DESCRIPTION,TESTS,EXECUTED,FAILED,SKIPPED%s", unix_newline ? "\n" : "\r\n");
}
/*
internally used
print csv summary header with newlines according to specification
*/
void tric_csv_summary_header_standard(struct tric_suite *suite, struct tric_test *test, void *data) {
tric_csv_summary_header(false);
}
/*
internally used
print csv summary header with unix newlines
*/
void tric_csv_summary_header_unix(struct tric_suite *suite, struct tric_test *test, void *data) {
tric_csv_summary_header(true);
}
/*
internally used
print csv summary record
*/
void tric_csv_summary_record(struct tric_suite *suite, bool unix_newline) {
printf("\"%s\",%zu,%zu,%zu,%zu%s", suite->description, suite->number_of_tests, suite->executed_tests, suite->failed_tests, suite->skipped_tests, unix_newline ? "\n" : "\r\n");
}
/*
internally used
print csv summary record with newlines according to standard
*/
void tric_csv_summary_record_standard(struct tric_suite *suite, struct tric_test *test, void *data) {
tric_csv_summary_record(suite, false);
}
/*
internally used
print csv summary record with unix newlines
*/
void tric_csv_summary_record_unix(struct tric_suite *suite, struct tric_test *test, void *data) {
tric_csv_summary_record(suite, true);
}
/**
* \brief CSV summary output
*
* Output a summary of the test results in CSV (Comma Separated Values) format according to the specification in <a href="https://www.rfc-editor.org/rfc/rfc4180">RFC 4180</a>. The output of the csv header may be disabled by setting the header parameter to false..
*
* RFC 4180 requires CRLF newlines ("\r\n"). With the parameter unix_newline it is possible to report the test summary with unix style LF newlines ("\n").
*
* This function must be called before any test in the test suite is executed (i.e. in the test suite setup fixture).
*
* \param header If set to true, the optional csv header is included in the test summary reporting.
* \param unix_newline If set to true, unix style newlines ("\n") will be used for the csv output. Otherwise CRLF newlines ("\r\n") will be used.
*/
void tric_output_csv_summary(bool header, bool unix_newline) {
tric_logger_t start = NULL;
if (header) {
start = unix_newline ? tric_csv_summary_header_unix : tric_csv_summary_header_standard;
}
tric_log(start, NULL, unix_newline ? tric_csv_summary_record_unix : tric_csv_summary_record_standard, NULL);
}
/*
internally used
print test as json
*/
void tric_json_test(struct tric_test *test) {
printf("{ \"id\": %zu, \"description\": \"%s\", \"before\": \"", test->id, test->description);
tric_print_result(test->before);
printf("\", \"result\": \"");
tric_print_result(test->result);
printf("\", \"after\": \"");
tric_print_result(test->after);
printf("\", \"line\": %zu, \"signal\": %zu }", test->line, test->signal);
}
/*
internally used
print all tests as json list
*/
void tric_json_tests(struct tric_suite *suite) {
printf("[ ");
struct tric_test *test = suite->tests;
while (test != NULL) {
tric_json_test(test);
if (test->next == NULL) {
printf(" ");
break;
}
printf(", ");
test = test->next;
}
printf("] ");
}
/*
internally used
print suite as json
*/
void tric_json_suite(struct tric_suite *suite, struct tric_test *test, void *data) {
printf("{ \"description\": \"%s\", \"number_of_tests\": %zu, \"executed_tests\": %zu, \"failed_tests\": %zu, \"skipped_tests\": %zu, \"tests\": ", suite->description, suite->number_of_tests, suite->executed_tests, suite->failed_tests, suite->skipped_tests);
tric_json_tests(suite);
printf("}\n");
}
/**
* \brief JSON output
*
* Output the test results in <a href="https://en.wikipedia.org/wiki/JSON">JSON (JavaScript Object Notation)</a> format.
*
* This function must be called before any test in the test suite is executed (i.e. in the test suite setup fixture).
*/
void tric_output_json(void) {
tric_log(NULL, NULL, tric_json_suite, NULL);
}
/*
internally used
read TRIC_OUTPUT_FORMAT environment variable
*/
char *tric_environment_format(void) {
extern char **environ;
size_t i;
char *current;
for (i = 0; (current = environ[i]) != NULL; i++) {
char *separator = strchr(current, '=');
if (separator == NULL
|| (separator - current) != strlen(TRIC_OUTPUT_FORMAT)) {
continue;
}
if (strncmp(current, TRIC_OUTPUT_FORMAT, strlen(TRIC_OUTPUT_FORMAT)) == 0) {
return separator + 1;
}
}
return NULL;
}
/*
internally used
test if string of environment variable matches output format string
*/
bool tric_environment_match(char *environment_value, char *format) {
if (strlen(environment_value) != strlen(format)
|| strcmp(environment_value, format) != 0) {
return false;
}
return true;
}
/**
* \brief Set output format with environment variable
*
* Output the test results in the format determined by the environment variable TRIC_OUTPUT_FORMAT. The following values for TRIC_OUTPUT_FORMAT are recognized:
*
* - tap
*
* Output TAP format (i.e. tric_output_tap() will be called).
*
* - csv
*
* Output CSV format without header (i.e. tric_output_csv(false, false) will be called).
*
* - csv_header
*
* Output CSV format with header (i.e. tric_output_csv(true, false) will be called).
*
* - csv_unix
*
* Output CSV format without header and with unix style newlines (i.e. tric_output_csv(false, true) will be called).
*
* - csv_header_unix
*
* Output CSV format with header and with unix style newlines (i.e. tric_output_csv(true, true) will be called).
*
* - csv_summary
*
* Output CSV summary format without header (i.e. tric_output_csv_summary(false, false) will be called).
*
* - csv_summary_header
*
* Output CSV summary format with header (i.e. tric_output_csv_summary(true, false) will be called).
*
* - csv_summary_unix
*
* Output CSV summary format without header and with unix style newlines (i.e. tric_output_csv_summary(false, true) will be called).
*
* - csv_summary_header_unix
*
* Output CSV summary format with header and with unix style newlines (i.e. tric_output_csv_summary(true, true) will be called).
*
* - json
*
* Output JSON format (i.e. tric_output_json() will be called).
*
* - none
*
* Do not report any test results (i.e. tric_log(NULL, NULL, NULL, NULL) will be called).
*
* Any other value for TRIC_OUTPUT_FORMAT or if TRIC_OUTPUT_FORMAT is not defined will set the default reporting of TRIC.
*
* This function must be called before any test in the test suite is executed (i.e. in the test suite setup fixture).
*/
void tric_output_environment(void) {
char *format = tric_environment_format();
if (format == NULL) {
return;
} else if (tric_environment_match(format, "tap")) {
tric_output_tap();
} else if (tric_environment_match(format, "csv")) {
tric_output_csv(false, false);
} else if (tric_environment_match(format, "csv_header")) {
tric_output_csv(true, false);
} else if (tric_environment_match(format, "csv_unix")) {
tric_output_csv(false, true);
} else if (tric_environment_match(format, "csv_header_unix")) {
tric_output_csv(true, true);
} else if (tric_environment_match(format, "csv_summary")) {
tric_output_csv_summary(false, false);
} else if (tric_environment_match(format, "csv_summary_header")) {
tric_output_csv_summary(true, false);
} else if (tric_environment_match(format, "csv_summary_unix")) {
tric_output_csv_summary(false, true);
} else if (tric_environment_match(format, "csv_summary_header_unix")) {
tric_output_csv_summary(true, true);
} else if (tric_environment_match(format, "json")) {
tric_output_json();
} else if (tric_environment_match(format, "none")) {
tric_log(NULL, NULL, NULL, NULL);
}
}
#endif