]> git.ozlabs.org Git - ccan/blob - ccan/tap/tap.h
tdb2: copy tdb1's changed expansion logic.
[ccan] / ccan / tap / tap.h
1 #ifndef CCAN_TAP_H
2 #define CCAN_TAP_H
3 /*-
4  * Copyright (c) 2004 Nik Clayton
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #include <ccan/compiler/compiler.h>
29
30 /**
31  * plan_tests - announce the number of tests you plan to run
32  * @tests: the number of tests
33  *
34  * This should be the first call in your test program: it allows tracing
35  * of failures which mean that not all tests are run.
36  *
37  * If you don't know how many tests will actually be run, assume all of them
38  * and use skip() if you don't actually run some tests.
39  *
40  * Example:
41  *      plan_tests(13);
42  */
43 void plan_tests(unsigned int tests);
44
45 #if (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L) && !defined(__GNUC__)
46 # error "Needs gcc or C99 compiler for variadic macros."
47 #else
48
49 /**
50  * ok1 - Simple conditional test
51  * @e: the expression which we expect to be true.
52  *
53  * This is the simplest kind of test: if the expression is true, the
54  * test passes.  The name of the test which is printed will simply be
55  * file name, line number, and the expression itself.
56  *
57  * Example:
58  *      ok1(somefunc() == 1);
59  */
60 # define ok1(e) ((e) ?                                                  \
61                  _gen_result(1, __func__, __FILE__, __LINE__, "%s", #e) : \
62                  _gen_result(0, __func__, __FILE__, __LINE__, "%s", #e))
63
64 /**
65  * ok - Conditional test with a name
66  * @e: the expression which we expect to be true.
67  * @...: the printf-style name of the test.
68  *
69  * If the expression is true, the test passes.  The name of the test will be
70  * the filename, line number, and the printf-style string.  This can be clearer
71  * than simply the expression itself.
72  *
73  * Example:
74  *      ok1(somefunc() == 1);
75  *      ok(somefunc() == 0, "Second somefunc() should fail");
76  */
77 # define ok(e, ...) ((e) ?                                              \
78                      _gen_result(1, __func__, __FILE__, __LINE__,       \
79                                  __VA_ARGS__) :                         \
80                      _gen_result(0, __func__, __FILE__, __LINE__,       \
81                                  __VA_ARGS__))
82
83 /**
84  * pass - Note that a test passed
85  * @...: the printf-style name of the test.
86  *
87  * For complicated code paths, it can be easiest to simply call pass() in one
88  * branch and fail() in another.
89  *
90  * Example:
91  *      int x = somefunc();
92  *      if (x > 0)
93  *              pass("somefunc() returned a valid value");
94  *      else
95  *              fail("somefunc() returned an invalid value");
96  */
97 # define pass(...) ok(1, __VA_ARGS__)
98
99 /**
100  * fail - Note that a test failed
101  * @...: the printf-style name of the test.
102  *
103  * For complicated code paths, it can be easiest to simply call pass() in one
104  * branch and fail() in another.
105  */
106 # define fail(...) ok(0, __VA_ARGS__)
107
108 /* I don't find these to be useful. */
109 # define skip_if(cond, n, ...)                          \
110         if (cond) skip((n), __VA_ARGS__);               \
111         else
112
113 # define skip_start(test, n, ...)                       \
114         do {                                            \
115                 if((test)) {                            \
116                         skip(n,  __VA_ARGS__);          \
117                         continue;                       \
118                 }
119
120 # define skip_end } while(0)
121
122 unsigned int _gen_result(int, const char *, const char *, unsigned int,
123    const char *, ...) PRINTF_FMT(5, 6);
124
125 /**
126  * diag - print a diagnostic message (use instead of printf/fprintf)
127  * @fmt: the format of the printf-style message
128  *
129  * diag ensures that the output will not be considered to be a test
130  * result by the TAP test harness.  It will append '\n' for you.
131  *
132  * Example:
133  *      diag("Now running complex tests");
134  */
135 void diag(const char *fmt, ...) PRINTF_FMT(1, 2);
136
137 /**
138  * skip - print a diagnostic message (use instead of printf/fprintf)
139  * @n: number of tests you're skipping.
140  * @fmt: the format of the reason you're skipping the tests.
141  *
142  * Sometimes tests cannot be run because the test system lacks some feature:
143  * you should explicitly document that you're skipping tests using skip().
144  *
145  * From the Test::More documentation:
146  *   If it's something the user might not be able to do, use SKIP.  This
147  *   includes optional modules that aren't installed, running under an OS that
148  *   doesn't have some feature (like fork() or symlinks), or maybe you need an
149  *   Internet connection and one isn't available.
150  *
151  * Example:
152  *      #ifdef HAVE_SOME_FEATURE
153  *      ok1(somefunc());
154  *      #else
155  *      skip(1, "Don't have SOME_FEATURE");
156  *      #endif
157  */
158 void skip(unsigned int n, const char *fmt, ...) PRINTF_FMT(2, 3);
159
160 /**
161  * todo_start - mark tests that you expect to fail.
162  * @fmt: the reason they currently fail.
163  *
164  * It's extremely useful to write tests before you implement the matching fix
165  * or features: surround these tests by todo_start()/todo_end().  These tests
166  * will still be run, but with additional output that indicates that they are
167  * expected to fail.
168  *
169  * This way, should a test start to succeed unexpectedly, tools like prove(1)
170  * will indicate this and you can move the test out of the todo block.  This
171  * is much more useful than simply commenting out (or '#if 0') the tests.
172  *
173  * From the Test::More documentation:
174  *   If it's something the programmer hasn't done yet, use TODO.  This is for
175  *   any code you haven't written yet, or bugs you have yet to fix, but want to
176  *   put tests in your testing script (always a good idea).
177  *
178  * Example:
179  * static bool dwim(void)
180  * {
181  *      return false; // NYI
182  * }
183  * ...
184  *      todo_start("dwim() not returning true yet");
185  *      ok(dwim(), "Did what the user wanted");
186  *      todo_end();
187  */
188 void todo_start(const char *fmt, ...) PRINTF_FMT(1, 2);
189
190 /**
191  * todo_end - end of tests you expect to fail.
192  *
193  * See todo_start().
194  */
195 void todo_end(void);
196
197 /**
198  * exit_status - the value that main should return.
199  *
200  * For maximum compatibility your test program should return a particular exit
201  * code (ie. 0 if all tests were run, and every test which was expected to
202  * succeed succeeded).
203  *
204  * Example:
205  *      exit(exit_status());
206  */
207 int exit_status(void);
208
209 /**
210  * plan_no_plan - I have no idea how many tests I'm going to run.
211  *
212  * In some situations you may not know how many tests you will be running, or
213  * you are developing your test program, and do not want to update the
214  * plan_tests() call every time you make a change.  For those situations use
215  * plan_no_plan() instead of plan_tests().  It indicates to the test harness
216  * that an indeterminate number of tests will be run.
217  *
218  * Remember, if you fail to plan, you plan to fail.
219  *
220  * Example:
221  *      plan_no_plan();
222  *      while (random() % 2)
223  *              ok1(somefunc());
224  *      exit(exit_status());
225  */
226 void plan_no_plan(void);
227
228 /**
229  * plan_skip_all - Indicate that you will skip all tests.
230  * @reason: the string indicating why you can't run any tests.
231  *
232  * If your test program detects at run time that some required functionality
233  * is missing (for example, it relies on a database connection which is not
234  * present, or a particular configuration option that has not been included
235  * in the running kernel) use plan_skip_all() instead of plan_tests().
236  *
237  * Example:
238  *      #ifndef HAVE_SOME_FEATURE
239  *      plan_skip_all("Need SOME_FEATURE support");
240  *      exit(exit_status());
241  *      #else
242  *      plan_tests(13);
243  *      ...
244  *      #endif
245  */
246 void plan_skip_all(const char *reason);
247
248 /**
249  * tap_fail_callback - function to call when we fail
250  *
251  * This can be used to ease debugging, or exit on the first failure.
252  */
253 void (*tap_fail_callback)(void);
254
255 #endif /* C99 or gcc */
256 #endif /* CCAN_TAP_H */