]> begriffs open source - libtap/blob - tap.h
compile libtap tests to .t on *nix so they get run by prove
[libtap] / tap.h
1 #ifndef __TAP_H__
2 #define __TAP_H__
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7
8 #define NO_PLAN          -1
9 #define ok(...)          ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
10 #define pass(...)        ok(1, ## __VA_ARGS__)
11 #define fail(...)        ok(0, ## __VA_ARGS__)
12 #define is(...)          is_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
13 #define isnt(...)        isnt_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
14 #define cmp_ok(...)      cmp_ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
15
16 int     vok_at_loc      (const char *file, int line, int test, const char *fmt,
17                          va_list args);
18 void    plan            (int tests);
19 int     ok_at_loc       (const char *file, int line, int test, const char *fmt,
20                          ...);
21 int     diag            (const char *fmt, ...);
22 int     note            (const char *fmt, ...);
23 int     exit_status     (void);
24 void    skippy          (int n, const char *fmt, ...);
25 void    ctodo           (int ignore, const char *fmt, ...);
26 void    cendtodo        (void);
27 int     is_at_loc       (const char *file, int line, const char *got,
28                          const char *expected, const char *fmt, ...);
29 int     isnt_at_loc     (const char *file, int line, const char *got,
30                          const char *expected, const char *fmt, ...);
31 int     cmp_ok_at_loc   (const char *file, int line, int a, const char *op,
32                          int b, const char *fmt, ...);
33
34 #ifdef _WIN32
35 #define like(...)   skippy(1, "like is not implemented on MSWin32")
36 #define unlike(...) like()
37 #else
38 #define like(...)   like_at_loc(1, __FILE__, __LINE__, __VA_ARGS__, NULL)
39 #define unlike(...) like_at_loc(0, __FILE__, __LINE__, __VA_ARGS__, NULL)
40 int     like_at_loc     (int for_match, const char *file, int line,
41                          const char *got, const char *expected,
42                          const char *fmt, ...);
43 #endif
44
45 #define skip(test, ...)  do {if (test) {skippy(__VA_ARGS__, NULL); break;}
46 #define endskip          } while (0)
47
48 #define todo(...)        ctodo(0, ## __VA_ARGS__, NULL)
49 #define endtodo          cendtodo()
50
51 #define dies_ok(code, ...)  dies_ok_common(code, 1, ## __VA_ARGS__)
52 #define lives_ok(code, ...) dies_ok_common(code, 0, ## __VA_ARGS__)
53
54 #ifdef _WIN32
55 #define dies_ok_common(...) \
56     skippy(1, "Death detection is not supported on MSWin32")
57 #else
58 #include <unistd.h>
59 #include <sys/types.h>
60 #include <sys/wait.h>
61 int tap_test_died (int status);
62 #define dies_ok_common(code, for_death, ...)                \
63     do {                                                    \
64         tap_test_died(1);                                   \
65         int cpid = fork();                                  \
66         switch (cpid) {                                     \
67         case -1:                                            \
68             perror("fork error");                           \
69             exit(EXIT_FAILURE);                             \
70         case 0: /* child  */                                \
71             close(1); close(2);                             \
72             code                                            \
73             tap_test_died(0);                               \
74             exit(EXIT_SUCCESS);                             \
75         }                                                   \
76         if (waitpid(cpid, NULL, 0) < 0) {                   \
77             perror("waitpid error");                        \
78             exit(EXIT_FAILURE);                             \
79         }                                                   \
80         int it_died = tap_test_died(0);                     \
81         if (!it_died) {code}                                \
82         ok(for_death ? it_died : !it_died, ## __VA_ARGS__); \
83     } while (0)
84 #endif
85 #endif