X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fopt%2Ftest%2Frun-checkopt.c;fp=ccan%2Fopt%2Ftest%2Frun-checkopt.c;h=9e2f88a7ae9fd40b55d10a748ec976b81db0cbb1;hb=9056c31b46452c92c7dd9c276664f514720a84c6;hp=0000000000000000000000000000000000000000;hpb=9071df66fc6a29cc8b6cd52c91134efe7b57f007;p=ccan diff --git a/ccan/opt/test/run-checkopt.c b/ccan/opt/test/run-checkopt.c new file mode 100644 index 00000000..9e2f88a7 --- /dev/null +++ b/ccan/opt/test/run-checkopt.c @@ -0,0 +1,144 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#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 +#include +#include +#include + +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(); +}