| 1 | /* Copyright (c) 2006, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | #ifndef TAP_H |
| 26 | #define TAP_H |
| 27 | |
| 28 | #include "my_compiler.h" |
| 29 | |
| 30 | /* |
| 31 | @defgroup MyTAP MySQL support for performing unit tests according to |
| 32 | the Test Anything Protocol (TAP). |
| 33 | */ |
| 34 | |
| 35 | #define NO_PLAN (0) |
| 36 | |
| 37 | /** |
| 38 | Data about test plan. |
| 39 | |
| 40 | @ingroup MyTAP_Internal |
| 41 | |
| 42 | @internal We are using the "typedef struct X { ... } X" idiom to |
| 43 | create class/struct X both in C and C++. |
| 44 | */ |
| 45 | |
| 46 | typedef struct TEST_DATA { |
| 47 | /** |
| 48 | Number of tests that is planned to execute. |
| 49 | |
| 50 | Can be zero (<code>NO_PLAN</code>) meaning that the plan string |
| 51 | will be printed at the end of test instead. |
| 52 | */ |
| 53 | int plan; |
| 54 | |
| 55 | /** Number of last test that was done or skipped. */ |
| 56 | int last; |
| 57 | |
| 58 | /** Number of tests that failed. */ |
| 59 | int failed; |
| 60 | |
| 61 | /** Todo reason. */ |
| 62 | char todo[128]; |
| 63 | } TEST_DATA; |
| 64 | |
| 65 | #ifdef __cplusplus |
| 66 | extern "C" { |
| 67 | #endif |
| 68 | |
| 69 | /** |
| 70 | Defines whether "big" tests should be skipped. |
| 71 | |
| 72 | This variable is set by plan() function unless MYTAP_CONFIG environment |
| 73 | variable is set to the string "big". It is supposed to be used as |
| 74 | |
| 75 | @code |
| 76 | if (skip_big_tests) { |
| 77 | skip(1, "Big test skipped"); |
| 78 | } else { |
| 79 | ok(life_universe_and_everything() == 42, "The answer is CORRECT"); |
| 80 | } |
| 81 | @endcode |
| 82 | |
| 83 | @see SKIP_BIG_TESTS |
| 84 | */ |
| 85 | extern int skip_big_tests; |
| 86 | |
| 87 | /** |
| 88 | @defgroup MyTAP_API MyTAP API |
| 89 | |
| 90 | MySQL support for performing unit tests according to TAP. |
| 91 | |
| 92 | @{ |
| 93 | */ |
| 94 | |
| 95 | /** |
| 96 | Set number of tests that is planned to execute. |
| 97 | |
| 98 | The function also accepts the predefined constant <code>NO_PLAN</code>. |
| 99 | If invoked with this constant -- or not invoked at all -- |
| 100 | the test plan will be printed after all the test lines. |
| 101 | |
| 102 | The plan() function will install signal handlers for all signals |
| 103 | that generate a core, so if you want to override these signals, do |
| 104 | it <em>after</em> you have called the plan() function. |
| 105 | |
| 106 | It will also set skip_big_tests variable if MYTAP_CONFIG environment |
| 107 | variable is defined. |
| 108 | |
| 109 | @see skip_big_tests |
| 110 | |
| 111 | @param count The planned number of tests to run. |
| 112 | */ |
| 113 | |
| 114 | void plan(int const count); |
| 115 | |
| 116 | /** |
| 117 | Report test result as a TAP line. |
| 118 | |
| 119 | Function used to write status of an individual test. Call this |
| 120 | function in the following manner: |
| 121 | |
| 122 | @code |
| 123 | ok(ducks == paddling, |
| 124 | "%d ducks did not paddle", ducks - paddling); |
| 125 | @endcode |
| 126 | |
| 127 | @param pass Zero if the test failed, non-zero if it passed. |
| 128 | @param fmt Format string in printf() format. NULL is not allowed, |
| 129 | use ok1() in this case. |
| 130 | */ |
| 131 | |
| 132 | void ok(int const pass, char const *fmt, ...) |
| 133 | MY_ATTRIBUTE((format(printf, 2, 3))); |
| 134 | |
| 135 | /** |
| 136 | Report test result as a TAP line. |
| 137 | |
| 138 | Same as ok() but does not take a message to be printed. |
| 139 | |
| 140 | @param pass Zero if the test failed, non-zero if it passed. |
| 141 | */ |
| 142 | |
| 143 | void ok1(int const pass); |
| 144 | |
| 145 | /** |
| 146 | Skip a determined number of tests. |
| 147 | |
| 148 | Function to print that <em>how_many</em> tests have been skipped. |
| 149 | The reason is printed for each skipped test. Observe that this |
| 150 | function does not do the actual skipping for you, it just prints |
| 151 | information that tests have been skipped. This function is not |
| 152 | usually used, but rather the macro @c SKIP_BLOCK_IF, which does the |
| 153 | skipping for you. |
| 154 | |
| 155 | It shall be used in the following manner: |
| 156 | |
| 157 | @code |
| 158 | if (ducks == 0) { |
| 159 | skip(2, "No ducks in the pond"); |
| 160 | } else { |
| 161 | int i; |
| 162 | for (i = 0 ; i < 2 ; ++i) |
| 163 | ok(duck[i] == paddling, "is duck %d paddling?", i); |
| 164 | } |
| 165 | @endcode |
| 166 | |
| 167 | @see SKIP_BLOCK_IF |
| 168 | |
| 169 | @param how_many Number of tests that are to be skipped. |
| 170 | @param reason A reason for skipping the tests |
| 171 | */ |
| 172 | |
| 173 | void skip(int how_many, char const *reason, ...) |
| 174 | MY_ATTRIBUTE((format(printf, 2, 3))); |
| 175 | |
| 176 | /** |
| 177 | Helper macro to skip a block of code. The macro can be used to |
| 178 | simplify conditionally skipping a block of code. It is used in the |
| 179 | following manner: |
| 180 | |
| 181 | @code |
| 182 | SKIP_BLOCK_IF(ducks == 0, 2, "No ducks in the pond") |
| 183 | { |
| 184 | int i; |
| 185 | for (i = 0 ; i < 2 ; ++i) |
| 186 | ok(duck[i] == paddling, "is duck %d paddling?", i); |
| 187 | } |
| 188 | @endcode |
| 189 | |
| 190 | @see skip |
| 191 | */ |
| 192 | |
| 193 | #define SKIP_BLOCK_IF(SKIP_IF_TRUE, COUNT, REASON) \ |
| 194 | if (SKIP_IF_TRUE) \ |
| 195 | skip((COUNT), (REASON)); \ |
| 196 | else |
| 197 | |
| 198 | /** |
| 199 | Helper macro to skip a group of "big" tests. It is used in the following |
| 200 | manner: |
| 201 | |
| 202 | @code |
| 203 | SKIP_BIG_TESTS(1) |
| 204 | { |
| 205 | ok(life_universe_and_everything() == 42, "The answer is CORRECT"); |
| 206 | } |
| 207 | @endcode |
| 208 | |
| 209 | @see skip_big_tests |
| 210 | */ |
| 211 | |
| 212 | #define SKIP_BIG_TESTS(COUNT) \ |
| 213 | if (skip_big_tests) \ |
| 214 | skip((COUNT), "big test"); \ |
| 215 | else |
| 216 | |
| 217 | /** |
| 218 | Print a diagnostics message. |
| 219 | |
| 220 | @param fmt Diagnostics message in printf() format. |
| 221 | */ |
| 222 | |
| 223 | void diag(char const *fmt, ...) MY_ATTRIBUTE((format(printf, 1, 2))); |
| 224 | |
| 225 | /** |
| 226 | Print a bail out message. |
| 227 | |
| 228 | A bail out message can be issued when no further testing can be |
| 229 | done, e.g., when there are missing dependencies. |
| 230 | |
| 231 | The test will exit with status 255. This function does not return. |
| 232 | |
| 233 | @code |
| 234 | BAIL_OUT("Lost connection to server %s", server_name); |
| 235 | @endcode |
| 236 | |
| 237 | @note A bail out message is printed if a signal that generates a |
| 238 | core is raised. |
| 239 | |
| 240 | @param fmt Bail out message in printf() format. |
| 241 | */ |
| 242 | |
| 243 | [[noreturn]] void BAIL_OUT(char const *fmt, ...) |
| 244 | MY_ATTRIBUTE((format(printf, 1, 2))); |
| 245 | |
| 246 | /** |
| 247 | Print summary report and return exit status. |
| 248 | |
| 249 | This function will print a summary report of how many tests passed, |
| 250 | how many were skipped, and how many remains to do. The function |
| 251 | should be called after all tests are executed in the following |
| 252 | manner: |
| 253 | |
| 254 | @code |
| 255 | return exit_status(); |
| 256 | @endcode |
| 257 | |
| 258 | @returns @c EXIT_SUCCESS if all tests passed, @c EXIT_FAILURE if |
| 259 | one or more tests failed. |
| 260 | */ |
| 261 | |
| 262 | int exit_status(void); |
| 263 | |
| 264 | /** |
| 265 | Skip entire test suite. |
| 266 | |
| 267 | To skip the entire test suite, use this function. It will |
| 268 | automatically call exit(), so there is no need to have checks |
| 269 | around it. |
| 270 | */ |
| 271 | |
| 272 | [[noreturn]] void skip_all(char const *reason, ...) |
| 273 | MY_ATTRIBUTE((format(printf, 1, 2))); |
| 274 | |
| 275 | /** |
| 276 | Start section of tests that are not yet ready. |
| 277 | |
| 278 | To start a section of tests that are not ready and are expected to |
| 279 | fail, use this function and todo_end() in the following manner: |
| 280 | |
| 281 | @code |
| 282 | todo_start("Not ready yet"); |
| 283 | ok(is_rocketeering(duck), "Rocket-propelled ducks"); |
| 284 | ok(is_kamikaze(duck), "Kamikaze ducks"); |
| 285 | todo_end(); |
| 286 | @endcode |
| 287 | |
| 288 | @see todo_end |
| 289 | |
| 290 | @note |
| 291 | It is not possible to nest todo sections. |
| 292 | |
| 293 | @param message Message that will be printed before the todo tests. |
| 294 | */ |
| 295 | |
| 296 | void todo_start(char const *message, ...) MY_ATTRIBUTE((format(printf, 1, 2))); |
| 297 | |
| 298 | /** |
| 299 | End a section of tests that are not yet ready. |
| 300 | */ |
| 301 | |
| 302 | void todo_end(); |
| 303 | |
| 304 | /** @} */ |
| 305 | |
| 306 | #ifdef __cplusplus |
| 307 | } |
| 308 | #endif |
| 309 | |
| 310 | #endif /* TAP_H */ |