truncate: New module.
[gnulib/ericb.git] / tests / test-argp.c
blobfe9c4152396b7f271fed743a08af5096eecf5990
1 /* Test suite for argp.
2 Copyright (C) 2006-2007, 2009-2017 Free Software Foundation, Inc.
3 This file is part of the GNUlib Library.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include "argp.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #if HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
29 struct test_args
31 int test;
32 int verbose;
33 char *file;
34 int read;
35 char *hidden;
36 int opt;
37 char *optional;
38 int optional_set;
39 int group_2_1_option;
40 int group_1_1_option;
43 static struct argp_option group1_option[] = {
44 { NULL, 0, NULL, 0, "Option Group 1", 0 },
45 { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 },
46 { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 },
47 { "input", 0, NULL, OPTION_ALIAS, NULL, 1 },
48 { "read", 'r', NULL, OPTION_ALIAS, NULL, 1 },
49 { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 },
50 { NULL, 0, NULL, 0, NULL, 0 }
53 static error_t
54 group1_parser (int key, char *arg, struct argp_state *state)
56 struct test_args *args = state->input;
58 switch (key)
60 case 'v':
61 args->verbose++;
62 break;
64 case 'r':
65 args->read = 1;
66 /* fall through */
67 case 'f':
68 args->file = arg;
69 break;
71 case 'H':
72 args->hidden = arg;
73 break;
75 default:
76 return ARGP_ERR_UNKNOWN;
78 return 0;
81 struct argp group1_argp = {
82 group1_option,
83 group1_parser
86 struct argp_child group1_child = {
87 &group1_argp,
89 "",
94 static struct argp_option group1_1_option[] = {
95 { NULL, 0, NULL, 0, "Option Group 1.1", 0 },
96 { "cantiga", 'C', NULL, 0, "create a cantiga" },
97 { "sonet", 'S', NULL, 0, "create a sonet" },
98 { NULL, 0, NULL, 0, NULL, 0 }
101 static error_t
102 group1_1_parser (int key, char *arg, struct argp_state *state)
104 struct test_args *args = state->input;
105 switch (key)
107 case 'C':
108 case 'S':
109 args->group_1_1_option = key;
110 break;
111 default:
112 return ARGP_ERR_UNKNOWN;
114 return 0;
117 struct argp group1_1_argp = {
118 group1_1_option,
119 group1_1_parser
122 struct argp_child group1_1_child = {
123 &group1_1_argp,
130 static struct argp_option group2_option[] = {
131 { NULL, 0, NULL, 0, "Option Group 2", 0 },
132 { "option", 'O', NULL, 0, "An option", 1 },
133 { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL,
134 "Option with an optional argument. ARG is one of the following:", 2 },
135 { "one", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "one unit", 3 },
136 { "two", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "two units", 3 },
137 { "many", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "many units", 3 },
138 { NULL, 0, NULL, 0, NULL, 0 }
141 static error_t
142 group2_parser (int key, char *arg, struct argp_state *state)
144 struct test_args *args = state->input;
146 switch (key)
148 case 'O':
149 args->opt = 1;
150 break;
152 case 'o':
153 args->optional_set = 1;
154 args->optional = arg;
155 break;
157 default:
158 return ARGP_ERR_UNKNOWN;
160 return 0;
163 struct argp group2_argp = {
164 group2_option,
165 group2_parser
168 struct argp_child group2_child = {
169 &group2_argp,
176 static struct argp_option group2_1_option[] = {
177 { NULL, 0, NULL, 0, "Option Group 2.1", 0 },
178 { "poem", 'p', NULL, 0, "create a poem" },
179 { "limerick", 'l', NULL, 0, "create a limerick" },
180 { NULL, 0, NULL, 0, NULL, 0 }
183 static error_t
184 group2_1_parser (int key, char *arg, struct argp_state *state)
186 struct test_args *args = state->input;
187 switch (key)
189 case 'p':
190 case 'e':
191 args->group_2_1_option = key;
192 break;
193 default:
194 return ARGP_ERR_UNKNOWN;
196 return 0;
199 struct argp group2_1_argp = {
200 group2_1_option,
201 group2_1_parser
204 struct argp_child group2_1_child = {
205 &group2_1_argp,
212 static struct argp_option main_options[] = {
213 { NULL, 0, NULL, 0, "Main options", 0 },
214 { "test", 't', NULL, 0, NULL, 1 },
215 { NULL, 0, NULL, 0, NULL, 0 }
218 static error_t
219 parse_opt (int key, char *arg, struct argp_state *state)
221 struct test_args *args = state->input;
222 int i;
224 switch (key)
226 case ARGP_KEY_INIT:
227 for (i = 0; state->root_argp->children[i].argp; i++)
228 state->child_inputs[i] = args;
229 break;
231 case 't':
232 args->test = 1;
233 break;
235 default:
236 return ARGP_ERR_UNKNOWN;
238 return 0;
241 const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION;
242 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
243 static char doc[] = "documentation string";
245 struct argp test_argp = {
246 main_options,
247 parse_opt,
248 "ARGS...",
249 doc,
250 NULL,
251 NULL,
252 NULL
255 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1)
256 #define ARGV0 "test-argp"
257 #define init_args(a) memset (&(a), 0, sizeof (a));
259 #define INIT_TEST_COMMON(n) \
260 int argc = NARGS (argv); \
261 struct test_args test_args; \
262 init_args (test_args); \
263 test_number = n;
265 #define INIT_TEST1(n, arg1) \
266 char *argv[] = { ARGV0, arg1, NULL }; \
267 INIT_TEST_COMMON (n)
269 #define INIT_TEST2(n, arg1, arg2) \
270 char *argv[] = { ARGV0, arg1, arg2, NULL }; \
271 INIT_TEST_COMMON (n)
273 #define INIT_TEST3(n, arg1, arg2, arg3) \
274 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
275 INIT_TEST_COMMON (n)
277 int test_number;
278 unsigned failure_count = 0;
280 void
281 fail (const char *msg)
283 fprintf (stderr, "Test %d: %s\n", test_number, msg);
284 failure_count++;
287 void
288 test1 (struct argp *argp)
290 INIT_TEST1 (1, "--test");
291 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
292 fail ("argp_parse failed");
293 else if (test_args.test != 1)
294 fail ("option not processed");
297 void
298 test2 (struct argp *argp)
300 INIT_TEST1 (2, "-t");
301 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
302 fail ("argp_parse failed");
303 else if (test_args.test != 1)
304 fail ("option not processed");
307 void
308 test_file (struct argp *argp, int argc, char **argv, struct test_args *args)
310 if (argp_parse (argp, argc, argv, 0, NULL, args))
311 fail ("argp_parse failed");
312 else if (!args->file)
313 fail ("option not processed");
314 else if (strcmp (args->file, "FILE"))
315 fail ("option processed incorrectly");
318 void
319 test3 (struct argp *argp)
321 INIT_TEST1 (3, "--file=FILE");
322 test_file (argp, argc, argv, &test_args);
325 void
326 test4 (struct argp *argp)
328 INIT_TEST2 (4, "--file", "FILE");
329 test_file (argp, argc, argv, &test_args);
332 void
333 test5 (struct argp *argp)
335 INIT_TEST1 (5, "--input=FILE");
336 test_file (argp, argc, argv, &test_args);
339 void
340 test6 (struct argp *argp)
342 INIT_TEST2 (6, "--input", "FILE");
343 test_file (argp, argc, argv, &test_args);
346 void
347 test_optional (struct argp *argp, int argc, char **argv,
348 struct test_args *args, const char *val, const char *a)
350 int index;
351 if (argp_parse (argp, argc, argv, 0, &index, args))
352 fail ("argp_parse failed");
353 else if (!args->optional_set)
354 fail ("option not processed");
356 if (!val)
358 if (args->optional)
359 fail ("option processed incorrectly");
361 else if (strcmp (args->optional, val))
362 fail ("option processed incorrectly");
364 if (a)
366 if (index == argc)
367 fail ("expected command line argument not found");
368 else if (strcmp (argv[index], a))
369 fail ("expected command line argument does not match");
373 void
374 test7 (struct argp *argp)
376 INIT_TEST1 (7, "-oARG");
377 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
380 void
381 test8 (struct argp *argp)
383 INIT_TEST2 (8, "-o", "ARG");
384 test_optional (argp, argc, argv, &test_args, NULL, "ARG");
387 void
388 test9 (struct argp *argp)
390 INIT_TEST1 (9, "--optional=ARG");
391 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
394 void
395 test10 (struct argp *argp)
397 INIT_TEST2 (10, "--optional", "ARG");
398 test_optional (argp, argc, argv, &test_args, NULL, "ARG");
401 void
402 test11 (struct argp *argp)
404 INIT_TEST1 (11, "--optiona=ARG");
405 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
408 void
409 test12 (struct argp *argp)
411 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
412 test_optional (argp, argc, argv, &test_args, "OPT", "FILE");
415 void
416 test13 (struct argp *argp)
418 INIT_TEST1 (1, "--cantiga");
419 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
420 fail ("argp_parse failed");
421 else if (test_args.group_1_1_option != 'C')
422 fail ("option not processed");
425 void
426 test14 (struct argp *argp)
428 INIT_TEST1 (1, "--limerick");
429 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
430 fail ("argp_parse failed");
431 else if (test_args.group_2_1_option != 'l')
432 fail ("option not processed");
435 void
436 test15 (struct argp *argp)
438 INIT_TEST2 (1, "-r", "FILE");
439 test_file (argp, argc, argv, &test_args);
440 if (!test_args.read)
441 fail ("short alias not recognized properly");
445 typedef void (*test_fp) (struct argp *argp);
447 test_fp test_fun[] = {
448 test1, test2, test3, test4,
449 test5, test6, test7, test8,
450 test9, test10, test11, test12,
451 test13, test14, test15,
452 NULL
456 main (int argc, char **argv)
458 struct argp_child argp_children[3], group1_children[2], group2_children[2];
459 test_fp *fun;
461 group1_children[0] = group1_1_child;
462 group1_children[1].argp = NULL;
463 group1_argp.children = group1_children;
465 group2_children[0] = group2_1_child;
466 group2_children[1].argp = NULL;
467 group2_argp.children = group2_children;
469 argp_children[0] = group1_child;
470 argp_children[1] = group2_child;
471 argp_children[2].argp = NULL;
472 test_argp.children = argp_children;
474 if (argc > 0)
476 struct test_args test_args;
477 init_args (test_args);
478 return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
481 for (fun = test_fun; *fun; fun++)
482 (*fun) (&test_argp);
484 if (failure_count)
485 return 1;
487 return 0;