test: add tap utility files
[transsip-mirror.git] / src / test / tap.h
blobb6c9e8335073732eda8af1243a639d106e79a083
1 /*
2 * transsip - the telephony toolkit
3 * libtap (Write tests in C, by Jake Gelbman)
4 * Copyright 2012 Jake Gelbman <gelbman@gmail.com>
5 * Copyright 2012 Daniel Borkmann <borkmann@iogearbox.net>
6 * Subject to the GPL, version 2.
7 */
9 #ifndef TAP_H
10 #define TAP_H
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
19 #include "../die.h"
21 extern int vok_at_loc(const char *file, int line, int test, const char *fmt,
22 va_list args);
23 extern int ok_at_loc(const char *file, int line, int test, const char *fmt,
24 ...);
25 extern int is_at_loc(const char *file, int line, const char *got,
26 const char *expected, const char *fmt, ...);
27 extern int isnt_at_loc(const char *file, int line, const char *got,
28 const char *expected, const char *fmt, ...);
29 extern int cmp_ok_at_loc(const char *file, int line, int a, const char *op,
30 int b, const char *fmt, ...);
31 extern int bail_out(int ignore, const char *fmt, ...);
32 extern void cplan(int tests, const char *fmt, ...);
33 extern int diag(const char *fmt, ...);
34 extern int note(const char *fmt, ...);
35 extern int exit_status(void);
36 extern void skippy(int n, const char *fmt, ...);
37 extern void ctodo(int ignore, const char *fmt, ...);
38 extern void cendtodo(void);
40 #define NO_PLAN -1
41 #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)
48 #define plan(...) cplan(__VA_ARGS__, NULL)
49 #define done_testing() return exit_status()
50 #define BAIL_OUT(...) bail_out(0, "" __VA_ARGS__, NULL)
52 #define pass(...) ok(1, "" __VA_ARGS__)
53 #define fail(...) ok(0, "" __VA_ARGS__)
55 #define skip(test, ...) do { if (test) { skippy(__VA_ARGS__, NULL); break; }
56 #define endskip } while (0)
58 #define todo(...) ctodo(0, "" __VA_ARGS__, NULL)
59 #define endtodo cendtodo()
61 #define dies_ok(...) dies_ok_common(1, __VA_ARGS__)
62 #define lives_ok(...) dies_ok_common(0, __VA_ARGS__)
64 #define like(...) like_at_loc(1, __FILE__, __LINE__, __VA_ARGS__, NULL)
65 #define unlike(...) like_at_loc(0, __FILE__, __LINE__, __VA_ARGS__, NULL)
67 extern int like_at_loc(int for_match, const char *file, int line,
68 const char *got, const char *expected,
69 const char *fmt, ...);
70 extern int tap_test_died (int status);
72 #define dies_ok_common(for_death, code, ...) \
73 do { \
74 int cpid; \
75 int it_died; \
77 tap_test_died(1); \
78 cpid = fork(); \
80 switch (cpid) { \
81 case -1: \
82 panic("fork error!\n"); \
83 case 0: \
84 close(1); \
85 close(2); \
86 {code} \
87 tap_test_died(0); \
88 die(); \
89 } \
91 if (waitpid(cpid, NULL, 0) < 0) \
92 panic("waitpid error!\n"); \
94 it_died = tap_test_died(0); \
95 if (!it_died) \
96 {code} \
98 ok(for_death ? it_died : \
99 !it_died, "" __VA_ARGS__);\
100 } while (0)
102 #endif /* TAP_H */