Line data Source code
1 : #include "test.h"
2 :
3 : static struct margs_tests args;
4 :
5 : /* Testing all possibilities with a NULL args somewhere */
6 2 : TEST(opts_NULL) {
7 2 : char *tab[] = {"test", "test2"};
8 : mopts_t t;
9 2 : mlist_t *lst = NULL;;
10 :
11 : /* 0 0 0 0 */
12 2 : TEST_ASSERT(read_opt(0, NULL, NULL, NULL) == 0,
13 : "Not handling null arguments");
14 : /* 0 0 0 1 */
15 2 : TEST_ASSERT(read_opt(0, NULL, NULL, &lst) == 0,
16 : "Not handling null arguments");
17 : /* 0 0 1 0 */
18 2 : TEST_ASSERT(read_opt(0, NULL, &t, NULL) == 0,
19 : "Not handling null arguments");
20 : /* 0 0 1 1 */
21 2 : TEST_ASSERT(read_opt(0, NULL, &t, &lst) == 0,
22 : "Not handling null arguments");
23 : /* 0 1 0 0 */
24 2 : TEST_ASSERT(read_opt(0, tab, NULL, NULL) == 0,
25 : "Not handling null arguments");
26 : /* 0 1 0 1 */
27 2 : TEST_ASSERT(read_opt(0, tab, NULL, &lst) == 0,
28 : "Not handling null arguments");
29 : /* 0 1 1 0 */
30 2 : TEST_ASSERT(read_opt(0, tab, &t, NULL) == 0,
31 : "Not handling null arguments");
32 : /* 0 1 1 1 */
33 2 : TEST_ASSERT(read_opt(0, tab, &t, &lst) == 0,
34 : "Not handling null arguments");
35 : /* 1 0 0 0 */
36 2 : TEST_ASSERT(read_opt(10, NULL, NULL, NULL) == 0,
37 : "Not handling null arguments");
38 : /* 1 0 0 1 */
39 2 : TEST_ASSERT(read_opt(10, NULL, NULL, &lst) == 0,
40 : "Not handling null arguments");
41 : /* 1 0 1 0 */
42 2 : TEST_ASSERT(read_opt(10, NULL, &t, NULL) == 0,
43 : "Not handling null arguments");
44 : /* 1 0 1 1 */
45 2 : TEST_ASSERT(read_opt(10, NULL, &t, &lst) == 0,
46 : "Not handling null arguments");
47 : /* 1 1 0 0 */
48 2 : TEST_ASSERT(read_opt(10, tab, NULL, NULL) == 0,
49 : "Not handling null arguments");
50 : /* 1 1 0 1 */
51 2 : TEST_ASSERT(read_opt(10, tab, NULL, &lst) == 0,
52 : "Not handling null arguments");
53 : /* 1 1 1 0 */
54 2 : TEST_ASSERT(read_opt(10, tab, &t, NULL) == 0,
55 : "Not handling null arguments");
56 :
57 2 : return TEST_SUCCESS;
58 : }
59 :
60 : /* Testing with av being an array full of empty strings */
61 2 : TEST(opts_empty_1) {
62 : mopts_t t;
63 2 : char *tab[] = {"", "", ""};
64 2 : mlist_t *lst = NULL;;
65 :
66 2 : TEST_ASSERT(read_opt(3, tab, &t, &lst) == 0, "Not handling null arguments");
67 2 : return TEST_SUCCESS;
68 : }
69 :
70 : /* Testing with av being an array full of NULLs */
71 2 : TEST(opts_empty_2) {
72 : mopts_t t;
73 2 : char *tab[] = {NULL, NULL, NULL};
74 2 : mlist_t *lst = NULL;;
75 :
76 2 : TEST_ASSERT(read_opt(3, tab, &t, &lst) == 0, "Not handling null arguments");
77 2 : return TEST_SUCCESS;
78 : }
79 :
80 : /* Testing with unknown single dash option */
81 2 : TEST(opts_unhandled_1) {
82 2 : mopts_t opt[] = {
83 : {'z', "zoiberg", "No idea.", false, NULL},
84 : ARGS_EOL
85 : };
86 2 : char *av[] = {"./test", "-q"};
87 : int st, fd[2];
88 : pid_t pid;
89 2 : mlist_t *lst = NULL;
90 :
91 2 : pipe(fd);
92 2 : if ((pid = fork()) == 0) {
93 2 : DUP_ALL_OUTPUTS(fd);
94 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
95 : "Not handling unknown option");
96 0 : exit(0);
97 : } else {
98 2 : WAIT_AND_CLOSE(pid, st, fd);
99 2 : TEST_ASSERT((WEXITSTATUS(st) == 1), "Wrong return");
100 : }
101 2 : return TEST_SUCCESS;
102 : }
103 :
104 : /* Testing with unknown double dash option */
105 2 : TEST(opts_unhandled_2) {
106 2 : mopts_t opt[] = {
107 : {'z', "zoiberg", "No idea.", false, NULL},
108 : ARGS_EOL
109 : };
110 2 : char *av[] = {"./tests", "--oui"};
111 : pid_t pid;
112 : int st, fd[2];
113 2 : mlist_t *lst = NULL;;
114 :
115 2 : pipe(fd);
116 2 : fflush(stdout);
117 2 : if ((pid = fork()) == 0) {
118 :
119 2 : DUP_ALL_OUTPUTS(fd);
120 2 : TEST_ASSERT(read_opt(sizeof(av), av, opt, &lst) == 0,
121 : "Not handling properly unknown arguments");
122 : } else {
123 2 : WAIT_AND_CLOSE(pid, st, fd);
124 2 : TEST_ASSERT((WEXITSTATUS(st) == 1), "Wrong return");
125 : }
126 2 : return TEST_SUCCESS;
127 : }
128 :
129 : /* Testing with triple dash */
130 2 : TEST(opts_unhandled_3) {
131 2 : mopts_t opt[] = {
132 : {'z', "zoiberg", "No idea.", false, NULL},
133 : ARGS_EOL
134 : };
135 2 : char *av[] = {"./test", "---wrong-option"};
136 : int st, fd[2];
137 : pid_t pid;
138 2 : mlist_t *lst = NULL;;
139 :
140 2 : pipe(fd);
141 2 : if ((pid = fork()) == 0) {
142 2 : DUP_ALL_OUTPUTS(fd);
143 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
144 : "Not handling triple '-' arguments");
145 2 : exit(0);
146 : } else {
147 2 : WAIT_AND_CLOSE(pid, st, fd);
148 2 : TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return");
149 : }
150 2 : return TEST_SUCCESS;
151 : }
152 :
153 : /* Testing a call to -h builtin option */
154 2 : TEST(opts_help_1) {
155 2 : mopts_t opt[] = {
156 : {'z', "zoiberg", "No idea.", false, NULL},
157 : ARGS_EOL
158 : };
159 2 : char *av[] = {"./test", "-h"};
160 : int st, fd[2];
161 : pid_t pid;
162 2 : mlist_t *lst = NULL;
163 :
164 2 : pipe(fd);
165 2 : if ((pid = fork()) == 0) {
166 2 : DUP_ALL_OUTPUTS(fd);
167 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
168 : "Not handling -h arguments");
169 0 : exit(5);
170 : } else {
171 2 : WAIT_AND_CLOSE(pid, st, fd);
172 2 : TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return");
173 : }
174 2 : return TEST_SUCCESS;
175 : }
176 :
177 : /* Testing a call to --help builtin option */
178 2 : TEST(opts_help_2) {
179 2 : mopts_t opt[] = {
180 : {'z', "zoiberg", "No idea.", false, NULL},
181 : ARGS_EOL
182 : };
183 2 : char *av[] = {"./test", "--help"};
184 : int st, fd[2];
185 : pid_t pid;
186 2 : mlist_t *lst = NULL;
187 :
188 2 : pipe(fd);
189 2 : if ((pid = fork()) == 0) {
190 2 : DUP_ALL_OUTPUTS(fd);
191 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
192 : "Not handling --help arguments");
193 0 : exit(5);
194 : } else {
195 2 : WAIT_AND_CLOSE(pid, st, fd);
196 2 : TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return");
197 : }
198 2 : return TEST_SUCCESS;
199 : }
200 :
201 : /* Testing that --help properly exits instantly */
202 2 : TEST(opts_help_3) {
203 2 : mopts_t opt[] = {
204 : {'z', "zoiberg", "No idea.", false, NULL},
205 : ARGS_EOL
206 : };
207 2 : char *av[] = {"./test", "--help", "--zoiberg"};
208 : int st, fd[2];
209 : pid_t pid;
210 2 : mlist_t *lst = NULL;
211 :
212 2 : pipe(fd);
213 2 : if ((pid = fork()) == 0) {
214 2 : DUP_ALL_OUTPUTS(fd);
215 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
216 : "Not handling --help arguments");
217 0 : exit(5);
218 : } else {
219 2 : WAIT_AND_CLOSE(pid, st, fd);
220 2 : TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return");
221 : }
222 2 : return TEST_SUCCESS;
223 : }
224 :
225 : /* Testing a call to -V builtin option */
226 2 : TEST(opts_version_1) {
227 2 : mopts_t opt[] = {
228 : {'z', "zoiberg", "No idea.", false, NULL},
229 : ARGS_EOL
230 : };
231 2 : char *av[] = {"./test", "-V"};
232 : int st, fd[2];
233 : pid_t pid;
234 2 : mlist_t *lst = NULL;
235 :
236 2 : pipe(fd);
237 2 : if ((pid = fork()) == 0) {
238 2 : DUP_ALL_OUTPUTS(fd);
239 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
240 : "Not handling --version arguments");
241 0 : exit(5);
242 : } else {
243 2 : WAIT_AND_CLOSE(pid, st, fd);
244 2 : TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return");
245 : }
246 2 : return TEST_SUCCESS;
247 : }
248 :
249 : /* Testing a call to --version builtin option */
250 2 : TEST(opts_version_2) {
251 2 : mopts_t opt[] = {
252 : {'z', "zoiberg", "No idea.", false, NULL},
253 : ARGS_EOL
254 : };
255 2 : char *av[] = {"./test", "--version"};
256 : int st, fd[2];
257 : pid_t pid;
258 2 : mlist_t *lst = NULL;
259 :
260 2 : pipe(fd);
261 2 : if ((pid = fork()) == 0) {
262 2 : DUP_ALL_OUTPUTS(fd);
263 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
264 : "Not handling -v arguments");
265 0 : exit(5);
266 : } else {
267 2 : WAIT_AND_CLOSE(pid, st, fd);
268 2 : TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return");
269 : }
270 2 : return TEST_SUCCESS;
271 : }
272 :
273 : /* Testing that --version properly exit instantly */
274 2 : TEST(opts_version_3) {
275 2 : mopts_t opt[] = {
276 : {'z', "zoiberg", "No idea.", false, NULL},
277 : ARGS_EOL
278 : };
279 2 : char *av[] = {"./test", "--version", "--zoiberg"};
280 : int st, fd[2];
281 : pid_t pid;
282 2 : mlist_t *lst = NULL;
283 :
284 2 : pipe(fd);
285 2 : if ((pid = fork()) == 0) {
286 2 : DUP_ALL_OUTPUTS(fd);
287 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
288 : "Not handling -v arguments");
289 0 : exit(5);
290 : } else {
291 2 : WAIT_AND_CLOSE(pid, st, fd);
292 2 : TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return");
293 : }
294 2 : return TEST_SUCCESS;
295 : }
296 :
297 : /* Testing reading of 1 option without parameters */
298 2 : TEST(opts_base_1) {
299 2 : mopts_t opt[] = OPT_DEF(false);
300 2 : char *av[] = {"./test", "-q"};
301 2 : mlist_t *lst = NULL;
302 :
303 2 : reset_args();
304 2 : TEST_ASSERT((read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 1),
305 : "Wrong return");
306 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
307 2 : TEST_ASSERT(args.opt_w != true, "Argument not read");
308 2 : return TEST_SUCCESS;
309 : }
310 :
311 : /* Testing reading of 2 packed options without parameters */
312 2 : TEST(opts_base_2) {
313 2 : mopts_t opt[] = OPT_DEF(false);
314 2 : char *av[] = {"./test", "-qw"};
315 2 : mlist_t *lst = NULL;
316 :
317 2 : reset_args();
318 2 : TEST_ASSERT((read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 2),
319 : "Wrong return");
320 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
321 2 : TEST_ASSERT(args.opt_w == true, "Argument not read");
322 2 : TEST_ASSERT(args.opt_e != true, "Argument not read");
323 2 : return TEST_SUCCESS;
324 : }
325 :
326 : /* Testing reading of 2 separated options without parameters */
327 2 : TEST(opts_base_3) {
328 2 : mopts_t opt[] = OPT_DEF(false);
329 2 : char *av[] = {"./test", "-q", "-w"};
330 2 : mlist_t *lst = NULL;
331 :
332 2 : reset_args();
333 2 : TEST_ASSERT((read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 2),
334 : "Wrong return");
335 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
336 2 : TEST_ASSERT(args.opt_w == true, "Argument not read");
337 2 : TEST_ASSERT(args.opt_e != true, "Argument not read");
338 2 : return TEST_SUCCESS;
339 : }
340 :
341 : /* Testing reading of 1 long and 1 short options without parameters */
342 2 : TEST(opts_base_4) {
343 2 : mopts_t opt[] = OPT_DEF(false);
344 2 : char *av[] = {"./test", "--qwerty", "-w"};
345 2 : mlist_t *lst = NULL;
346 :
347 2 : reset_args();
348 2 : TEST_ASSERT((read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 2),
349 : "Wrong return");
350 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
351 2 : TEST_ASSERT(args.opt_w == true, "Argument not read");
352 2 : TEST_ASSERT(args.opt_e != true, "Argument not read");
353 2 : return TEST_SUCCESS;
354 : }
355 :
356 : /* Testing reading of 2 long options without parameters */
357 2 : TEST(opts_base_5) {
358 2 : mopts_t opt[] = OPT_DEF(false);
359 2 : char *av[] = {"./test", "--qwerty", "--wertyu"};
360 2 : mlist_t *lst = NULL;
361 :
362 2 : reset_args();
363 2 : TEST_ASSERT((read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 2),
364 : "Wrong return");
365 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
366 2 : TEST_ASSERT(args.opt_w == true, "Argument not read");
367 2 : TEST_ASSERT(args.opt_e != true, "Argument not read");
368 2 : return TEST_SUCCESS;
369 : }
370 :
371 : /* Testing reading 6 packed options that makes the name of a long option */
372 2 : TEST(opts_base_6) {
373 2 : mopts_t opt[] = OPT_DEF(false);
374 2 : char *av[] = {"./test", "-qwerty"};
375 2 : mlist_t *lst = NULL;
376 :
377 2 : reset_args();
378 2 : TEST_ASSERT((read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 6),
379 : "Wrong return");
380 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
381 2 : TEST_ASSERT(args.opt_w == true, "Argument not read");
382 2 : TEST_ASSERT(args.opt_e == true, "Argument not read");
383 2 : TEST_ASSERT(args.opt_r == true, "Argument not read");
384 2 : TEST_ASSERT(args.opt_t == true, "Argument not read");
385 2 : TEST_ASSERT(args.opt_y == true, "Argument not read");
386 2 : return TEST_SUCCESS;
387 : }
388 :
389 : /* Testing reading of 6 long options without parameters */
390 2 : TEST(opts_base_7) {
391 2 : mopts_t opt[] = OPT_DEF(false);
392 2 : char *av[] = {"./test",
393 : "--qwerty",
394 : "--wertyu",
395 : "--ertyui",
396 : "--rtyuio",
397 : "--tyuiop",
398 : "--yuiop["};
399 2 : mlist_t *lst = NULL;
400 :
401 2 : reset_args();
402 2 : TEST_ASSERT((read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 6),
403 : "Wrong return");
404 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
405 2 : TEST_ASSERT(args.opt_w == true, "Argument not read");
406 2 : TEST_ASSERT(args.opt_e == true, "Argument not read");
407 2 : TEST_ASSERT(args.opt_r == true, "Argument not read");
408 2 : TEST_ASSERT(args.opt_t == true, "Argument not read");
409 2 : TEST_ASSERT(args.opt_y == true, "Argument not read");
410 2 : return TEST_SUCCESS;
411 : }
412 :
413 : /* Testing missing argument in short options-argument pair */
414 2 : TEST(opts_missing_value_1) {
415 2 : mopts_t opt[] = OPT_DEF(true);
416 2 : char *av[] = {"./test", "-q"};
417 : int st, fd[2];
418 : pid_t pid;
419 2 : mlist_t *lst = NULL;
420 :
421 2 : reset_args();
422 2 : pipe(fd);
423 2 : if ((pid = fork()) == 0) {
424 2 : DUP_ALL_OUTPUTS(fd);
425 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
426 : "Not handling missing argument");
427 0 : exit(5);
428 : } else {
429 2 : WAIT_AND_CLOSE(pid, st, fd);
430 2 : TEST_ASSERT((WEXITSTATUS(st) == 1), "Wrong return");
431 : }
432 2 : return TEST_SUCCESS;
433 : }
434 :
435 : /* Testing missing argument in long options-argument pair */
436 2 : TEST(opts_missing_value_2) {
437 2 : mopts_t opt[] = OPT_DEF(true);
438 2 : char *av[] = {"./test", "--qwerty"};
439 : int st, fd[2];
440 : pid_t pid;
441 2 : mlist_t *lst = NULL;
442 :
443 2 : reset_args();
444 2 : pipe(fd);
445 2 : if ((pid = fork()) == 0) {
446 2 : DUP_ALL_OUTPUTS(fd);
447 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
448 : "Not handling missing argument");
449 0 : exit(5);
450 : } else {
451 2 : WAIT_AND_CLOSE(pid, st, fd);
452 2 : TEST_ASSERT((WEXITSTATUS(st) == 1), "Wrong return");
453 : }
454 2 : return TEST_SUCCESS;
455 : }
456 :
457 : /* Testing good reading of short options-argument pair */
458 2 : TEST(opts_value_1) {
459 2 : mopts_t opt[] = OPT_DEF(true);
460 2 : char *av[] = {"./test", "-q", "toto"};
461 2 : mlist_t *lst = NULL;
462 :
463 2 : reset_args();
464 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 1,
465 : "Wrong return");
466 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
467 2 : TEST_ASSERT(strcmp(args.str_q, "toto") == 0, "Value not read");
468 2 : return TEST_SUCCESS;
469 : }
470 :
471 : /* Testing good reading of long options-argument pair */
472 2 : TEST(opts_value_2) {
473 2 : mopts_t opt[] = OPT_DEF(true);
474 2 : char *av[] = {"./test", "--qwerty=toto"};
475 2 : mlist_t *lst = NULL;
476 :
477 2 : reset_args();
478 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 1,
479 : "Wrong return");
480 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
481 2 : TEST_ASSERT(strcmp(args.str_q, "toto") == 0, "Value not read");
482 2 : return TEST_SUCCESS;
483 : }
484 :
485 : /* Mixing both tests above */
486 2 : TEST(opts_value_3) {
487 2 : mopts_t opt[] = OPT_DEF(true);
488 2 : char *av[] = {"./test", "--qwerty=toto", "-w", "tata"};
489 2 : mlist_t *lst = NULL;
490 :
491 2 : reset_args();
492 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 2,
493 : "Wrong return");
494 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
495 2 : TEST_ASSERT(strcmp(args.str_q, "toto") == 0, "Value not read");
496 2 : TEST_ASSERT(args.opt_w == true, "Argument not read");
497 2 : TEST_ASSERT(strcmp(args.str_w, "tata") == 0, "Value not read");
498 2 : return TEST_SUCCESS;
499 : }
500 :
501 : /* Testing good reading of multiple long options-argument pair */
502 2 : TEST(opts_value_4) {
503 2 : mopts_t opt[] = OPT_DEF(true);
504 2 : char *av[] = {"./test", "--qwerty=toto", "--wertyu=tata"};
505 2 : mlist_t *lst = NULL;
506 :
507 2 : reset_args();
508 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 2,
509 : "Wrong return");
510 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
511 2 : TEST_ASSERT(strcmp(args.str_q, "toto") == 0, "Value not read");
512 2 : TEST_ASSERT(args.opt_w == true, "Argument not read");
513 2 : TEST_ASSERT(strcmp(args.str_w, "tata") == 0, "Value not read");
514 2 : return TEST_SUCCESS;
515 : }
516 :
517 : /* Testing existence of long-only options */
518 2 : TEST(opts_long_only_1) {
519 2 : mopts_t opt[] = OPT_DEF(false);
520 2 : char *av[] = {"./test", "--qwerty"};
521 2 : mlist_t *lst = NULL;
522 :
523 2 : reset_args();
524 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 1,
525 : "Wrong return");
526 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
527 2 : TEST_ASSERT(args.opt_w == false, "Wrong argument");
528 2 : return TEST_SUCCESS;
529 : }
530 :
531 : /* Testing existence of short-only options */
532 2 : TEST(opts_short_only_1) {
533 2 : mopts_t opt[] = OPT_DEF(false);
534 2 : char *av[] = {"./test", "-q"};
535 2 : mlist_t *lst = NULL;
536 :
537 2 : reset_args();
538 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 1,
539 : "Wrong return");
540 2 : TEST_ASSERT(args.opt_q == true, "Argument not read");
541 2 : TEST_ASSERT(args.opt_w == false, "Wrong argument");
542 2 : return TEST_SUCCESS;
543 : }
544 :
545 : /* Testing reading of a params alone */
546 2 : TEST(params_reading_1) {
547 2 : mopts_t opt[] = OPT_DEF(false);
548 2 : char *av[] = {"./test", "hey"};
549 2 : mlist_t *lst = NULL;
550 :
551 2 : reset_args();
552 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
553 : "Wrong return");
554 2 : TEST_ASSERT(args.opt_q == false, "Wrong argument");
555 2 : TEST_ASSERT(lst, "List not created");
556 2 : TEST_ASSERT(lst->size == strlen(av[1]) + 1, "Parameter not read");
557 2 : TEST_ASSERT(!strcmp(lst->member, "hey"), "Parameter not read");
558 2 : return TEST_SUCCESS;
559 : }
560 :
561 : /* Testing reading of a params then a short option */
562 2 : TEST(params_reading_2) {
563 2 : mopts_t opt[] = OPT_DEF(false);
564 2 : char *av[] = {"./test", "hey", "-q"};
565 2 : mlist_t *lst = NULL;
566 :
567 2 : reset_args();
568 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 1,
569 : "Wrong return");
570 2 : TEST_ASSERT(args.opt_q == true, "Didn't parsed option");
571 2 : TEST_ASSERT(lst, "List not created");
572 2 : TEST_ASSERT(!strcmp(lst->member, "hey"), "Parameter not read");
573 2 : TEST_ASSERT(lst->size == strlen(av[1]) + 1, "Wrong size allocated");
574 2 : TEST_ASSERT(!lst->next, "Unwanted node created");
575 2 : return TEST_SUCCESS;
576 : }
577 :
578 : /* Testing reading of a params then a long option */
579 2 : TEST(params_reading_3) {
580 2 : mopts_t opt[] = OPT_DEF(false);
581 2 : char *av[] = {"./test", "hey", "--qwerty"};
582 2 : mlist_t *lst = NULL;
583 :
584 2 : reset_args();
585 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 1,
586 : "Wrong return");
587 2 : TEST_ASSERT(args.opt_q == true, "Didn't parsed option");
588 2 : TEST_ASSERT(lst, "List not created");
589 2 : TEST_ASSERT(!strcmp(lst->member, "hey"), "Parameter not read");
590 2 : TEST_ASSERT(lst->size == strlen(av[1]) + 1, "Wrong size allocated");
591 2 : TEST_ASSERT(!lst->next, "Unwanted node created");
592 2 : return TEST_SUCCESS;
593 : }
594 :
595 : /* Testing with empty param */
596 2 : TEST(empty_param_1) {
597 2 : mopts_t opt[] = OPT_DEF(false);
598 2 : char *av[] = {"./test", ""};
599 2 : mlist_t *lst = NULL;
600 :
601 2 : reset_args();
602 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
603 : "Wrong return");
604 2 : TEST_ASSERT(args.opt_q == false, "Wrong argument");
605 2 : TEST_ASSERT(!lst, "List created with empty param");
606 2 : return TEST_SUCCESS;
607 : }
608 :
609 : /* Testing with null param */
610 2 : TEST(empty_param_2) {
611 2 : mopts_t opt[] = OPT_DEF(false);
612 2 : char *av[] = {"./test", NULL};
613 2 : mlist_t *lst = NULL;
614 :
615 2 : reset_args();
616 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
617 : "Wrong return");
618 2 : TEST_ASSERT(args.opt_q == false, "Wrong argument");
619 2 : TEST_ASSERT(!lst, "List created with null param");
620 2 : return TEST_SUCCESS;
621 : }
622 :
623 : /* Testing with empty param followed by a param */
624 2 : TEST(empty_param_3) {
625 2 : mopts_t opt[] = {
626 : {'q', NULL, "qwerty", false, &callback_q},
627 : ARGS_EOL
628 : };
629 2 : char *av[] = {"./test", "", "hey"};
630 2 : mlist_t *lst = NULL;
631 :
632 2 : reset_args();
633 2 : TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst) == 0,
634 : "Wrong return");
635 2 : TEST_ASSERT(args.opt_q == false, "Wrong argument");
636 2 : TEST_ASSERT(lst, "List not created");
637 2 : TEST_ASSERT(!strcmp(lst->member, "hey"), "Parameter not read");
638 2 : TEST_ASSERT(lst->size == strlen(av[2]) + 1, "Wrong size allocated");
639 2 : return TEST_SUCCESS;
640 : }
641 :
642 : /* Testing reading of parameters, options, and empty cases in av */
643 2 : TEST(mixed_1) {
644 2 : mopts_t opt[] = OPT_DEF(false);
645 2 : char *av[] = {"./test", "hey", "--qwerty", "-q", "-w",
646 : "Ne02ptzero", "is", "--rtyuio", "", "-e"};
647 2 : mlist_t *lst = NULL;
648 2 : int i = 0;
649 :
650 2 : reset_args();
651 2 : TEST_ASSERT((i = read_opt(sizeof(av) / sizeof(av[0]), av, opt, &lst)) == 5,
652 : "Wrong return");
653 2 : TEST_ASSERT(args.opt_q == true, "Didn't parsed option");
654 2 : TEST_ASSERT(args.opt_w == true, "Didn't parsed option");
655 2 : TEST_ASSERT(args.opt_e == true, "Didn't parsed option");
656 2 : TEST_ASSERT(args.opt_r == true, "Didn't parsed option");
657 2 : TEST_ASSERT(args.opt_t != true, "Didn't parsed option");
658 2 : TEST_ASSERT(lst, "List not created");
659 2 : TEST_ASSERT(!strcmp(lst->member, "hey"), "Parameter not read");
660 2 : TEST_ASSERT(lst->size == strlen(av[1]) + 1, "Wrong size allocated");
661 2 : TEST_ASSERT(lst->next, "Node not created");
662 2 : TEST_ASSERT(!strcmp(lst->next->member, "Ne02ptzero"), "Parameter not read");
663 2 : TEST_ASSERT(lst->next->size == strlen(av[5]) + 1, "Wrong size allocated");
664 2 : TEST_ASSERT(lst->next->next, "Node not created");
665 2 : TEST_ASSERT(!strcmp(lst->next->next->member, "is"),
666 : "Parameter not read");
667 2 : TEST_ASSERT(lst->next->next->size == strlen(av[6]) + 1,
668 : "Wrong size allocated");
669 2 : TEST_ASSERT(!lst->next->next->next, "Unwanted node created");
670 2 : return TEST_SUCCESS;
671 : }
672 :
673 34 : void callback_q(const char *s) {
674 34 : args.opt_q = true;
675 34 : if (s == NULL)
676 26 : strcpy(args.str_q, "");
677 : else
678 8 : strcpy(args.str_q, s);
679 34 : }
680 :
681 18 : void callback_w(const char *s) {
682 18 : args.opt_w = true;
683 18 : if (s == NULL)
684 14 : strcpy(args.str_w, "");
685 : else
686 4 : strcpy(args.str_w, s);
687 18 : }
688 :
689 6 : void callback_e(const char *s) {
690 6 : args.opt_e = true;
691 6 : if (s == NULL)
692 6 : strcpy(args.str_e, "");
693 : else
694 0 : strcpy(args.str_e, s);
695 6 : }
696 :
697 6 : void callback_r(const char *s) {
698 6 : args.opt_r = true;
699 6 : if (s == NULL)
700 6 : strcpy(args.str_r, "");
701 : else
702 0 : strcpy(args.str_r, s);
703 6 : }
704 :
705 4 : void callback_t(const char *s) {
706 4 : args.opt_t = true;
707 4 : if (s == NULL)
708 4 : strcpy(args.str_t, "");
709 : else
710 0 : strcpy(args.str_t, s);
711 4 : }
712 :
713 4 : void callback_y(const char *s) {
714 4 : args.opt_y = true;
715 4 : if (s == NULL)
716 4 : strcpy(args.str_y, "");
717 : else
718 0 : strcpy(args.str_y, s);
719 4 : }
720 :
721 44 : void reset_args(void) {
722 44 : args.opt_q = false;
723 44 : args.opt_w = false;
724 44 : args.opt_e = false;
725 44 : args.opt_r = false;
726 44 : args.opt_t = false;
727 44 : args.opt_y = false;
728 44 : strcpy(args.str_q, "");
729 44 : strcpy(args.str_w, "");
730 44 : strcpy(args.str_e, "");
731 44 : strcpy(args.str_r, "");
732 44 : strcpy(args.str_t, "");
733 44 : strcpy(args.str_y, "");
734 44 : }
735 :
736 2 : void register_args_tests(void) {
737 : /* Testing all possibilities with a NULL args somewhere */
738 2 : reg_test("m_args", opts_NULL);
739 : /* Testing with av being an array full of empty strings */
740 2 : reg_test("m_args", opts_empty_1);
741 : /* Testing with av being an array full of NULLs */
742 2 : reg_test("m_args", opts_empty_2);
743 : /* Testing with unknown single dash option */
744 2 : reg_test("m_args", opts_unhandled_1);
745 : /* Testing with unknown double dash option */
746 2 : reg_test("m_args", opts_unhandled_2);
747 : /* Testing with triple dash */
748 2 : reg_test("m_args", opts_unhandled_3);
749 : /* Testing a call to -h builtin option */
750 2 : reg_test("m_args", opts_help_1);
751 : /* Testing a call to --help builtin option */
752 2 : reg_test("m_args", opts_help_2);
753 : /* Testing that --help properly exits instantly */
754 2 : reg_test("m_args", opts_help_3);
755 : /* Testing a call to -V builtin option */
756 2 : reg_test("m_args", opts_version_1);
757 : /* Testing a call to --version builtin option */
758 2 : reg_test("m_args", opts_version_2);
759 : /* Testing that --version properly exit instantly */
760 2 : reg_test("m_args", opts_version_3);
761 : /* Testing reading of 1 option without parameters */
762 2 : reg_test("m_args", opts_base_1);
763 : /* Testing reading of 2 packed options without parameters */
764 2 : reg_test("m_args", opts_base_2);
765 : /* Testing reading of 2 separated options without parameters */
766 2 : reg_test("m_args", opts_base_3);
767 : /* Testing reading of 1 long and 1 short options without parameters */
768 2 : reg_test("m_args", opts_base_4);
769 : /* Testing reading of 2 long options without parameters */
770 2 : reg_test("m_args", opts_base_5);
771 : /* Testing reading 6 packed options that makes the name of a long option */
772 2 : reg_test("m_args", opts_base_6);
773 : /* Testing reading of 6 long options without parameters */
774 2 : reg_test("m_args", opts_base_7);
775 : /* Testing missing argument in short options-argument pair */
776 2 : reg_test("m_args", opts_missing_value_1);
777 : /* Testing missing argument in long options-argument pair */
778 2 : reg_test("m_args", opts_missing_value_2);
779 : /* Testing good reading of short options-argument pair */
780 2 : reg_test("m_args", opts_value_1);
781 : /* Testing good reading of long options-argument pair */
782 2 : reg_test("m_args", opts_value_2);
783 : /* Mixing both tests above */
784 2 : reg_test("m_args", opts_value_3);
785 : /* Testing good reading of multiple long options-argument pair */
786 2 : reg_test("m_args", opts_value_4);
787 : /* Testing existence of long-only options */
788 2 : reg_test("m_args", opts_long_only_1);
789 : /* Testing existence of short-only options */
790 2 : reg_test("m_args", opts_short_only_1);
791 : /* Testing reading of a params alone */
792 2 : reg_test("m_args", params_reading_1);
793 : /* Testing reading of a params then a short option */
794 2 : reg_test("m_args", params_reading_2);
795 : /* Testing reading of a params then a long option */
796 2 : reg_test("m_args", params_reading_3);
797 : /* Testing with empty param */
798 2 : reg_test("m_args", empty_param_1);
799 : /* Testing with null param */
800 2 : reg_test("m_args", empty_param_2);
801 : /* Testing with empty param followed by a param */
802 2 : reg_test("m_args", empty_param_3);
803 : /* Testing reading of parameters, options, and empty cases in av */
804 2 : reg_test("m_args", mixed_1);
805 2 : }
|