Tag
Hash :
a0d14d15
Author :
Date :
2011-07-06T16:47:25
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
#include "config.h"
#include <stdio.h>
#include <ccan/tap/tap.h>
#include <setjmp.h>
#include <stdlib.h>
#include <limits.h>
#include <err.h>
#include "utils.h"
/* We don't actually want it to exit... */
static jmp_buf exited;
#define errx save_and_jump
static void save_and_jump(int ecode, const char *fmt, ...);
#include <ccan/opt/helpers.c>
#include <ccan/opt/opt.c>
#include <ccan/opt/usage.c>
#include <ccan/opt/parse.c>
static char *output = NULL;
static int saved_vprintf(const char *fmt, va_list ap)
{
char *p;
int ret = vasprintf(&p, fmt, ap);
if (output) {
output = realloc(output, strlen(output) + strlen(p) + 1);
strcat(output, p);
free(p);
} else
output = p;
return ret;
}
static void save_and_jump(int ecode, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
saved_vprintf(fmt, ap);
va_end(ap);
longjmp(exited, ecode + 1);
}
static void reset(void)
{
free(output);
output = NULL;
free(opt_table);
opt_table = NULL;
opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
}
int main(int argc, char *argv[])
{
int exitval;
plan_tests(14);
exitval = setjmp(exited);
if (exitval == 0) {
/* Bad type. */
_opt_register("-a", OPT_SUBTABLE, (void *)opt_version_and_exit,
NULL, NULL, "1.2.3", "");
fail("_opt_register returned?");
} else {
ok1(exitval - 1 == 1);
ok1(strstr(output, "Option -a: unknown entry type"));
}
reset();
exitval = setjmp(exited);
if (exitval == 0) {
/* NULL description. */
opt_register_noarg("-a", test_noarg, "", NULL);
fail("_opt_register returned?");
} else {
ok1(exitval - 1 == 1);
ok1(strstr(output, "Option -a: description cannot be NULL"));
}
reset();
exitval = setjmp(exited);
if (exitval == 0) {
/* Bad option name. */
opt_register_noarg("a", test_noarg, "", "");
fail("_opt_register returned?");
} else {
ok1(exitval - 1 == 1);
ok1(strstr(output, "Option a: does not begin with '-'"));
}
reset();
exitval = setjmp(exited);
if (exitval == 0) {
/* Bad option name. */
opt_register_noarg("--", test_noarg, "", "");
fail("_opt_register returned?");
} else {
ok1(exitval - 1 == 1);
ok1(strstr(output, "Option --: invalid long option '--'"));
}
reset();
exitval = setjmp(exited);
if (exitval == 0) {
/* Bad option name. */
opt_register_noarg("--a|-aaa", test_noarg, "", "");
fail("_opt_register returned?");
} else {
ok1(exitval - 1 == 1);
ok1(strstr(output,
"Option --a|-aaa: invalid short option '-aaa'"));
}
reset();
exitval = setjmp(exited);
if (exitval == 0) {
/* Documentation for non-optios. */
opt_register_noarg("--a foo", test_noarg, "", "");
fail("_opt_register returned?");
} else {
ok1(exitval - 1 == 1);
ok1(strstr(output,
"Option --a foo: does not take arguments 'foo'"));
}
reset();
exitval = setjmp(exited);
if (exitval == 0) {
/* Documentation for non-optios. */
opt_register_noarg("--a=foo", test_noarg, "", "");
fail("_opt_register returned?");
} else {
ok1(exitval - 1 == 1);
ok1(strstr(output,
"Option --a=foo: does not take arguments 'foo'"));
}
return exit_status();
}