tap.h 8.1 KB

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