Commit | Line | Data |
---|---|---|
903a5b8a | 1 | /* |
fc07e526 SM |
2 | * Copyright (c) 2019-2021 Philippe Proulx <pproulx@efficios.com> |
3 | * Copyright (c) 2020-2021 Simon Marchi <simon.marchi@efficios.com> | |
903a5b8a SM |
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; under version 2 of the License. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along | |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | */ | |
18 | ||
7ac57709 | 19 | #include <assert.h> |
903a5b8a | 20 | #include <stdlib.h> |
d1f7bbdb | 21 | #include <stdio.h> |
903a5b8a | 22 | #include <string.h> |
11003cd5 | 23 | #include <stdbool.h> |
903a5b8a SM |
24 | #include <glib.h> |
25 | ||
26 | #include "tap/tap.h" | |
903a5b8a SM |
27 | #include "argpar/argpar.h" |
28 | ||
29 | /* | |
11003cd5 PP |
30 | * Formats `item` and appends the resulting string to `res_str` to |
31 | * incrementally build an expected command line string. | |
32 | * | |
33 | * This function: | |
34 | * | |
35 | * * Prefers the `--long-opt=arg` style over the `-s arg` style. | |
36 | * | |
37 | * * Uses the `arg<A,B>` form for non-option arguments, where `A` is the | |
38 | * original argument index and `B` is the non-option argument index. | |
903a5b8a SM |
39 | */ |
40 | static | |
11003cd5 PP |
41 | void append_to_res_str(GString * const res_str, |
42 | const struct argpar_item * const item) | |
fc07e526 SM |
43 | { |
44 | if (res_str->len > 0) { | |
45 | g_string_append_c(res_str, ' '); | |
46 | } | |
47 | ||
d4539a90 | 48 | switch (argpar_item_type(item)) { |
fc07e526 SM |
49 | case ARGPAR_ITEM_TYPE_OPT: |
50 | { | |
d4539a90 PP |
51 | const struct argpar_opt_descr * const descr = |
52 | argpar_item_opt_descr(item); | |
53 | const char * const arg = argpar_item_opt_arg(item); | |
fc07e526 | 54 | |
d4539a90 | 55 | if (descr->long_name) { |
fc07e526 | 56 | g_string_append_printf(res_str, "--%s", |
d4539a90 | 57 | descr->long_name); |
fc07e526 | 58 | |
d4539a90 PP |
59 | if (arg) { |
60 | g_string_append_printf(res_str, "=%s", arg); | |
fc07e526 | 61 | } |
d4539a90 | 62 | } else if (descr->short_name) { |
fc07e526 | 63 | g_string_append_printf(res_str, "-%c", |
d4539a90 | 64 | descr->short_name); |
fc07e526 | 65 | |
d4539a90 PP |
66 | if (arg) { |
67 | g_string_append_printf(res_str, " %s", arg); | |
fc07e526 SM |
68 | } |
69 | } | |
70 | ||
71 | break; | |
72 | } | |
73 | case ARGPAR_ITEM_TYPE_NON_OPT: | |
74 | { | |
d4539a90 PP |
75 | const char * const arg = argpar_item_non_opt_arg(item); |
76 | const unsigned int orig_index = | |
77 | argpar_item_non_opt_orig_index(item); | |
78 | const unsigned int non_opt_index = | |
79 | argpar_item_non_opt_non_opt_index(item); | |
fc07e526 | 80 | |
d4539a90 PP |
81 | g_string_append_printf(res_str, "%s<%u,%u>", arg, orig_index, |
82 | non_opt_index); | |
fc07e526 SM |
83 | break; |
84 | } | |
85 | default: | |
86 | abort(); | |
87 | } | |
88 | } | |
89 | ||
11003cd5 PP |
90 | /* |
91 | * Parses `cmdline` with argpar_parse() using the option descriptors | |
92 | * `descrs`, and ensures that the resulting effective command line is | |
93 | * `expected_cmd_line` and that the number of ingested original | |
94 | * arguments is `expected_ingested_orig_args`. | |
95 | * | |
96 | * This function splits `cmdline` on spaces to create an original | |
97 | * argument array. | |
98 | * | |
99 | * This function builds the resulting command line from parsing items | |
100 | * by space-separating each formatted item (see append_to_res_str()). | |
101 | */ | |
fc07e526 | 102 | static |
11003cd5 PP |
103 | void test_succeed_argpar_parse(const char * const cmdline, |
104 | const char * const expected_cmd_line, | |
105 | const struct argpar_opt_descr * const descrs, | |
106 | const unsigned int expected_ingested_orig_args) | |
903a5b8a | 107 | { |
1c9a6bde | 108 | struct argpar_parse_ret parse_ret; |
11003cd5 PP |
109 | GString * const res_str = g_string_new(NULL); |
110 | gchar ** const argv = g_strsplit(cmdline, " ", 0); | |
903a5b8a SM |
111 | unsigned int i; |
112 | ||
7ac57709 SM |
113 | assert(argv); |
114 | assert(res_str); | |
1c9a6bde | 115 | parse_ret = argpar_parse(g_strv_length(argv), |
903a5b8a SM |
116 | (const char * const *) argv, descrs, false); |
117 | ok(parse_ret.items, | |
1c9a6bde | 118 | "argpar_parse() succeeds for command line `%s`", cmdline); |
903a5b8a | 119 | ok(!parse_ret.error, |
11003cd5 PP |
120 | "argpar_parse() doesn't set an error for command line `%s`", |
121 | cmdline); | |
903a5b8a | 122 | ok(parse_ret.ingested_orig_args == expected_ingested_orig_args, |
1c9a6bde | 123 | "argpar_parse() returns the correct number of ingested " |
903a5b8a | 124 | "original arguments for command line `%s`", cmdline); |
11003cd5 | 125 | |
903a5b8a SM |
126 | if (parse_ret.ingested_orig_args != expected_ingested_orig_args) { |
127 | diag("Expected: %u Got: %u", expected_ingested_orig_args, | |
128 | parse_ret.ingested_orig_args); | |
129 | } | |
130 | ||
131 | if (!parse_ret.items) { | |
11003cd5 | 132 | fail("argpar_parse() returns the expected parsing items " |
903a5b8a SM |
133 | "for command line `%s`", cmdline); |
134 | goto end; | |
135 | } | |
136 | ||
7ac57709 | 137 | for (i = 0; i < parse_ret.items->n_items; i++) { |
11003cd5 | 138 | append_to_res_str(res_str, parse_ret.items->items[i]); |
fc07e526 SM |
139 | } |
140 | ||
141 | ok(strcmp(expected_cmd_line, res_str->str) == 0, | |
142 | "argpar_parse() returns the expected parsed arguments " | |
143 | "for command line `%s`", cmdline); | |
11003cd5 | 144 | |
fc07e526 SM |
145 | if (strcmp(expected_cmd_line, res_str->str) != 0) { |
146 | diag("Expected: `%s`", expected_cmd_line); | |
147 | diag("Got: `%s`", res_str->str); | |
148 | } | |
149 | ||
150 | end: | |
151 | argpar_parse_ret_fini(&parse_ret); | |
152 | g_string_free(res_str, TRUE); | |
153 | g_strfreev(argv); | |
154 | } | |
155 | ||
11003cd5 PP |
156 | /* |
157 | * Parses `cmdline` with the iterator API using the option descriptors | |
158 | * `descrs`, and ensures that the resulting effective command line is | |
159 | * `expected_cmd_line` and that the number of ingested original | |
160 | * arguments is `expected_ingested_orig_args`. | |
161 | * | |
162 | * This function splits `cmdline` on spaces to create an original | |
163 | * argument array. | |
164 | * | |
165 | * This function builds the resulting command line from parsing items | |
166 | * by space-separating each formatted item (see append_to_res_str()). | |
167 | */ | |
fc07e526 | 168 | static |
11003cd5 PP |
169 | void test_succeed_argpar_iter(const char * const cmdline, |
170 | const char * const expected_cmd_line, | |
171 | const struct argpar_opt_descr * const descrs, | |
172 | const unsigned int expected_ingested_orig_args) | |
fc07e526 SM |
173 | { |
174 | struct argpar_iter *iter = NULL; | |
175 | const struct argpar_item *item = NULL; | |
176 | char *error = NULL; | |
11003cd5 PP |
177 | GString * const res_str = g_string_new(NULL); |
178 | gchar ** const argv = g_strsplit(cmdline, " ", 0); | |
fc07e526 SM |
179 | unsigned int i, actual_ingested_orig_args; |
180 | ||
181 | assert(argv); | |
182 | assert(res_str); | |
fc07e526 SM |
183 | iter = argpar_iter_create(g_strv_length(argv), |
184 | (const char * const *) argv, descrs); | |
185 | assert(iter); | |
186 | ||
187 | for (i = 0; ; i++) { | |
2af370d0 | 188 | enum argpar_iter_next_status status; |
fc07e526 SM |
189 | |
190 | ARGPAR_ITEM_DESTROY_AND_RESET(item); | |
2af370d0 | 191 | status = argpar_iter_next(iter, &item, &error); |
fc07e526 | 192 | |
2af370d0 PP |
193 | ok(status == ARGPAR_ITER_NEXT_STATUS_OK || |
194 | status == ARGPAR_ITER_NEXT_STATUS_END || | |
195 | status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, | |
196 | "argpar_iter_next() returns the expected status " | |
11003cd5 PP |
197 | "(%d) for command line `%s` (call %u)", |
198 | status, cmdline, i + 1); | |
fc07e526 | 199 | |
2af370d0 | 200 | if (status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT) { |
11003cd5 | 201 | ok(error, |
2af370d0 PP |
202 | "argpar_iter_next() sets an error for " |
203 | "status `ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT` " | |
11003cd5 PP |
204 | "and command line `%s` (call %u)", |
205 | cmdline, i + 1); | |
fc07e526 | 206 | } else { |
11003cd5 | 207 | ok(!error, |
2af370d0 | 208 | "argpar_iter_next() doesn't set an error " |
11003cd5 | 209 | "for other status than " |
2af370d0 | 210 | "`ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT` " |
11003cd5 PP |
211 | "and command line `%s` (call %u)", |
212 | cmdline, i + 1); | |
903a5b8a | 213 | } |
fc07e526 | 214 | |
2af370d0 PP |
215 | if (status == ARGPAR_ITER_NEXT_STATUS_END || |
216 | status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT) { | |
11003cd5 | 217 | ok(!item, |
2af370d0 PP |
218 | "argpar_iter_next() doesn't set an item " |
219 | "for status `ARGPAR_ITER_NEXT_STATUS_END` " | |
220 | "or `ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT` " | |
11003cd5 PP |
221 | "and command line `%s` (call %u)", |
222 | cmdline, i + 1); | |
903a5b8a SM |
223 | break; |
224 | } | |
fc07e526 SM |
225 | |
226 | append_to_res_str(res_str, item); | |
903a5b8a SM |
227 | } |
228 | ||
f3ab5ca1 | 229 | actual_ingested_orig_args = argpar_iter_ingested_orig_args(iter); |
fc07e526 | 230 | ok(actual_ingested_orig_args == expected_ingested_orig_args, |
f3ab5ca1 | 231 | "argpar_iter_ingested_orig_args() returns the expected " |
11003cd5 PP |
232 | "number of ingested original arguments for command line `%s`", |
233 | cmdline); | |
234 | ||
fc07e526 SM |
235 | if (actual_ingested_orig_args != expected_ingested_orig_args) { |
236 | diag("Expected: %u Got: %u", expected_ingested_orig_args, | |
237 | actual_ingested_orig_args); | |
903a5b8a SM |
238 | } |
239 | ||
240 | ok(strcmp(expected_cmd_line, res_str->str) == 0, | |
2af370d0 | 241 | "argpar_iter_next() returns the expected parsing items " |
903a5b8a | 242 | "for command line `%s`", cmdline); |
11003cd5 | 243 | |
903a5b8a SM |
244 | if (strcmp(expected_cmd_line, res_str->str) != 0) { |
245 | diag("Expected: `%s`", expected_cmd_line); | |
246 | diag("Got: `%s`", res_str->str); | |
247 | } | |
248 | ||
fc07e526 SM |
249 | argpar_item_destroy(item); |
250 | argpar_iter_destroy(iter); | |
903a5b8a SM |
251 | g_string_free(res_str, TRUE); |
252 | g_strfreev(argv); | |
fc07e526 SM |
253 | free(error); |
254 | } | |
255 | ||
256 | /* | |
11003cd5 PP |
257 | * Calls test_succeed_argpar_parse() and test_succeed_argpar_iter() |
258 | * with the provided parameters. | |
fc07e526 SM |
259 | */ |
260 | static | |
11003cd5 PP |
261 | void test_succeed(const char * const cmdline, |
262 | const char * const expected_cmd_line, | |
263 | const struct argpar_opt_descr * const descrs, | |
264 | const unsigned int expected_ingested_orig_args) | |
fc07e526 SM |
265 | { |
266 | test_succeed_argpar_parse(cmdline, expected_cmd_line, descrs, | |
267 | expected_ingested_orig_args); | |
268 | test_succeed_argpar_iter(cmdline, expected_cmd_line, descrs, | |
269 | expected_ingested_orig_args); | |
903a5b8a SM |
270 | } |
271 | ||
272 | static | |
273 | void succeed_tests(void) | |
274 | { | |
275 | /* No arguments */ | |
276 | { | |
1c9a6bde SM |
277 | const struct argpar_opt_descr descrs[] = { |
278 | ARGPAR_OPT_DESCR_SENTINEL | |
903a5b8a SM |
279 | }; |
280 | ||
281 | test_succeed( | |
282 | "", | |
283 | "", | |
284 | descrs, 0); | |
285 | } | |
286 | ||
287 | /* Single long option */ | |
288 | { | |
1c9a6bde | 289 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 290 | { 0, '\0', "salut", false }, |
1c9a6bde | 291 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
292 | }; |
293 | ||
294 | test_succeed( | |
295 | "--salut", | |
296 | "--salut", | |
297 | descrs, 1); | |
298 | } | |
299 | ||
300 | /* Single short option */ | |
301 | { | |
1c9a6bde | 302 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 303 | { 0, 'f', NULL, false }, |
1c9a6bde | 304 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
305 | }; |
306 | ||
307 | test_succeed( | |
308 | "-f", | |
309 | "-f", | |
310 | descrs, 1); | |
311 | } | |
312 | ||
313 | /* Short and long option (aliases) */ | |
314 | { | |
1c9a6bde | 315 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 316 | { 0, 'f', "flaw", false }, |
1c9a6bde | 317 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
318 | }; |
319 | ||
320 | test_succeed( | |
321 | "-f --flaw", | |
322 | "--flaw --flaw", | |
323 | descrs, 2); | |
324 | } | |
325 | ||
326 | /* Long option with argument (space form) */ | |
327 | { | |
1c9a6bde | 328 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 329 | { 0, '\0', "tooth", true }, |
1c9a6bde | 330 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
331 | }; |
332 | ||
333 | test_succeed( | |
334 | "--tooth 67", | |
335 | "--tooth=67", | |
336 | descrs, 2); | |
337 | } | |
338 | ||
339 | /* Long option with argument (equal form) */ | |
340 | { | |
1c9a6bde | 341 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 342 | { 0, '\0', "polish", true }, |
1c9a6bde | 343 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
344 | }; |
345 | ||
346 | test_succeed( | |
347 | "--polish=brick", | |
348 | "--polish=brick", | |
349 | descrs, 1); | |
350 | } | |
351 | ||
352 | /* Short option with argument (space form) */ | |
353 | { | |
1c9a6bde | 354 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 355 | { 0, 'c', NULL, true }, |
1c9a6bde | 356 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
357 | }; |
358 | ||
359 | test_succeed( | |
360 | "-c chilly", | |
361 | "-c chilly", | |
362 | descrs, 2); | |
363 | } | |
364 | ||
365 | /* Short option with argument (glued form) */ | |
366 | { | |
1c9a6bde | 367 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 368 | { 0, 'c', NULL, true }, |
1c9a6bde | 369 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
370 | }; |
371 | ||
372 | test_succeed( | |
373 | "-cchilly", | |
374 | "-c chilly", | |
375 | descrs, 1); | |
376 | } | |
377 | ||
378 | /* Short and long option (aliases) with argument (all forms) */ | |
379 | { | |
1c9a6bde | 380 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 381 | { 0, 'd', "dry", true }, |
1c9a6bde | 382 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
383 | }; |
384 | ||
385 | test_succeed( | |
386 | "--dry=rate -dthing --dry street --dry=shape", | |
387 | "--dry=rate --dry=thing --dry=street --dry=shape", | |
388 | descrs, 5); | |
389 | } | |
390 | ||
391 | /* Many short options, last one with argument (glued form) */ | |
392 | { | |
1c9a6bde | 393 | const struct argpar_opt_descr descrs[] = { |
903a5b8a SM |
394 | { 0, 'd', NULL, false }, |
395 | { 0, 'e', NULL, false }, | |
396 | { 0, 'f', NULL, true }, | |
1c9a6bde | 397 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
398 | }; |
399 | ||
400 | test_succeed( | |
401 | "-defmeow", | |
402 | "-d -e -f meow", | |
403 | descrs, 1); | |
404 | } | |
405 | ||
406 | /* Many options */ | |
407 | { | |
1c9a6bde | 408 | const struct argpar_opt_descr descrs[] = { |
903a5b8a SM |
409 | { 0, 'd', NULL, false }, |
410 | { 0, 'e', "east", true }, | |
411 | { 0, '\0', "mind", false }, | |
1c9a6bde | 412 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
413 | }; |
414 | ||
415 | test_succeed( | |
416 | "-d --mind -destart --mind --east cough -d --east=itch", | |
417 | "-d --mind -d --east=start --mind --east=cough -d --east=itch", | |
418 | descrs, 8); | |
419 | } | |
420 | ||
421 | /* Single non-option argument */ | |
422 | { | |
1c9a6bde SM |
423 | const struct argpar_opt_descr descrs[] = { |
424 | ARGPAR_OPT_DESCR_SENTINEL | |
903a5b8a SM |
425 | }; |
426 | ||
427 | test_succeed( | |
428 | "kilojoule", | |
429 | "kilojoule<0,0>", | |
430 | descrs, 1); | |
431 | } | |
432 | ||
433 | /* Two non-option arguments */ | |
434 | { | |
1c9a6bde SM |
435 | const struct argpar_opt_descr descrs[] = { |
436 | ARGPAR_OPT_DESCR_SENTINEL | |
903a5b8a SM |
437 | }; |
438 | ||
439 | test_succeed( | |
440 | "kilojoule mitaine", | |
441 | "kilojoule<0,0> mitaine<1,1>", | |
442 | descrs, 2); | |
443 | } | |
444 | ||
445 | /* Single non-option argument mixed with options */ | |
446 | { | |
1c9a6bde | 447 | const struct argpar_opt_descr descrs[] = { |
903a5b8a SM |
448 | { 0, 'd', NULL, false }, |
449 | { 0, '\0', "squeeze", true }, | |
1c9a6bde | 450 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
451 | }; |
452 | ||
453 | test_succeed( | |
454 | "-d sprout yes --squeeze little bag -d", | |
455 | "-d sprout<1,0> yes<2,1> --squeeze=little bag<5,2> -d", | |
456 | descrs, 7); | |
457 | } | |
458 | ||
459 | /* Unknown short option (space form) */ | |
460 | { | |
1c9a6bde | 461 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 462 | { 0, 'd', NULL, true }, |
1c9a6bde | 463 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
464 | }; |
465 | ||
466 | test_succeed( | |
467 | "-d salut -e -d meow", | |
468 | "-d salut", | |
469 | descrs, 2); | |
470 | } | |
471 | ||
472 | /* Unknown short option (glued form) */ | |
473 | { | |
1c9a6bde | 474 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 475 | { 0, 'd', NULL, true }, |
1c9a6bde | 476 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
477 | }; |
478 | ||
479 | test_succeed( | |
480 | "-dsalut -e -d meow", | |
481 | "-d salut", | |
482 | descrs, 1); | |
483 | } | |
484 | ||
485 | /* Unknown long option (space form) */ | |
486 | { | |
1c9a6bde | 487 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 488 | { 0, '\0', "sink", true }, |
1c9a6bde | 489 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
490 | }; |
491 | ||
492 | test_succeed( | |
493 | "--sink party --food --sink impulse", | |
494 | "--sink=party", | |
495 | descrs, 2); | |
496 | } | |
497 | ||
498 | /* Unknown long option (equal form) */ | |
499 | { | |
1c9a6bde | 500 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 501 | { 0, '\0', "sink", true }, |
1c9a6bde | 502 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
503 | }; |
504 | ||
505 | test_succeed( | |
506 | "--sink=party --food --sink=impulse", | |
507 | "--sink=party", | |
508 | descrs, 1); | |
509 | } | |
510 | ||
511 | /* Unknown option before non-option argument */ | |
512 | { | |
1c9a6bde | 513 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 514 | { 0, '\0', "thumb", true }, |
1c9a6bde | 515 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
516 | }; |
517 | ||
518 | test_succeed( | |
519 | "--thumb=party --food bateau --thumb waves", | |
520 | "--thumb=party", | |
521 | descrs, 1); | |
522 | } | |
523 | ||
524 | /* Unknown option after non-option argument */ | |
525 | { | |
1c9a6bde | 526 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 527 | { 0, '\0', "thumb", true }, |
1c9a6bde | 528 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
529 | }; |
530 | ||
531 | test_succeed( | |
532 | "--thumb=party wound --food --thumb waves", | |
533 | "--thumb=party wound<1,0>", | |
534 | descrs, 2); | |
535 | } | |
536 | ||
537 | /* Valid `---opt` */ | |
538 | { | |
1c9a6bde | 539 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 540 | { 0, '\0', "-fuel", true }, |
1c9a6bde | 541 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
542 | }; |
543 | ||
544 | test_succeed( | |
545 | "---fuel=three", | |
546 | "---fuel=three", | |
547 | descrs, 1); | |
548 | } | |
549 | ||
550 | /* Long option containing `=` in argument (equal form) */ | |
551 | { | |
1c9a6bde | 552 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 553 | { 0, '\0', "zebra", true }, |
1c9a6bde | 554 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
555 | }; |
556 | ||
557 | test_succeed( | |
558 | "--zebra=three=yes", | |
559 | "--zebra=three=yes", | |
560 | descrs, 1); | |
561 | } | |
562 | ||
563 | /* Short option's argument starting with `-` (glued form) */ | |
564 | { | |
1c9a6bde | 565 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 566 | { 0, 'z', NULL, true }, |
1c9a6bde | 567 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
568 | }; |
569 | ||
570 | test_succeed( | |
571 | "-z-will", | |
572 | "-z -will", | |
573 | descrs, 1); | |
574 | } | |
575 | ||
576 | /* Short option's argument starting with `-` (space form) */ | |
577 | { | |
1c9a6bde | 578 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 579 | { 0, 'z', NULL, true }, |
1c9a6bde | 580 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
581 | }; |
582 | ||
583 | test_succeed( | |
584 | "-z -will", | |
585 | "-z -will", | |
586 | descrs, 2); | |
587 | } | |
588 | ||
589 | /* Long option's argument starting with `-` (space form) */ | |
590 | { | |
1c9a6bde | 591 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 592 | { 0, '\0', "janine", true }, |
1c9a6bde | 593 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
594 | }; |
595 | ||
596 | test_succeed( | |
597 | "--janine -sutto", | |
598 | "--janine=-sutto", | |
599 | descrs, 2); | |
600 | } | |
601 | ||
602 | /* Long option's argument starting with `-` (equal form) */ | |
603 | { | |
1c9a6bde | 604 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 605 | { 0, '\0', "janine", true }, |
1c9a6bde | 606 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
607 | }; |
608 | ||
609 | test_succeed( | |
610 | "--janine=-sutto", | |
611 | "--janine=-sutto", | |
612 | descrs, 1); | |
613 | } | |
614 | ||
615 | /* Long option's empty argument (equal form) */ | |
616 | { | |
1c9a6bde | 617 | const struct argpar_opt_descr descrs[] = { |
903a5b8a SM |
618 | { 0, 'f', NULL, false }, |
619 | { 0, '\0', "yeah", true }, | |
1c9a6bde | 620 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
621 | }; |
622 | ||
623 | test_succeed( | |
624 | "-f --yeah= -f", | |
625 | "-f --yeah= -f", | |
626 | descrs, 3); | |
627 | } | |
dd757a65 PP |
628 | |
629 | /* `-` non-option argument */ | |
630 | { | |
631 | const struct argpar_opt_descr descrs[] = { | |
632 | { 0, 'f', NULL, false }, | |
633 | ARGPAR_OPT_DESCR_SENTINEL | |
634 | }; | |
635 | ||
636 | test_succeed( | |
637 | "-f - -f", | |
638 | "-f -<1,0> -f", | |
639 | descrs, 3); | |
640 | } | |
641 | ||
642 | /* `--` non-option argument */ | |
643 | { | |
644 | const struct argpar_opt_descr descrs[] = { | |
645 | { 0, 'f', NULL, false }, | |
646 | ARGPAR_OPT_DESCR_SENTINEL | |
647 | }; | |
648 | ||
649 | test_succeed( | |
650 | "-f -- -f", | |
651 | "-f --<1,0> -f", | |
652 | descrs, 3); | |
653 | } | |
d1f7bbdb PP |
654 | |
655 | /* Very long name of long option */ | |
656 | { | |
657 | const char opt_name[] = | |
658 | "kale-chips-waistcoat-yr-bicycle-rights-gochujang-" | |
659 | "woke-tumeric-flexitarian-biodiesel-chillwave-cliche-" | |
660 | "ethical-cardigan-listicle-pok-pok-sustainable-live-" | |
661 | "edge-jianbing-gochujang-butcher-disrupt-tattooed-" | |
662 | "tumeric-prism-photo-booth-vape-kogi-jean-shorts-" | |
663 | "blog-williamsburg-fingerstache-palo-santo-artisan-" | |
664 | "affogato-occupy-skateboard-adaptogen-neutra-celiac-" | |
665 | "put-a-bird-on-it-kombucha-everyday-carry-hot-chicken-" | |
666 | "craft-beer-subway-tile-tote-bag-disrupt-selvage-" | |
667 | "raclette-art-party-readymade-paleo-heirloom-trust-" | |
668 | "fund-small-batch-kinfolk-woke-cardigan-prism-" | |
669 | "chambray-la-croix-hashtag-unicorn-edison-bulb-tbh-" | |
670 | "cornhole-cliche-tattooed-green-juice-adaptogen-" | |
671 | "kitsch-lo-fi-vexillologist-migas-gentrify-" | |
672 | "viral-raw-denim"; | |
673 | const struct argpar_opt_descr descrs[] = { | |
674 | { 0, '\0', opt_name, true }, | |
675 | ARGPAR_OPT_DESCR_SENTINEL | |
676 | }; | |
677 | char cmdline[1024]; | |
678 | ||
679 | sprintf(cmdline, "--%s=23", opt_name); | |
680 | test_succeed(cmdline, cmdline, descrs, 1); | |
681 | } | |
903a5b8a SM |
682 | } |
683 | ||
11003cd5 PP |
684 | /* |
685 | * Parses `cmdline` with argpar_parse() using the option descriptors | |
686 | * `descrs`, and ensures that the function fails and that it sets an | |
687 | * error which is equal to `expected_error`. | |
688 | * | |
689 | * This function splits `cmdline` on spaces to create an original | |
690 | * argument array. | |
691 | */ | |
903a5b8a | 692 | static |
11003cd5 PP |
693 | void test_fail_argpar_parse(const char * const cmdline, |
694 | const char * const expected_error, | |
695 | const struct argpar_opt_descr * const descrs) | |
903a5b8a | 696 | { |
1c9a6bde | 697 | struct argpar_parse_ret parse_ret; |
11003cd5 | 698 | gchar ** const argv = g_strsplit(cmdline, " ", 0); |
903a5b8a | 699 | |
1c9a6bde | 700 | parse_ret = argpar_parse(g_strv_length(argv), |
903a5b8a SM |
701 | (const char * const *) argv, descrs, true); |
702 | ok(!parse_ret.items, | |
1c9a6bde | 703 | "argpar_parse() fails for command line `%s`", cmdline); |
903a5b8a | 704 | ok(parse_ret.error, |
11003cd5 | 705 | "argpar_parse() sets an error string for command line `%s`", |
903a5b8a | 706 | cmdline); |
11003cd5 | 707 | |
903a5b8a | 708 | if (parse_ret.items) { |
11003cd5 | 709 | fail("argpar_parse() sets the expected error string"); |
903a5b8a SM |
710 | goto end; |
711 | } | |
712 | ||
7ac57709 | 713 | ok(strcmp(expected_error, parse_ret.error) == 0, |
11003cd5 | 714 | "argpar_parse() sets the expected error string " |
903a5b8a | 715 | "for command line `%s`", cmdline); |
11003cd5 | 716 | |
7ac57709 | 717 | if (strcmp(expected_error, parse_ret.error) != 0) { |
903a5b8a | 718 | diag("Expected: `%s`", expected_error); |
7ac57709 | 719 | diag("Got: `%s`", parse_ret.error); |
903a5b8a SM |
720 | } |
721 | ||
722 | end: | |
1c9a6bde | 723 | argpar_parse_ret_fini(&parse_ret); |
903a5b8a SM |
724 | g_strfreev(argv); |
725 | } | |
726 | ||
11003cd5 PP |
727 | /* |
728 | * Parses `cmdline` with the iterator API using the option descriptors | |
2af370d0 | 729 | * `descrs`, and ensures that argpar_iter_next() fails with status |
d4d05805 PP |
730 | * `expected_status` and that it sets an error which is equal to |
731 | * `expected_error`. | |
11003cd5 PP |
732 | * |
733 | * This function splits `cmdline` on spaces to create an original | |
734 | * argument array. | |
735 | */ | |
fc07e526 | 736 | static |
11003cd5 PP |
737 | void test_fail_argpar_iter(const char * const cmdline, |
738 | const char * const expected_error, | |
2af370d0 | 739 | const enum argpar_iter_next_status expected_status, |
11003cd5 | 740 | const struct argpar_opt_descr * const descrs) |
fc07e526 SM |
741 | { |
742 | struct argpar_iter *iter = NULL; | |
743 | const struct argpar_item *item = NULL; | |
11003cd5 | 744 | gchar ** const argv = g_strsplit(cmdline, " ", 0); |
fc07e526 SM |
745 | unsigned int i; |
746 | char *error = NULL; | |
747 | ||
748 | iter = argpar_iter_create(g_strv_length(argv), | |
749 | (const char * const *) argv, descrs); | |
750 | assert(iter); | |
751 | ||
752 | for (i = 0; ; i++) { | |
2af370d0 | 753 | enum argpar_iter_next_status status; |
fc07e526 SM |
754 | |
755 | ARGPAR_ITEM_DESTROY_AND_RESET(item); | |
2af370d0 PP |
756 | status = argpar_iter_next(iter, &item, &error); |
757 | ok(status == ARGPAR_ITER_NEXT_STATUS_OK || | |
d4d05805 | 758 | status == expected_status, |
2af370d0 | 759 | "argpar_iter_next() returns the expected status " |
11003cd5 PP |
760 | "(%d) for command line `%s` (call %u)", |
761 | status, cmdline, i + 1); | |
fc07e526 | 762 | |
2af370d0 | 763 | if (status != ARGPAR_ITER_NEXT_STATUS_OK) { |
11003cd5 | 764 | ok(!item, |
2af370d0 | 765 | "argpar_iter_next() doesn't set an item " |
11003cd5 | 766 | "for other status than " |
2af370d0 | 767 | "`ARGPAR_ITER_NEXT_STATUS_OK` " |
11003cd5 PP |
768 | "and command line `%s` (call %u)", |
769 | cmdline, i + 1); | |
770 | ok(error, | |
2af370d0 | 771 | "argpar_iter_next() sets an error for " |
11003cd5 | 772 | "other status than " |
2af370d0 | 773 | " `ARGPAR_ITER_NEXT_STATUS_OK` " |
11003cd5 PP |
774 | "and command line `%s` (call %u)", |
775 | cmdline, i + 1); | |
fc07e526 SM |
776 | break; |
777 | } | |
778 | ||
11003cd5 | 779 | ok(item, |
2af370d0 PP |
780 | "argpar_iter_next() sets an item for status " |
781 | "`ARGPAR_ITER_NEXT_STATUS_OK` " | |
11003cd5 PP |
782 | "and command line `%s` (call %u)", |
783 | cmdline, i + 1); | |
784 | ok(!error, | |
2af370d0 PP |
785 | "argpar_iter_next() doesn't set an error for status " |
786 | "`ARGPAR_ITER_NEXT_STATUS_OK` " | |
11003cd5 PP |
787 | "and command line `%s` (call %u)", |
788 | cmdline, i + 1); | |
fc07e526 SM |
789 | } |
790 | ||
791 | ok(strcmp(expected_error, error) == 0, | |
2af370d0 | 792 | "argpar_iter_next() sets the expected error string " |
fc07e526 | 793 | "for command line `%s`", cmdline); |
11003cd5 | 794 | |
fc07e526 SM |
795 | if (strcmp(expected_error, error) != 0) { |
796 | diag("Expected: `%s`", expected_error); | |
797 | diag("Got: `%s`", error); | |
798 | } | |
799 | ||
800 | argpar_item_destroy(item); | |
801 | argpar_iter_destroy(iter); | |
802 | free(error); | |
803 | g_strfreev(argv); | |
804 | } | |
805 | ||
806 | /* | |
11003cd5 PP |
807 | * Calls test_fail_argpar_parse() and test_fail_argpar_iter() with the |
808 | * provided parameters. | |
fc07e526 | 809 | */ |
fc07e526 | 810 | static |
11003cd5 | 811 | void test_fail(const char * const cmdline, const char * const expected_error, |
2af370d0 | 812 | const enum argpar_iter_next_status expected_iter_next_status, |
11003cd5 | 813 | const struct argpar_opt_descr * const descrs) |
fc07e526 SM |
814 | { |
815 | test_fail_argpar_parse(cmdline, expected_error, descrs); | |
d4d05805 PP |
816 | test_fail_argpar_iter(cmdline, expected_error, |
817 | expected_iter_next_status, descrs); | |
fc07e526 SM |
818 | } |
819 | ||
903a5b8a SM |
820 | static |
821 | void fail_tests(void) | |
822 | { | |
823 | /* Unknown long option */ | |
824 | { | |
1c9a6bde | 825 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 826 | { 0, '\0', "thumb", true }, |
1c9a6bde | 827 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
828 | }; |
829 | ||
830 | test_fail( | |
831 | "--thumb=party --meow", | |
832 | "While parsing argument #2 (`--meow`): Unknown option `--meow`", | |
2af370d0 | 833 | ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, |
903a5b8a SM |
834 | descrs); |
835 | } | |
836 | ||
837 | /* Unknown short option */ | |
838 | { | |
1c9a6bde | 839 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 840 | { 0, '\0', "thumb", true }, |
1c9a6bde | 841 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
842 | }; |
843 | ||
844 | test_fail( | |
845 | "--thumb=party -x", | |
846 | "While parsing argument #2 (`-x`): Unknown option `-x`", | |
2af370d0 | 847 | ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, |
903a5b8a SM |
848 | descrs); |
849 | } | |
850 | ||
851 | /* Missing long option argument */ | |
852 | { | |
1c9a6bde | 853 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 854 | { 0, '\0', "thumb", true }, |
1c9a6bde | 855 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
856 | }; |
857 | ||
858 | test_fail( | |
859 | "--thumb", | |
860 | "While parsing argument #1 (`--thumb`): Missing required argument for option `--thumb`", | |
2af370d0 | 861 | ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, |
903a5b8a SM |
862 | descrs); |
863 | } | |
864 | ||
865 | /* Missing short option argument */ | |
866 | { | |
1c9a6bde | 867 | const struct argpar_opt_descr descrs[] = { |
903a5b8a | 868 | { 0, 'k', NULL, true }, |
1c9a6bde | 869 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
870 | }; |
871 | ||
872 | test_fail( | |
873 | "-k", | |
874 | "While parsing argument #1 (`-k`): Missing required argument for option `-k`", | |
2af370d0 | 875 | ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, |
903a5b8a SM |
876 | descrs); |
877 | } | |
878 | ||
879 | /* Missing short option argument (multiple glued) */ | |
880 | { | |
1c9a6bde | 881 | const struct argpar_opt_descr descrs[] = { |
903a5b8a SM |
882 | { 0, 'a', NULL, false }, |
883 | { 0, 'b', NULL, false }, | |
884 | { 0, 'c', NULL, true }, | |
1c9a6bde | 885 | ARGPAR_OPT_DESCR_SENTINEL |
903a5b8a SM |
886 | }; |
887 | ||
888 | test_fail( | |
889 | "-abc", | |
890 | "While parsing argument #1 (`-abc`): Missing required argument for option `-c`", | |
2af370d0 | 891 | ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, |
903a5b8a SM |
892 | descrs); |
893 | } | |
894 | ||
dd757a65 | 895 | /* Unexpected long option argument */ |
430fe886 SM |
896 | { |
897 | const struct argpar_opt_descr descrs[] = { | |
898 | { 0, 'c', "chevre", false }, | |
899 | ARGPAR_OPT_DESCR_SENTINEL | |
900 | }; | |
901 | ||
902 | test_fail( | |
903 | "--chevre=fromage", | |
904 | "While parsing argument #1 (`--chevre=fromage`): Unexpected argument for option `--chevre`", | |
2af370d0 | 905 | ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG, |
430fe886 SM |
906 | descrs); |
907 | } | |
903a5b8a SM |
908 | } |
909 | ||
910 | int main(void) | |
911 | { | |
d1f7bbdb | 912 | plan_tests(434); |
903a5b8a SM |
913 | succeed_tests(); |
914 | fail_tests(); | |
915 | return exit_status(); | |
916 | } |