opt: list ccan/typesafe_cb as a dependency.
[ccan] / ccan / opt / test / run-helpers.c
index 8e1a751fc2da88b3a651f9e51f65747408a6b624..34ffbab834cb11923d2ba7719fd1091850860b5e 100644 (file)
@@ -3,6 +3,7 @@
 #include <ccan/tap/tap.h>
 #include <setjmp.h>
 #include <stdlib.h>
+#include <limits.h>
 #include "utils.h"
 
 /* We don't actually want it to exit... */
@@ -12,6 +13,12 @@ static jmp_buf exited;
 #define printf saved_printf
 static int saved_printf(const char *fmt, ...);
 
+#define fprintf saved_fprintf
+static int saved_fprintf(FILE *ignored, const char *fmt, ...);
+
+#define vfprintf(f, fmt, ap) saved_vprintf(fmt, ap)
+static int saved_vprintf(const char *fmt, va_list ap);
+
 #include <ccan/opt/helpers.c>
 #include <ccan/opt/opt.c>
 #include <ccan/opt/usage.c>
@@ -20,20 +27,15 @@ static void reset_options(void)
 {
        free(opt_table);
        opt_table = NULL;
-       opt_count = 0;
+       opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
 }
 
 static char *output = NULL;
 
-static int saved_printf(const char *fmt, ...)
+static int saved_vprintf(const char *fmt, va_list ap)
 {
-       va_list ap;
        char *p;
-       int ret;
-
-       va_start(ap, fmt);
-       ret = vasprintf(&p, fmt, ap);
-       va_end(ap);
+       int ret = vasprintf(&p, fmt, ap);
 
        if (output) {
                output = realloc(output, strlen(output) + strlen(p) + 1);
@@ -41,23 +43,44 @@ static int saved_printf(const char *fmt, ...)
                free(p);
        } else
                output = p;
+       return ret;
+}
+
+static int saved_printf(const char *fmt, ...)
+{
+       va_list ap;
+       int ret;
 
+       va_start(ap, fmt);
+       ret = saved_vprintf(fmt, ap);
+       va_end(ap);
+       return ret;
+}      
+
+static int saved_fprintf(FILE *ignored, const char *fmt, ...)
+{
+       va_list ap;
+       int ret;
+
+       va_start(ap, fmt);
+       ret = saved_vprintf(fmt, ap);
+       va_end(ap);
        return ret;
 }      
 
 /* Test helpers. */
 int main(int argc, char *argv[])
 {
-       plan_tests(88);
+       plan_tests(100);
 
        /* opt_set_bool */
        {
                bool arg = false;
                reset_options();
-               opt_register_noarg(NULL, 'a', opt_set_bool, &arg, NULL);
+               opt_register_noarg("-a", opt_set_bool, &arg, "");
                ok1(parse_args(&argc, &argv, "-a", NULL));
                ok1(arg);
-               opt_register_arg(NULL, 'b', opt_set_bool_arg, NULL, &arg, NULL);
+               opt_register_arg("-b", opt_set_bool_arg, NULL, &arg, "");
                ok1(parse_args(&argc, &argv, "-b", "no", NULL));
                ok1(!arg);
                ok1(parse_args(&argc, &argv, "-b", "yes", NULL));
@@ -66,16 +89,19 @@ int main(int argc, char *argv[])
                ok1(!arg);
                ok1(parse_args(&argc, &argv, "-b", "true", NULL));
                ok1(arg);
+               ok1(!parse_args(&argc, &argv, "-b", "unknown", NULL));
+               ok1(arg);
+               ok1(strstr(err_output, ": -b: Invalid argument 'unknown'"));
        }
        /* opt_set_invbool */
        {
                bool arg = true;
                reset_options();
-               opt_register_noarg(NULL, 'a', opt_set_invbool, &arg, NULL);
+               opt_register_noarg("-a", opt_set_invbool, &arg, "");
                ok1(parse_args(&argc, &argv, "-a", NULL));
                ok1(!arg);
-               opt_register_arg(NULL, 'b', opt_set_invbool_arg, NULL,
-                                &arg, NULL);
+               opt_register_arg("-b", opt_set_invbool_arg, NULL,
+                                &arg, "");
                ok1(parse_args(&argc, &argv, "-b", "no", NULL));
                ok1(arg);
                ok1(parse_args(&argc, &argv, "-b", "yes", NULL));
@@ -84,12 +110,15 @@ int main(int argc, char *argv[])
                ok1(arg);
                ok1(parse_args(&argc, &argv, "-b", "true", NULL));
                ok1(!arg);
+               ok1(!parse_args(&argc, &argv, "-b", "unknown", NULL));
+               ok1(!arg);
+               ok1(strstr(err_output, ": -b: Invalid argument 'unknown'"));
        }
        /* opt_set_charp */
        {
                char *arg = (char *)"wrong";
                reset_options();
-               opt_register_arg(NULL, 'a', opt_set_charp, NULL, &arg, NULL);
+               opt_register_arg("-a", opt_set_charp, NULL, &arg, "All");
                ok1(parse_args(&argc, &argv, "-a", "string", NULL));
                ok1(strcmp(arg, "string") == 0);
        }
@@ -97,7 +126,7 @@ int main(int argc, char *argv[])
        {
                int arg = 1000;
                reset_options();
-               opt_register_arg(NULL, 'a', opt_set_intval, NULL, &arg, NULL);
+               opt_register_arg("-a", opt_set_intval, NULL, &arg, "All");
                ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
                ok1(arg == 9999);
                ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
@@ -114,7 +143,7 @@ int main(int argc, char *argv[])
        {
                unsigned int arg = 1000;
                reset_options();
-               opt_register_arg(NULL, 'a', opt_set_uintval, NULL, &arg, NULL);
+               opt_register_arg("-a", opt_set_uintval, NULL, &arg, "All");
                ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
                ok1(arg == 9999);
                ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
@@ -122,12 +151,23 @@ int main(int argc, char *argv[])
                ok1(arg == 0);
                ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
                ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL));
+               if (ULONG_MAX == UINT_MAX) {
+                       pass("Can't test overflow");
+                       pass("Can't test error message");
+               } else {
+                       char buf[30];
+                       sprintf(buf, "%lu", ULONG_MAX);
+                       ok1(!parse_args(&argc, &argv, "-a", buf, NULL));
+                       ok1(strstr(err_output, ": -a: value '")
+                           && strstr(err_output, buf)
+                           && strstr(err_output, "' does not fit into an integer"));
+               }
        }
        /* opt_set_longval */
        {
                long int arg = 1000;
                reset_options();
-               opt_register_arg(NULL, 'a', opt_set_longval, NULL, &arg, NULL);
+               opt_register_arg("-a", opt_set_longval, NULL, &arg, "All");
                ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
                ok1(arg == 9999);
                ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
@@ -146,7 +186,7 @@ int main(int argc, char *argv[])
        {
                unsigned long int arg = 1000;
                reset_options();
-               opt_register_arg(NULL, 'a', opt_set_ulongval, NULL, &arg, NULL);
+               opt_register_arg("-a", opt_set_ulongval, NULL, &arg, "All");
                ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
                ok1(arg == 9999);
                ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
@@ -164,7 +204,7 @@ int main(int argc, char *argv[])
        {
                int arg = 1000;
                reset_options();
-               opt_register_noarg(NULL, 'a', opt_inc_intval, &arg, NULL);
+               opt_register_noarg("-a", opt_inc_intval, &arg, "");
                ok1(parse_args(&argc, &argv, "-a", NULL));
                ok1(arg == 1001);
                ok1(parse_args(&argc, &argv, "-a", "-a", NULL));
@@ -177,8 +217,8 @@ int main(int argc, char *argv[])
        {
                int exitval;
                reset_options();
-               opt_register_noarg(NULL, 'a',
-                                  opt_version_and_exit, "1.2.3", NULL);
+               opt_register_noarg("-a",
+                                  opt_version_and_exit, "1.2.3", "");
                exitval = setjmp(exited);
                if (exitval == 0) {
                        parse_args(&argc, &argv, "-a", NULL);
@@ -195,8 +235,8 @@ int main(int argc, char *argv[])
        {
                int exitval;
                reset_options();
-               opt_register_noarg(NULL, 'a',
-                                  opt_usage_and_exit, "[args]", NULL);
+               opt_register_noarg("-a",
+                                  opt_usage_and_exit, "[args]", "");
                exitval = setjmp(exited);
                if (exitval == 0) {
                        parse_args(&argc, &argv, "-a", NULL);
@@ -321,5 +361,47 @@ int main(int argc, char *argv[])
                ok1(buf[OPT_SHOW_LEN] == '!');
        }
 
+       /* opt_log_stderr. */
+       {
+               reset_options();
+               opt_register_noarg("-a",
+                                  opt_usage_and_exit, "[args]", "");
+
+               argc = 2;
+               argv = malloc(sizeof(argv[0]) * 3);
+               argv[0] = "thisprog";
+               argv[1] = "--garbage";
+               argv[2] = NULL;
+               ok1(!opt_parse(&argc, argv, opt_log_stderr));
+               ok1(!strcmp(output,
+                           "thisprog: --garbage: unrecognized option\n"));
+               free(output);
+               output = NULL;
+       }
+
+       /* opt_log_stderr_exit. */
+       {
+               int exitval;
+               reset_options();
+               opt_register_noarg("-a",
+                                  opt_usage_and_exit, "[args]", "");
+               exitval = setjmp(exited);
+               if (exitval == 0) {
+                       argc = 2;
+                       argv = malloc(sizeof(argv[0]) * 3);
+                       argv[0] = "thisprog";
+                       argv[1] = "--garbage";
+                       argv[2] = NULL;
+                       opt_parse(&argc, argv, opt_log_stderr_exit);
+                       fail("opt_log_stderr_exit returned?");
+               } else {
+                       ok1(exitval - 1 == 1);
+               }
+               ok1(!strcmp(output,
+                           "thisprog: --garbage: unrecognized option\n"));
+               free(output);
+               output = NULL;
+       }
+
        return exit_status();
 }