]> begriffs open source - libtap/blob - tap.h
Update license to GPLv2+ to be compatible with the Apache 2.0 license
[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 GPLv2 or any later version
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     bail_out        (int ignore, const char *fmt, ...);
37 void    tap_plan        (int tests, const char *fmt, ...);
38 int     diag            (const char *fmt, ...);
39 int     note            (const char *fmt, ...);
40 int     exit_status     (void);
41 void    tap_skip        (int n, const char *fmt, ...);
42 void    tap_todo        (int ignore, const char *fmt, ...);
43 void    tap_end_todo    (void);
44
45 #define NO_PLAN          -1
46 #define SKIP_ALL         -2
47 #define ok(...)          ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
48 #define is(...)          is_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
49 #define isnt(...)        isnt_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
50 #define cmp_ok(...)      cmp_ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
51 #define plan(...)        tap_plan(__VA_ARGS__, NULL)
52 #define done_testing()   return exit_status()
53 #define BAIL_OUT(...)    bail_out(0, "" __VA_ARGS__, NULL)
54 #define pass(...)        ok(1, "" __VA_ARGS__)
55 #define fail(...)        ok(0, "" __VA_ARGS__)
56
57 #define skip(test, ...)  do {if (test) {tap_skip(__VA_ARGS__, NULL); break;}
58 #define end_skip         } while (0)
59
60 #define todo(...)        tap_todo(0, "" __VA_ARGS__, NULL)
61 #define end_todo         tap_end_todo()
62
63 #define dies_ok(...)     dies_ok_common(1, __VA_ARGS__)
64 #define lives_ok(...)    dies_ok_common(0, __VA_ARGS__)
65
66 #ifdef _WIN32
67 #define like(...)        tap_skip(1, "like is not implemented on Windows")
68 #define unlike           tap_skip(1, "unlike is not implemented on Windows")
69 #define dies_ok_common(...) \
70                          tap_skip(1, "Death detection is not supported on Windows")
71 #else
72 #define like(...)        like_at_loc(1, __FILE__, __LINE__, __VA_ARGS__, NULL)
73 #define unlike(...)      like_at_loc(0, __FILE__, __LINE__, __VA_ARGS__, NULL)
74 int     like_at_loc     (int for_match, const char *file, int line,
75                          const char *got, const char *expected,
76                          const char *fmt, ...);
77 #include <unistd.h>
78 #include <sys/types.h>
79 #include <sys/wait.h>
80 int tap_test_died (int status);
81 #define dies_ok_common(for_death, code, ...)                \
82     do {                                                    \
83         int cpid;                                           \
84         int it_died;                                        \
85         tap_test_died(1);                                   \
86         cpid = fork();                                      \
87         switch (cpid) {                                     \
88         case -1:                                            \
89             perror("fork error");                           \
90             exit(1);                                        \
91         case 0:                                             \
92             close(1);                                       \
93             close(2);                                       \
94             code                                            \
95             tap_test_died(0);                               \
96             exit(0);                                        \
97         }                                                   \
98         if (waitpid(cpid, NULL, 0) < 0) {                   \
99             perror("waitpid error");                        \
100             exit(1);                                        \
101         }                                                   \
102         it_died = tap_test_died(0);                         \
103         if (!it_died)                                       \
104             {code}                                          \
105         ok(for_death ? it_died : !it_died, "" __VA_ARGS__); \
106     } while (0)
107 #endif
108
109 #ifdef __cplusplus
110 }
111 #endif
112
113 #endif
114