]> git.ozlabs.org Git - ccan/blobdiff - ccan/opt/test/run-helpers.c
various: add LICENSE comments.
[ccan] / ccan / opt / test / run-helpers.c
index 91e66374bbaf42e7e688521165cc93c343426ed5..a58e4d91797d2c56befa958b40fb08a14fac3594 100644 (file)
@@ -1,4 +1,4 @@
-#define _GNU_SOURCE
+#include "config.h"
 #include <stdio.h>
 #include <ccan/tap/tap.h>
 #include <setjmp.h>
@@ -13,9 +13,19 @@ 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);
+
+#define malloc(size) saved_malloc(size)
+static void *saved_malloc(size_t size);
+
 #include <ccan/opt/helpers.c>
 #include <ccan/opt/opt.c>
 #include <ccan/opt/usage.c>
+#include <ccan/opt/parse.c>
 
 static void reset_options(void)
 {
@@ -26,15 +36,10 @@ static void reset_options(void)
 
 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);
@@ -42,14 +47,42 @@ 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;
 }      
 
+#undef malloc
+static void *last_allocation;
+static void *saved_malloc(size_t size)
+{
+       return last_allocation = malloc(size);
+}
+
 /* Test helpers. */
 int main(int argc, char *argv[])
 {
-       plan_tests(96);
+       plan_tests(100);
 
        /* opt_set_bool */
        {
@@ -197,15 +230,25 @@ int main(int argc, char *argv[])
                reset_options();
                opt_register_noarg("-a",
                                   opt_version_and_exit, "1.2.3", "");
+               /* parse_args allocates argv */
+               free(argv);
+
+               argc = 2;
+               argv = malloc(sizeof(argv[0]) * 3);
+               argv[0] = "thisprog";
+               argv[1] = "-a";
+               argv[2] = NULL;
+
                exitval = setjmp(exited);
                if (exitval == 0) {
-                       parse_args(&argc, &argv, "-a", NULL);
+                       opt_parse(&argc, argv, save_err_output);
                        fail("opt_show_version_and_exit returned?");
                } else {
                        ok1(exitval - 1 == 0);
                }
                ok1(strcmp(output, "1.2.3\n") == 0);
                free(output);
+               free(argv);
                output = NULL;
        }
 
@@ -215,9 +258,16 @@ int main(int argc, char *argv[])
                reset_options();
                opt_register_noarg("-a",
                                   opt_usage_and_exit, "[args]", "");
+
+               argc = 2;
+               argv = malloc(sizeof(argv[0]) * 3);
+               argv[0] = "thisprog";
+               argv[1] = "-a";
+               argv[2] = NULL;
+
                exitval = setjmp(exited);
                if (exitval == 0) {
-                       parse_args(&argc, &argv, "-a", NULL);
+                       opt_parse(&argc, argv, save_err_output);
                        fail("opt_usage_and_exit returned?");
                } else {
                        ok1(exitval - 1 == 0);
@@ -226,6 +276,9 @@ int main(int argc, char *argv[])
                ok1(strstr(output, argv[0]));
                ok1(strstr(output, "[-a]"));
                free(output);
+               free(argv);
+               /* It exits without freeing usage string. */
+               free(last_allocation);
                output = NULL;
        }
 
@@ -339,5 +392,49 @@ 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);
+               free(argv);
+               output = NULL;
+       }
+
+       /* opt_log_stderr_exit. */
+       {
+               int exitval;
+               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;
+               exitval = setjmp(exited);
+               if (exitval == 0) {
+                       opt_parse(&argc, argv, opt_log_stderr_exit);
+                       fail("opt_log_stderr_exit returned?");
+               } else {
+                       ok1(exitval - 1 == 1);
+               }
+               free(argv);
+               ok1(!strcmp(output,
+                           "thisprog: --garbage: unrecognized option\n"));
+               free(output);
+               output = NULL;
+       }
+
        return exit_status();
 }