2 * libdpkg - Debian packaging suite library routines
3 * test.h - test suite support
5 * Copyright © 2009-2014 Guillem Jover <guillem@debian.org>
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 #ifndef LIBDPKG_TEST_H
22 #define LIBDPKG_TEST_H
29 #include <dpkg/macros.h>
30 #ifndef TEST_MAIN_CTOR
31 #include <dpkg/ehandle.h>
32 #define TEST_MAIN_CTOR push_error_context()
33 #define TEST_MAIN_DTOR pop_error_context(ehflag_normaltidy)
39 * @defgroup dpkg_test Test suite support
40 * @ingroup dpkg-internal
44 #define test_bail(reason) \
46 printf("Bail out! %s\n", (reason)); \
50 #define test_xstringify(str) \
52 #define test_stringify(str) \
56 test_alloc(void *ptr
, const char *reason
)
63 #define test_alloc(ptr) \
64 test_alloc((ptr), "cannot allocate memory for " #ptr " in " __FILE__ ":" test_stringify(__LINE__))
66 #define test_try(jmp) \
67 push_error_context_jump(&(jmp), DPKG_NULL, "test try"); \
71 #define test_finally \
72 pop_error_context(ehflag_normaltidy);
74 static inline const char *
75 test_get_envdir(const char *envvar
)
77 const char *envdir
= getenv(envvar
);
78 return envdir
? envdir
: ".";
81 #define test_get_srcdir() \
82 test_get_envdir("srcdir")
83 #define test_get_builddir() \
84 test_get_envdir("builddir")
87 test_data_file(const char *filename
)
92 rc
= asprintf(&pathname
, "%s/t/data/%s", test_get_srcdir(), filename
);
94 test_bail("cannot allocate data filename");
100 test_is_verbose(void)
102 const char *verbose
= getenv("TEST_VERBOSE");
103 return verbose
!= DPKG_NULL
&& strcmp(verbose
, "1") == 0;
106 #ifndef TEST_OMIT_VARIABLES
107 static int test_id
= 1;
108 static int test_skip_code
;
109 static const char *test_skip_prefix
;
110 static const char *test_skip_reason
;
113 #define test_plan(n) \
114 printf("1..%d\n", n);
116 #define test_skip_all(reason) \
118 printf("1..0 # SKIP %s\n", (reason)); \
121 #define test_skip(reason) \
122 printf("ok %d # SKIP %s\n", test_id++, (reason))
123 #define test_skip_block(cond) \
124 for (test_skip_prefix = " # SKIP ", \
125 test_skip_reason = cond ? #cond : DPKG_NULL, \
126 test_skip_code = 1; \
128 test_skip_prefix = test_skip_reason = DPKG_NULL, \
131 #define test_todo(a, reason, desc) \
133 test_skip_prefix = " # TODO "; \
134 test_skip_reason = reason; \
135 test_case(a, "%s", desc); \
136 test_skip_prefix = test_skip_reason = DPKG_NULL; \
138 #define test_todo_block(reason) \
139 for (test_skip_prefix = " # TODO ", test_skip_reason = reason; \
141 test_skip_prefix = test_skip_reason = DPKG_NULL)
143 #define test_case(a, fmt, ...) \
144 printf("%sok %d - " fmt "%s%s\n", \
145 test_skip_code || (a) ? "" : "not ", \
146 test_id++, __VA_ARGS__, \
147 test_skip_reason ? test_skip_prefix : "", \
148 test_skip_reason ? test_skip_reason : "")
150 #define test_pass(a) \
151 test_case((a), "pass %s", #a)
152 #define test_fail(a) \
153 test_case(!(a), "fail %s", #a)
154 #define test_str(a, op, b) \
155 test_case(strcmp((a), (b)) op 0, "strcmp '%s' %s '%s'", a, #op, b)
156 #define test_mem(a, op, b, size) \
157 test_case(memcmp((a), (b), (size)) op 0, "memcmp %p %s %p", a, #op, b)
159 /* Specific test macros. */
160 #define test_warn(e) \
162 test_pass((e).type == DPKG_MSG_WARN); \
163 dpkg_error_destroy(&(e)); \
165 #define test_error(e) \
167 test_pass((e).type == DPKG_MSG_ERROR); \
168 dpkg_error_destroy(&(e)); \
175 #define TEST_ENTRY(name) \
176 static void name(void); \
178 main(int argc, char **argv) \
180 setvbuf(stdout, DPKG_NULL, _IOLBF, 0); \