Mercurial > gnulib
annotate tests/test-argp.c @ 17363:5a51fb7777a9
sys_select, sys_time: port 2013-01-30 Solaris 2.6 fix to Cygwin
Problem reported by Marco Atzeri in
<http://lists.gnu.org/archive/html/bug-gnulib/2013-03/msg00000.html>.
* lib/sys_select.in.h [HAVE_SYS_SELECT_H && _CYGWIN_SYS_TIME_H]:
Simply delegate to the system <sys/select.h> in this case too.
Also, pay attention to _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H only
if OSF/1, since otherwise Cygwin breaks, and it doesn't seem to
be needed on Solaris either.
* lib/sys_time.in.h [_CYGWIN_SYS_TIME_H]:
Simply delgate to the system <sys/time.h> in this case.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Tue, 19 Mar 2013 09:08:47 -0700 |
parents | e542fd46ad6f |
children | 344018b6e5d7 |
rev | line source |
---|---|
6579 | 1 /* Test suite for argp. |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
2 Copyright (C) 2006-2007, 2009-2013 Free Software Foundation, Inc. |
6579 | 3 This file is part of the GNUlib Library. |
4 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
6579 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
8 (at your option) any later version. |
6579 | 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
15 You should have received a copy of the GNU General Public License |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
6579 | 17 |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
18 #include <config.h> |
6579 | 19 |
8285
fd56d321b961
Provide a program_name variable on platforms other than glibc.
Bruno Haible <bruno@clisp.org>
parents:
6579
diff
changeset
|
20 #include "argp.h" |
fd56d321b961
Provide a program_name variable on platforms other than glibc.
Bruno Haible <bruno@clisp.org>
parents:
6579
diff
changeset
|
21 |
6579 | 22 #include <stdio.h> |
23 #include <stdlib.h> | |
14186
cd4635d4dbe6
assume <ctype.h>, ..., <time.h> exist
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
24 #include <string.h> |
6579 | 25 #if HAVE_STRINGS_H |
26 # include <strings.h> | |
27 #endif | |
8766
27923c808d14
Restore #include "progname.h"
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8757
diff
changeset
|
28 #include "progname.h" |
6579 | 29 |
30 struct test_args | |
31 { | |
32 int test; | |
33 int verbose; | |
34 char *file; | |
12851
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
35 int read; |
6579 | 36 char *hidden; |
37 int opt; | |
38 char *optional; | |
39 int optional_set; | |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
40 int group_2_1_option; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
41 int group_1_1_option; |
6579 | 42 }; |
43 | |
12850 | 44 static struct argp_option group1_option[] = { |
45 { NULL, 0, NULL, 0, "Option Group 1", 0 }, | |
6579 | 46 { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 }, |
47 { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 }, | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
48 { "input", 0, NULL, OPTION_ALIAS, NULL, 1 }, |
12851
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
49 { "read", 'r', NULL, OPTION_ALIAS, NULL, 1 }, |
6579 | 50 { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 }, |
51 { NULL, 0, NULL, 0, NULL, 0 } | |
52 }; | |
53 | |
54 static error_t | |
55 group1_parser (int key, char *arg, struct argp_state *state) | |
56 { | |
57 struct test_args *args = state->input; | |
58 | |
59 switch (key) | |
60 { | |
61 case 'v': | |
62 args->verbose++; | |
63 break; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
64 |
12851
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
65 case 'r': |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
66 args->read = 1; |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
67 /* fall through */ |
6579 | 68 case 'f': |
69 args->file = arg; | |
70 break; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
71 |
6579 | 72 case 'H': |
73 args->hidden = arg; | |
74 break; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
75 |
6579 | 76 default: |
77 return ARGP_ERR_UNKNOWN; | |
78 } | |
79 return 0; | |
80 } | |
81 | |
82 struct argp group1_argp = { | |
83 group1_option, | |
84 group1_parser | |
85 }; | |
86 | |
87 struct argp_child group1_child = { | |
88 &group1_argp, | |
89 0, | |
90 "", | |
91 1 | |
92 }; | |
12850 | 93 |
6579 | 94 |
12850 | 95 static struct argp_option group1_1_option[] = { |
96 { NULL, 0, NULL, 0, "Option Group 1.1", 0 }, | |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
97 { "cantiga", 'C', NULL, 0, "create a cantiga" }, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
98 { "sonet", 'S', NULL, 0, "create a sonet" }, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
99 { NULL, 0, NULL, 0, NULL, 0 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
100 }; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
101 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
102 static error_t |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
103 group1_1_parser (int key, char *arg, struct argp_state *state) |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
104 { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
105 struct test_args *args = state->input; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
106 switch (key) |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
107 { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
108 case 'C': |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
109 case 'S': |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
110 args->group_1_1_option = key; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
111 break; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
112 default: |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
113 return ARGP_ERR_UNKNOWN; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
114 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
115 return 0; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
116 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
117 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
118 struct argp group1_1_argp = { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
119 group1_1_option, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
120 group1_1_parser |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
121 }; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
122 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
123 struct argp_child group1_1_child = { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
124 &group1_1_argp, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
125 0, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
126 "", |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
127 2 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
128 }; |
12850 | 129 |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
130 |
12850 | 131 static struct argp_option group2_option[] = { |
132 { NULL, 0, NULL, 0, "Option Group 2", 0 }, | |
6579 | 133 { "option", 'O', NULL, 0, "An option", 1 }, |
134 { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL, | |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
135 "Option with an optional argument. ARG is one of the following:", 2 }, |
12850 | 136 { "one", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "one unit", 3 }, |
137 { "two", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "two units", 3 }, | |
138 { "many", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "many units", 3 }, | |
6579 | 139 { NULL, 0, NULL, 0, NULL, 0 } |
140 }; | |
141 | |
142 static error_t | |
143 group2_parser (int key, char *arg, struct argp_state *state) | |
144 { | |
145 struct test_args *args = state->input; | |
146 | |
147 switch (key) | |
148 { | |
149 case 'O': | |
150 args->opt = 1; | |
151 break; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
152 |
6579 | 153 case 'o': |
154 args->optional_set = 1; | |
155 args->optional = arg; | |
156 break; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
157 |
6579 | 158 default: |
159 return ARGP_ERR_UNKNOWN; | |
160 } | |
161 return 0; | |
162 } | |
163 | |
164 struct argp group2_argp = { | |
165 group2_option, | |
166 group2_parser | |
167 }; | |
168 | |
169 struct argp_child group2_child = { | |
170 &group2_argp, | |
171 0, | |
172 "", | |
173 2 | |
174 }; | |
12850 | 175 |
6579 | 176 |
12850 | 177 static struct argp_option group2_1_option[] = { |
178 { NULL, 0, NULL, 0, "Option Group 2.1", 0 }, | |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
179 { "poem", 'p', NULL, 0, "create a poem" }, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
180 { "limerick", 'l', NULL, 0, "create a limerick" }, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
181 { NULL, 0, NULL, 0, NULL, 0 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
182 }; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
183 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
184 static error_t |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
185 group2_1_parser (int key, char *arg, struct argp_state *state) |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
186 { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
187 struct test_args *args = state->input; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
188 switch (key) |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
189 { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
190 case 'p': |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
191 case 'e': |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
192 args->group_2_1_option = key; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
193 break; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
194 default: |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
195 return ARGP_ERR_UNKNOWN; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
196 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
197 return 0; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
198 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
199 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
200 struct argp group2_1_argp = { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
201 group2_1_option, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
202 group2_1_parser |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
203 }; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
204 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
205 struct argp_child group2_1_child = { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
206 &group2_1_argp, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
207 0, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
208 "", |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
209 2 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
210 }; |
12850 | 211 |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
212 |
12850 | 213 static struct argp_option main_options[] = { |
214 { NULL, 0, NULL, 0, "Main options", 0 }, | |
215 { "test", 't', NULL, 0, NULL, 1 }, | |
216 { NULL, 0, NULL, 0, NULL, 0 } | |
217 }; | |
6579 | 218 |
219 static error_t | |
220 parse_opt (int key, char *arg, struct argp_state *state) | |
221 { | |
222 struct test_args *args = state->input; | |
223 int i; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
224 |
6579 | 225 switch (key) |
226 { | |
227 case ARGP_KEY_INIT: | |
228 for (i = 0; state->root_argp->children[i].argp; i++) | |
15338
03e2cd1cbbe8
maint: indent with spaces, not TABs, and add a rule to check this
Jim Meyering <meyering@redhat.com>
parents:
14186
diff
changeset
|
229 state->child_inputs[i] = args; |
6579 | 230 break; |
231 | |
232 case 't': | |
233 args->test = 1; | |
234 break; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
235 |
6579 | 236 default: |
237 return ARGP_ERR_UNKNOWN; | |
238 } | |
239 return 0; | |
240 } | |
241 | |
242 const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION; | |
243 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">"; | |
244 static char doc[] = "documentation string"; | |
245 | |
246 struct argp test_argp = { | |
247 main_options, | |
248 parse_opt, | |
249 "ARGS...", | |
250 doc, | |
251 NULL, | |
252 NULL, | |
253 NULL | |
254 }; | |
255 | |
256 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1) | |
257 #define ARGV0 "test-argp" | |
258 #define init_args(a) memset (&(a), 0, sizeof (a)); | |
259 | |
260 #define INIT_TEST_COMMON(n) \ | |
261 int argc = NARGS (argv); \ | |
262 struct test_args test_args; \ | |
13051 | 263 init_args (test_args); \ |
6579 | 264 test_number = n; |
265 | |
266 #define INIT_TEST1(n, arg1) \ | |
267 char *argv[] = { ARGV0, arg1, NULL }; \ | |
13051 | 268 INIT_TEST_COMMON (n) |
6579 | 269 |
270 #define INIT_TEST2(n, arg1, arg2) \ | |
271 char *argv[] = { ARGV0, arg1, arg2, NULL }; \ | |
13051 | 272 INIT_TEST_COMMON (n) |
6579 | 273 |
274 #define INIT_TEST3(n, arg1, arg2, arg3) \ | |
275 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \ | |
13051 | 276 INIT_TEST_COMMON (n) |
6579 | 277 |
278 int test_number; | |
279 unsigned failure_count = 0; | |
280 | |
281 void | |
12850 | 282 fail (const char *msg) |
6579 | 283 { |
12850 | 284 fprintf (stderr, "Test %d: %s\n", test_number, msg); |
6579 | 285 failure_count++; |
286 } | |
287 | |
288 void | |
12850 | 289 test1 (struct argp *argp) |
6579 | 290 { |
291 INIT_TEST1 (1, "--test"); | |
292 if (argp_parse (argp, argc, argv, 0, NULL, &test_args)) | |
12850 | 293 fail ("argp_parse failed"); |
6579 | 294 else if (test_args.test != 1) |
12850 | 295 fail ("option not processed"); |
6579 | 296 } |
297 | |
298 void | |
12850 | 299 test2 (struct argp *argp) |
6579 | 300 { |
301 INIT_TEST1 (2, "-t"); | |
302 if (argp_parse (argp, argc, argv, 0, NULL, &test_args)) | |
12850 | 303 fail ("argp_parse failed"); |
6579 | 304 else if (test_args.test != 1) |
12850 | 305 fail ("option not processed"); |
6579 | 306 } |
307 | |
308 void | |
12850 | 309 test_file (struct argp *argp, int argc, char **argv, struct test_args *args) |
6579 | 310 { |
311 if (argp_parse (argp, argc, argv, 0, NULL, args)) | |
12850 | 312 fail ("argp_parse failed"); |
6579 | 313 else if (!args->file) |
12850 | 314 fail ("option not processed"); |
6579 | 315 else if (strcmp (args->file, "FILE")) |
12850 | 316 fail ("option processed incorrectly"); |
6579 | 317 } |
318 | |
319 void | |
12850 | 320 test3 (struct argp *argp) |
6579 | 321 { |
322 INIT_TEST1 (3, "--file=FILE"); | |
323 test_file (argp, argc, argv, &test_args); | |
324 } | |
325 | |
326 void | |
12850 | 327 test4 (struct argp *argp) |
6579 | 328 { |
329 INIT_TEST2 (4, "--file", "FILE"); | |
330 test_file (argp, argc, argv, &test_args); | |
331 } | |
332 | |
333 void | |
12850 | 334 test5 (struct argp *argp) |
6579 | 335 { |
336 INIT_TEST1 (5, "--input=FILE"); | |
337 test_file (argp, argc, argv, &test_args); | |
338 } | |
339 | |
340 void | |
12850 | 341 test6 (struct argp *argp) |
6579 | 342 { |
343 INIT_TEST2 (6, "--input", "FILE"); | |
344 test_file (argp, argc, argv, &test_args); | |
345 } | |
346 | |
347 void | |
12850 | 348 test_optional (struct argp *argp, int argc, char **argv, |
15338
03e2cd1cbbe8
maint: indent with spaces, not TABs, and add a rule to check this
Jim Meyering <meyering@redhat.com>
parents:
14186
diff
changeset
|
349 struct test_args *args, const char *val, const char *a) |
6579 | 350 { |
351 int index; | |
352 if (argp_parse (argp, argc, argv, 0, &index, args)) | |
12850 | 353 fail ("argp_parse failed"); |
6579 | 354 else if (!args->optional_set) |
12850 | 355 fail ("option not processed"); |
6579 | 356 |
357 if (!val) | |
358 { | |
359 if (args->optional) | |
15338
03e2cd1cbbe8
maint: indent with spaces, not TABs, and add a rule to check this
Jim Meyering <meyering@redhat.com>
parents:
14186
diff
changeset
|
360 fail ("option processed incorrectly"); |
6579 | 361 } |
362 else if (strcmp (args->optional, val)) | |
12850 | 363 fail ("option processed incorrectly"); |
6579 | 364 |
365 if (a) | |
366 { | |
367 if (index == argc) | |
15338
03e2cd1cbbe8
maint: indent with spaces, not TABs, and add a rule to check this
Jim Meyering <meyering@redhat.com>
parents:
14186
diff
changeset
|
368 fail ("expected command line argument not found"); |
12850 | 369 else if (strcmp (argv[index], a)) |
15338
03e2cd1cbbe8
maint: indent with spaces, not TABs, and add a rule to check this
Jim Meyering <meyering@redhat.com>
parents:
14186
diff
changeset
|
370 fail ("expected command line argument does not match"); |
6579 | 371 } |
372 } | |
373 | |
374 void | |
12850 | 375 test7 (struct argp *argp) |
6579 | 376 { |
377 INIT_TEST1 (7, "-oARG"); | |
12850 | 378 test_optional (argp, argc, argv, &test_args, "ARG", NULL); |
6579 | 379 } |
380 | |
381 void | |
12850 | 382 test8 (struct argp *argp) |
6579 | 383 { |
384 INIT_TEST2 (8, "-o", "ARG"); | |
12850 | 385 test_optional (argp, argc, argv, &test_args, NULL, "ARG"); |
6579 | 386 } |
387 | |
388 void | |
12850 | 389 test9 (struct argp *argp) |
6579 | 390 { |
391 INIT_TEST1 (9, "--optional=ARG"); | |
12850 | 392 test_optional (argp, argc, argv, &test_args, "ARG", NULL); |
6579 | 393 } |
394 | |
395 void | |
12850 | 396 test10 (struct argp *argp) |
6579 | 397 { |
398 INIT_TEST2 (10, "--optional", "ARG"); | |
12850 | 399 test_optional (argp, argc, argv, &test_args, NULL, "ARG"); |
6579 | 400 } |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
401 |
6579 | 402 void |
12850 | 403 test11 (struct argp *argp) |
6579 | 404 { |
405 INIT_TEST1 (11, "--optiona=ARG"); | |
12850 | 406 test_optional (argp, argc, argv, &test_args, "ARG", NULL); |
6579 | 407 } |
408 | |
409 void | |
12850 | 410 test12 (struct argp *argp) |
6579 | 411 { |
412 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE"); | |
12850 | 413 test_optional (argp, argc, argv, &test_args, "OPT", "FILE"); |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
414 } |
6579 | 415 |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
416 void |
12850 | 417 test13 (struct argp *argp) |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
418 { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
419 INIT_TEST1 (1, "--cantiga"); |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
420 if (argp_parse (argp, argc, argv, 0, NULL, &test_args)) |
12850 | 421 fail ("argp_parse failed"); |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
422 else if (test_args.group_1_1_option != 'C') |
12850 | 423 fail ("option not processed"); |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
424 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
425 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
426 void |
12850 | 427 test14 (struct argp *argp) |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
428 { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
429 INIT_TEST1 (1, "--limerick"); |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
430 if (argp_parse (argp, argc, argv, 0, NULL, &test_args)) |
12850 | 431 fail ("argp_parse failed"); |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
432 else if (test_args.group_2_1_option != 'l') |
12850 | 433 fail ("option not processed"); |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
434 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
435 |
12851
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
436 void |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
437 test15 (struct argp *argp) |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
438 { |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
439 INIT_TEST2 (1, "-r", "FILE"); |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
440 test_file (argp, argc, argv, &test_args); |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
441 if (!test_args.read) |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
442 fail ("short alias not recognized properly"); |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
443 } |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
444 |
6579 | 445 |
446 typedef void (*test_fp) (struct argp *argp); | |
447 | |
448 test_fp test_fun[] = { | |
449 test1, test2, test3, test4, | |
450 test5, test6, test7, test8, | |
451 test9, test10, test11, test12, | |
12851
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
452 test13, test14, test15, |
6579 | 453 NULL |
454 }; | |
455 | |
456 int | |
457 main (int argc, char **argv) | |
458 { | |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
459 struct argp_child argp_children[3], group1_children[2], group2_children[2]; |
6579 | 460 test_fp *fun; |
8285
fd56d321b961
Provide a program_name variable on platforms other than glibc.
Bruno Haible <bruno@clisp.org>
parents:
6579
diff
changeset
|
461 |
fd56d321b961
Provide a program_name variable on platforms other than glibc.
Bruno Haible <bruno@clisp.org>
parents:
6579
diff
changeset
|
462 set_program_name (argv[0]); |
fd56d321b961
Provide a program_name variable on platforms other than glibc.
Bruno Haible <bruno@clisp.org>
parents:
6579
diff
changeset
|
463 |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
464 group1_children[0] = group1_1_child; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
465 group1_children[1].argp = NULL; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
466 group1_argp.children = group1_children; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
467 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
468 group2_children[0] = group2_1_child; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
469 group2_children[1].argp = NULL; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
470 group2_argp.children = group2_children; |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
471 |
6579 | 472 argp_children[0] = group1_child; |
473 argp_children[1] = group2_child; | |
474 argp_children[2].argp = NULL; | |
475 test_argp.children = argp_children; | |
476 | |
477 if (argc > 0) | |
478 { | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
479 struct test_args test_args; |
12850 | 480 init_args (test_args); |
6579 | 481 return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args); |
482 } | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
483 |
6579 | 484 for (fun = test_fun; *fun; fun++) |
485 (*fun) (&test_argp); | |
486 | |
487 if (failure_count) | |
488 return 1; | |
489 | |
490 return 0; | |
491 } |