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