]> begriffs open source - libtap/blob - tap.h
Make color optional
[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 TAP_COLOR_OUTPUT
11 #define AC_RED     "\x1b[31m"
12 #define AC_GREEN   "\x1b[32m"
13 #define AC_YELLOW  "\x1b[33m"
14 #define AC_BLUE    "\x1b[34m"
15 #define AC_MAGENTA "\x1b[35m"
16 #define AC_CYAN    "\x1b[36m"
17 #define AC_RESET   "\x1b[0m"
18
19 #define AC_CLR(color, text) AC_##color text AC_RESET
20 #define AC_OK(text) AC_CLR(GREEN, text)
21 #define AC_BAD(text) AC_CLR(RED, text)
22 #else
23 #define AC_OK(text) text
24 #define AC_BAD(text) text
25 #endif
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 #ifndef va_copy
32 #ifdef __va_copy
33 #define va_copy __va_copy
34 #else
35 #define va_copy(d, s) ((d) = (s))
36 #endif
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <stdarg.h>
42
43 int     vok_at_loc      (const char *file, int line, int test, const char *fmt,
44                          va_list args);
45 int     ok_at_loc       (const char *file, int line, int test, const char *fmt,
46                          ...);
47 int     is_at_loc       (const char *file, int line, const char *got,
48                          const char *expected, const char *fmt, ...);
49 int     isnt_at_loc     (const char *file, int line, const char *got,
50                          const char *expected, const char *fmt, ...);
51 int     cmp_ok_at_loc   (const char *file, int line, int a, const char *op,
52                          int b, const char *fmt, ...);
53 int     cmp_mem_at_loc  (const char *file, int line, const void *got,
54                          const void *expected, size_t n, const char *fmt, ...);
55 int     bail_out        (int ignore, const char *fmt, ...);
56 void    tap_plan        (int tests, const char *fmt, ...);
57 int     diag            (const char *fmt, ...);
58 int     note            (const char *fmt, ...);
59 int     exit_status     (void);
60 void    tap_skip        (int n, const char *fmt, ...);
61 void    tap_todo        (int ignore, const char *fmt, ...);
62 void    tap_end_todo    (void);
63
64 #define NO_PLAN          -1
65 #define SKIP_ALL         -2
66 #define ok(...)          ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
67 #define is(...)          is_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
68 #define isnt(...)        isnt_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
69 #define cmp_ok(...)      cmp_ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
70 #define cmp_mem(...)     cmp_mem_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL);
71 #define plan(...)        tap_plan(__VA_ARGS__, NULL)
72 #define done_testing()   return exit_status()
73 #define BAIL_OUT(...)    bail_out(0, "" __VA_ARGS__, NULL)
74 #define pass(...)        ok(1, "" __VA_ARGS__)
75 #define fail(...)        ok(0, "" __VA_ARGS__)
76
77 #define skip(test, ...)  do {if (test) {tap_skip(__VA_ARGS__, NULL); break;}
78 #define end_skip         } while (0)
79
80 #define todo(...)        tap_todo(0, "" __VA_ARGS__, NULL)
81 #define end_todo         tap_end_todo()
82
83 #define dies_ok(...)     dies_ok_common(1, __VA_ARGS__)
84 #define lives_ok(...)    dies_ok_common(0, __VA_ARGS__)
85
86 #ifdef _WIN32
87 #define like(...)        tap_skip(1, "like is not implemented on Windows")
88 #define unlike           tap_skip(1, "unlike is not implemented on Windows")
89 #define dies_ok_common(...) \
90                          tap_skip(1, "Death detection is not supported on Windows")
91 #else
92 #define like(...)        like_at_loc(1, __FILE__, __LINE__, __VA_ARGS__, NULL)
93 #define unlike(...)      like_at_loc(0, __FILE__, __LINE__, __VA_ARGS__, NULL)
94 int     like_at_loc     (int for_match, const char *file, int line,
95                          const char *got, const char *expected,
96                          const char *fmt, ...);
97 #include <unistd.h>
98 #include <sys/types.h>
99 #include <sys/wait.h>
100 int tap_test_died (int status);
101 #define dies_ok_common(for_death, code, ...)                \
102     do {                                                    \
103         int cpid;                                           \
104         int it_died;                                        \
105         tap_test_died(1);                                   \
106         cpid = fork();                                      \
107         switch (cpid) {                                     \
108         case -1:                                            \
109             perror("fork error");                           \
110             exit(1);                                        \
111         case 0:                                             \
112             close(1);                                       \
113             close(2);                                       \
114             code                                            \
115             tap_test_died(0);                               \
116             exit(0);                                        \
117         }                                                   \
118         if (waitpid(cpid, NULL, 0) < 0) {                   \
119             perror("waitpid error");                        \
120             exit(1);                                        \
121         }                                                   \
122         it_died = tap_test_died(0);                         \
123         if (!it_died)                                       \
124             {code}                                          \
125         ok(for_death ? it_died : !it_died, "" __VA_ARGS__); \
126     } while (0)
127 #endif
128
129 #ifdef __cplusplus
130 }
131 #endif
132
133 #endif