]> git.ozlabs.org Git - ccan/blob - ccan/tap/tap.h
tap: assume variadic macros.
[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 /**
46  * ok1 - Simple conditional test
47  * @e: the expression which we expect to be true.
48  *
49  * This is the simplest kind of test: if the expression is true, the
50  * test passes.  The name of the test which is printed will simply be
51  * file name, line number, and the expression itself.
52  *
53  * Example:
54  *      ok1(somefunc() == 1);
55  */
56 # define ok1(e) ((e) ?                                                  \
57                  _gen_result(1, __func__, __FILE__, __LINE__, "%s", #e) : \
58                  _gen_result(0, __func__, __FILE__, __LINE__, "%s", #e))
59
60 /**
61  * ok - Conditional test with a name
62  * @e: the expression which we expect to be true.
63  * @...: the printf-style name of the test.
64  *
65  * If the expression is true, the test passes.  The name of the test will be
66  * the filename, line number, and the printf-style string.  This can be clearer
67  * than simply the expression itself.
68  *
69  * Example:
70  *      ok1(somefunc() == 1);
71  *      ok(somefunc() == 0, "Second somefunc() should fail");
72  */
73 # define ok(e, ...) ((e) ?                                              \
74                      _gen_result(1, __func__, __FILE__, __LINE__,       \
75                                  __VA_ARGS__) :                         \
76                      _gen_result(0, __func__, __FILE__, __LINE__,       \
77                                  __VA_ARGS__))
78
79 /**
80  * pass - Note that a test passed
81  * @...: the printf-style name of the test.
82  *
83  * For complicated code paths, it can be easiest to simply call pass() in one
84  * branch and fail() in another.
85  *
86  * Example:
87  *      int x = somefunc();
88  *      if (x > 0)
89  *              pass("somefunc() returned a valid value");
90  *      else
91  *              fail("somefunc() returned an invalid value");
92  */
93 # define pass(...) ok(1, __VA_ARGS__)
94
95 /**
96  * fail - Note that a test failed
97  * @...: the printf-style name of the test.
98  *
99  * For complicated code paths, it can be easiest to simply call pass() in one
100  * branch and fail() in another.
101  */
102 # define fail(...) ok(0, __VA_ARGS__)
103
104 /* I don't find these to be useful. */
105 # define skip_if(cond, n, ...)                          \
106         if (cond) skip((n), __VA_ARGS__);               \
107         else
108
109 # define skip_start(test, n, ...)                       \
110         do {                                            \
111                 if((test)) {                            \
112                         skip(n,  __VA_ARGS__);          \
113                         continue;                       \
114                 }
115
116 # define skip_end } while(0)
117
118 unsigned int _gen_result(int, const char *, const char *, unsigned int,
119    const char *, ...) PRINTF_FMT(5, 6);
120
121 /**
122  * diag - print a diagnostic message (use instead of printf/fprintf)
123  * @fmt: the format of the printf-style message
124  *
125  * diag ensures that the output will not be considered to be a test
126  * result by the TAP test harness.  It will append '\n' for you.
127  *
128  * Example:
129  *      diag("Now running complex tests");
130  */
131 void diag(const char *fmt, ...) PRINTF_FMT(1, 2);
132
133 /**
134  * skip - print a diagnostic message (use instead of printf/fprintf)
135  * @n: number of tests you're skipping.
136  * @fmt: the format of the reason you're skipping the tests.
137  *
138  * Sometimes tests cannot be run because the test system lacks some feature:
139  * you should explicitly document that you're skipping tests using skip().
140  *
141  * From the Test::More documentation:
142  *   If it's something the user might not be able to do, use SKIP.  This
143  *   includes optional modules that aren't installed, running under an OS that
144  *   doesn't have some feature (like fork() or symlinks), or maybe you need an
145  *   Internet connection and one isn't available.
146  *
147  * Example:
148  *      #ifdef HAVE_SOME_FEATURE
149  *      ok1(somefunc());
150  *      #else
151  *      skip(1, "Don't have SOME_FEATURE");
152  *      #endif
153  */
154 void skip(unsigned int n, const char *fmt, ...) PRINTF_FMT(2, 3);
155
156 /**
157  * todo_start - mark tests that you expect to fail.
158  * @fmt: the reason they currently fail.
159  *
160  * It's extremely useful to write tests before you implement the matching fix
161  * or features: surround these tests by todo_start()/todo_end().  These tests
162  * will still be run, but with additional output that indicates that they are
163  * expected to fail.
164  *
165  * This way, should a test start to succeed unexpectedly, tools like prove(1)
166  * will indicate this and you can move the test out of the todo block.  This
167  * is much more useful than simply commenting out (or '#if 0') the tests.
168  *
169  * From the Test::More documentation:
170  *   If it's something the programmer hasn't done yet, use TODO.  This is for
171  *   any code you haven't written yet, or bugs you have yet to fix, but want to
172  *   put tests in your testing script (always a good idea).
173  *
174  * Example:
175  * static bool dwim(void)
176  * {
177  *      return false; // NYI
178  * }
179  * ...
180  *      todo_start("dwim() not returning true yet");
181  *      ok(dwim(), "Did what the user wanted");
182  *      todo_end();
183  */
184 void todo_start(const char *fmt, ...) PRINTF_FMT(1, 2);
185
186 /**
187  * todo_end - end of tests you expect to fail.
188  *
189  * See todo_start().
190  */
191 void todo_end(void);
192
193 /**
194  * exit_status - the value that main should return.
195  *
196  * For maximum compatibility your test program should return a particular exit
197  * code (ie. 0 if all tests were run, and every test which was expected to
198  * succeed succeeded).
199  *
200  * Example:
201  *      exit(exit_status());
202  */
203 int exit_status(void);
204
205 /**
206  * plan_no_plan - I have no idea how many tests I'm going to run.
207  *
208  * In some situations you may not know how many tests you will be running, or
209  * you are developing your test program, and do not want to update the
210  * plan_tests() call every time you make a change.  For those situations use
211  * plan_no_plan() instead of plan_tests().  It indicates to the test harness
212  * that an indeterminate number of tests will be run.
213  *
214  * Remember, if you fail to plan, you plan to fail.
215  *
216  * Example:
217  *      plan_no_plan();
218  *      while (random() % 2)
219  *              ok1(somefunc());
220  *      exit(exit_status());
221  */
222 void plan_no_plan(void);
223
224 /**
225  * plan_skip_all - Indicate that you will skip all tests.
226  * @reason: the string indicating why you can't run any tests.
227  *
228  * If your test program detects at run time that some required functionality
229  * is missing (for example, it relies on a database connection which is not
230  * present, or a particular configuration option that has not been included
231  * in the running kernel) use plan_skip_all() instead of plan_tests().
232  *
233  * Example:
234  *      #ifndef HAVE_SOME_FEATURE
235  *      plan_skip_all("Need SOME_FEATURE support");
236  *      exit(exit_status());
237  *      #else
238  *      plan_tests(13);
239  *      ...
240  *      #endif
241  */
242 void plan_skip_all(const char *reason);
243
244 /**
245  * tap_fail_callback - function to call when we fail
246  *
247  * This can be used to ease debugging, or exit on the first failure.
248  */
249 void (*tap_fail_callback)(void);
250
251 #endif /* CCAN_TAP_H */