| 1 | /* |
| 2 | * Copyright (c) 2019-2021 Philippe Proulx <pproulx@efficios.com> |
| 3 | * Copyright (c) 2020-2021 Simon Marchi <simon.marchi@efficios.com> |
| 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 | |
| 19 | #include <assert.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <glib.h> |
| 25 | |
| 26 | #include "tap/tap.h" |
| 27 | #include "argpar/argpar.h" |
| 28 | |
| 29 | /* |
| 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. |
| 39 | */ |
| 40 | static |
| 41 | void append_to_res_str(GString * const res_str, |
| 42 | const struct argpar_item * const item) |
| 43 | { |
| 44 | if (res_str->len > 0) { |
| 45 | g_string_append_c(res_str, ' '); |
| 46 | } |
| 47 | |
| 48 | switch (argpar_item_type(item)) { |
| 49 | case ARGPAR_ITEM_TYPE_OPT: |
| 50 | { |
| 51 | const struct argpar_opt_descr * const descr = |
| 52 | argpar_item_opt_descr(item); |
| 53 | const char * const arg = argpar_item_opt_arg(item); |
| 54 | |
| 55 | if (descr->long_name) { |
| 56 | g_string_append_printf(res_str, "--%s", |
| 57 | descr->long_name); |
| 58 | |
| 59 | if (arg) { |
| 60 | g_string_append_printf(res_str, "=%s", arg); |
| 61 | } |
| 62 | } else if (descr->short_name) { |
| 63 | g_string_append_printf(res_str, "-%c", |
| 64 | descr->short_name); |
| 65 | |
| 66 | if (arg) { |
| 67 | g_string_append_printf(res_str, " %s", arg); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | break; |
| 72 | } |
| 73 | case ARGPAR_ITEM_TYPE_NON_OPT: |
| 74 | { |
| 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); |
| 80 | |
| 81 | g_string_append_printf(res_str, "%s<%u,%u>", arg, orig_index, |
| 82 | non_opt_index); |
| 83 | break; |
| 84 | } |
| 85 | default: |
| 86 | abort(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Parses `cmdline` with the argpar API 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 | */ |
| 102 | static |
| 103 | void test_succeed(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) |
| 107 | { |
| 108 | struct argpar_iter *iter = NULL; |
| 109 | const struct argpar_item *item = NULL; |
| 110 | char *error = NULL; |
| 111 | GString * const res_str = g_string_new(NULL); |
| 112 | gchar ** const argv = g_strsplit(cmdline, " ", 0); |
| 113 | unsigned int i, actual_ingested_orig_args; |
| 114 | |
| 115 | assert(argv); |
| 116 | assert(res_str); |
| 117 | iter = argpar_iter_create(g_strv_length(argv), |
| 118 | (const char * const *) argv, descrs); |
| 119 | assert(iter); |
| 120 | |
| 121 | for (i = 0; ; i++) { |
| 122 | enum argpar_iter_next_status status; |
| 123 | |
| 124 | ARGPAR_ITEM_DESTROY_AND_RESET(item); |
| 125 | status = argpar_iter_next(iter, &item, &error); |
| 126 | |
| 127 | ok(status == ARGPAR_ITER_NEXT_STATUS_OK || |
| 128 | status == ARGPAR_ITER_NEXT_STATUS_END || |
| 129 | status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, |
| 130 | "argpar_iter_next() returns the expected status " |
| 131 | "(%d) for command line `%s` (call %u)", |
| 132 | status, cmdline, i + 1); |
| 133 | |
| 134 | if (status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT) { |
| 135 | ok(error, |
| 136 | "argpar_iter_next() sets an error for " |
| 137 | "status `ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT` " |
| 138 | "and command line `%s` (call %u)", |
| 139 | cmdline, i + 1); |
| 140 | } else { |
| 141 | ok(!error, |
| 142 | "argpar_iter_next() doesn't set an error " |
| 143 | "for other status than " |
| 144 | "`ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT` " |
| 145 | "and command line `%s` (call %u)", |
| 146 | cmdline, i + 1); |
| 147 | } |
| 148 | |
| 149 | if (status == ARGPAR_ITER_NEXT_STATUS_END || |
| 150 | status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT) { |
| 151 | ok(!item, |
| 152 | "argpar_iter_next() doesn't set an item " |
| 153 | "for status `ARGPAR_ITER_NEXT_STATUS_END` " |
| 154 | "or `ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT` " |
| 155 | "and command line `%s` (call %u)", |
| 156 | cmdline, i + 1); |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | append_to_res_str(res_str, item); |
| 161 | } |
| 162 | |
| 163 | actual_ingested_orig_args = argpar_iter_ingested_orig_args(iter); |
| 164 | ok(actual_ingested_orig_args == expected_ingested_orig_args, |
| 165 | "argpar_iter_ingested_orig_args() returns the expected " |
| 166 | "number of ingested original arguments for command line `%s`", |
| 167 | cmdline); |
| 168 | |
| 169 | if (actual_ingested_orig_args != expected_ingested_orig_args) { |
| 170 | diag("Expected: %u Got: %u", expected_ingested_orig_args, |
| 171 | actual_ingested_orig_args); |
| 172 | } |
| 173 | |
| 174 | ok(strcmp(expected_cmd_line, res_str->str) == 0, |
| 175 | "argpar_iter_next() returns the expected parsing items " |
| 176 | "for command line `%s`", cmdline); |
| 177 | |
| 178 | if (strcmp(expected_cmd_line, res_str->str) != 0) { |
| 179 | diag("Expected: `%s`", expected_cmd_line); |
| 180 | diag("Got: `%s`", res_str->str); |
| 181 | } |
| 182 | |
| 183 | argpar_item_destroy(item); |
| 184 | argpar_iter_destroy(iter); |
| 185 | g_string_free(res_str, TRUE); |
| 186 | g_strfreev(argv); |
| 187 | free(error); |
| 188 | } |
| 189 | |
| 190 | static |
| 191 | void succeed_tests(void) |
| 192 | { |
| 193 | /* No arguments */ |
| 194 | { |
| 195 | const struct argpar_opt_descr descrs[] = { |
| 196 | ARGPAR_OPT_DESCR_SENTINEL |
| 197 | }; |
| 198 | |
| 199 | test_succeed( |
| 200 | "", |
| 201 | "", |
| 202 | descrs, 0); |
| 203 | } |
| 204 | |
| 205 | /* Single long option */ |
| 206 | { |
| 207 | const struct argpar_opt_descr descrs[] = { |
| 208 | { 0, '\0', "salut", false }, |
| 209 | ARGPAR_OPT_DESCR_SENTINEL |
| 210 | }; |
| 211 | |
| 212 | test_succeed( |
| 213 | "--salut", |
| 214 | "--salut", |
| 215 | descrs, 1); |
| 216 | } |
| 217 | |
| 218 | /* Single short option */ |
| 219 | { |
| 220 | const struct argpar_opt_descr descrs[] = { |
| 221 | { 0, 'f', NULL, false }, |
| 222 | ARGPAR_OPT_DESCR_SENTINEL |
| 223 | }; |
| 224 | |
| 225 | test_succeed( |
| 226 | "-f", |
| 227 | "-f", |
| 228 | descrs, 1); |
| 229 | } |
| 230 | |
| 231 | /* Short and long option (aliases) */ |
| 232 | { |
| 233 | const struct argpar_opt_descr descrs[] = { |
| 234 | { 0, 'f', "flaw", false }, |
| 235 | ARGPAR_OPT_DESCR_SENTINEL |
| 236 | }; |
| 237 | |
| 238 | test_succeed( |
| 239 | "-f --flaw", |
| 240 | "--flaw --flaw", |
| 241 | descrs, 2); |
| 242 | } |
| 243 | |
| 244 | /* Long option with argument (space form) */ |
| 245 | { |
| 246 | const struct argpar_opt_descr descrs[] = { |
| 247 | { 0, '\0', "tooth", true }, |
| 248 | ARGPAR_OPT_DESCR_SENTINEL |
| 249 | }; |
| 250 | |
| 251 | test_succeed( |
| 252 | "--tooth 67", |
| 253 | "--tooth=67", |
| 254 | descrs, 2); |
| 255 | } |
| 256 | |
| 257 | /* Long option with argument (equal form) */ |
| 258 | { |
| 259 | const struct argpar_opt_descr descrs[] = { |
| 260 | { 0, '\0', "polish", true }, |
| 261 | ARGPAR_OPT_DESCR_SENTINEL |
| 262 | }; |
| 263 | |
| 264 | test_succeed( |
| 265 | "--polish=brick", |
| 266 | "--polish=brick", |
| 267 | descrs, 1); |
| 268 | } |
| 269 | |
| 270 | /* Short option with argument (space form) */ |
| 271 | { |
| 272 | const struct argpar_opt_descr descrs[] = { |
| 273 | { 0, 'c', NULL, true }, |
| 274 | ARGPAR_OPT_DESCR_SENTINEL |
| 275 | }; |
| 276 | |
| 277 | test_succeed( |
| 278 | "-c chilly", |
| 279 | "-c chilly", |
| 280 | descrs, 2); |
| 281 | } |
| 282 | |
| 283 | /* Short option with argument (glued form) */ |
| 284 | { |
| 285 | const struct argpar_opt_descr descrs[] = { |
| 286 | { 0, 'c', NULL, true }, |
| 287 | ARGPAR_OPT_DESCR_SENTINEL |
| 288 | }; |
| 289 | |
| 290 | test_succeed( |
| 291 | "-cchilly", |
| 292 | "-c chilly", |
| 293 | descrs, 1); |
| 294 | } |
| 295 | |
| 296 | /* Short and long option (aliases) with argument (all forms) */ |
| 297 | { |
| 298 | const struct argpar_opt_descr descrs[] = { |
| 299 | { 0, 'd', "dry", true }, |
| 300 | ARGPAR_OPT_DESCR_SENTINEL |
| 301 | }; |
| 302 | |
| 303 | test_succeed( |
| 304 | "--dry=rate -dthing --dry street --dry=shape", |
| 305 | "--dry=rate --dry=thing --dry=street --dry=shape", |
| 306 | descrs, 5); |
| 307 | } |
| 308 | |
| 309 | /* Many short options, last one with argument (glued form) */ |
| 310 | { |
| 311 | const struct argpar_opt_descr descrs[] = { |
| 312 | { 0, 'd', NULL, false }, |
| 313 | { 0, 'e', NULL, false }, |
| 314 | { 0, 'f', NULL, true }, |
| 315 | ARGPAR_OPT_DESCR_SENTINEL |
| 316 | }; |
| 317 | |
| 318 | test_succeed( |
| 319 | "-defmeow", |
| 320 | "-d -e -f meow", |
| 321 | descrs, 1); |
| 322 | } |
| 323 | |
| 324 | /* Many options */ |
| 325 | { |
| 326 | const struct argpar_opt_descr descrs[] = { |
| 327 | { 0, 'd', NULL, false }, |
| 328 | { 0, 'e', "east", true }, |
| 329 | { 0, '\0', "mind", false }, |
| 330 | ARGPAR_OPT_DESCR_SENTINEL |
| 331 | }; |
| 332 | |
| 333 | test_succeed( |
| 334 | "-d --mind -destart --mind --east cough -d --east=itch", |
| 335 | "-d --mind -d --east=start --mind --east=cough -d --east=itch", |
| 336 | descrs, 8); |
| 337 | } |
| 338 | |
| 339 | /* Single non-option argument */ |
| 340 | { |
| 341 | const struct argpar_opt_descr descrs[] = { |
| 342 | ARGPAR_OPT_DESCR_SENTINEL |
| 343 | }; |
| 344 | |
| 345 | test_succeed( |
| 346 | "kilojoule", |
| 347 | "kilojoule<0,0>", |
| 348 | descrs, 1); |
| 349 | } |
| 350 | |
| 351 | /* Two non-option arguments */ |
| 352 | { |
| 353 | const struct argpar_opt_descr descrs[] = { |
| 354 | ARGPAR_OPT_DESCR_SENTINEL |
| 355 | }; |
| 356 | |
| 357 | test_succeed( |
| 358 | "kilojoule mitaine", |
| 359 | "kilojoule<0,0> mitaine<1,1>", |
| 360 | descrs, 2); |
| 361 | } |
| 362 | |
| 363 | /* Single non-option argument mixed with options */ |
| 364 | { |
| 365 | const struct argpar_opt_descr descrs[] = { |
| 366 | { 0, 'd', NULL, false }, |
| 367 | { 0, '\0', "squeeze", true }, |
| 368 | ARGPAR_OPT_DESCR_SENTINEL |
| 369 | }; |
| 370 | |
| 371 | test_succeed( |
| 372 | "-d sprout yes --squeeze little bag -d", |
| 373 | "-d sprout<1,0> yes<2,1> --squeeze=little bag<5,2> -d", |
| 374 | descrs, 7); |
| 375 | } |
| 376 | |
| 377 | /* Unknown short option (space form) */ |
| 378 | { |
| 379 | const struct argpar_opt_descr descrs[] = { |
| 380 | { 0, 'd', NULL, true }, |
| 381 | ARGPAR_OPT_DESCR_SENTINEL |
| 382 | }; |
| 383 | |
| 384 | test_succeed( |
| 385 | "-d salut -e -d meow", |
| 386 | "-d salut", |
| 387 | descrs, 2); |
| 388 | } |
| 389 | |
| 390 | /* Unknown short option (glued form) */ |
| 391 | { |
| 392 | const struct argpar_opt_descr descrs[] = { |
| 393 | { 0, 'd', NULL, true }, |
| 394 | ARGPAR_OPT_DESCR_SENTINEL |
| 395 | }; |
| 396 | |
| 397 | test_succeed( |
| 398 | "-dsalut -e -d meow", |
| 399 | "-d salut", |
| 400 | descrs, 1); |
| 401 | } |
| 402 | |
| 403 | /* Unknown long option (space form) */ |
| 404 | { |
| 405 | const struct argpar_opt_descr descrs[] = { |
| 406 | { 0, '\0', "sink", true }, |
| 407 | ARGPAR_OPT_DESCR_SENTINEL |
| 408 | }; |
| 409 | |
| 410 | test_succeed( |
| 411 | "--sink party --food --sink impulse", |
| 412 | "--sink=party", |
| 413 | descrs, 2); |
| 414 | } |
| 415 | |
| 416 | /* Unknown long option (equal form) */ |
| 417 | { |
| 418 | const struct argpar_opt_descr descrs[] = { |
| 419 | { 0, '\0', "sink", true }, |
| 420 | ARGPAR_OPT_DESCR_SENTINEL |
| 421 | }; |
| 422 | |
| 423 | test_succeed( |
| 424 | "--sink=party --food --sink=impulse", |
| 425 | "--sink=party", |
| 426 | descrs, 1); |
| 427 | } |
| 428 | |
| 429 | /* Unknown option before non-option argument */ |
| 430 | { |
| 431 | const struct argpar_opt_descr descrs[] = { |
| 432 | { 0, '\0', "thumb", true }, |
| 433 | ARGPAR_OPT_DESCR_SENTINEL |
| 434 | }; |
| 435 | |
| 436 | test_succeed( |
| 437 | "--thumb=party --food bateau --thumb waves", |
| 438 | "--thumb=party", |
| 439 | descrs, 1); |
| 440 | } |
| 441 | |
| 442 | /* Unknown option after non-option argument */ |
| 443 | { |
| 444 | const struct argpar_opt_descr descrs[] = { |
| 445 | { 0, '\0', "thumb", true }, |
| 446 | ARGPAR_OPT_DESCR_SENTINEL |
| 447 | }; |
| 448 | |
| 449 | test_succeed( |
| 450 | "--thumb=party wound --food --thumb waves", |
| 451 | "--thumb=party wound<1,0>", |
| 452 | descrs, 2); |
| 453 | } |
| 454 | |
| 455 | /* Valid `---opt` */ |
| 456 | { |
| 457 | const struct argpar_opt_descr descrs[] = { |
| 458 | { 0, '\0', "-fuel", true }, |
| 459 | ARGPAR_OPT_DESCR_SENTINEL |
| 460 | }; |
| 461 | |
| 462 | test_succeed( |
| 463 | "---fuel=three", |
| 464 | "---fuel=three", |
| 465 | descrs, 1); |
| 466 | } |
| 467 | |
| 468 | /* Long option containing `=` in argument (equal form) */ |
| 469 | { |
| 470 | const struct argpar_opt_descr descrs[] = { |
| 471 | { 0, '\0', "zebra", true }, |
| 472 | ARGPAR_OPT_DESCR_SENTINEL |
| 473 | }; |
| 474 | |
| 475 | test_succeed( |
| 476 | "--zebra=three=yes", |
| 477 | "--zebra=three=yes", |
| 478 | descrs, 1); |
| 479 | } |
| 480 | |
| 481 | /* Short option's argument starting with `-` (glued form) */ |
| 482 | { |
| 483 | const struct argpar_opt_descr descrs[] = { |
| 484 | { 0, 'z', NULL, true }, |
| 485 | ARGPAR_OPT_DESCR_SENTINEL |
| 486 | }; |
| 487 | |
| 488 | test_succeed( |
| 489 | "-z-will", |
| 490 | "-z -will", |
| 491 | descrs, 1); |
| 492 | } |
| 493 | |
| 494 | /* Short option's argument starting with `-` (space form) */ |
| 495 | { |
| 496 | const struct argpar_opt_descr descrs[] = { |
| 497 | { 0, 'z', NULL, true }, |
| 498 | ARGPAR_OPT_DESCR_SENTINEL |
| 499 | }; |
| 500 | |
| 501 | test_succeed( |
| 502 | "-z -will", |
| 503 | "-z -will", |
| 504 | descrs, 2); |
| 505 | } |
| 506 | |
| 507 | /* Long option's argument starting with `-` (space form) */ |
| 508 | { |
| 509 | const struct argpar_opt_descr descrs[] = { |
| 510 | { 0, '\0', "janine", true }, |
| 511 | ARGPAR_OPT_DESCR_SENTINEL |
| 512 | }; |
| 513 | |
| 514 | test_succeed( |
| 515 | "--janine -sutto", |
| 516 | "--janine=-sutto", |
| 517 | descrs, 2); |
| 518 | } |
| 519 | |
| 520 | /* Long option's argument starting with `-` (equal form) */ |
| 521 | { |
| 522 | const struct argpar_opt_descr descrs[] = { |
| 523 | { 0, '\0', "janine", true }, |
| 524 | ARGPAR_OPT_DESCR_SENTINEL |
| 525 | }; |
| 526 | |
| 527 | test_succeed( |
| 528 | "--janine=-sutto", |
| 529 | "--janine=-sutto", |
| 530 | descrs, 1); |
| 531 | } |
| 532 | |
| 533 | /* Long option's empty argument (equal form) */ |
| 534 | { |
| 535 | const struct argpar_opt_descr descrs[] = { |
| 536 | { 0, 'f', NULL, false }, |
| 537 | { 0, '\0', "yeah", true }, |
| 538 | ARGPAR_OPT_DESCR_SENTINEL |
| 539 | }; |
| 540 | |
| 541 | test_succeed( |
| 542 | "-f --yeah= -f", |
| 543 | "-f --yeah= -f", |
| 544 | descrs, 3); |
| 545 | } |
| 546 | |
| 547 | /* `-` non-option argument */ |
| 548 | { |
| 549 | const struct argpar_opt_descr descrs[] = { |
| 550 | { 0, 'f', NULL, false }, |
| 551 | ARGPAR_OPT_DESCR_SENTINEL |
| 552 | }; |
| 553 | |
| 554 | test_succeed( |
| 555 | "-f - -f", |
| 556 | "-f -<1,0> -f", |
| 557 | descrs, 3); |
| 558 | } |
| 559 | |
| 560 | /* `--` non-option argument */ |
| 561 | { |
| 562 | const struct argpar_opt_descr descrs[] = { |
| 563 | { 0, 'f', NULL, false }, |
| 564 | ARGPAR_OPT_DESCR_SENTINEL |
| 565 | }; |
| 566 | |
| 567 | test_succeed( |
| 568 | "-f -- -f", |
| 569 | "-f --<1,0> -f", |
| 570 | descrs, 3); |
| 571 | } |
| 572 | |
| 573 | /* Very long name of long option */ |
| 574 | { |
| 575 | const char opt_name[] = |
| 576 | "kale-chips-waistcoat-yr-bicycle-rights-gochujang-" |
| 577 | "woke-tumeric-flexitarian-biodiesel-chillwave-cliche-" |
| 578 | "ethical-cardigan-listicle-pok-pok-sustainable-live-" |
| 579 | "edge-jianbing-gochujang-butcher-disrupt-tattooed-" |
| 580 | "tumeric-prism-photo-booth-vape-kogi-jean-shorts-" |
| 581 | "blog-williamsburg-fingerstache-palo-santo-artisan-" |
| 582 | "affogato-occupy-skateboard-adaptogen-neutra-celiac-" |
| 583 | "put-a-bird-on-it-kombucha-everyday-carry-hot-chicken-" |
| 584 | "craft-beer-subway-tile-tote-bag-disrupt-selvage-" |
| 585 | "raclette-art-party-readymade-paleo-heirloom-trust-" |
| 586 | "fund-small-batch-kinfolk-woke-cardigan-prism-" |
| 587 | "chambray-la-croix-hashtag-unicorn-edison-bulb-tbh-" |
| 588 | "cornhole-cliche-tattooed-green-juice-adaptogen-" |
| 589 | "kitsch-lo-fi-vexillologist-migas-gentrify-" |
| 590 | "viral-raw-denim"; |
| 591 | const struct argpar_opt_descr descrs[] = { |
| 592 | { 0, '\0', opt_name, true }, |
| 593 | ARGPAR_OPT_DESCR_SENTINEL |
| 594 | }; |
| 595 | char cmdline[1024]; |
| 596 | |
| 597 | sprintf(cmdline, "--%s=23", opt_name); |
| 598 | test_succeed(cmdline, cmdline, descrs, 1); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Parses `cmdline` with the argpar API using the option descriptors |
| 604 | * `descrs`, and ensures that argpar_iter_next() fails with status |
| 605 | * `expected_status` and that it sets an error which is equal to |
| 606 | * `expected_error`. |
| 607 | * |
| 608 | * This function splits `cmdline` on spaces to create an original |
| 609 | * argument array. |
| 610 | */ |
| 611 | static |
| 612 | void test_fail(const char * const cmdline, const char * const expected_error, |
| 613 | const enum argpar_iter_next_status expected_status, |
| 614 | const struct argpar_opt_descr * const descrs) |
| 615 | { |
| 616 | struct argpar_iter *iter = NULL; |
| 617 | const struct argpar_item *item = NULL; |
| 618 | gchar ** const argv = g_strsplit(cmdline, " ", 0); |
| 619 | unsigned int i; |
| 620 | char *error = NULL; |
| 621 | |
| 622 | iter = argpar_iter_create(g_strv_length(argv), |
| 623 | (const char * const *) argv, descrs); |
| 624 | assert(iter); |
| 625 | |
| 626 | for (i = 0; ; i++) { |
| 627 | enum argpar_iter_next_status status; |
| 628 | |
| 629 | ARGPAR_ITEM_DESTROY_AND_RESET(item); |
| 630 | status = argpar_iter_next(iter, &item, &error); |
| 631 | ok(status == ARGPAR_ITER_NEXT_STATUS_OK || |
| 632 | status == expected_status, |
| 633 | "argpar_iter_next() returns the expected status " |
| 634 | "(%d) for command line `%s` (call %u)", |
| 635 | status, cmdline, i + 1); |
| 636 | |
| 637 | if (status != ARGPAR_ITER_NEXT_STATUS_OK) { |
| 638 | ok(!item, |
| 639 | "argpar_iter_next() doesn't set an item " |
| 640 | "for other status than " |
| 641 | "`ARGPAR_ITER_NEXT_STATUS_OK` " |
| 642 | "and command line `%s` (call %u)", |
| 643 | cmdline, i + 1); |
| 644 | ok(error, |
| 645 | "argpar_iter_next() sets an error for " |
| 646 | "other status than " |
| 647 | " `ARGPAR_ITER_NEXT_STATUS_OK` " |
| 648 | "and command line `%s` (call %u)", |
| 649 | cmdline, i + 1); |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | ok(item, |
| 654 | "argpar_iter_next() sets an item for status " |
| 655 | "`ARGPAR_ITER_NEXT_STATUS_OK` " |
| 656 | "and command line `%s` (call %u)", |
| 657 | cmdline, i + 1); |
| 658 | ok(!error, |
| 659 | "argpar_iter_next() doesn't set an error for status " |
| 660 | "`ARGPAR_ITER_NEXT_STATUS_OK` " |
| 661 | "and command line `%s` (call %u)", |
| 662 | cmdline, i + 1); |
| 663 | } |
| 664 | |
| 665 | ok(strcmp(expected_error, error) == 0, |
| 666 | "argpar_iter_next() sets the expected error string " |
| 667 | "for command line `%s`", cmdline); |
| 668 | |
| 669 | if (strcmp(expected_error, error) != 0) { |
| 670 | diag("Expected: `%s`", expected_error); |
| 671 | diag("Got: `%s`", error); |
| 672 | } |
| 673 | |
| 674 | argpar_item_destroy(item); |
| 675 | argpar_iter_destroy(iter); |
| 676 | free(error); |
| 677 | g_strfreev(argv); |
| 678 | } |
| 679 | |
| 680 | static |
| 681 | void fail_tests(void) |
| 682 | { |
| 683 | /* Unknown long option */ |
| 684 | { |
| 685 | const struct argpar_opt_descr descrs[] = { |
| 686 | { 0, '\0', "thumb", true }, |
| 687 | ARGPAR_OPT_DESCR_SENTINEL |
| 688 | }; |
| 689 | |
| 690 | test_fail( |
| 691 | "--thumb=party --meow", |
| 692 | "While parsing argument #2 (`--meow`): Unknown option `--meow`", |
| 693 | ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, |
| 694 | descrs); |
| 695 | } |
| 696 | |
| 697 | /* Unknown short option */ |
| 698 | { |
| 699 | const struct argpar_opt_descr descrs[] = { |
| 700 | { 0, '\0', "thumb", true }, |
| 701 | ARGPAR_OPT_DESCR_SENTINEL |
| 702 | }; |
| 703 | |
| 704 | test_fail( |
| 705 | "--thumb=party -x", |
| 706 | "While parsing argument #2 (`-x`): Unknown option `-x`", |
| 707 | ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, |
| 708 | descrs); |
| 709 | } |
| 710 | |
| 711 | /* Missing long option argument */ |
| 712 | { |
| 713 | const struct argpar_opt_descr descrs[] = { |
| 714 | { 0, '\0', "thumb", true }, |
| 715 | ARGPAR_OPT_DESCR_SENTINEL |
| 716 | }; |
| 717 | |
| 718 | test_fail( |
| 719 | "--thumb", |
| 720 | "While parsing argument #1 (`--thumb`): Missing required argument for option `--thumb`", |
| 721 | ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, |
| 722 | descrs); |
| 723 | } |
| 724 | |
| 725 | /* Missing short option argument */ |
| 726 | { |
| 727 | const struct argpar_opt_descr descrs[] = { |
| 728 | { 0, 'k', NULL, true }, |
| 729 | ARGPAR_OPT_DESCR_SENTINEL |
| 730 | }; |
| 731 | |
| 732 | test_fail( |
| 733 | "-k", |
| 734 | "While parsing argument #1 (`-k`): Missing required argument for option `-k`", |
| 735 | ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, |
| 736 | descrs); |
| 737 | } |
| 738 | |
| 739 | /* Missing short option argument (multiple glued) */ |
| 740 | { |
| 741 | const struct argpar_opt_descr descrs[] = { |
| 742 | { 0, 'a', NULL, false }, |
| 743 | { 0, 'b', NULL, false }, |
| 744 | { 0, 'c', NULL, true }, |
| 745 | ARGPAR_OPT_DESCR_SENTINEL |
| 746 | }; |
| 747 | |
| 748 | test_fail( |
| 749 | "-abc", |
| 750 | "While parsing argument #1 (`-abc`): Missing required argument for option `-c`", |
| 751 | ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, |
| 752 | descrs); |
| 753 | } |
| 754 | |
| 755 | /* Unexpected long option argument */ |
| 756 | { |
| 757 | const struct argpar_opt_descr descrs[] = { |
| 758 | { 0, 'c', "chevre", false }, |
| 759 | ARGPAR_OPT_DESCR_SENTINEL |
| 760 | }; |
| 761 | |
| 762 | test_fail( |
| 763 | "--chevre=fromage", |
| 764 | "While parsing argument #1 (`--chevre=fromage`): Unexpected argument for option `--chevre`", |
| 765 | ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG, |
| 766 | descrs); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | int main(void) |
| 771 | { |
| 772 | plan_tests(296); |
| 773 | succeed_tests(); |
| 774 | fail_tests(); |
| 775 | return exit_status(); |
| 776 | } |