| 1 | /*- |
|---|
| 2 | * Copyright (c) 2012 Dag-Erling Smørgrav |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | * modification, are permitted provided that the following conditions |
|---|
| 7 | * are met: |
|---|
| 8 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 9 | * notice, this list of conditions and the following disclaimer |
|---|
| 10 | * in this position and unchanged. |
|---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 13 | * documentation and/or other materials provided with the distribution. |
|---|
| 14 | * |
|---|
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
|---|
| 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
|---|
| 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|---|
| 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|---|
| 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|---|
| 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|---|
| 25 | * SUCH DAMAGE. |
|---|
| 26 | * |
|---|
| 27 | * $Id$ |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #include <err.h> |
|---|
| 31 | #include <stdarg.h> |
|---|
| 32 | #include <stdio.h> |
|---|
| 33 | #include <stdlib.h> |
|---|
| 34 | #include <string.h> |
|---|
| 35 | #include <unistd.h> |
|---|
| 36 | |
|---|
| 37 | #include "t.h" |
|---|
| 38 | |
|---|
| 39 | const char *t_progname; |
|---|
| 40 | |
|---|
| 41 | static int verbose; |
|---|
| 42 | |
|---|
| 43 | void |
|---|
| 44 | t_verbose(const char *fmt, ...) |
|---|
| 45 | { |
|---|
| 46 | va_list ap; |
|---|
| 47 | |
|---|
| 48 | if (verbose) { |
|---|
| 49 | va_start(ap, fmt); |
|---|
| 50 | vfprintf(stderr, fmt, ap); |
|---|
| 51 | va_end(ap); |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | static void |
|---|
| 56 | usage(void) |
|---|
| 57 | { |
|---|
| 58 | |
|---|
| 59 | fprintf(stderr, "usage: [-v] %s\n", t_progname); |
|---|
| 60 | exit(1); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | int |
|---|
| 64 | main(int argc, char *argv[]) |
|---|
| 65 | { |
|---|
| 66 | const struct t_test **t_plan; |
|---|
| 67 | const char *desc; |
|---|
| 68 | int n, pass, fail; |
|---|
| 69 | int opt; |
|---|
| 70 | |
|---|
| 71 | if ((t_progname = strrchr(argv[0], '/')) != NULL) |
|---|
| 72 | t_progname++; /* one past the slash */ |
|---|
| 73 | else |
|---|
| 74 | t_progname = argv[0]; |
|---|
| 75 | |
|---|
| 76 | while ((opt = getopt(argc, argv, "v")) != -1) |
|---|
| 77 | switch (opt) { |
|---|
| 78 | case 'v': |
|---|
| 79 | verbose = 1; |
|---|
| 80 | break; |
|---|
| 81 | default: |
|---|
| 82 | usage(); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | argc -= optind; |
|---|
| 86 | argv += optind; |
|---|
| 87 | |
|---|
| 88 | /* prepare the test plan */ |
|---|
| 89 | if ((t_plan = t_prepare(argc, argv)) == NULL) |
|---|
| 90 | errx(1, "no plan\n"); |
|---|
| 91 | |
|---|
| 92 | /* count the tests */ |
|---|
| 93 | for (n = 0; t_plan[n] != NULL; ++n) |
|---|
| 94 | /* nothing */; |
|---|
| 95 | printf("1..%d\n", n); |
|---|
| 96 | |
|---|
| 97 | /* run the tests */ |
|---|
| 98 | for (n = pass = fail = 0; t_plan[n] != NULL; ++n) { |
|---|
| 99 | desc = t_plan[n]->desc ? t_plan[n]->desc : "no description"; |
|---|
| 100 | if ((*t_plan[n]->func)()) { |
|---|
| 101 | printf("ok %d - %s\n", n + 1, desc); |
|---|
| 102 | ++pass; |
|---|
| 103 | } else { |
|---|
| 104 | printf("not ok %d - %s\n", n + 1, desc); |
|---|
| 105 | ++fail; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /* clean up and exit */ |
|---|
| 110 | t_cleanup(); |
|---|
| 111 | exit(fail > 0 ? 1 : 0); |
|---|
| 112 | } |
|---|