tap.h 7.9 KB

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