|
| 1 | +#include "test.h" |
| 2 | + |
| 3 | +/* |
| 4 | + * Test module, based on TAP 14 specification [1]. |
| 5 | + * [1]: https://testanything.org/tap-version-14-specification.html |
| 6 | + */ |
| 7 | + |
| 8 | +/* Need for `PATH_MAX` in diagnostic definition. */ |
| 9 | +#include <limits.h> |
| 10 | +#include <setjmp.h> |
| 11 | +#include <stdarg.h> |
| 12 | +/* Need for `strchr()` in diagnostic parsing. */ |
| 13 | +#include <string.h> |
| 14 | + |
| 15 | +/* |
| 16 | + * Test level: 0 for the parent test, >0 for any subtests. |
| 17 | + */ |
| 18 | +static int level = -1; |
| 19 | + |
| 20 | +/* |
| 21 | + * The last diagnostic data to be used in the YAML Diagnostic |
| 22 | + * block. |
| 23 | + * |
| 24 | + * Contains filename, line number and failed expression or assert |
| 25 | + * name and "got" and "expected" fields. All entries are separated |
| 26 | + * by \n. |
| 27 | + * The longest field is filename here, so PATH_MAX * 3 as |
| 28 | + * the diagnostic string length should be enough. |
| 29 | + * |
| 30 | + * The first \0 means the end of diagnostic data. |
| 31 | + * |
| 32 | + * As far as `strchr()` searches until \0, all previous entries |
| 33 | + * are suppressed by the last one. If the first byte is \0 -- |
| 34 | + * diagnostic is empty. |
| 35 | + */ |
| 36 | +#define TEST_DIAG_DATA_MAX (PATH_MAX * 3) |
| 37 | +char test_diag_buf[TEST_DIAG_DATA_MAX] = {0}; |
| 38 | + |
| 39 | +const char *skip_reason = NULL; |
| 40 | +const char *todo_reason = NULL; |
| 41 | + |
| 42 | +/* Indent for the TAP. 4 spaces is default for subtest. */ |
| 43 | +static void indent(void) |
| 44 | +{ |
| 45 | + int i; |
| 46 | + for (i = 0; i < level; i++) |
| 47 | + printf(" "); |
| 48 | +} |
| 49 | + |
| 50 | +void test_message(const char *fmt, ...) |
| 51 | +{ |
| 52 | + va_list ap; |
| 53 | + indent(); |
| 54 | + va_start(ap, fmt); |
| 55 | + vprintf(fmt, ap); |
| 56 | + printf("\n"); |
| 57 | + va_end(ap); |
| 58 | +} |
| 59 | + |
| 60 | +static void test_print_tap_version(void) |
| 61 | +{ |
| 62 | + /* |
| 63 | + * Since several TAP13 parsers in popular usage treat |
| 64 | + * a repeated Version declaration as an error, even if the |
| 65 | + * Version is indented, Subtests _should not_ include a |
| 66 | + * Version, if TAP13 Harness compatibility is |
| 67 | + * desirable [1]. |
| 68 | + */ |
| 69 | + if (level == 0) |
| 70 | + test_message("TAP version %d", TAP_VERSION); |
| 71 | +} |
| 72 | + |
| 73 | +static void test_start_comment(const char *t_name) |
| 74 | +{ |
| 75 | + if (level > -1) |
| 76 | + /* |
| 77 | + * Inform about starting subtest, easier for |
| 78 | + * humans to read. |
| 79 | + * Subtest with a name must be terminated by a |
| 80 | + * Test Point with a matching Description [1]. |
| 81 | + */ |
| 82 | + test_comment("Subtest: %s", t_name); |
| 83 | +} |
| 84 | + |
| 85 | +void _test_print_skip_all(const char *group_name, const char *reason) |
| 86 | +{ |
| 87 | + test_start_comment(group_name); |
| 88 | + /* |
| 89 | + * XXX: This test isn't started yet, so set indent level |
| 90 | + * manually. |
| 91 | + */ |
| 92 | + level++; |
| 93 | + test_print_tap_version(); |
| 94 | + /* |
| 95 | + * XXX: `SKIP_DIRECTIVE` is not necessary here according |
| 96 | + * to the TAP14 specification [1], but some harnesses may |
| 97 | + * fail to parse the output without it. |
| 98 | + */ |
| 99 | + test_message("1..0" SKIP_DIRECTIVE "%s", reason); |
| 100 | + level--; |
| 101 | +} |
| 102 | + |
| 103 | +/* Just inform TAP parser how many tests we want to run. */ |
| 104 | +static void test_plan(size_t planned) |
| 105 | +{ |
| 106 | + test_message("1..%lu", planned); |
| 107 | +} |
| 108 | + |
| 109 | +/* Human-readable output how many tests/subtests are failed. */ |
| 110 | +static void test_finish(size_t planned, size_t failed) |
| 111 | +{ |
| 112 | + const char *t_type = level == 0 ? "tests" : "subtests"; |
| 113 | + if (failed > 0) |
| 114 | + test_comment("Failed %lu %s out of %lu", |
| 115 | + failed, t_type, planned); |
| 116 | + fflush(stdout); |
| 117 | +} |
| 118 | + |
| 119 | +void test_set_skip_reason(const char *reason) |
| 120 | +{ |
| 121 | + skip_reason = reason; |
| 122 | +} |
| 123 | + |
| 124 | +void test_set_todo_reason(const char *reason) |
| 125 | +{ |
| 126 | + todo_reason = reason; |
| 127 | +} |
| 128 | + |
| 129 | +void test_save_diag_data(const char *fmt, ...) |
| 130 | +{ |
| 131 | + va_list ap; |
| 132 | + va_start(ap, fmt); |
| 133 | + vsnprintf(test_diag_buf, TEST_DIAG_DATA_MAX, fmt, ap); |
| 134 | + va_end(ap); |
| 135 | +} |
| 136 | + |
| 137 | +static void test_clear_diag_data(void) |
| 138 | +{ |
| 139 | + /* |
| 140 | + * Limit buffer with zero byte to show that there is no |
| 141 | + * any entry. |
| 142 | + */ |
| 143 | + test_diag_buf[0] = '\0'; |
| 144 | +} |
| 145 | + |
| 146 | +static int test_diagnostic_is_set(void) |
| 147 | +{ |
| 148 | + return test_diag_buf[0] != '\0'; |
| 149 | +} |
| 150 | + |
| 151 | +/* |
| 152 | + * Parse the last diagnostic data entry and print it in YAML |
| 153 | + * format with the corresponding additional half-indent in TAP |
| 154 | + * (2 spaces). |
| 155 | + * Clear diagnostic message to be sure that it's printed once. |
| 156 | + * XXX: \n separators are changed to \0 during parsing and |
| 157 | + * printing output for convenience in usage. |
| 158 | + */ |
| 159 | +static void test_diagnostic(void) |
| 160 | +{ |
| 161 | + test_message(" ---"); |
| 162 | + char *ent = test_diag_buf; |
| 163 | + char *ent_end = NULL; |
| 164 | + while ((ent_end = strchr(ent, '\n')) != NULL) { |
| 165 | + char *next_ent = ent_end + 1; |
| 166 | + /* |
| 167 | + * Limit string with with the zero byte for |
| 168 | + * formatted output. Anyway, don't need this \n |
| 169 | + * anymore. |
| 170 | + */ |
| 171 | + *ent_end = '\0'; |
| 172 | + test_message(" %s", ent); |
| 173 | + ent = next_ent; |
| 174 | + } |
| 175 | + test_message(" ..."); |
| 176 | + test_clear_diag_data(); |
| 177 | +} |
| 178 | + |
| 179 | +static jmp_buf test_run_env; |
| 180 | + |
| 181 | +TEST_NORET void _test_exit(int status) |
| 182 | +{ |
| 183 | + longjmp(test_run_env, status); |
| 184 | +} |
| 185 | + |
| 186 | +static int test_run(const struct test_unit *test, size_t test_number, |
| 187 | + void *test_state) |
| 188 | +{ |
| 189 | + int status = TEST_EXIT_SUCCESS; |
| 190 | + /* |
| 191 | + * Run unit test. Diagnostic in case of failure setup by |
| 192 | + * helpers assert macros defined in the header. |
| 193 | + */ |
| 194 | + int jmp_status; |
| 195 | + if ((jmp_status = setjmp(test_run_env)) == 0) { |
| 196 | + if (test->f(test_state) != TEST_EXIT_SUCCESS) |
| 197 | + status = TEST_EXIT_FAILURE; |
| 198 | + } else { |
| 199 | + status = jmp_status - TEST_JMP_STATUS_SHIFT; |
| 200 | + } |
| 201 | + const char *result = status == TEST_EXIT_SUCCESS ? "ok" : "not ok"; |
| 202 | + |
| 203 | + /* |
| 204 | + * Format suffix of the test message for SKIP or TODO |
| 205 | + * directives. |
| 206 | + */ |
| 207 | +#define SUFFIX_SZ 1024 |
| 208 | + char suffix[SUFFIX_SZ] = {0}; |
| 209 | + if (skip_reason) { |
| 210 | + snprintf(suffix, SUFFIX_SZ, SKIP_DIRECTIVE "%s", skip_reason); |
| 211 | + skip_reason = NULL; |
| 212 | + } else if (todo_reason) { |
| 213 | + /* Prevent count this test as failed. */ |
| 214 | + status = TEST_EXIT_SUCCESS; |
| 215 | + snprintf(suffix, SUFFIX_SZ, TODO_DIRECTIVE "%s", todo_reason); |
| 216 | + todo_reason = NULL; |
| 217 | + } |
| 218 | +#undef SUFFIX_SZ |
| 219 | + |
| 220 | + test_message("%s %lu - %s%s", result, test_number, test->name, |
| 221 | + suffix); |
| 222 | + |
| 223 | + if (status && test_diagnostic_is_set()) |
| 224 | + test_diagnostic(); |
| 225 | + return status; |
| 226 | +} |
| 227 | + |
| 228 | +int _test_run_group(const char *group_name, const struct test_unit tests[], |
| 229 | + size_t n_tests, void *test_state) |
| 230 | +{ |
| 231 | + test_start_comment(group_name); |
| 232 | + |
| 233 | + level++; |
| 234 | + test_print_tap_version(); |
| 235 | + |
| 236 | + test_plan(n_tests); |
| 237 | + |
| 238 | + size_t n_failed = 0; |
| 239 | + |
| 240 | + size_t i; |
| 241 | + for (i = 0; i < n_tests; i++) { |
| 242 | + size_t test_number = i + 1; |
| 243 | + /* Return 1 on failure, 0 on success. */ |
| 244 | + n_failed += test_run(&tests[i], test_number, test_state); |
| 245 | + } |
| 246 | + |
| 247 | + test_finish(n_tests, n_failed); |
| 248 | + |
| 249 | + level--; |
| 250 | + return n_failed > 0 ? TEST_EXIT_FAILURE : TEST_EXIT_SUCCESS; |
| 251 | +} |
0 commit comments