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