97
98
char *argv[16], **args;
98
int oldstderr, ret = 0, argc;
100
printf ("Testing nih_option_parser()\n");
101
TEST_FUNCTION ("nih_option_parser");
101
103
program_name = "test";
104
oldstderr = dup (STDERR_FILENO);
107
printf ("...with no arguments\n");
106
/* Check that the option parser can be called with no arguments,
107
* which results in a single-element array being returned with
110
TEST_FEATURE ("with no arguments");
109
112
argv[argc++] = "ignored";
110
113
argv[argc] = NULL;
111
115
args = nih_option_parser (NULL, argc, argv, options, FALSE);
113
/* Return value should be a NULL array */
114
if (args[0] != NULL) {
115
printf ("BAD: return value wasn't what we expected.\n");
117
TEST_NE_P (args, NULL);
118
TEST_EQ_P (args[0], NULL);
122
printf ("...with all non-option arguments\n");
123
/* Check that all non-option arguments are passed through into the
124
* returned NULL-terminated array.
126
TEST_FEATURE ("with all non-option arguments");
124
128
argv[argc++] = "ignored";
125
129
argv[argc++] = "foo";
126
130
argv[argc++] = "bar";
127
131
argv[argc++] = "baz";
128
132
argv[argc] = NULL;
129
134
args = nih_option_parser (NULL, argc, argv, options, FALSE);
131
/* First array entry should be first argument */
132
if (strcmp (args[0], "foo")) {
133
printf ("BAD: return value wasn't what we expected.\n");
137
/* Second array entry should be second argument */
138
if (strcmp (args[1], "bar")) {
139
printf ("BAD: return value wasn't what we expected.\n");
143
/* Third array entry should be third argument */
144
if (strcmp (args[2], "baz")) {
145
printf ("BAD: return value wasn't what we expected.\n");
149
/* Return value should be a NULL array */
150
if (args[3] != NULL) {
151
printf ("BAD: return value wasn't what we expected.\n");
136
TEST_NE_P (args, NULL);
137
TEST_EQ_STR (args[0], "foo");
138
TEST_EQ_STR (args[1], "bar");
139
TEST_EQ_STR (args[2], "baz");
140
TEST_EQ_P (args[3], NULL);
158
printf ("...with single short option\n");
145
/* Check that a single short option is taken from the arguments and
146
* the appropriate variable set.
148
TEST_FEATURE ("with single short option");
160
150
argv[argc++] = "ignored";
161
151
argv[argc++] = "-d";
162
152
argv[argc] = NULL;
164
155
args = nih_option_parser (NULL, argc, argv, options, FALSE);
166
/* Return value should be a NULL array */
167
if (args[0] != NULL) {
168
printf ("BAD: return value wasn't what we expected.\n");
157
TEST_NE_P (args, NULL);
158
TEST_EQ_P (args[0], NULL);
172
/* Daemonise variable should have been set */
174
printf ("BAD: daemonise value wasn't what we expected.\n");
160
TEST_TRUE (daemonise);
181
printf ("...with multiple short options\n");
165
/* Check that all short options are taken from the arguments and
166
* all of the appropriate variables set.
168
TEST_FEATURE ("with multiple short options");
183
170
argv[argc++] = "ignored";
184
171
argv[argc++] = "-d";
185
172
argv[argc++] = "-R";
186
173
argv[argc] = NULL;
189
177
args = nih_option_parser (NULL, argc, argv, options, FALSE);
191
/* Return value should be a NULL array */
192
if (args[0] != NULL) {
193
printf ("BAD: return value wasn't what we expected.\n");
197
/* Daemonise variable should have been set */
199
printf ("BAD: daemonise value wasn't what we expected.\n");
203
/* Recursive variable should have been set */
205
printf ("BAD: recursive value wasn't what we expected.\n");
179
TEST_NE_P (args, NULL);
180
TEST_EQ_P (args[0], NULL);
182
TEST_TRUE (daemonise);
183
TEST_TRUE (recursive);
212
printf ("...with combined short options\n");
188
/* Check that multiple short options can be combined into a single
189
* argument, and that they're all handled.
191
TEST_FEATURE ("with combined short options");
214
193
argv[argc++] = "ignored";
215
194
argv[argc++] = "-dR";
216
195
argv[argc] = NULL;
219
args = nih_option_parser (NULL, argc, argv, options, FALSE);
221
/* Return value should be a NULL array */
222
if (args[0] != NULL) {
223
printf ("BAD: return value wasn't what we expected.\n");
227
/* Daemonise variable should have been set */
229
printf ("BAD: daemonise value wasn't what we expected.\n");
233
/* Recursive variable should have been set */
235
printf ("BAD: recursive value wasn't what we expected.\n");
242
printf ("...with intermixed short options and arguments\n");
244
argv[argc++] = "ignored";
245
argv[argc++] = "foo";
247
argv[argc++] = "bar";
249
argv[argc++] = "baz";
253
args = nih_option_parser (NULL, argc, argv, options, FALSE);
255
/* First array entry should be first argument */
256
if (strcmp (args[0], "foo")) {
257
printf ("BAD: return value wasn't what we expected.\n");
261
/* Second array entry should be second argument */
262
if (strcmp (args[1], "bar")) {
263
printf ("BAD: return value wasn't what we expected.\n");
267
/* Third array entry should be third argument */
268
if (strcmp (args[2], "baz")) {
269
printf ("BAD: return value wasn't what we expected.\n");
273
/* Return value should be a NULL array */
274
if (args[3] != NULL) {
275
printf ("BAD: return value wasn't what we expected.\n");
279
/* Daemonise variable should have been set */
281
printf ("BAD: daemonise value wasn't what we expected.\n");
285
/* Recursive variable should have been set */
287
printf ("BAD: recursive value wasn't what we expected.\n");
294
printf ("...with command-mode short options and arguments\n");
296
argv[argc++] = "ignored";
297
argv[argc++] = "foo";
299
argv[argc++] = "bar";
301
argv[argc++] = "baz";
199
args = nih_option_parser (NULL, argc, argv, options, FALSE);
201
TEST_NE_P (args, NULL);
202
TEST_EQ_P (args[0], NULL);
204
TEST_TRUE (daemonise);
205
TEST_TRUE (recursive);
210
/* Check that short options and ordinary arguments can be intermixed,
211
* the arguments are returned in the array and the option values set.
213
TEST_FEATURE ("with intermixed short options and arguments");
215
argv[argc++] = "ignored";
216
argv[argc++] = "foo";
218
argv[argc++] = "bar";
220
argv[argc++] = "baz";
225
args = nih_option_parser (NULL, argc, argv, options, FALSE);
227
TEST_NE_P (args, NULL);
228
TEST_EQ_STR (args[0], "foo");
229
TEST_EQ_STR (args[1], "bar");
230
TEST_EQ_STR (args[2], "baz");
231
TEST_EQ_P (args[3], NULL);
233
TEST_TRUE (daemonise);
234
TEST_TRUE (recursive);
239
/* Check that the first non-option argument can terminate the
240
* processing of other options when in command mode, and that the
241
* remaining options are returned in the array and the values NOT
244
TEST_FEATURE ("with command-mode short options and arguments");
246
argv[argc++] = "ignored";
247
argv[argc++] = "foo";
249
argv[argc++] = "bar";
251
argv[argc++] = "baz";
305
256
args = nih_option_parser (NULL, argc, argv, options, TRUE);
307
/* First array entry should be first argument */
308
if (strcmp (args[0], "foo")) {
309
printf ("BAD: return value wasn't what we expected.\n");
313
/* Second array entry should be first option */
314
if (strcmp (args[1], "-d")) {
315
printf ("BAD: return value wasn't what we expected.\n");
319
/* Third array entry should be second argument */
320
if (strcmp (args[2], "bar")) {
321
printf ("BAD: return value wasn't what we expected.\n");
325
/* Fourth array entry should be second option */
326
if (strcmp (args[3], "-R")) {
327
printf ("BAD: return value wasn't what we expected.\n");
331
/* Firth array entry should be third argument */
332
if (strcmp (args[4], "baz")) {
333
printf ("BAD: return value wasn't what we expected.\n");
337
/* Return value should be a NULL array */
338
if (args[5] != NULL) {
339
printf ("BAD: return value wasn't what we expected.\n");
343
/* Daemonise variable should not have been set */
345
printf ("BAD: daemonise value wasn't what we expected.\n");
349
/* Recursive variable should have been set */
351
printf ("BAD: recursive value wasn't what we expected.\n");
258
TEST_NE_P (args, NULL);
259
TEST_EQ_STR (args[0], "foo");
260
TEST_EQ_STR (args[1], "-d");
261
TEST_EQ_STR (args[2], "bar");
262
TEST_EQ_STR (args[3], "-R");
263
TEST_EQ_STR (args[4], "baz");
264
TEST_EQ_P (args[5], NULL);
266
TEST_FALSE (daemonise);
267
TEST_FALSE (recursive);
358
printf ("...with short options and terminator\n");
272
/* Check that option processing can be terminated by a double-dash,
273
* and that following options are placed in the arguments and the
276
TEST_FEATURE ("with short options and terminator");
360
278
argv[argc++] = "ignored";
361
279
argv[argc++] = "foo";
490
365
argv[argc++] = "bar";
491
366
argv[argc++] = "baz";
492
367
argv[argc] = NULL;
496
372
args = nih_option_parser (NULL, argc, argv, options, FALSE);
498
/* First array entry should be previous to options */
499
if (strcmp (args[0], "wibble")) {
500
printf ("BAD: return value wasn't what we expected.\n");
504
/* Second array entry should be non-option */
505
if (strcmp (args[1], "-R")) {
506
printf ("BAD: return value wasn't what we expected.\n");
510
/* Third array entry should be first argument */
511
if (strcmp (args[2], "bar")) {
512
printf ("BAD: return value wasn't what we expected.\n");
516
/* Fourth array entry should be second argument */
517
if (strcmp (args[3], "baz")) {
518
printf ("BAD: return value wasn't what we expected.\n");
522
/* Return value should be a NULL array */
523
if (args[4] != NULL) {
524
printf ("BAD: return value wasn't what we expected.\n");
528
/* Daemonise variable should have been set */
530
printf ("BAD: daemonise value wasn't what we expected.\n");
534
/* Recursive variable should NOT have been set */
536
printf ("BAD: recursive value wasn't what we expected.\n");
540
/* Filename variable should be set to argument */
541
if (strcmp (filename, "foo")) {
542
printf ("BAD: filename value wasn't what we expected.\n");
374
TEST_NE_P (args, NULL);
375
TEST_EQ_STR (args[0], "wibble");
376
TEST_EQ_STR (args[1], "-R");
377
TEST_EQ_STR (args[2], "bar");
378
TEST_EQ_STR (args[3], "baz");
379
TEST_EQ_P (args[4], NULL);
381
TEST_TRUE (daemonise);
382
TEST_FALSE (recursive);
383
TEST_EQ_STR (filename, "foo");
546
385
nih_free (filename);
550
printf ("...with short option and embedded argument\n");
389
/* Check that the argument for a short option can immediately
390
* follow it, combined into one word. Check that the characters
391
* of this word aren't treated as options.
393
TEST_FEATURE ("with short option and embedded argument");
552
395
argv[argc++] = "ignored";
553
396
argv[argc++] = "-fROOT";
554
397
argv[argc] = NULL;
557
401
args = nih_option_parser (NULL, argc, argv, options, FALSE);
559
/* Return value should be a NULL array */
560
if (args[0] != NULL) {
561
printf ("BAD: return value wasn't what we expected.\n");
565
/* Filename variable should be set to remainder of argument */
566
if (strcmp (filename, "ROOT")) {
567
printf ("BAD: filename value wasn't what we expected.\n");
571
/* Recursive variable should not have been set */
573
printf ("BAD: recursive value wasn't what we expected.\n");
403
TEST_NE_P (args, NULL);
404
TEST_EQ_P (args[0], NULL);
406
TEST_EQ_STR (filename, "ROOT");
407
TEST_FALSE (recursive);
577
409
nih_free (filename);
581
printf ("...with short option and non-embedded argument\n");
413
/* Check that the short option may be inside a sequence of short
414
* options in one argument, and then only the next non-option argument
415
* is considered, not the remainder of the option argument.
417
TEST_FEATURE ("with short option and non-embedded argument");
583
419
argv[argc++] = "ignored";
584
420
argv[argc++] = "-dfR";
585
421
argv[argc++] = "foo";
586
422
argv[argc] = NULL;
590
427
args = nih_option_parser (NULL, argc, argv, options, FALSE);
592
/* Return value should be a NULL array */
593
if (args[0] != NULL) {
594
printf ("BAD: return value wasn't what we expected.\n");
598
/* Filename variable should be set to first argument */
599
if (strcmp (filename, "foo")) {
600
printf ("BAD: filename value wasn't what we expected.\n");
604
/* Daemonise variable should have been set */
606
printf ("BAD: daemonise value wasn't what we expected.\n");
610
/* Recursive variable should have been set */
612
printf ("BAD: recursive value wasn't what we expected.\n");
429
TEST_NE_P (args, NULL);
430
TEST_EQ_P (args[0], NULL);
432
TEST_TRUE (daemonise);
433
TEST_TRUE (recursive);
434
TEST_EQ_STR (filename, "foo");
616
436
nih_free (filename);
620
printf ("...with multiple short argument options\n");
440
/* Check that multiple short options which accept arguments each
441
* take the next non-option argument, not themselves or the same
444
TEST_FEATURE ("with multiple short argument options");
622
446
argv[argc++] = "ignored";
623
447
argv[argc++] = "-f";
625
449
argv[argc++] = "foo";
626
450
argv[argc++] = "bar";
627
451
argv[argc] = NULL;
630
455
args = nih_option_parser (NULL, argc, argv, options, FALSE);
632
/* Return value should be a NULL array */
633
if (args[0] != NULL) {
634
printf ("BAD: return value wasn't what we expected.\n");
638
/* Filename variable should be set to argument */
639
if (strcmp (filename, "foo")) {
640
printf ("BAD: filename value wasn't what we expected.\n");
644
/* Option variable should be set to argument */
645
if (strcmp (option, "bar")) {
646
printf ("BAD: option value wasn't what we expected.\n");
457
TEST_NE_P (args, NULL);
458
TEST_EQ_P (args[0], NULL);
460
TEST_EQ_STR (filename, "foo");
461
TEST_EQ_STR (option, "bar");
650
463
nih_free (filename);
651
464
nih_free (option);
655
printf ("...with single long option\n");
657
argv[argc++] = "ignored";
658
argv[argc++] = "--wibble";
661
args = nih_option_parser (NULL, argc, argv, options, FALSE);
663
/* Return value should be a NULL array */
664
if (args[0] != NULL) {
665
printf ("BAD: return value wasn't what we expected.\n");
669
/* Wibble variable should have been set */
671
printf ("BAD: wibble value wasn't what we expected.\n");
678
printf ("...with multiple long options\n");
680
argv[argc++] = "ignored";
681
argv[argc++] = "--wibble";
682
argv[argc++] = "--recursive";
686
args = nih_option_parser (NULL, argc, argv, options, FALSE);
688
/* Return value should be a NULL array */
689
if (args[0] != NULL) {
690
printf ("BAD: return value wasn't what we expected.\n");
694
/* Wibble variable should have been set */
696
printf ("BAD: wibble value wasn't what we expected.\n");
700
/* Recursive variable should have been set */
702
printf ("BAD: recursive value wasn't what we expected.\n");
709
printf ("...with intermixed long options and arguments\n");
711
argv[argc++] = "ignored";
712
argv[argc++] = "foo";
713
argv[argc++] = "--wibble";
714
argv[argc++] = "bar";
715
argv[argc++] = "--recursive";
716
argv[argc++] = "baz";
720
args = nih_option_parser (NULL, argc, argv, options, FALSE);
722
/* First array entry should be first argument */
723
if (strcmp (args[0], "foo")) {
724
printf ("BAD: return value wasn't what we expected.\n");
728
/* Second array entry should be second argument */
729
if (strcmp (args[1], "bar")) {
730
printf ("BAD: return value wasn't what we expected.\n");
734
/* Third array entry should be third argument */
735
if (strcmp (args[2], "baz")) {
736
printf ("BAD: return value wasn't what we expected.\n");
740
/* Return value should be a NULL array */
741
if (args[3] != NULL) {
742
printf ("BAD: return value wasn't what we expected.\n");
746
/* Wibble variable should have been set */
748
printf ("BAD: wibble value wasn't what we expected.\n");
752
/* Recursive variable should have been set */
754
printf ("BAD: recursive value wasn't what we expected.\n");
761
printf ("...with command-mode long options and arguments\n");
763
argv[argc++] = "ignored";
764
argv[argc++] = "foo";
765
argv[argc++] = "--wibble";
766
argv[argc++] = "bar";
767
argv[argc++] = "--recursive";
768
argv[argc++] = "baz";
468
/* Check that a single long option is taken from the arguments and
469
* the appropriate variable set.
471
TEST_FEATURE ("with single long option");
473
argv[argc++] = "ignored";
474
argv[argc++] = "--wibble";
478
args = nih_option_parser (NULL, argc, argv, options, FALSE);
480
TEST_NE_P (args, NULL);
481
TEST_EQ_P (args[0], NULL);
488
/* Check that multiple long options are taken from the arguments
489
* and the appropriate variables set.
491
TEST_FEATURE ("with multiple long options");
493
argv[argc++] = "ignored";
494
argv[argc++] = "--wibble";
495
argv[argc++] = "--recursive";
500
args = nih_option_parser (NULL, argc, argv, options, FALSE);
502
TEST_NE_P (args, NULL);
503
TEST_EQ_P (args[0], NULL);
506
TEST_TRUE (recursive);
511
/* Check that only the long options are taken from the arguments,
512
* and the non-option arguments are returned in the array.
514
TEST_FEATURE ("with intermixed long options and arguments");
516
argv[argc++] = "ignored";
517
argv[argc++] = "foo";
518
argv[argc++] = "--wibble";
519
argv[argc++] = "bar";
520
argv[argc++] = "--recursive";
521
argv[argc++] = "baz";
526
args = nih_option_parser (NULL, argc, argv, options, FALSE);
528
TEST_NE_P (args, NULL);
529
TEST_EQ_STR (args[0], "foo");
530
TEST_EQ_STR (args[1], "bar");
531
TEST_EQ_STR (args[2], "baz");
532
TEST_EQ_P (args[3], NULL);
535
TEST_TRUE (recursive);
540
/* Check that long options after the first non-option argument can
541
* be ignored when in command mode, and returned in the array with
542
* their value NOT being set.
544
TEST_FEATURE ("with command-mode long options and arguments");
546
argv[argc++] = "ignored";
547
argv[argc++] = "foo";
548
argv[argc++] = "--wibble";
549
argv[argc++] = "bar";
550
argv[argc++] = "--recursive";
551
argv[argc++] = "baz";
772
556
args = nih_option_parser (NULL, argc, argv, options, TRUE);
774
/* First array entry should be first argument */
775
if (strcmp (args[0], "foo")) {
776
printf ("BAD: return value wasn't what we expected.\n");
780
/* Second array entry should be first option */
781
if (strcmp (args[1], "--wibble")) {
782
printf ("BAD: return value wasn't what we expected.\n");
786
/* Third array entry should be second argument */
787
if (strcmp (args[2], "bar")) {
788
printf ("BAD: return value wasn't what we expected.\n");
792
/* Fourth array entry should be third argument */
793
if (strcmp (args[3], "--recursive")) {
794
printf ("BAD: return value wasn't what we expected.\n");
798
/* Third array entry should be third argument */
799
if (strcmp (args[4], "baz")) {
800
printf ("BAD: return value wasn't what we expected.\n");
804
/* Return value should be a NULL array */
805
if (args[5] != NULL) {
806
printf ("BAD: return value wasn't what we expected.\n");
810
/* Wibble variable should not have been set */
812
printf ("BAD: wibble value wasn't what we expected.\n");
816
/* Recursive variable should not have been set */
818
printf ("BAD: recursive value wasn't what we expected.\n");
558
TEST_NE_P (args, NULL);
559
TEST_EQ_STR (args[0], "foo");
560
TEST_EQ_STR (args[1], "--wibble");
561
TEST_EQ_STR (args[2], "bar");
562
TEST_EQ_STR (args[3], "--recursive");
563
TEST_EQ_STR (args[4], "baz");
564
TEST_EQ_P (args[5], NULL);
567
TEST_FALSE (recursive);
825
printf ("...with long options and terminator\n");
572
/* Check that long options after the double-dash terminator are
573
* ignored and returned in the array without their value being set.
575
TEST_FEATURE ("with long options and terminator");
827
577
argv[argc++] = "ignored";
828
578
argv[argc++] = "foo";
1046
718
argv[argc++] = "foo";
1047
719
argv[argc++] = "bar";
1048
720
argv[argc] = NULL;
1049
722
filename = NULL;
1051
724
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1053
/* Return value should be a NULL array */
1054
if (args[0] != NULL) {
1055
printf ("BAD: return value wasn't what we expected.\n");
1059
/* Filename variable should be set to argument */
1060
if (strcmp (filename, "foo")) {
1061
printf ("BAD: filename value wasn't what we expected.\n");
1065
/* Option variable should be set to argument */
1066
if (strcmp (option, "bar")) {
1067
printf ("BAD: option value wasn't what we expected.\n");
726
TEST_NE_P (args, NULL);
727
TEST_EQ_P (args[0], NULL);
729
TEST_EQ_STR (filename, "foo");
730
TEST_EQ_STR (option, "bar");
1071
732
nih_free (filename);
1072
733
nih_free (option);
1073
734
nih_free (args);
1076
printf ("...with invalid short option\n");
1078
argv[argc++] = "ignored";
1079
argv[argc++] = "-z";
1082
dup2 (fileno (output), STDERR_FILENO);
1083
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1084
dup2 (oldstderr, STDERR_FILENO);
1088
/* Return value should be NULL */
1090
printf ("BAD: return value wasn't what we expected.\n");
1094
/* Output should be message with program name and newline */
1095
fgets (text, sizeof (text), output);
1096
if (strcmp (text, "test: invalid option: -z\n")) {
1097
printf ("BAD: output wasn't what we expected.\n");
1101
/* Should include a suggestion of --help */
1102
fgets (text, sizeof (text), output);
1103
if (strcmp (text, "Try `test --help' for more information.\n")) {
1104
printf ("BAD: output wasn't what we expected.\n");
1108
/* Should be no more output */
1109
if (fgets (text, sizeof (text), output)) {
1110
printf ("BAD: more output than we expected.\n");
1115
ftruncate (fileno (output), 0);
1118
printf ("...with invalid short option and catch-all\n");
1120
argv[argc++] = "ignored";
1121
argv[argc++] = "-z";
1123
args = nih_option_parser (NULL, argc, argv, catch_options, FALSE);
1125
/* Return value should not be NULL */
1127
printf ("BAD: return value wasn't what we expected.\n");
1132
printf ("...with invalid long option\n");
1134
argv[argc++] = "ignored";
1135
argv[argc++] = "--zoiks";
1138
dup2 (fileno (output), STDERR_FILENO);
1139
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1140
dup2 (oldstderr, STDERR_FILENO);
1144
/* Return value should be NULL */
1146
printf ("BAD: return value wasn't what we expected.\n");
1150
/* Output should be message with program name and newline */
1151
fgets (text, sizeof (text), output);
1152
if (strcmp (text, "test: invalid option: --zoiks\n")) {
1153
printf ("BAD: output wasn't what we expected.\n");
1157
/* Should include a suggestion of --help */
1158
fgets (text, sizeof (text), output);
1159
if (strcmp (text, "Try `test --help' for more information.\n")) {
1160
printf ("BAD: output wasn't what we expected.\n");
1164
/* Should be no more output */
1165
if (fgets (text, sizeof (text), output)) {
1166
printf ("BAD: more output than we expected.\n");
1171
ftruncate (fileno (output), 0);
1174
printf ("...with invalid long option and catch-all\n");
1176
argv[argc++] = "ignored";
1177
argv[argc++] = "--zoiks";
1179
args = nih_option_parser (NULL, argc, argv, catch_options, FALSE);
1181
/* Return value should not be NULL */
1183
printf ("BAD: return value wasn't what we expected.\n");
1188
printf ("...with unexpected long option argument\n");
737
/* Check that an invalid short option causes an error message to
738
* be output with a suggestion of help, and NULL to be returned.
740
TEST_FEATURE ("with invalid short option");
742
argv[argc++] = "ignored";
746
TEST_DIVERT_STDERR (output) {
747
args = nih_option_parser (NULL, argc, argv, options, FALSE);
751
TEST_EQ_P (args, NULL);
753
TEST_FILE_EQ (output, "test: invalid option: -z\n");
754
TEST_FILE_EQ (output, "Try `test --help' for more information.\n");
755
TEST_FILE_END (output);
757
TEST_FILE_RESET (output);
760
/* Check that an invalid short option is ignored if there's a
761
* catch-all option in the list.
763
TEST_FEATURE ("with invalid short option and catch-all");
765
argv[argc++] = "ignored";
768
args = nih_option_parser (NULL, argc, argv, catch_options, FALSE);
770
TEST_NE_P (args, NULL);
775
/* Check that an invalid long option causes an error message to
776
* be output with a suggestion of help, and NULL to be returned.
778
TEST_FEATURE ("with invalid long option");
780
argv[argc++] = "ignored";
781
argv[argc++] = "--zoiks";
784
TEST_DIVERT_STDERR (output) {
785
args = nih_option_parser (NULL, argc, argv, options, FALSE);
789
TEST_EQ_P (args, NULL);
791
TEST_FILE_EQ (output, "test: invalid option: --zoiks\n");
792
TEST_FILE_EQ (output, "Try `test --help' for more information.\n");
793
TEST_FILE_END (output);
795
TEST_FILE_RESET (output);
798
/* Check that an invalid long option is ignored if there's a
799
* catch-all option in the list.
801
TEST_FEATURE ("with invalid long option and catch-all");
803
argv[argc++] = "ignored";
804
argv[argc++] = "--zoiks";
806
args = nih_option_parser (NULL, argc, argv, catch_options, FALSE);
808
TEST_NE_P (args, NULL);
813
/* Check that an unexpected argument to a long option causes an
814
* error message to be output with a suggestion of help, and NULL
817
TEST_FEATURE ("with unexpected long option argument");
1190
819
argv[argc++] = "ignored";
1191
820
argv[argc++] = "--wibble=woo";
1192
821
argv[argc] = NULL;
1194
dup2 (fileno (output), STDERR_FILENO);
1195
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1196
dup2 (oldstderr, STDERR_FILENO);
1200
/* Return value should be NULL */
1202
printf ("BAD: return value wasn't what we expected.\n");
1206
/* Output should be message with program name and newline */
1207
fgets (text, sizeof (text), output);
1208
if (strcmp (text, "test: unexpected argument: --wibble=woo\n")) {
1209
printf ("BAD: output wasn't what we expected.\n");
1213
/* Should include a suggestion of --help */
1214
fgets (text, sizeof (text), output);
1215
if (strcmp (text, "Try `test --help' for more information.\n")) {
1216
printf ("BAD: output wasn't what we expected.\n");
1220
/* Should be no more output */
1221
if (fgets (text, sizeof (text), output)) {
1222
printf ("BAD: more output than we expected.\n");
1227
ftruncate (fileno (output), 0);
1230
printf ("...with missing short option argument\n");
823
TEST_DIVERT_STDERR (output) {
824
args = nih_option_parser (NULL, argc, argv, options, FALSE);
828
TEST_EQ_P (args, NULL);
830
TEST_FILE_EQ (output, "test: unexpected argument: --wibble=woo\n");
831
TEST_FILE_EQ (output, "Try `test --help' for more information.\n");
832
TEST_FILE_END (output);
834
TEST_FILE_RESET (output);
837
/* Check that an missing argument to a short option causes an error
838
* message to be output with a suggestion of help, and NULL to be
841
TEST_FEATURE ("with missing short option argument");
1232
843
argv[argc++] = "ignored";
1233
844
argv[argc++] = "-f";
1234
845
argv[argc] = NULL;
1235
dup2 (fileno (output), STDERR_FILENO);
1236
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1237
dup2 (oldstderr, STDERR_FILENO);
1241
/* Return value should be NULL */
1243
printf ("BAD: return value wasn't what we expected.\n");
1247
/* Output should be message with program name and newline */
1248
fgets (text, sizeof (text), output);
1249
if (strcmp (text, "test: missing argument: -f\n")) {
1250
printf ("BAD: output wasn't what we expected.\n");
1254
/* Should include a suggestion of --help */
1255
fgets (text, sizeof (text), output);
1256
if (strcmp (text, "Try `test --help' for more information.\n")) {
1257
printf ("BAD: output wasn't what we expected.\n");
1261
/* Should be no more output */
1262
if (fgets (text, sizeof (text), output)) {
1263
printf ("BAD: more output than we expected.\n");
1268
ftruncate (fileno (output), 0);
1271
printf ("...with missing long option argument\n");
847
TEST_DIVERT_STDERR (output) {
848
args = nih_option_parser (NULL, argc, argv, options, FALSE);
852
TEST_EQ_P (args, NULL);
854
TEST_FILE_EQ (output, "test: missing argument: -f\n");
855
TEST_FILE_EQ (output, "Try `test --help' for more information.\n");
856
TEST_FILE_END (output);
858
TEST_FILE_RESET (output);
861
/* Check that an missing argument to a long option causes an error
862
* message to be output with a suggestion of help, and NULL to be
865
TEST_FEATURE ("with missing long option argument");
1273
867
argv[argc++] = "ignored";
1274
868
argv[argc++] = "--filename";
1275
869
argv[argc] = NULL;
1277
dup2 (fileno (output), STDERR_FILENO);
1278
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1279
dup2 (oldstderr, STDERR_FILENO);
1283
/* Return value should be NULL */
1285
printf ("BAD: return value wasn't what we expected.\n");
1289
/* Output should be message with program name and newline */
1290
fgets (text, sizeof (text), output);
1291
if (strcmp (text, "test: missing argument: --filename\n")) {
1292
printf ("BAD: output wasn't what we expected.\n");
1296
/* Should include a suggestion of --help */
1297
fgets (text, sizeof (text), output);
1298
if (strcmp (text, "Try `test --help' for more information.\n")) {
1299
printf ("BAD: output wasn't what we expected.\n");
1303
/* Should be no more output */
1304
if (fgets (text, sizeof (text), output)) {
1305
printf ("BAD: more output than we expected.\n");
1310
ftruncate (fileno (output), 0);
1313
printf ("...with short setter option\n");
871
TEST_DIVERT_STDERR (output) {
872
args = nih_option_parser (NULL, argc, argv, options, FALSE);
876
TEST_EQ_P (args, NULL);
878
TEST_FILE_EQ (output, "test: missing argument: --filename\n");
879
TEST_FILE_EQ (output, "Try `test --help' for more information.\n");
880
TEST_FILE_END (output);
882
TEST_FILE_RESET (output);
885
/* Check that a short option may result in a function call, and
886
* that the arguments to that call are correct.
888
TEST_FEATURE ("with short setter option");
1315
890
argv[argc++] = "ignored";
1316
891
argv[argc++] = "-x";
1317
892
argv[argc++] = "foo";
1318
893
argv[argc] = NULL;
1320
896
last_option = NULL;
1321
897
last_arg = NULL;
1322
898
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1324
/* First array entry should be first argument */
1325
if (strcmp (args[0], "foo")) {
1326
printf ("BAD: return value wasn't what we expected.\n");
1330
/* Return value should be a NULL array */
1331
if (args[1] != NULL) {
1332
printf ("BAD: return value wasn't what we expected.\n");
1336
/* Setter function should have been called */
1338
printf ("BAD: setter function was not called.\n");
1342
/* Option passed should have been the execute one */
1343
if (last_option != &(options[6])) {
1344
printf ("BAD: setter's option wasn't what we expected.\n");
1348
/* Argument passed to setter should have been NULL */
1349
if (last_arg != NULL) {
1350
printf ("BAD setter's argument wasn't what we expected.\n");
900
TEST_NE_P (args, NULL);
901
TEST_EQ_STR (args[0], "foo");
902
TEST_EQ_P (args[1], NULL);
904
TEST_TRUE (was_called);
905
TEST_EQ (last_option->option, options[6].option);
906
TEST_EQ_P (last_arg, NULL);
1354
908
nih_free (args);
1357
printf ("...with short setter argument option\n");
909
nih_free (last_option);
912
/* Check that a short option that takens an argument can result in
913
* a function call, and that the argument is also passed to the
916
TEST_FEATURE ("with short setter argument option");
1359
918
argv[argc++] = "ignored";
1360
919
argv[argc++] = "-s";
1361
920
argv[argc++] = "foo";
1362
921
argv[argc] = NULL;
1364
924
last_option = NULL;
1365
925
last_arg = NULL;
1366
926
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1368
/* Return value should be a NULL array */
1369
if (args[0] != NULL) {
1370
printf ("BAD: return value wasn't what we expected.\n");
1374
/* Setter function should have been called */
1376
printf ("BAD: setter function was not called.\n");
1380
/* Option passed should have been the special one */
1381
if (last_option != &(options[5])) {
1382
printf ("BAD: setter's option wasn't what we expected.\n");
1386
/* Argument passed to setter should have been foo */
1387
if (strcmp (last_arg, "foo")) {
1388
printf ("BAD setter's argument wasn't what we expected.\n");
928
TEST_NE_P (args, NULL);
929
TEST_EQ_P (args[0], NULL);
931
TEST_TRUE (was_called);
932
TEST_EQ (last_option->option, options[5].option);
933
TEST_EQ_STR (last_arg, "foo");
1392
935
nih_free (args);
1395
printf ("...with short setter embedded argument option\n");
936
nih_free (last_option);
939
/* Check that the setter function is stilled correctly if the
940
* argument to the short option is embedded within it.
942
TEST_FEATURE ("with short setter embedded argument option");
1397
944
argv[argc++] = "ignored";
1398
945
argv[argc++] = "-sfoo";
1399
946
argv[argc] = NULL;
1401
949
last_option = NULL;
1402
950
last_arg = NULL;
1403
951
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1405
/* Return value should be a NULL array */
1406
if (args[0] != NULL) {
1407
printf ("BAD: return value wasn't what we expected.\n");
1411
/* Setter function should have been called */
1413
printf ("BAD: setter function was not called.\n");
1417
/* Option passed should have been the special one */
1418
if (last_option != &(options[5])) {
1419
printf ("BAD: setter's option wasn't what we expected.\n");
1423
/* Argument passed to setter should have been foo */
1424
if (strcmp (last_arg, "foo")) {
1425
printf ("BAD setter's argument wasn't what we expected.\n");
953
TEST_NE_P (args, NULL);
954
TEST_EQ_P (args[0], NULL);
956
TEST_TRUE (was_called);
957
TEST_EQ (last_option->option, options[5].option);
958
TEST_EQ_STR (last_arg, "foo");
1429
960
nih_free (args);
1432
printf ("...with long setter option\n");
961
nih_free (last_option);
964
/* Check that a long option may result in a function call, and
965
* that the arguments to that call are correct.
967
TEST_FEATURE ("with long setter option");
1434
969
argv[argc++] = "ignored";
1435
970
argv[argc++] = "--execute";
1436
971
argv[argc++] = "foo";
1437
972
argv[argc] = NULL;
1439
975
last_option = NULL;
1440
976
last_arg = NULL;
1441
977
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1443
/* First array entry should be first argument */
1444
if (strcmp (args[0], "foo")) {
1445
printf ("BAD: return value wasn't what we expected.\n");
1449
/* Return value should be a NULL array */
1450
if (args[1] != NULL) {
1451
printf ("BAD: return value wasn't what we expected.\n");
1455
/* Setter function should have been called */
1457
printf ("BAD: setter function was not called.\n");
1461
/* Option passed should have been the execute one */
1462
if (last_option != &(options[6])) {
1463
printf ("BAD: setter's option wasn't what we expected.\n");
1467
/* Argument passed to setter should have been NULL */
1468
if (last_arg != NULL) {
1469
printf ("BAD setter's argument wasn't what we expected.\n");
979
TEST_NE_P (args, NULL);
980
TEST_EQ_STR (args[0], "foo");
981
TEST_EQ_P (args[1], NULL);
983
TEST_TRUE (was_called);
984
TEST_EQ (last_option->option, options[6].option);
985
TEST_EQ_P (last_arg, NULL);
1473
987
nih_free (args);
1476
printf ("...with long setter argument option\n");
988
nih_free (last_option);
991
/* Check that a short option that takens an argument can result in
992
* a function call, and that the argument is also passed to the
995
TEST_FEATURE ("with long setter argument option");
1478
997
argv[argc++] = "ignored";
1479
998
argv[argc++] = "--special";
1484
1003
last_arg = NULL;
1485
1004
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1487
/* Return value should be a NULL array */
1488
if (args[0] != NULL) {
1489
printf ("BAD: return value wasn't what we expected.\n");
1493
/* Setter function should have been called */
1495
printf ("BAD: setter function was not called.\n");
1499
/* Option passed should have been the special one */
1500
if (last_option != &(options[5])) {
1501
printf ("BAD: setter's option wasn't what we expected.\n");
1505
/* Argument passed to setter should have been foo */
1506
if (strcmp (last_arg, "foo")) {
1507
printf ("BAD setter's argument wasn't what we expected.\n");
1006
TEST_NE_P (args, NULL);
1007
TEST_EQ_P (args[0], NULL);
1009
TEST_TRUE (was_called);
1010
TEST_EQ (last_option->option, options[5].option);
1011
TEST_EQ_STR (last_arg, "foo");
1511
1013
nih_free (args);
1514
printf ("...with long setter embedded argument option\n");
1014
nih_free (last_option);
1017
/* Check that the setter function is stilled correctly if the
1018
* argument to the short option is embedded within it.
1020
TEST_FEATURE ("with long setter embedded argument option");
1516
1022
argv[argc++] = "ignored";
1517
1023
argv[argc++] = "--special=foo";
1518
1024
argv[argc] = NULL;
1519
1026
was_called = 0;
1520
1027
last_option = NULL;
1521
1028
last_arg = NULL;
1522
1029
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1524
/* Return value should be a NULL array */
1525
if (args[0] != NULL) {
1526
printf ("BAD: return value wasn't what we expected.\n");
1530
/* Setter function should have been called */
1532
printf ("BAD: setter function was not called.\n");
1536
/* Option passed should have been the special one */
1537
if (last_option != &(options[5])) {
1538
printf ("BAD: setter's option wasn't what we expected.\n");
1542
/* Argument passed to setter should have been foo */
1543
if (strcmp (last_arg, "foo")) {
1544
printf ("BAD setter's argument wasn't what we expected.\n");
1031
TEST_NE_P (args, NULL);
1032
TEST_EQ_P (args[0], NULL);
1034
TEST_TRUE (was_called);
1035
TEST_EQ (last_option->option, options[5].option);
1036
TEST_EQ_STR (last_arg, "foo");
1548
1038
nih_free (args);
1551
printf ("...with short setter embedded argument error\n");
1039
nih_free (last_option);
1042
/* Check that an error code returned from a setter function for a
1043
* short option results in NULL being returned by the parser, but
1044
* no error message output (that's left up to the function).
1046
TEST_FEATURE ("with short setter embedded argument error");
1553
1048
argv[argc++] = "ignored";
1554
1049
argv[argc++] = "-sfail";
1555
1050
argv[argc] = NULL;
1556
1052
was_called = 0;
1557
1053
last_option = NULL;
1558
1054
last_arg = NULL;
1560
dup2 (fileno (output), STDERR_FILENO);
1561
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1562
dup2 (oldstderr, STDERR_FILENO);
1566
/* Return value should be NULL */
1568
printf ("BAD: return value wasn't what we expected.\n");
1572
/* Setter function should have been called */
1574
printf ("BAD: setter function was not called.\n");
1578
/* Option passed should have been the special one */
1579
if (last_option != &(options[5])) {
1580
printf ("BAD: setter's option wasn't what we expected.\n");
1584
/* Argument passed to setter should have been fail */
1585
if (strcmp (last_arg, "fail")) {
1586
printf ("BAD setter's argument wasn't what we expected.\n");
1590
/* Should be no output */
1591
if (fgets (text, sizeof (text), output)) {
1592
printf ("BAD: more output than we expected.\n");
1597
ftruncate (fileno (output), 0);
1600
printf ("...with long setter embedded argument error\n");
1056
TEST_DIVERT_STDERR (output) {
1057
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1061
TEST_EQ_P (args, NULL);
1063
TEST_TRUE (was_called);
1064
TEST_EQ (last_option->option, options[5].option);
1065
TEST_EQ_STR (last_arg, "fail");
1067
TEST_FILE_END (output);
1069
TEST_FILE_RESET (output);
1070
nih_free (last_option);
1073
/* Check that an error code returned from a setter function for a
1074
* long option results in NULL being returned by the parser, but
1075
* no error message output (that's left up to the function).
1077
TEST_FEATURE ("with long setter embedded argument error");
1602
1079
argv[argc++] = "ignored";
1603
1080
argv[argc++] = "--special=fail";
1604
1081
argv[argc] = NULL;
1605
1083
was_called = 0;
1606
1084
last_option = NULL;
1607
1085
last_arg = NULL;
1609
dup2 (fileno (output), STDERR_FILENO);
1610
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1611
dup2 (oldstderr, STDERR_FILENO);
1615
/* Return value should be NULL */
1617
printf ("BAD: return value wasn't what we expected.\n");
1621
/* Setter function should have been called */
1623
printf ("BAD: setter function was not called.\n");
1627
/* Option passed should have been the special one */
1628
if (last_option != &(options[5])) {
1629
printf ("BAD: setter's option wasn't what we expected.\n");
1633
/* Argument passed to setter should have been fail */
1634
if (strcmp (last_arg, "fail")) {
1635
printf ("BAD setter's argument wasn't what we expected.\n");
1639
/* Should be no output */
1640
if (fgets (text, sizeof (text), output)) {
1641
printf ("BAD: more output than we expected.\n");
1646
ftruncate (fileno (output), 0);
1087
TEST_DIVERT_STDERR (output) {
1088
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1092
TEST_EQ_P (args, NULL);
1094
TEST_TRUE (was_called);
1095
TEST_EQ (last_option->option, options[5].option);
1096
TEST_EQ_STR (last_arg, "fail");
1098
TEST_FILE_END (output);
1100
nih_free (last_option);
1649
1102
fclose (output);
1657
1107
test_count (void)
1660
int ret = 0, value = 0, retval;
1662
printf ("Testing nih_option_count()\n");
1112
TEST_FUNCTION ("nih_option_count");
1114
/* Check that the count function treats the option value as an
1115
* integer pointer, and increments it.
1117
TEST_FEATURE ("with zero value");
1663
1118
opt.value = &value;
1664
retval = nih_option_count (&opt, NULL);
1666
/* Return value should be zero */
1668
printf ("BAD: return value wasn't what we expected.\n");
1672
/* Value should be incremented */
1674
printf ("BAD: value wasn't what we expected.\n");
1679
retval = nih_option_count (&opt, NULL);
1681
/* Return value should be zero */
1683
printf ("BAD: return value wasn't what we expected.\n");
1687
/* Value should be incremented */
1689
printf ("BAD: value wasn't what we expected.\n");
1119
ret = nih_option_count (&opt, NULL);
1125
/* Check that calling again increments the value to two. */
1126
TEST_FEATURE ("with non-zero value");
1127
ret = nih_option_count (&opt, NULL);
1710
1146
test_quiet (void)
1712
1148
char *argv[3], **args;
1715
printf ("Testing nih_option_quiet()\n");
1151
TEST_FUNCTION ("nih_option_quiet");
1716
1152
program_name = "test";
1717
1153
nih_log_set_logger (my_logger);
1719
printf ("...with long option\n");
1155
/* Check that the --quiet option is automatically understood, and
1156
* sets the log level such that only the error message is output.
1158
TEST_FEATURE ("with long option");
1721
1160
argv[argc++] = "ignored";
1722
1161
argv[argc++] = "--quiet";
1723
1162
argv[argc] = NULL;
1724
1165
nih_log_set_priority (NIH_LOG_WARN);
1726
1167
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1169
nih_debug ("test message");
1727
1170
nih_info ("test message");
1728
1171
nih_warn ("test message");
1729
1172
nih_error ("test message");
1731
/* Return value should be a NULL array */
1732
if (args[0] != NULL) {
1733
printf ("BAD: return value wasn't what we expected.\n");
1737
/* Logger should have been called only once */
1738
if (logger_called != 1) {
1739
printf ("BAD: priority wasn't what we expected.\n");
1174
TEST_NE_P (args, NULL);
1175
TEST_EQ_P (args[0], NULL);
1176
TEST_EQ (logger_called, 1);
1743
1178
nih_free (args);
1746
printf ("...with short option\n");
1181
/* Check that the -q option has the same effect. */
1182
TEST_FEATURE ("with short option");
1748
1184
argv[argc++] = "ignored";
1749
1185
argv[argc++] = "-q";
1750
1186
argv[argc] = NULL;
1751
1189
nih_log_set_priority (NIH_LOG_WARN);
1753
1191
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1193
nih_debug ("test message");
1754
1194
nih_info ("test message");
1755
1195
nih_warn ("test message");
1756
1196
nih_error ("test message");
1758
/* Return value should be a NULL array */
1759
if (args[0] != NULL) {
1760
printf ("BAD: return value wasn't what we expected.\n");
1764
/* Logger should have been called only once */
1765
if (logger_called != 1) {
1766
printf ("BAD: priority wasn't what we expected.\n");
1198
TEST_NE_P (args, NULL);
1199
TEST_EQ_P (args[0], NULL);
1200
TEST_EQ (logger_called, 1);
1770
1202
nih_free (args);
1773
1205
nih_log_set_priority (NIH_LOG_INFO);
1774
1206
nih_log_set_logger (nih_logger_printf);
1780
1210
test_verbose (void)
1782
1212
char *argv[3], **args;
1785
printf ("Testing nih_option_verbose()\n");
1215
TEST_FUNCTION ("nih_option_verbose");
1786
1216
program_name = "test";
1787
1217
nih_log_set_logger (my_logger);
1789
printf ("...with long option\n");
1219
/* Check that the --verbose option is automatically understood,
1220
* and sets the log level such that messages of info, warn and
1221
* error priority are output.
1223
TEST_FEATURE ("with long option");
1791
1225
argv[argc++] = "ignored";
1792
1226
argv[argc++] = "--verbose";
1793
1227
argv[argc] = NULL;
1794
1230
nih_log_set_priority (NIH_LOG_WARN);
1796
1232
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1234
nih_debug ("test message");
1797
1235
nih_info ("test message");
1798
1236
nih_warn ("test message");
1799
1237
nih_error ("test message");
1801
/* Return value should be a NULL array */
1802
if (args[0] != NULL) {
1803
printf ("BAD: return value wasn't what we expected.\n");
1807
/* Logger should have been called three times */
1808
if (logger_called != 3) {
1809
printf ("BAD: priority wasn't what we expected.\n");
1816
printf ("...with short option\n");
1239
TEST_NE_P (args, NULL);
1240
TEST_EQ_P (args[0], NULL);
1241
TEST_EQ (logger_called, 3);
1244
/* Check that the -v option has the same effect. */
1245
TEST_FEATURE ("with short option");
1819
1247
argv[argc++] = "ignored";
1820
1248
argv[argc++] = "-v";
1821
1249
argv[argc] = NULL;
1822
1252
nih_log_set_priority (NIH_LOG_WARN);
1824
1254
args = nih_option_parser (NULL, argc, argv, options, FALSE);
1256
nih_debug ("test message");
1825
1257
nih_info ("test message");
1826
1258
nih_warn ("test message");
1827
1259
nih_error ("test message");
1829
/* Return value should be a NULL array */
1830
if (args[0] != NULL) {
1831
printf ("BAD: return value wasn't what we expected.\n");
1835
/* Logger should have been called three times */
1836
if (logger_called != 3) {
1837
printf ("BAD: priority wasn't what we expected.\n");
1261
TEST_NE_P (args, NULL);
1262
TEST_EQ_P (args[0], NULL);
1263
TEST_EQ (logger_called, 3);
1841
1265
nih_free (args);
1844
1268
nih_log_set_priority (NIH_LOG_INFO);
1845
1269
nih_log_set_logger (nih_logger_printf);
1851
1273
test_debug (void)
1853
1275
char *argv[3], **args;
1856
printf ("Testing nih_option_debug()\n");
1278
/* Check that the --debug option is automatically understood,
1279
* and sets the log level such that messages of all priorities
1282
TEST_FUNCTION ("nih_option_debug");
1857
1283
program_name = "test";
1858
1284
nih_log_set_logger (my_logger);
1911
1330
argv[argc] = NULL;
1913
1332
output = tmpfile ();
1916
dup2 (fileno (output), STDOUT_FILENO);
1917
nih_option_parser (NULL, argc, argv, options, FALSE);
1334
TEST_DIVERT_STDOUT (output) {
1335
nih_option_parser (NULL, argc, argv, options, FALSE);
1921
1340
waitpid (pid, &status, 0);
1922
1341
rewind (output);
1924
/* Should have exited normally */
1925
if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0)) {
1926
printf ("BAD: process did not exit normally.\n");
1930
/* First line of output should be package string */
1931
fgets (text, sizeof (text), output);
1932
if (strcmp (text, "test (wibble 1.0)\n")) {
1933
printf ("BAD: package line wasn't what we expected.\n");
1937
/* Second line of output should be copyright message */
1938
fgets (text, sizeof (text), output);
1939
if (strcmp (text, "Copyright Message\n")) {
1940
printf ("BAD: copyright line wasn't what we expected.\n");
1944
/* Third line of output should be a blank line */
1945
fgets (text, sizeof (text), output);
1946
if (strcmp (text, "\n")) {
1947
printf ("BAD: output wasn't what we expected.\n");
1951
/* Fourth line should be start of GPL preamble */
1952
fgets (text, sizeof (text), output);
1953
if (strncmp (text, "This is free software;", 22)) {
1954
printf ("BAD: first licence line wasn't what we expected.\n");
1958
/* Fifth line should be GPL preamble */
1959
fgets (text, sizeof (text), output);
1960
if (strncmp (text, "warranty; not even for", 22)) {
1961
printf ("BAD: second licence line wasn't what we expected.\n");
1965
/* Should be no more output */
1966
if (fgets (text, sizeof (text), output)) {
1967
printf ("BAD: more output than we expected.\n");
1343
TEST_TRUE (WIFEXITED (status));
1344
TEST_EQ (WEXITSTATUS (status), 0);
1346
TEST_FILE_EQ (output, "test (wibble 1.0)\n");
1347
TEST_FILE_EQ (output, "Copyright Message\n");
1348
TEST_FILE_EQ (output, "\n");
1349
TEST_FILE_EQ_N (output, "This is free software;");
1350
TEST_FILE_EQ_N (output, "warranty; not even for");
1351
TEST_FILE_END (output);
1971
1353
fclose (output);
1977
1357
test_help (void)
1983
int ret = 0, argc, status;
1985
printf ("Testing nih_option_set_usage()\n");
1986
nih_option_set_usage ("CMD [ARG]...\n");
1989
printf ("Testing nih_option_help()\n");
1990
program_name = "test";
1991
package_bugreport = "foo@bar.com";
1364
/* Check that these functions set their appropriate string, this
1365
* is only possible by checking the help output, so we call them
1366
* and do the tests later.
1368
TEST_FUNCTION ("nih_option_set_usage_stem");
1369
nih_option_set_usage_stem ("[OPT]...");
1371
TEST_FUNCTION ("nih_option_set_usage");
1372
nih_option_set_usage ("CMD [ARG]...");
1374
TEST_FUNCTION ("nih_option_set_synopsis");
1375
nih_option_set_synopsis ("Frobnicates bars carefully, taking into "
1376
"account things that are important when "
1379
TEST_FUNCTION ("nih_option_set_help");
1380
nih_option_set_help ("This is the help text for the bar frobnication "
1382
"It is also wrapped to the screen width, so it "
1383
"can be as long as we like, and can also include "
1384
"paragraph breaks and stuff.");
1386
TEST_FUNCTION ("nih_option_set_footer");
1387
nih_option_set_footer ("Go away!");
1390
/* Check that the --help option is caught, dealt with by outputting
1391
* information about the options to standard output, and terminating
1392
* the process with a zero exit code.
1394
TEST_FUNCTION ("nih_option_help");
1395
nih_main_init_full ("test", "wibble", "1.0",
1396
"foo@bar.com", "Copyright Message");
1994
1399
argv[argc++] = "ignored";
1996
1401
argv[argc] = NULL;
1998
1403
output = tmpfile ();
2001
dup2 (fileno (output), STDOUT_FILENO);
2002
nih_option_parser (NULL, argc, argv, options, FALSE);
1405
unsetenv ("COLUMNS");
1407
TEST_DIVERT_STDOUT (output) {
1408
nih_option_parser (NULL, argc, argv, options, FALSE);
2006
1413
waitpid (pid, &status, 0);
2007
1414
rewind (output);
2009
/* Should have exited normally */
2010
if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0)) {
2011
printf ("BAD: process did not exit normally.\n");
2015
/* First line of output should be usage string */
2016
fgets (text, sizeof (text), output);
2017
if (strcmp (text, "Usage: test [OPTION]... CMD [ARG]...\n")) {
2018
printf ("BAD: usage line wasn't what we expected.\n");
2022
/* Second line of output should be a blank line */
2023
fgets (text, sizeof (text), output);
2024
if (strcmp (text, "\n")) {
2025
printf ("BAD: output wasn't what we expected.\n");
2030
/* Start of first option group encountered */
2031
fgets (text, sizeof (text), output);
2032
if (strcmp (text, "First test group options:\n")) {
2033
printf ("BAD: output wasn't what we expected.\n");
2037
fgets (text, sizeof (text), output);
2038
if (strcmp (text, (" -d "
2039
"become daemon\n"))) {
2040
printf ("BAD: output wasn't what we expected.\n");
2044
fgets (text, sizeof (text), output);
2045
if (strcmp (text, (" -f, --filename=FILENAME "
2046
"read this file\n"))) {
2047
printf ("BAD: output wasn't what we expected.\n");
2051
fgets (text, sizeof (text), output);
2052
if (strcmp (text, (" -x, --execute "
2053
"run something, give this a really long help\n"))) {
2054
printf ("BAD: output wasn't what we expected.\n");
2058
fgets (text, sizeof (text), output);
2059
if (strcmp (text, (" "
2060
" message so that it word wraps\n"))) {
2061
printf ("BAD: output wasn't what we expected.\n");
2065
fgets (text, sizeof (text), output);
2066
if (strcmp (text, (" -I DIRECTORY "
2067
"add directory to include list\n"))) {
2068
printf ("BAD: output wasn't what we expected.\n");
2072
/* Next line of output should be a blank line */
2073
fgets (text, sizeof (text), output);
2074
if (strcmp (text, "\n")) {
2075
printf ("BAD: output wasn't what we expected.\n");
2080
/* Start of second option group encountered */
2081
fgets (text, sizeof (text), output);
2082
if (strcmp (text, "Second test group options:\n")) {
2083
printf ("BAD: output wasn't what we expected.\n");
2087
fgets (text, sizeof (text), output);
2088
if (strcmp (text, (" -R, --recursive "
2089
"descend into sub-directories\n"))) {
2090
printf ("BAD: output wasn't what we expected.\n");
2094
fgets (text, sizeof (text), output);
2095
if (strcmp (text, (" --wibble "
2096
"bored of inventing names\n"))) {
2097
printf ("BAD: output wasn't what we expected.\n");
2101
fgets (text, sizeof (text), output);
2102
if (strcmp (text, (" -o, --option=OPTION "
2103
"extended options\n"))) {
2104
printf ("BAD: output wasn't what we expected.\n");
2108
fgets (text, sizeof (text), output);
2109
if (strcmp (text, (" -s, --special=SPECIAL-LONG-ARGUMENT-NAME\n"))) {
2110
printf ("BAD: output wasn't what we expected.\n");
2114
fgets (text, sizeof (text), output);
2115
if (strcmp (text, (" "
2116
"something with special treatment\n"))) {
2117
printf ("BAD: output wasn't what we expected.\n");
2121
/* Next line of output should be a blank line */
2122
fgets (text, sizeof (text), output);
2123
if (strcmp (text, "\n")) {
2124
printf ("BAD: output wasn't what we expected.\n");
2129
/* Start of default option group encountered */
2130
fgets (text, sizeof (text), output);
2131
if (strcmp (text, "Other options:\n")) {
2132
printf ("BAD: output wasn't what we expected.\n");
2136
fgets (text, sizeof (text), output);
2137
if (strcmp (text, (" -q, --quiet "
2138
"reduce output to errors only\n"))) {
2139
printf ("BAD: output wasn't what we expected.\n");
2143
fgets (text, sizeof (text), output);
2144
if (strcmp (text, (" -v, --verbose "
2145
"increase output to include informational "
2147
printf ("BAD: output wasn't what we expected.\n");
2151
fgets (text, sizeof (text), output);
2152
if (strcmp (text, (" --help "
2153
"display this help and exit\n"))) {
2154
printf ("BAD: output wasn't what we expected.\n");
2158
fgets (text, sizeof (text), output);
2159
if (strcmp (text, (" --version "
2160
"output version information and exit\n"))) {
2161
printf ("BAD: output wasn't what we expected.\n");
2165
/* Next line of output should be a blank line */
2166
fgets (text, sizeof (text), output);
2167
if (strcmp (text, "\n")) {
2168
printf ("BAD: output wasn't what we expected.\n");
2173
/* Last line should be bug report address */
2174
fgets (text, sizeof (text), output);
2175
if (strcmp (text, "Report bugs to <foo@bar.com>\n")) {
2176
printf ("BAD: bug report line wasn't what we expected.\n");
2180
/* Should be no more output */
2181
if (fgets (text, sizeof (text), output)) {
2182
printf ("BAD: more output than we expected.\n");
1416
TEST_TRUE (WIFEXITED (status));
1417
TEST_EQ (WEXITSTATUS (status), 0);
1419
TEST_FILE_EQ (output, "Usage: test [OPT]... CMD [ARG]...\n");
1420
TEST_FILE_EQ (output, ("Frobnicates bars carefully, taking into "
1421
"account things that are important when\n"));
1422
TEST_FILE_EQ (output, ("doing that\n"));
1423
TEST_FILE_EQ (output, "\n");
1424
TEST_FILE_EQ (output, "First test group options:\n");
1425
TEST_FILE_EQ (output, (" -d "
1426
"become daemon\n"));
1427
TEST_FILE_EQ (output, (" -f, --filename=FILENAME "
1428
"read this file\n"));
1429
TEST_FILE_EQ (output, (" -x, --execute "
1430
"run something, give this a really long help\n"));
1431
TEST_FILE_EQ (output, (" "
1432
" message so that it word wraps\n"));
1433
TEST_FILE_EQ (output, (" -I DIRECTORY "
1434
"add directory to include list\n"));
1435
TEST_FILE_EQ (output, "\n");
1436
TEST_FILE_EQ (output, "Second test group options:\n");
1437
TEST_FILE_EQ (output, (" -R, --recursive "
1438
"descend into sub-directories\n"));
1439
TEST_FILE_EQ (output, (" --wibble "
1440
"bored of inventing names\n"));
1441
TEST_FILE_EQ (output, (" -o, --option=OPTION "
1442
"extended options\n"));
1443
TEST_FILE_EQ (output, (" -s, --special=SPECIAL-LONG-ARGUMENT-NAME\n"));
1444
TEST_FILE_EQ (output, (" "
1445
"something with special treatment\n"));
1446
TEST_FILE_EQ (output, "\n");
1447
TEST_FILE_EQ (output, "Other options:\n");
1448
TEST_FILE_EQ (output, (" -q, --quiet "
1449
"reduce output to errors only\n"));
1450
TEST_FILE_EQ (output, (" -v, --verbose "
1451
"increase output to include informational "
1453
TEST_FILE_EQ (output, (" --help "
1454
"display this help and exit\n"));
1455
TEST_FILE_EQ (output, (" --version "
1456
"output version information and exit\n"));
1457
TEST_FILE_EQ (output, "\n");
1458
TEST_FILE_EQ (output, ("This is the help text for the bar frobnication "
1460
TEST_FILE_EQ (output, "\n");
1461
TEST_FILE_EQ (output, ("It is also wrapped to the screen width, so it "
1462
"can be as long as we like, and\n"));
1463
TEST_FILE_EQ (output, "can also include paragraph breaks and stuff.\n");
1464
TEST_FILE_EQ (output, "\n");
1465
TEST_FILE_EQ (output, "Go away!\n");
1466
TEST_FILE_EQ (output, "\n");
1467
TEST_FILE_EQ (output, "Report bugs to <foo@bar.com>\n");
1468
TEST_FILE_END (output);
2186
1470
fclose (output);