1 #include <ccan/tap/tap.h>
2 #include <ccan/opt/opt.c>
3 #include <ccan/opt/usage.c>
4 #include <ccan/opt/helpers.c>
5 #include <ccan/opt/parse.c>
7 static bool show_10(char *buf, size_t len, const void *arg UNNEEDED)
14 static bool show_10_false(char *buf, size_t len, const void *arg UNNEEDED)
21 static bool show_max(char *buf, size_t len, const void *arg UNNEEDED)
23 memset(buf, 'X', OPT_SHOW_LEN);
27 /* Test add_desc helper. */
38 opt.desc = "0123456789 0";
43 ret = add_desc(NULL, &len, &max, 10, 30, &opt);
46 ok1(strcmp(ret, "01234 0123456789 0\n") == 0);
47 free(ret); len = max = 0;
50 ret = add_desc(NULL, &len, &max, 7, 30, &opt);
53 ok1(strcmp(ret, "01234 0123456789 0\n") == 0);
54 free(ret); len = max = 0;
56 /* Name doesn't fit. */
57 ret = add_desc(NULL, &len, &max, 6, 30, &opt);
62 " 0123456789 0\n") == 0);
63 free(ret); len = max = 0;
65 /* Description just fits. */
66 ret = add_desc(NULL, &len, &max, 7, 19, &opt);
69 ok1(strcmp(ret, "01234 0123456789 0\n") == 0);
70 free(ret); len = max = 0;
72 /* Description doesn't quite fit. */
73 ret = add_desc(NULL, &len, &max, 7, 18, &opt);
79 free(ret); len = max = 0;
81 /* Neither quite fits. */
82 ret = add_desc(NULL, &len, &max, 6, 17, &opt);
89 free(ret); len = max = 0;
91 /* With show function, fits just. */
93 ret = add_desc(NULL, &len, &max, 7, 41, &opt);
96 ok1(strcmp(ret, "01234 0123456789 0 (default: XXXXXXXXXX)\n") == 0);
97 free(ret); len = max = 0;
99 /* With show function, just too long. */
100 ret = add_desc(NULL, &len, &max, 7, 40, &opt);
104 "01234 0123456789 0\n"
105 " (default: XXXXXXXXXX)\n") == 0);
106 free(ret); len = max = 0;
108 /* With maximal show function, fits just (we assume OPT_SHOW_LEN = 80. */
110 ret = add_desc(NULL, &len, &max, 7, 114, &opt);
113 ok1(strcmp(ret, "01234 0123456789 0 (default: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...)\n") == 0);
114 free(ret); len = max = 0;
116 /* With maximal show function, just too long. */
117 ret = add_desc(NULL, &len, &max, 7, 113, &opt);
121 "01234 0123456789 0\n"
122 " (default: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...)\n") == 0);
123 free(ret); len = max = 0;
125 /* With show function which fails doesn't print. */
126 opt.show = show_10_false;
127 ret = add_desc(NULL, &len, &max, 7, 41, &opt);
130 ok1(strcmp(ret, "01234 0123456789 0\n") == 0);
131 free(ret); len = max = 0;
133 /* With added " <arg>". Fits, just. */
135 opt.type = OPT_HASARG;
136 ret = add_desc(NULL, &len, &max, 13, 25, &opt);
139 ok1(strcmp(ret, "01234 <arg> 0123456789 0\n") == 0);
140 free(ret); len = max = 0;
142 /* With added " <arg>". Name doesn't quite fit. */
143 ret = add_desc(NULL, &len, &max, 12, 25, &opt);
148 " 0123456789 0\n") == 0);
149 free(ret); len = max = 0;
151 /* With added " <arg>". Desc doesn't quite fit. */
152 ret = add_desc(NULL, &len, &max, 13, 24, &opt);
156 "01234 <arg> 0123456789\n"
158 free(ret); len = max = 0;
160 /* Empty description, with <arg> and default. Just fits. */
163 ret = add_desc(NULL, &len, &max, 13, 35, &opt);
166 ok1(strcmp(ret, "01234 <arg> (default: XXXXXXXXXX)\n") == 0);
167 free(ret); len = max = 0;
169 /* Empty description, with <arg> and default. Doesn't quite fit. */
172 ret = add_desc(NULL, &len, &max, 13, 34, &opt);
177 " (default: XXXXXXXXXX)\n") == 0);
178 free(ret); len = max = 0;
180 return exit_status();