]> git.ozlabs.org Git - ccan/blob - tap.h
1dad63650e40a2c26f62f99d90964e6c9bedcd93
[ccan] / 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
27 /**
28  * plan_tests - announce the number of tests you plan to run
29  * @tests: the number of tests
30  *
31  * This should be the first call in your test program: it allows tracing
32  * of failures which mean that not all tests are run.
33  *
34  * If you don't know how many tests will actually be run, assume all of them
35  * and use skip() if you don't actually run some tests.
36  *
37  * Example:
38  *      plan_tests(13);
39  */
40 void plan_tests(unsigned int tests);
41
42 #if (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L) && !defined(__GNUC__)
43 # error "Needs gcc or C99 compiler for variadic macros."
44 #else
45
46 /**
47  * ok1 - Simple conditional test
48  * @e: the expression which we expect to be true.
49  *
50  * This is the simplest kind of test: if the expression is true, the
51  * test passes.  The name of the test which is printed will simply be
52  * file name, line number, and the expression itself.
53  *
54  * Example:
55  *      ok1(init_subsystem() == 1);
56  */
57 # define ok1(e) ((e) ?                                                  \
58                  _gen_result(1, __func__, __FILE__, __LINE__, "%s", #e) : \
59                  _gen_result(0, __func__, __FILE__, __LINE__, "%s", #e))
60
61 /**
62  * ok - Conditional test with a name
63  * @e: the expression which we expect to be true.
64  * @...: the printf-style name of the test.
65  *
66  * If the expression is true, the test passes.  The name of the test will be
67  * the filename, line number, and the printf-style string.  This can be clearer
68  * than simply the expression itself.
69  *
70  * Example:
71  *      ok1(init_subsystem() == 1);
72  *      ok(init_subsystem() == 0, "Second initialization should fail");
73  */
74 # define ok(e, ...) ((e) ?                                              \
75                      _gen_result(1, __func__, __FILE__, __LINE__,       \
76                                  __VA_ARGS__) :                         \
77                      _gen_result(0, __func__, __FILE__, __LINE__,       \
78                                  __VA_ARGS__))
79
80 /**
81  * pass - Note that a test passed
82  * @...: the printf-style name of the test.
83  *
84  * For complicated code paths, it can be easiest to simply call pass() in one
85  * branch and fail() in another.
86  *
87  * Example:
88  *      x = do_something();
89  *      if (!checkable(x) || check_value(x))
90  *              pass("do_something() returned a valid value");
91  *      else            
92  *              fail("do_something() returned an invalid value");
93  */
94 # define pass(...) ok(1, __VA_ARGS__)
95
96 /**
97  * fail - Note that a test failed
98  * @...: the printf-style name of the test.
99  *
100  * For complicated code paths, it can be easiest to simply call pass() in one
101  * branch and fail() in another.
102  */
103 # define fail(...) ok(0, __VA_ARGS__)
104
105 /* I don't find these to be useful. */
106 # define skip_if(cond, n, ...)                          \
107         if (cond) skip((n), __VA_ARGS__);               \
108         else
109
110 # define skip_start(test, n, ...)                       \
111         do {                                            \
112                 if((test)) {                            \
113                         skip(n,  __VA_ARGS__);          \
114                         continue;                       \
115                 }
116
117 # define skip_end } while(0)
118
119 #ifndef PRINTF_ATTRIBUTE
120 #ifdef __GNUC__
121 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
122 #else
123 #define PRINTF_ATTRIBUTE(a1, a2)
124 #endif
125 #endif
126
127 unsigned int _gen_result(int, const char *, const char *, unsigned int,
128    const char *, ...) PRINTF_ATTRIBUTE(5, 6);
129
130 /**
131  * diag - print a diagnostic message (use instead of printf/fprintf)
132  * @fmt: the format of the printf-style message
133  *
134  * diag ensures that the output will not be considered to be a test
135  * result by the TAP test harness.  It will append '\n' for you.
136  *
137  * Example:
138  *      diag("Now running complex tests");
139  */
140 void diag(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
141
142 /**
143  * skip - print a diagnostic message (use instead of printf/fprintf)
144  * @n: number of tests you're skipping.
145  * @fmt: the format of the reason you're skipping the tests.
146  *
147  * Sometimes tests cannot be run because the test system lacks some feature:
148  * you should explicitly document that you're skipping tests using skip().
149  *
150  * From the Test::More documentation:
151  *   If it's something the user might not be able to do, use SKIP.  This
152  *   includes optional modules that aren't installed, running under an OS that
153  *   doesn't have some feature (like fork() or symlinks), or maybe you need an
154  *   Internet connection and one isn't available.
155  *
156  * Example:
157  *      #ifdef HAVE_SOME_FEATURE
158  *      ok1(test_some_feature());
159  *      #else
160  *      skip(1, "Don't have SOME_FEATURE");
161  *      #endif
162  */
163 void skip(unsigned int n, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
164
165 /**
166  * todo_start - mark tests that you expect to fail.
167  * @fmt: the reason they currently fail.
168  *
169  * It's extremely useful to write tests before you implement the matching fix
170  * or features: surround these tests by todo_start()/todo_end().  These tests
171  * will still be run, but with additional output that indicates that they are
172  * expected to fail.
173  *
174  * This way, should a test start to succeed unexpectedly, tools like prove(1)
175  * will indicate this and you can move the test out of the todo block.  This
176  * is much more useful than simply commenting out (or '#if 0') the tests.
177  * 
178  * From the Test::More documentation:
179  *   If it's something the programmer hasn't done yet, use TODO.  This is for
180  *   any code you haven't written yet, or bugs you have yet to fix, but want to
181  *   put tests in your testing script (always a good idea).
182  *
183  * Example:
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_ATTRIBUTE(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 compatability 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(some_test());
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  *      if (!have_some_feature) {
239  *              plan_skip_all("Need some_feature support");
240  *              exit(exit_status());
241  *      }
242  *      plan_tests(13);
243  */
244 void plan_skip_all(const char *reason);
245
246 #endif /* C99 or gcc */