| 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 <glib.h> |
| 21 | #include <stdbool.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "argpar/argpar.h" |
| 27 | #include "tap/tap.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 void append_to_res_str(GString * const res_str, const argpar_item_t * const item) |
| 41 | { |
| 42 | if (res_str->len > 0) { |
| 43 | g_string_append_c(res_str, ' '); |
| 44 | } |
| 45 | |
| 46 | switch (argpar_item_type(item)) { |
| 47 | case ARGPAR_ITEM_TYPE_OPT: |
| 48 | { |
| 49 | const argpar_opt_descr_t * const descr = argpar_item_opt_descr(item); |
| 50 | const char * const arg = argpar_item_opt_arg(item); |
| 51 | |
| 52 | if (descr->long_name) { |
| 53 | g_string_append_printf(res_str, "--%s", descr->long_name); |
| 54 | |
| 55 | if (arg) { |
| 56 | g_string_append_printf(res_str, "=%s", arg); |
| 57 | } |
| 58 | } else if (descr->short_name) { |
| 59 | g_string_append_printf(res_str, "-%c", descr->short_name); |
| 60 | |
| 61 | if (arg) { |
| 62 | g_string_append_printf(res_str, " %s", arg); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | break; |
| 67 | } |
| 68 | case ARGPAR_ITEM_TYPE_NON_OPT: |
| 69 | { |
| 70 | const char * const arg = argpar_item_non_opt_arg(item); |
| 71 | const unsigned int orig_index = argpar_item_non_opt_orig_index(item); |
| 72 | const unsigned int non_opt_index = argpar_item_non_opt_non_opt_index(item); |
| 73 | |
| 74 | g_string_append_printf(res_str, "%s<%u,%u>", arg, orig_index, non_opt_index); |
| 75 | break; |
| 76 | } |
| 77 | default: |
| 78 | abort(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Parses `cmdline` with the argpar API using the option descriptors |
| 84 | * `descrs`, and ensures that the resulting effective command line is |
| 85 | * `expected_cmd_line` and that the number of ingested original |
| 86 | * arguments is `expected_ingested_orig_args`. |
| 87 | * |
| 88 | * This function splits `cmdline` on spaces to create an original |
| 89 | * argument array. |
| 90 | * |
| 91 | * This function builds the resulting command line from parsing items |
| 92 | * by space-separating each formatted item (see append_to_res_str()). |
| 93 | */ |
| 94 | static void test_succeed(const char * const cmdline, const char * const expected_cmd_line, |
| 95 | const argpar_opt_descr_t * const descrs, |
| 96 | const unsigned int expected_ingested_orig_args) |
| 97 | { |
| 98 | argpar_iter_t *iter = NULL; |
| 99 | const argpar_item_t *item = NULL; |
| 100 | const argpar_error_t *error = NULL; |
| 101 | GString * const res_str = g_string_new(NULL); |
| 102 | gchar ** const argv = g_strsplit(cmdline, " ", 0); |
| 103 | unsigned int i, actual_ingested_orig_args; |
| 104 | |
| 105 | assert(argv); |
| 106 | assert(res_str); |
| 107 | iter = argpar_iter_create(g_strv_length(argv), (const char * const *) argv, descrs); |
| 108 | assert(iter); |
| 109 | |
| 110 | for (i = 0;; i++) { |
| 111 | argpar_iter_next_status_t status; |
| 112 | |
| 113 | ARGPAR_ITEM_DESTROY_AND_RESET(item); |
| 114 | status = argpar_iter_next(iter, &item, &error); |
| 115 | |
| 116 | ok(status == ARGPAR_ITER_NEXT_STATUS_OK || status == ARGPAR_ITER_NEXT_STATUS_END, |
| 117 | "argpar_iter_next() returns the expected status (%d) for command line `%s` (call %u)", |
| 118 | status, cmdline, i + 1); |
| 119 | ok(!error, "argpar_iter_next() doesn't set an error for command line `%s` (call %u)", |
| 120 | cmdline, i + 1); |
| 121 | |
| 122 | if (status == ARGPAR_ITER_NEXT_STATUS_END) { |
| 123 | ok(!item, |
| 124 | "argpar_iter_next() doesn't set an item for status `ARGPAR_ITER_NEXT_STATUS_END` " |
| 125 | "and command line `%s` (call %u)", |
| 126 | cmdline, i + 1); |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | append_to_res_str(res_str, item); |
| 131 | } |
| 132 | |
| 133 | actual_ingested_orig_args = argpar_iter_ingested_orig_args(iter); |
| 134 | ok(actual_ingested_orig_args == expected_ingested_orig_args, |
| 135 | "argpar_iter_ingested_orig_args() returns the expected number of ingested original " |
| 136 | "arguments for command line `%s`", |
| 137 | cmdline); |
| 138 | |
| 139 | if (actual_ingested_orig_args != expected_ingested_orig_args) { |
| 140 | diag("Expected: %u Got: %u", expected_ingested_orig_args, actual_ingested_orig_args); |
| 141 | } |
| 142 | |
| 143 | ok(strcmp(expected_cmd_line, res_str->str) == 0, |
| 144 | "argpar_iter_next() returns the expected parsing items for command line `%s`", cmdline); |
| 145 | |
| 146 | if (strcmp(expected_cmd_line, res_str->str) != 0) { |
| 147 | diag("Expected: `%s`", expected_cmd_line); |
| 148 | diag("Got: `%s`", res_str->str); |
| 149 | } |
| 150 | |
| 151 | argpar_item_destroy(item); |
| 152 | argpar_iter_destroy(iter); |
| 153 | assert(!error); |
| 154 | g_string_free(res_str, TRUE); |
| 155 | g_strfreev(argv); |
| 156 | } |
| 157 | |
| 158 | static void succeed_tests(void) |
| 159 | { |
| 160 | /* No arguments */ |
| 161 | { |
| 162 | const argpar_opt_descr_t descrs[] = {ARGPAR_OPT_DESCR_SENTINEL}; |
| 163 | |
| 164 | test_succeed("", "", descrs, 0); |
| 165 | } |
| 166 | |
| 167 | /* Single long option */ |
| 168 | { |
| 169 | const argpar_opt_descr_t descrs[] = {{0, '\0', "salut", false}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 170 | |
| 171 | test_succeed("--salut", "--salut", descrs, 1); |
| 172 | } |
| 173 | |
| 174 | /* Single short option */ |
| 175 | { |
| 176 | const argpar_opt_descr_t descrs[] = {{0, 'f', NULL, false}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 177 | |
| 178 | test_succeed("-f", "-f", descrs, 1); |
| 179 | } |
| 180 | |
| 181 | /* Short and long option (aliases) */ |
| 182 | { |
| 183 | const argpar_opt_descr_t descrs[] = {{0, 'f', "flaw", false}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 184 | |
| 185 | test_succeed("-f --flaw", "--flaw --flaw", descrs, 2); |
| 186 | } |
| 187 | |
| 188 | /* Long option with argument (space form) */ |
| 189 | { |
| 190 | const argpar_opt_descr_t descrs[] = {{0, '\0', "tooth", true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 191 | |
| 192 | test_succeed("--tooth 67", "--tooth=67", descrs, 2); |
| 193 | } |
| 194 | |
| 195 | /* Long option with argument (equal form) */ |
| 196 | { |
| 197 | const argpar_opt_descr_t descrs[] = {{0, '\0', "polish", true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 198 | |
| 199 | test_succeed("--polish=brick", "--polish=brick", descrs, 1); |
| 200 | } |
| 201 | |
| 202 | /* Short option with argument (space form) */ |
| 203 | { |
| 204 | const argpar_opt_descr_t descrs[] = {{0, 'c', NULL, true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 205 | |
| 206 | test_succeed("-c chilly", "-c chilly", descrs, 2); |
| 207 | } |
| 208 | |
| 209 | /* Short option with argument (glued form) */ |
| 210 | { |
| 211 | const argpar_opt_descr_t descrs[] = {{0, 'c', NULL, true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 212 | |
| 213 | test_succeed("-cchilly", "-c chilly", descrs, 1); |
| 214 | } |
| 215 | |
| 216 | /* Short and long option (aliases) with argument (all forms) */ |
| 217 | { |
| 218 | const argpar_opt_descr_t descrs[] = {{0, 'd', "dry", true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 219 | |
| 220 | test_succeed("--dry=rate -dthing --dry street --dry=shape", |
| 221 | "--dry=rate --dry=thing --dry=street --dry=shape", descrs, 5); |
| 222 | } |
| 223 | |
| 224 | /* Many short options, last one with argument (glued form) */ |
| 225 | { |
| 226 | const argpar_opt_descr_t descrs[] = {{0, 'd', NULL, false}, |
| 227 | {0, 'e', NULL, false}, |
| 228 | {0, 'f', NULL, true}, |
| 229 | ARGPAR_OPT_DESCR_SENTINEL}; |
| 230 | |
| 231 | test_succeed("-defmeow", "-d -e -f meow", descrs, 1); |
| 232 | } |
| 233 | |
| 234 | /* Many options */ |
| 235 | { |
| 236 | const argpar_opt_descr_t descrs[] = {{0, 'd', NULL, false}, |
| 237 | {0, 'e', "east", true}, |
| 238 | {0, '\0', "mind", false}, |
| 239 | ARGPAR_OPT_DESCR_SENTINEL}; |
| 240 | |
| 241 | test_succeed("-d --mind -destart --mind --east cough -d --east=itch", |
| 242 | "-d --mind -d --east=start --mind --east=cough -d --east=itch", descrs, 8); |
| 243 | } |
| 244 | |
| 245 | /* Single non-option argument */ |
| 246 | { |
| 247 | const argpar_opt_descr_t descrs[] = {ARGPAR_OPT_DESCR_SENTINEL}; |
| 248 | |
| 249 | test_succeed("kilojoule", "kilojoule<0,0>", descrs, 1); |
| 250 | } |
| 251 | |
| 252 | /* Two non-option arguments */ |
| 253 | { |
| 254 | const argpar_opt_descr_t descrs[] = {ARGPAR_OPT_DESCR_SENTINEL}; |
| 255 | |
| 256 | test_succeed("kilojoule mitaine", "kilojoule<0,0> mitaine<1,1>", descrs, 2); |
| 257 | } |
| 258 | |
| 259 | /* Single non-option argument mixed with options */ |
| 260 | { |
| 261 | const argpar_opt_descr_t descrs[] = {{0, 'd', NULL, false}, |
| 262 | {0, '\0', "squeeze", true}, |
| 263 | ARGPAR_OPT_DESCR_SENTINEL}; |
| 264 | |
| 265 | test_succeed("-d sprout yes --squeeze little bag -d", |
| 266 | "-d sprout<1,0> yes<2,1> --squeeze=little bag<5,2> -d", descrs, 7); |
| 267 | } |
| 268 | |
| 269 | /* Valid `---opt` */ |
| 270 | { |
| 271 | const argpar_opt_descr_t descrs[] = {{0, '\0', "-fuel", true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 272 | |
| 273 | test_succeed("---fuel=three", "---fuel=three", descrs, 1); |
| 274 | } |
| 275 | |
| 276 | /* Long option containing `=` in argument (equal form) */ |
| 277 | { |
| 278 | const argpar_opt_descr_t descrs[] = {{0, '\0', "zebra", true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 279 | |
| 280 | test_succeed("--zebra=three=yes", "--zebra=three=yes", descrs, 1); |
| 281 | } |
| 282 | |
| 283 | /* Short option's argument starting with `-` (glued form) */ |
| 284 | { |
| 285 | const argpar_opt_descr_t descrs[] = {{0, 'z', NULL, true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 286 | |
| 287 | test_succeed("-z-will", "-z -will", descrs, 1); |
| 288 | } |
| 289 | |
| 290 | /* Short option's argument starting with `-` (space form) */ |
| 291 | { |
| 292 | const argpar_opt_descr_t descrs[] = {{0, 'z', NULL, true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 293 | |
| 294 | test_succeed("-z -will", "-z -will", descrs, 2); |
| 295 | } |
| 296 | |
| 297 | /* Long option's argument starting with `-` (space form) */ |
| 298 | { |
| 299 | const argpar_opt_descr_t descrs[] = {{0, '\0', "janine", true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 300 | |
| 301 | test_succeed("--janine -sutto", "--janine=-sutto", descrs, 2); |
| 302 | } |
| 303 | |
| 304 | /* Long option's argument starting with `-` (equal form) */ |
| 305 | { |
| 306 | const argpar_opt_descr_t descrs[] = {{0, '\0', "janine", true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 307 | |
| 308 | test_succeed("--janine=-sutto", "--janine=-sutto", descrs, 1); |
| 309 | } |
| 310 | |
| 311 | /* Long option's empty argument (equal form) */ |
| 312 | { |
| 313 | const argpar_opt_descr_t descrs[] = {{0, 'f', NULL, false}, |
| 314 | {0, '\0', "yeah", true}, |
| 315 | ARGPAR_OPT_DESCR_SENTINEL}; |
| 316 | |
| 317 | test_succeed("-f --yeah= -f", "-f --yeah= -f", descrs, 3); |
| 318 | } |
| 319 | |
| 320 | /* `-` non-option argument */ |
| 321 | { |
| 322 | const argpar_opt_descr_t descrs[] = {{0, 'f', NULL, false}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 323 | |
| 324 | test_succeed("-f - -f", "-f -<1,0> -f", descrs, 3); |
| 325 | } |
| 326 | |
| 327 | /* `--` non-option argument */ |
| 328 | { |
| 329 | const argpar_opt_descr_t descrs[] = {{0, 'f', NULL, false}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 330 | |
| 331 | test_succeed("-f -- -f", "-f --<1,0> -f", descrs, 3); |
| 332 | } |
| 333 | |
| 334 | /* Very long name of long option */ |
| 335 | { |
| 336 | const char opt_name[] = "kale-chips-waistcoat-yr-bicycle-rights-gochujang-" |
| 337 | "woke-tumeric-flexitarian-biodiesel-chillwave-cliche-" |
| 338 | "ethical-cardigan-listicle-pok-pok-sustainable-live-" |
| 339 | "edge-jianbing-gochujang-butcher-disrupt-tattooed-" |
| 340 | "tumeric-prism-photo-booth-vape-kogi-jean-shorts-" |
| 341 | "blog-williamsburg-fingerstache-palo-santo-artisan-" |
| 342 | "affogato-occupy-skateboard-adaptogen-neutra-celiac-" |
| 343 | "put-a-bird-on-it-kombucha-everyday-carry-hot-chicken-" |
| 344 | "craft-beer-subway-tile-tote-bag-disrupt-selvage-" |
| 345 | "raclette-art-party-readymade-paleo-heirloom-trust-" |
| 346 | "fund-small-batch-kinfolk-woke-cardigan-prism-" |
| 347 | "chambray-la-croix-hashtag-unicorn-edison-bulb-tbh-" |
| 348 | "cornhole-cliche-tattooed-green-juice-adaptogen-" |
| 349 | "kitsch-lo-fi-vexillologist-migas-gentrify-" |
| 350 | "viral-raw-denim"; |
| 351 | const argpar_opt_descr_t descrs[] = {{0, '\0', opt_name, true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 352 | char cmdline[1024]; |
| 353 | |
| 354 | sprintf(cmdline, "--%s=23", opt_name); |
| 355 | test_succeed(cmdline, cmdline, descrs, 1); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Parses `cmdline` with the argpar API using the option descriptors |
| 361 | * `descrs`, and ensures that argpar_iter_next() fails with status |
| 362 | * `expected_status` and that it sets an error having: |
| 363 | * |
| 364 | * ‣ The original argument index `expected_orig_index`. |
| 365 | * |
| 366 | * ‣ If applicable: |
| 367 | * |
| 368 | * • The unknown option name `expected_unknown_opt_name`. |
| 369 | * |
| 370 | * • The option descriptor at index `expected_opt_descr_index` of |
| 371 | * `descrs`. |
| 372 | * |
| 373 | * • The option type `expected_is_short`. |
| 374 | * |
| 375 | * This function splits `cmdline` on spaces to create an original |
| 376 | * argument array. |
| 377 | */ |
| 378 | static void test_fail(const char * const cmdline, const argpar_error_type_t expected_error_type, |
| 379 | const unsigned int expected_orig_index, |
| 380 | const char * const expected_unknown_opt_name, |
| 381 | const unsigned int expected_opt_descr_index, const bool expected_is_short, |
| 382 | const argpar_opt_descr_t * const descrs) |
| 383 | { |
| 384 | argpar_iter_t *iter = NULL; |
| 385 | const argpar_item_t *item = NULL; |
| 386 | gchar ** const argv = g_strsplit(cmdline, " ", 0); |
| 387 | unsigned int i; |
| 388 | const argpar_error_t *error = NULL; |
| 389 | |
| 390 | iter = argpar_iter_create(g_strv_length(argv), (const char * const *) argv, descrs); |
| 391 | assert(iter); |
| 392 | |
| 393 | for (i = 0;; i++) { |
| 394 | argpar_iter_next_status_t status; |
| 395 | |
| 396 | ARGPAR_ITEM_DESTROY_AND_RESET(item); |
| 397 | status = argpar_iter_next(iter, &item, &error); |
| 398 | ok(status == ARGPAR_ITER_NEXT_STATUS_OK || |
| 399 | (status == ARGPAR_ITER_NEXT_STATUS_ERROR && |
| 400 | argpar_error_type(error) == expected_error_type), |
| 401 | "argpar_iter_next() returns the expected status and error type (%d) " |
| 402 | "for command line `%s` (call %u)", |
| 403 | expected_error_type, cmdline, i + 1); |
| 404 | |
| 405 | if (status != ARGPAR_ITER_NEXT_STATUS_OK) { |
| 406 | ok(!item, |
| 407 | "argpar_iter_next() doesn't set an item for other status than " |
| 408 | "`ARGPAR_ITER_NEXT_STATUS_OK` and command line `%s` (call %u)", |
| 409 | cmdline, i + 1); |
| 410 | ok(error, |
| 411 | "argpar_iter_next() sets an error for other status than " |
| 412 | "`ARGPAR_ITER_NEXT_STATUS_OK` and command line `%s` (call %u)", |
| 413 | cmdline, i + 1); |
| 414 | ok(argpar_error_orig_index(error) == expected_orig_index, |
| 415 | "argpar_iter_next() sets an error with the expected original argument index " |
| 416 | "for command line `%s` (call %u)", |
| 417 | cmdline, i + 1); |
| 418 | |
| 419 | if (argpar_error_type(error) == ARGPAR_ERROR_TYPE_UNKNOWN_OPT) { |
| 420 | ok(strcmp(argpar_error_unknown_opt_name(error), expected_unknown_opt_name) == 0, |
| 421 | "argpar_iter_next() sets an error with the expected unknown option name " |
| 422 | "for command line `%s` (call %u)", |
| 423 | cmdline, i + 1); |
| 424 | } else { |
| 425 | bool is_short; |
| 426 | |
| 427 | ok(argpar_error_opt_descr(error, &is_short) == &descrs[expected_opt_descr_index], |
| 428 | "argpar_iter_next() sets an error with the expected option descriptor " |
| 429 | "for command line `%s` (call %u)", |
| 430 | cmdline, i + 1); |
| 431 | ok(is_short == expected_is_short, |
| 432 | "argpar_iter_next() sets an error with the expected option type " |
| 433 | "for command line `%s` (call %u)", |
| 434 | cmdline, i + 1); |
| 435 | } |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | ok(item, |
| 440 | "argpar_iter_next() sets an item for status `ARGPAR_ITER_NEXT_STATUS_OK` " |
| 441 | "and command line `%s` (call %u)", |
| 442 | cmdline, i + 1); |
| 443 | ok(!error, |
| 444 | "argpar_iter_next() doesn't set an error for status `ARGPAR_ITER_NEXT_STATUS_OK` " |
| 445 | "and command line `%s` (call %u)", |
| 446 | cmdline, i + 1); |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | ok(strcmp(expected_error, error) == 0, |
| 451 | "argpar_iter_next() sets the expected error string " |
| 452 | "for command line `%s`", cmdline); |
| 453 | |
| 454 | if (strcmp(expected_error, error) != 0) { |
| 455 | diag("Expected: `%s`", expected_error); |
| 456 | diag("Got: `%s`", error); |
| 457 | } |
| 458 | */ |
| 459 | |
| 460 | argpar_item_destroy(item); |
| 461 | argpar_iter_destroy(iter); |
| 462 | argpar_error_destroy(error); |
| 463 | g_strfreev(argv); |
| 464 | } |
| 465 | |
| 466 | static void fail_tests(void) |
| 467 | { |
| 468 | /* Unknown short option (space form) */ |
| 469 | { |
| 470 | const argpar_opt_descr_t descrs[] = {{0, 'd', NULL, true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 471 | |
| 472 | test_fail("-d salut -e -d meow", ARGPAR_ERROR_TYPE_UNKNOWN_OPT, 2, "-e", 0, false, descrs); |
| 473 | } |
| 474 | |
| 475 | /* Unknown short option (glued form) */ |
| 476 | { |
| 477 | const argpar_opt_descr_t descrs[] = {{0, 'd', 0, true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 478 | |
| 479 | test_fail("-dsalut -e -d meow", ARGPAR_ERROR_TYPE_UNKNOWN_OPT, 1, "-e", 0, false, descrs); |
| 480 | } |
| 481 | |
| 482 | /* Unknown long option (space form) */ |
| 483 | { |
| 484 | const argpar_opt_descr_t descrs[] = {{0, '\0', "sink", true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 485 | |
| 486 | test_fail("--sink party --food --sink impulse", ARGPAR_ERROR_TYPE_UNKNOWN_OPT, 2, "--food", |
| 487 | 0, false, descrs); |
| 488 | } |
| 489 | |
| 490 | /* Unknown long option (equal form) */ |
| 491 | { |
| 492 | const argpar_opt_descr_t descrs[] = {{0, '\0', "sink", true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 493 | |
| 494 | test_fail("--sink=party --food --sink=impulse", ARGPAR_ERROR_TYPE_UNKNOWN_OPT, 1, "--food", |
| 495 | 0, false, descrs); |
| 496 | } |
| 497 | |
| 498 | /* Unknown option before non-option argument */ |
| 499 | { |
| 500 | const argpar_opt_descr_t descrs[] = {{0, '\0', "thumb", true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 501 | |
| 502 | test_fail("--thumb=party --food=18 bateau --thumb waves", ARGPAR_ERROR_TYPE_UNKNOWN_OPT, 1, |
| 503 | "--food", 0, false, descrs); |
| 504 | } |
| 505 | |
| 506 | /* Unknown option after non-option argument */ |
| 507 | { |
| 508 | const argpar_opt_descr_t descrs[] = {{0, '\0', "thumb", true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 509 | |
| 510 | test_fail("--thumb=party wound --food --thumb waves", ARGPAR_ERROR_TYPE_UNKNOWN_OPT, 2, |
| 511 | "--food", 0, false, descrs); |
| 512 | } |
| 513 | |
| 514 | /* Missing long option argument */ |
| 515 | { |
| 516 | const argpar_opt_descr_t descrs[] = {{0, '\0', "thumb", true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 517 | |
| 518 | test_fail("allo --thumb", ARGPAR_ERROR_TYPE_MISSING_OPT_ARG, 1, NULL, 0, false, descrs); |
| 519 | } |
| 520 | |
| 521 | /* Missing short option argument */ |
| 522 | { |
| 523 | const argpar_opt_descr_t descrs[] = {{0, 'k', NULL, true}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 524 | |
| 525 | test_fail("zoom heille -k", ARGPAR_ERROR_TYPE_MISSING_OPT_ARG, 2, NULL, 0, true, descrs); |
| 526 | } |
| 527 | |
| 528 | /* Missing short option argument (multiple glued) */ |
| 529 | { |
| 530 | const argpar_opt_descr_t descrs[] = {{0, 'a', NULL, false}, |
| 531 | {0, 'b', NULL, false}, |
| 532 | {0, 'c', NULL, true}, |
| 533 | ARGPAR_OPT_DESCR_SENTINEL}; |
| 534 | |
| 535 | test_fail("-abc", ARGPAR_ERROR_TYPE_MISSING_OPT_ARG, 0, NULL, 2, true, descrs); |
| 536 | } |
| 537 | |
| 538 | /* Unexpected long option argument */ |
| 539 | { |
| 540 | const argpar_opt_descr_t descrs[] = {{0, 'c', "chevre", false}, ARGPAR_OPT_DESCR_SENTINEL}; |
| 541 | |
| 542 | test_fail("ambulance --chevre=fromage tar -cjv", ARGPAR_ERROR_TYPE_UNEXPECTED_OPT_ARG, 1, |
| 543 | NULL, 0, false, descrs); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | int main(void) |
| 548 | { |
| 549 | plan_tests(309); |
| 550 | succeed_tests(); |
| 551 | fail_tests(); |
| 552 | return exit_status(); |
| 553 | } |