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