4 * Copyright (c) 2004 Nik Clayton
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 #include <ccan/compiler/compiler.h>
31 * plan_tests - announce the number of tests you plan to run
32 * @tests: the number of tests
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.
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.
43 void plan_tests(unsigned int tests);
46 * ok1 - Simple conditional test
47 * @e: the expression which we expect to be true.
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.
54 * ok1(somefunc() == 1);
56 # define ok1(e) ((e) ? \
57 _gen_result(1, __func__, __FILE__, __LINE__, "%s", #e) : \
58 _gen_result(0, __func__, __FILE__, __LINE__, "%s", #e))
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.
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.
70 * ok1(somefunc() == 1);
71 * ok(somefunc() == 0, "Second somefunc() should fail");
73 # define ok(e, ...) ((e) ? \
74 _gen_result(1, __func__, __FILE__, __LINE__, \
76 _gen_result(0, __func__, __FILE__, __LINE__, \
80 * pass - Note that a test passed
81 * @...: the printf-style name of the test.
83 * For complicated code paths, it can be easiest to simply call pass() in one
84 * branch and fail() in another.
89 * pass("somefunc() returned a valid value");
91 * fail("somefunc() returned an invalid value");
93 # define pass(...) ok(1, __VA_ARGS__)
96 * fail - Note that a test failed
97 * @...: the printf-style name of the test.
99 * For complicated code paths, it can be easiest to simply call pass() in one
100 * branch and fail() in another.
102 # define fail(...) ok(0, __VA_ARGS__)
104 /* I don't find these to be useful. */
105 # define skip_if(cond, n, ...) \
106 if (cond) skip((n), __VA_ARGS__); \
109 # define skip_start(test, n, ...) \
112 skip(n, __VA_ARGS__); \
116 # define skip_end } while(0)
118 unsigned int _gen_result(int, const char *, const char *, unsigned int,
119 const char *, ...) PRINTF_FMT(5, 6);
122 * diag - print a diagnostic message (use instead of printf/fprintf)
123 * @fmt: the format of the printf-style message
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.
129 * diag("Now running complex tests");
131 void diag(const char *fmt, ...) PRINTF_FMT(1, 2);
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.
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().
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.
148 * #ifdef HAVE_SOME_FEATURE
151 * skip(1, "Don't have SOME_FEATURE");
154 void skip(unsigned int n, const char *fmt, ...) PRINTF_FMT(2, 3);
157 * todo_start - mark tests that you expect to fail.
158 * @fmt: the reason they currently fail.
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
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.
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).
175 * static bool dwim(void)
177 * return false; // NYI
180 * todo_start("dwim() not returning true yet");
181 * ok(dwim(), "Did what the user wanted");
184 void todo_start(const char *fmt, ...) PRINTF_FMT(1, 2);
187 * todo_end - end of tests you expect to fail.
194 * exit_status - the value that main should return.
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).
201 * exit(exit_status());
203 int exit_status(void);
206 * plan_no_plan - I have no idea how many tests I'm going to run.
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.
214 * Remember, if you fail to plan, you plan to fail.
218 * while (random() % 2)
220 * exit(exit_status());
222 void plan_no_plan(void);
225 * plan_skip_all - Indicate that you will skip all tests.
226 * @reason: the string indicating why you can't run any tests.
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().
234 * #ifndef HAVE_SOME_FEATURE
235 * plan_skip_all("Need SOME_FEATURE support");
236 * exit(exit_status());
242 void plan_skip_all(const char *reason);
245 * tap_fail_callback - function to call when we fail
247 * This can be used to ease debugging, or exit on the first failure.
249 void (*tap_fail_callback)(void);
251 #endif /* CCAN_TAP_H */