]> git.draconx.ca Git - dxcommon.git/blob - src/tap.c
Import integer packing library.
[dxcommon.git] / src / tap.c
1 /*
2  * Copyright © 2015 Nick Bowler
3  *
4  * Simple TAP output library for C programs.
5  *
6  * License WTFPL2: Do What The Fuck You Want To Public License, version 2.
7  * This is free software: you are free to do what the fuck you want to.
8  * There is NO WARRANTY, to the extent permitted by law.
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <assert.h>
14
15 #include "tap.h"
16
17 static unsigned plan, total, passed, xpassed, failed, xfailed;
18
19 void tap_plan(unsigned expected_tests)
20 {
21         if (plan)
22                 tap_bail_out("tap_plan: tests already planned!");
23         if (!expected_tests)
24                 tap_bail_out("tap_plan: no tests to run!");
25
26         plan = expected_tests;
27         printf("1..%u\n", plan);
28 }
29
30 void tap_done(void)
31 {
32         if (!plan) {
33                 tap_plan(total);
34         } else if (total != plan) {
35                 tap_bail_out("tap_done: planned %u tests, ran %u", plan, total);
36         }
37
38         assert(total == passed + xpassed + failed + xfailed);
39         exit(failed || xpassed);
40 }
41
42 void tap_vbail_out(const char *fmt, va_list ap)
43 {
44         printf("Bail out!%*s", fmt != NULL, "");
45         if (fmt)
46                 vprintf(fmt, ap);
47         putchar('\n');
48         exit(99);
49 }
50
51 void tap_bail_out(const char *fmt, ...)
52 {
53         va_list ap;
54
55         va_start(ap, fmt);
56         tap_vbail_out(fmt, ap);
57         va_end(ap);
58 }
59
60 void tap_vskip_all(const char *fmt, va_list ap)
61 {
62         if (plan || total)
63                 tap_bail_out("tap_skip_all: already started tests");
64
65         printf("1..0 # skip%*s", fmt != NULL, "");
66         if (fmt)
67                 vprintf(fmt, ap);
68         putchar('\n');
69         exit(77);
70 }
71
72 void tap_skip_all(const char *fmt, ...)
73 {
74         va_list ap;
75
76         va_start(ap, fmt);
77         tap_vskip_all(fmt, ap);
78         va_end(ap);
79 }
80
81 void tap_vdiag(const char *fmt, va_list ap)
82 {
83         printf("#%*s", fmt != NULL, "");
84         if (fmt)
85                 vprintf(fmt, ap);
86         putchar('\n');
87 }
88
89 void tap_diag(const char *fmt, ...)
90 {
91         va_list ap;
92
93         va_start(ap, fmt);
94         tap_vdiag(fmt, ap);
95         va_end(ap);
96 }
97
98 int tap_vresult(int ok, const char *fmt, va_list ap)
99 {
100         if (!ok)
101                 printf("not ");
102         printf("ok %u%*s", ++total, fmt != NULL, "");
103         if (fmt)
104                 vprintf(fmt, ap);
105         putchar('\n');
106
107         if (!total)
108                 tap_bail_out("cannot handle so many tests");
109
110         passed += !!ok;
111         failed += !ok;
112
113         return ok;
114 }
115
116 int tap_result(int ok, const char *fmt, ...)
117 {
118         va_list ap;
119         int ret;
120
121         va_start(ap, fmt);
122         ret = tap_vresult(ok, fmt, ap);
123         va_end(ap);
124
125         return ret;
126 }