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