Bump to 1.14.1
[platform/upstream/augeas.git] / tests / test-argp.c
1 /* Test suite for argp.
2    Copyright (C) 2006-2007, 2009-2016 Free Software Foundation, Inc.
3    This file is part of the GNUlib Library.
4
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.
9
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.
14
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/>.  */
17
18 #include <config.h>
19
20 #include "argp.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #if HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28
29 struct test_args
30 {
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;
41 };
42
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 }
51 };
52
53 static error_t
54 group1_parser (int key, char *arg, struct argp_state *state)
55 {
56   struct test_args *args = state->input;
57
58   switch (key)
59     {
60     case 'v':
61       args->verbose++;
62       break;
63
64     case 'r':
65       args->read = 1;
66       /* fall through */
67     case 'f':
68       args->file = arg;
69       break;
70
71     case 'H':
72       args->hidden = arg;
73       break;
74
75     default:
76       return ARGP_ERR_UNKNOWN;
77     }
78   return 0;
79 }
80
81 struct argp group1_argp = {
82   group1_option,
83   group1_parser
84 };
85
86 struct argp_child group1_child = {
87   &group1_argp,
88   0,
89   "",
90   1
91 };
92 \f
93
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 }
99 };
100
101 static error_t
102 group1_1_parser (int key, char *arg, struct argp_state *state)
103 {
104   struct test_args *args = state->input;
105   switch (key)
106     {
107     case 'C':
108     case 'S':
109       args->group_1_1_option = key;
110       break;
111     default:
112       return ARGP_ERR_UNKNOWN;
113     }
114   return 0;
115 }
116
117 struct argp group1_1_argp = {
118   group1_1_option,
119   group1_1_parser
120 };
121
122 struct argp_child group1_1_child = {
123   &group1_1_argp,
124   0,
125   "",
126   2
127 };
128 \f
129
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 }
139 };
140
141 static error_t
142 group2_parser (int key, char *arg, struct argp_state *state)
143 {
144   struct test_args *args = state->input;
145
146   switch (key)
147     {
148     case 'O':
149       args->opt = 1;
150       break;
151
152     case 'o':
153       args->optional_set = 1;
154       args->optional = arg;
155       break;
156
157     default:
158       return ARGP_ERR_UNKNOWN;
159     }
160   return 0;
161 }
162
163 struct argp group2_argp = {
164   group2_option,
165   group2_parser
166 };
167
168 struct argp_child group2_child = {
169   &group2_argp,
170   0,
171   "",
172   2
173 };
174 \f
175
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 }
181 };
182
183 static error_t
184 group2_1_parser (int key, char *arg, struct argp_state *state)
185 {
186   struct test_args *args = state->input;
187   switch (key)
188     {
189     case 'p':
190     case 'e':
191       args->group_2_1_option = key;
192       break;
193     default:
194       return ARGP_ERR_UNKNOWN;
195     }
196   return 0;
197 }
198
199 struct argp group2_1_argp = {
200   group2_1_option,
201   group2_1_parser
202 };
203
204 struct argp_child group2_1_child = {
205   &group2_1_argp,
206   0,
207   "",
208   2
209 };
210 \f
211
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 }
216 };
217
218 static error_t
219 parse_opt (int key, char *arg, struct argp_state *state)
220 {
221   struct test_args *args = state->input;
222   int i;
223
224   switch (key)
225     {
226     case ARGP_KEY_INIT:
227       for (i = 0; state->root_argp->children[i].argp; i++)
228         state->child_inputs[i] = args;
229       break;
230
231     case 't':
232       args->test = 1;
233       break;
234
235     default:
236       return ARGP_ERR_UNKNOWN;
237     }
238   return 0;
239 }
240
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";
244
245 struct argp test_argp = {
246   main_options,
247   parse_opt,
248   "ARGS...",
249   doc,
250   NULL,
251   NULL,
252   NULL
253 };
254
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));
258
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;
264
265 #define INIT_TEST1(n, arg1)            \
266  char *argv[] = { ARGV0, arg1, NULL }; \
267  INIT_TEST_COMMON (n)
268
269 #define INIT_TEST2(n, arg1, arg2)            \
270  char *argv[] = { ARGV0, arg1, arg2, NULL }; \
271  INIT_TEST_COMMON (n)
272
273 #define INIT_TEST3(n, arg1, arg2, arg3)            \
274  char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
275  INIT_TEST_COMMON (n)
276
277 int test_number;
278 unsigned failure_count = 0;
279
280 void
281 fail (const char *msg)
282 {
283   fprintf (stderr, "Test %d: %s\n", test_number, msg);
284   failure_count++;
285 }
286
287 void
288 test1 (struct argp *argp)
289 {
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");
295 }
296
297 void
298 test2 (struct argp *argp)
299 {
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");
305 }
306
307 void
308 test_file (struct argp *argp, int argc, char **argv, struct test_args *args)
309 {
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");
316 }
317
318 void
319 test3 (struct argp *argp)
320 {
321   INIT_TEST1 (3, "--file=FILE");
322   test_file (argp, argc, argv, &test_args);
323 }
324
325 void
326 test4 (struct argp *argp)
327 {
328   INIT_TEST2 (4, "--file", "FILE");
329   test_file (argp, argc, argv, &test_args);
330 }
331
332 void
333 test5 (struct argp *argp)
334 {
335   INIT_TEST1 (5, "--input=FILE");
336   test_file (argp, argc, argv, &test_args);
337 }
338
339 void
340 test6 (struct argp *argp)
341 {
342   INIT_TEST2 (6, "--input", "FILE");
343   test_file (argp, argc, argv, &test_args);
344 }
345
346 void
347 test_optional (struct argp *argp, int argc, char **argv,
348                struct test_args *args, const char *val, const char *a)
349 {
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");
355
356   if (!val)
357     {
358       if (args->optional)
359         fail ("option processed incorrectly");
360     }
361   else if (strcmp (args->optional, val))
362     fail ("option processed incorrectly");
363
364   if (a)
365     {
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");
370     }
371 }
372
373 void
374 test7 (struct argp *argp)
375 {
376   INIT_TEST1 (7, "-oARG");
377   test_optional (argp, argc, argv, &test_args, "ARG", NULL);
378 }
379
380 void
381 test8 (struct argp *argp)
382 {
383   INIT_TEST2 (8, "-o", "ARG");
384   test_optional (argp, argc, argv, &test_args, NULL, "ARG");
385 }
386
387 void
388 test9 (struct argp *argp)
389 {
390   INIT_TEST1 (9, "--optional=ARG");
391   test_optional (argp, argc, argv, &test_args, "ARG", NULL);
392 }
393
394 void
395 test10 (struct argp *argp)
396 {
397   INIT_TEST2 (10, "--optional", "ARG");
398   test_optional (argp, argc, argv, &test_args, NULL, "ARG");
399 }
400
401 void
402 test11 (struct argp *argp)
403 {
404   INIT_TEST1 (11, "--optiona=ARG");
405   test_optional (argp, argc, argv, &test_args, "ARG", NULL);
406 }
407
408 void
409 test12 (struct argp *argp)
410 {
411   INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
412   test_optional (argp, argc, argv, &test_args, "OPT", "FILE");
413 }
414
415 void
416 test13 (struct argp *argp)
417 {
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");
423 }
424
425 void
426 test14 (struct argp *argp)
427 {
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");
433 }
434
435 void
436 test15 (struct argp *argp)
437 {
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");
442 }
443
444
445 typedef void (*test_fp) (struct argp *argp);
446
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
453 };
454
455 int
456 main (int argc, char **argv)
457 {
458   struct argp_child argp_children[3], group1_children[2], group2_children[2];
459   test_fp *fun;
460
461   group1_children[0] = group1_1_child;
462   group1_children[1].argp = NULL;
463   group1_argp.children = group1_children;
464
465   group2_children[0] = group2_1_child;
466   group2_children[1].argp = NULL;
467   group2_argp.children = group2_children;
468
469   argp_children[0] = group1_child;
470   argp_children[1] = group2_child;
471   argp_children[2].argp = NULL;
472   test_argp.children = argp_children;
473
474   if (argc > 0)
475     {
476       struct test_args test_args;
477       init_args (test_args);
478       return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
479     }
480
481   for (fun = test_fun; *fun; fun++)
482     (*fun) (&test_argp);
483
484   if (failure_count)
485     return 1;
486
487   return 0;
488 }