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