]> git.ozlabs.org Git - ccan/blob - ccan/opt/test/run-usage.c
opt: wean off getopt_long, beef up tests.
[ccan] / ccan / opt / test / run-usage.c
1 #define _GNU_SOURCE
2 #include <ccan/tap/tap.h>
3 #include <stdarg.h>
4 #include <setjmp.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include "utils.h"
8 #include <ccan/opt/opt.c>
9 #include <ccan/opt/usage.c>
10 #include <ccan/opt/helpers.c>
11 #include <ccan/opt/parse.c>
12
13 static char *my_cb(void *p)
14 {
15         return NULL;
16 }
17
18 static void reset_options(void)
19 {
20         free(opt_table);
21         opt_table = NULL;
22         opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
23 }
24
25 /* Test helpers. */
26 int main(int argc, char *argv[])
27 {
28         char *output;
29
30         plan_tests(42);
31         opt_register_table(subtables, NULL);
32         opt_register_noarg("--kkk|-k", my_cb, NULL, "magic kkk option");
33         opt_register_noarg("-?", opt_usage_and_exit, "<MyArgs>...",
34                            "This message");
35         output = opt_usage("my name", "ExTrA Args");
36         diag("%s", output);
37         ok1(strstr(output, "Usage: my name"));
38         ok1(strstr(output, "--jjj|-j|--lll|-l <arg>"));
39         ok1(strstr(output, "ExTrA Args"));
40         ok1(strstr(output, "-a "));
41         ok1(strstr(output, " Description of a\n"));
42         ok1(strstr(output, "-b <arg>"));
43         ok1(strstr(output, " Description of b (default: b)\n"));
44         ok1(strstr(output, "--ddd "));
45         ok1(strstr(output, " Description of ddd\n"));
46         ok1(strstr(output, "--eee <filename> "));
47         ok1(strstr(output, " (default: eee)\n"));
48         ok1(strstr(output, "long table options:\n"));
49         ok1(strstr(output, "--ggg|-g "));
50         ok1(strstr(output, " Description of ggg\n"));
51         ok1(strstr(output, "-h|--hhh <arg>"));
52         ok1(strstr(output, " Description of hhh\n"));
53         ok1(strstr(output, "--kkk|-k"));
54         ok1(strstr(output, "magic kkk option"));
55         /* This entry is hidden. */
56         ok1(!strstr(output, "--mmm|-m"));
57         free(output);
58
59         /* NULL should use string from registered options. */
60         output = opt_usage("my name", NULL);
61         diag("%s", output);
62         ok1(strstr(output, "Usage: my name"));
63         ok1(strstr(output, "--jjj|-j|--lll|-l <arg>"));
64         ok1(strstr(output, "<MyArgs>..."));
65         ok1(strstr(output, "-a "));
66         ok1(strstr(output, " Description of a\n"));
67         ok1(strstr(output, "-b <arg>"));
68         ok1(strstr(output, " Description of b (default: b)\n"));
69         ok1(strstr(output, "--ddd "));
70         ok1(strstr(output, " Description of ddd\n"));
71         ok1(strstr(output, "--eee <filename> "));
72         ok1(strstr(output, " (default: eee)\n"));
73         ok1(strstr(output, "long table options:\n"));
74         ok1(strstr(output, "--ggg|-g "));
75         ok1(strstr(output, " Description of ggg\n"));
76         ok1(strstr(output, "-h|--hhh <arg>"));
77         ok1(strstr(output, " Description of hhh\n"));
78         ok1(strstr(output, "--kkk|-k"));
79         ok1(strstr(output, "magic kkk option"));
80         /* This entry is hidden. */
81         ok1(!strstr(output, "--mmm|-m"));
82         free(output);
83
84         reset_options();
85         /* Empty table test. */
86         output = opt_usage("nothing", NULL);
87         ok1(strstr(output, "Usage: nothing \n"));
88         free(output);
89
90         /* No short args. */
91         opt_register_noarg("--aaa", test_noarg, NULL, "AAAAll");
92         output = opt_usage("onearg", NULL);
93         ok1(strstr(output, "Usage: onearg \n"));
94         ok1(strstr(output, "--aaa"));
95         ok1(strstr(output, "AAAAll"));
96         free(output);
97
98         return exit_status();
99 }