1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
#include "units.h"
#include <getopt.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <unistd.h>
//-----------------------------------------------------------------
#define MAX_COMPONENTS 16
struct token {
const char *b, *e;
};
static bool _pop_component(const char *path, struct token *result)
{
const char *b, *e;
while (*path && *path == '/')
path++;
b = path;
while (*path && (*path != '/'))
path++;
e = path;
if (b == e)
return false;
result->b = b;
result->e = e;
return true;
}
static unsigned _split_components(const char *path, struct token *toks, unsigned len)
{
unsigned count = 0;
struct token tok;
tok.e = path;
while (len && _pop_component(tok.e, &tok)) {
*toks = tok;
toks++;
count++;
len--;
}
return count;
}
static void _indent(FILE *stream, unsigned count)
{
unsigned i;
for (i = 0; i < count; i++)
fprintf(stream, " ");
}
static void _print_token(FILE *stream, struct token *t)
{
const char *ptr;
for (ptr = t->b; ptr != t->e; ptr++)
fprintf(stream, "%c", *ptr);
}
static int _char_cmp(char l, char r)
{
if (l < r)
return -1;
else if (r < l)
return 1;
else
return 0;
}
static int _tok_cmp(struct token *lhs, struct token *rhs)
{
const char *l = lhs->b, *le = lhs->e;
const char *r = rhs->b, *re = rhs->e;
while ((l != le) && (r != re) && (*l == *r)) {
l++;
r++;
}
if ((l != le) && (r != re))
return _char_cmp(*l, *r);
else if (r != re)
return -1;
else if (l != le)
return 1;
else
return 0;
}
static void _print_path_delta(FILE *stream,
struct token *old, unsigned old_len,
struct token *new, unsigned new_len,
const char *desc)
{
unsigned i, common_prefix = 0, len, d;
unsigned max_prefix = old_len < new_len ? old_len : new_len;
for (i = 0; i < max_prefix; i++) {
if (_tok_cmp(old + i, new + i))
break;
else
common_prefix++;
}
for (; i < new_len; i++) {
_indent(stream, common_prefix);
_print_token(stream, new + i);
common_prefix++;
if (i < new_len - 1)
fprintf(stream, "\n");
}
len = (new_len > 0) ? common_prefix * 2 + (new[new_len - 1].e - new[new_len - 1].b) : 0;
fprintf(stream, " ");
for (d = len; d < 60; d++)
fprintf(stream, ".");
fprintf(stream, " ");
fprintf(stream, "%s", desc);
fprintf(stream, "\n");
}
typedef struct token comp_t[MAX_COMPONENTS];
static void _list_tests(struct test_details **tests, unsigned nr)
{
unsigned i, current = 0, current_len, last_len = 0;
comp_t components[2];
for (i = 0; i < nr; i++) {
struct test_details *t = tests[i];
current_len = _split_components(t->path, components[current], MAX_COMPONENTS);
_print_path_delta(stderr, components[!current], last_len,
components[current], current_len, t->desc);
last_len = current_len;
current = !current;
}
}
static void _destroy_tests(struct dm_list *suites)
{
struct test_suite *ts, *tmp;
dm_list_iterate_items_safe (ts, tmp, suites)
test_suite_destroy(ts);
}
static const char *red(bool c)
{
return c ? "\x1B[31m" : "";
}
static const char *green(bool c)
{
return c ? "\x1B[32m" : "";
}
static const char *normal(bool c)
{
return c ? "\x1B[0m" : "";
}
static void _run_test(struct test_details *t, bool use_colour, unsigned *passed, unsigned *total)
{
void *fixture;
struct test_suite *ts = t->parent;
fprintf(stderr, "[RUN ] %s\n", t->path);
(*total)++;
if (setjmp(test_k))
fprintf(stderr, "%s[ FAIL]%s %s\n", red(use_colour), normal(use_colour), t->path);
else {
if (ts->fixture_init && ts->fixture_exit)
fixture = ts->fixture_init();
else
fixture = NULL;
t->fn(fixture);
if (ts->fixture_exit)
ts->fixture_exit(fixture);
(*passed)++;
fprintf(stderr, "%s[ OK]%s\n", green(use_colour), normal(use_colour));
}
}
static bool _run_tests(struct test_details **tests, unsigned nr)
{
bool use_colour = isatty(fileno(stderr));
unsigned i, passed = 0, total = 0;
for (i = 0; i < nr; i++)
_run_test(tests[i], use_colour, &passed, &total);
fprintf(stderr, "\n%u/%u tests passed\n", passed, total);
return passed == total;
}
static void _usage(void)
{
fprintf(stderr, "Usage: unit-test <list|run> [pattern]\n");
}
static int _cmp_paths(const void *lhs, const void *rhs)
{
struct test_details *l = *((struct test_details **) lhs);
struct test_details *r = *((struct test_details **) rhs);
return strcmp(l->path, r->path);
}
static unsigned _filter(const char *pattern, struct test_details **tests, unsigned nr)
{
unsigned i, found = 0;
regex_t rx;
if (regcomp(&rx, pattern, 0)) {
fprintf(stderr, "couldn't compile regex '%s'\n", pattern);
exit(1);
}
for (i = 0; i < nr; i++)
if (tests[i] && !regexec(&rx, tests[i]->path, 0, NULL, 0))
tests[found++] = tests[i];
regfree(&rx);
return found;
}
int main(int argc, char **argv)
{
int r;
unsigned i, nr_tests;
struct test_suite *ts;
struct test_details *t, **t_array;
struct dm_list suites;
dm_list_init(&suites);
register_all_tests(&suites);
// count all tests
nr_tests = 0;
dm_list_iterate_items (ts, &suites)
dm_list_iterate_items (t, &ts->tests)
nr_tests++;
// stick them in an array
t_array = malloc(sizeof(*t_array) * nr_tests);
if (!t_array) {
fprintf(stderr, "out of memory\n");
exit(1);
}
memset(t_array, 0, sizeof(*t_array) * nr_tests);
i = 0;
dm_list_iterate_items (ts, &suites)
dm_list_iterate_items (t, &ts->tests)
t_array[i++] = t;
// filter
if (argc == 3)
nr_tests = _filter(argv[2], t_array, nr_tests);
// sort
qsort(t_array, nr_tests, sizeof(*t_array), _cmp_paths);
// run or list them
if (argc == 1)
r = !_run_tests(t_array, nr_tests);
else {
const char *cmd = argv[1];
if (!strcmp(cmd, "run"))
r = !_run_tests(t_array, nr_tests);
else if (!strcmp(cmd, "list")) {
_list_tests(t_array, nr_tests);
r = 0;
} else {
_usage();
r = 1;
}
}
free(t_array);
_destroy_tests(&suites);
return r;
}
//-----------------------------------------------------------------
|