~ubuntu-branches/debian/sid/upstart/sid

« back to all changes in this revision

Viewing changes to nih/tests/test_option.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2006-12-13 17:27:37 UTC
  • mfrom: (1.1.7 upstream)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: james.westby@ubuntu.com-20061213172737-i8969k38jl3b8ruu
Tags: 0.3.1-1
* New upstream release:
  - start, stop and status are now symlinks to initctl, not to a
    different, separate utility.
  - initctl completely rewritten to behave properly.
  - some upstart-specific options to shutdown and reboot dropped, as
    these are considered SysV-compat tools.
  - "console none" fixed.  LP: #70782.
  - improved documentation.  LP: #68805.

* shutdown and reboot moved to upstart-compat-sysv.

* Replace the /usr/share/doc/* directory in upstart-logd,
  upstart-compat-sysv, system-services and startup-tasks with a symlink to
  /usr/share/doc/upstart.  This was actually done in a previous package,
  but the migration missed.  LP: #70895.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20
20
 */
21
21
 
22
 
#include <config.h>
23
 
 
 
22
#include <nih/test.h>
24
23
 
25
24
#include <sys/types.h>
26
25
#include <sys/wait.h>
27
26
 
28
27
#include <stdio.h>
 
28
#include <stdlib.h>
29
29
#include <string.h>
30
 
#include <unistd.h>
31
30
 
 
31
#include <nih/macros.h>
32
32
#include <nih/alloc.h>
33
33
#include <nih/main.h>
34
34
#include <nih/option.h>
49
49
my_setter (NihOption *option, const char *arg)
50
50
{
51
51
        was_called++;
52
 
        last_option = option;
 
52
 
 
53
        last_option = nih_new (NULL, NihOption);
 
54
        memcpy (last_option, option, sizeof (NihOption));
 
55
 
53
56
        last_arg = arg;
54
 
 
55
57
        if (arg && (! strcmp (arg, "fail")))
56
58
                return -1;
57
59
 
89
91
};
90
92
 
91
93
 
92
 
int
 
94
void
93
95
test_parser (void)
94
96
{
95
97
        FILE *output;
96
 
        char  text[81];
97
98
        char *argv[16], **args;
98
 
        int   oldstderr, ret = 0, argc;
 
99
        int   argc;
99
100
 
100
 
        printf ("Testing nih_option_parser()\n");
 
101
        TEST_FUNCTION ("nih_option_parser");
 
102
        output = tmpfile ();
101
103
        program_name = "test";
102
104
 
103
 
        output = tmpfile ();
104
 
        oldstderr = dup (STDERR_FILENO);
105
 
 
106
 
 
107
 
        printf ("...with no arguments\n");
 
105
 
 
106
        /* Check that the option parser can be called with no arguments,
 
107
         * which results in a single-element array being returned with
 
108
         * NULL in the array.
 
109
         */
 
110
        TEST_FEATURE ("with no arguments");
108
111
        argc = 0;
109
112
        argv[argc++] = "ignored";
110
113
        argv[argc] = NULL;
 
114
 
111
115
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
112
116
 
113
 
        /* Return value should be a NULL array */
114
 
        if (args[0] != NULL) {
115
 
                printf ("BAD: return value wasn't what we expected.\n");
116
 
                ret = 1;
117
 
        }
 
117
        TEST_NE_P (args, NULL);
 
118
        TEST_EQ_P (args[0], NULL);
118
119
 
119
120
        nih_free (args);
120
121
 
121
122
 
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.
 
125
         */
 
126
        TEST_FEATURE ("with all non-option arguments");
123
127
        argc = 0;
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;
 
133
 
129
134
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
130
135
 
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");
134
 
                ret = 1;
135
 
        }
136
 
 
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");
140
 
                ret = 1;
141
 
        }
142
 
 
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");
146
 
                ret = 1;
147
 
        }
148
 
 
149
 
        /* Return value should be a NULL array */
150
 
        if (args[3] != NULL) {
151
 
                printf ("BAD: return value wasn't what we expected.\n");
152
 
                ret = 1;
153
 
        }
 
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);
154
141
 
155
142
        nih_free (args);
156
143
 
157
144
 
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.
 
147
         */
 
148
        TEST_FEATURE ("with single short option");
159
149
        argc = 0;
160
150
        argv[argc++] = "ignored";
161
151
        argv[argc++] = "-d";
162
152
        argv[argc] = NULL;
 
153
 
163
154
        daemonise = 0;
164
155
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
165
156
 
166
 
        /* Return value should be a NULL array */
167
 
        if (args[0] != NULL) {
168
 
                printf ("BAD: return value wasn't what we expected.\n");
169
 
                ret = 1;
170
 
        }
 
157
        TEST_NE_P (args, NULL);
 
158
        TEST_EQ_P (args[0], NULL);
171
159
 
172
 
        /* Daemonise variable should have been set */
173
 
        if (! daemonise) {
174
 
                printf ("BAD: daemonise value wasn't what we expected.\n");
175
 
                ret = 1;
176
 
        }
 
160
        TEST_TRUE (daemonise);
177
161
 
178
162
        nih_free (args);
179
163
 
180
164
 
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.
 
167
         */
 
168
        TEST_FEATURE ("with multiple short options");
182
169
        argc = 0;
183
170
        argv[argc++] = "ignored";
184
171
        argv[argc++] = "-d";
185
172
        argv[argc++] = "-R";
186
173
        argv[argc] = NULL;
 
174
 
187
175
        daemonise = 0;
188
176
        recursive = 0;
189
177
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
190
178
 
191
 
        /* Return value should be a NULL array */
192
 
        if (args[0] != NULL) {
193
 
                printf ("BAD: return value wasn't what we expected.\n");
194
 
                ret = 1;
195
 
        }
196
 
 
197
 
        /* Daemonise variable should have been set */
198
 
        if (! daemonise) {
199
 
                printf ("BAD: daemonise value wasn't what we expected.\n");
200
 
                ret = 1;
201
 
        }
202
 
 
203
 
        /* Recursive variable should have been set */
204
 
        if (! recursive) {
205
 
                printf ("BAD: recursive value wasn't what we expected.\n");
206
 
                ret = 1;
207
 
        }
 
179
        TEST_NE_P (args, NULL);
 
180
        TEST_EQ_P (args[0], NULL);
 
181
 
 
182
        TEST_TRUE (daemonise);
 
183
        TEST_TRUE (recursive);
208
184
 
209
185
        nih_free (args);
210
186
 
211
187
 
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.
 
190
         */
 
191
        TEST_FEATURE ("with combined short options");
213
192
        argc = 0;
214
193
        argv[argc++] = "ignored";
215
194
        argv[argc++] = "-dR";
216
195
        argv[argc] = NULL;
217
 
        daemonise = 0;
218
 
        recursive = 0;
219
 
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
220
 
 
221
 
        /* Return value should be a NULL array */
222
 
        if (args[0] != NULL) {
223
 
                printf ("BAD: return value wasn't what we expected.\n");
224
 
                ret = 1;
225
 
        }
226
 
 
227
 
        /* Daemonise variable should have been set */
228
 
        if (! daemonise) {
229
 
                printf ("BAD: daemonise value wasn't what we expected.\n");
230
 
                ret = 1;
231
 
        }
232
 
 
233
 
        /* Recursive variable should have been set */
234
 
        if (! recursive) {
235
 
                printf ("BAD: recursive value wasn't what we expected.\n");
236
 
                ret = 1;
237
 
        }
238
 
 
239
 
        nih_free (args);
240
 
 
241
 
 
242
 
        printf ("...with intermixed short options and arguments\n");
243
 
        argc = 0;
244
 
        argv[argc++] = "ignored";
245
 
        argv[argc++] = "foo";
246
 
        argv[argc++] = "-d";
247
 
        argv[argc++] = "bar";
248
 
        argv[argc++] = "-R";
249
 
        argv[argc++] = "baz";
250
 
        argv[argc] = NULL;
251
 
        daemonise = 0;
252
 
        recursive = 0;
253
 
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
254
 
 
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");
258
 
                ret = 1;
259
 
        }
260
 
 
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");
264
 
                ret = 1;
265
 
        }
266
 
 
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");
270
 
                ret = 1;
271
 
        }
272
 
 
273
 
        /* Return value should be a NULL array */
274
 
        if (args[3] != NULL) {
275
 
                printf ("BAD: return value wasn't what we expected.\n");
276
 
                ret = 1;
277
 
        }
278
 
 
279
 
        /* Daemonise variable should have been set */
280
 
        if (! daemonise) {
281
 
                printf ("BAD: daemonise value wasn't what we expected.\n");
282
 
                ret = 1;
283
 
        }
284
 
 
285
 
        /* Recursive variable should have been set */
286
 
        if (! recursive) {
287
 
                printf ("BAD: recursive value wasn't what we expected.\n");
288
 
                ret = 1;
289
 
        }
290
 
 
291
 
        nih_free (args);
292
 
 
293
 
 
294
 
        printf ("...with command-mode short options and arguments\n");
295
 
        argc = 0;
296
 
        argv[argc++] = "ignored";
297
 
        argv[argc++] = "foo";
298
 
        argv[argc++] = "-d";
299
 
        argv[argc++] = "bar";
300
 
        argv[argc++] = "-R";
301
 
        argv[argc++] = "baz";
302
 
        argv[argc] = NULL;
 
196
 
 
197
        daemonise = 0;
 
198
        recursive = 0;
 
199
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
200
 
 
201
        TEST_NE_P (args, NULL);
 
202
        TEST_EQ_P (args[0], NULL);
 
203
 
 
204
        TEST_TRUE (daemonise);
 
205
        TEST_TRUE (recursive);
 
206
 
 
207
        nih_free (args);
 
208
 
 
209
 
 
210
        /* Check that short options and ordinary arguments can be intermixed,
 
211
         * the arguments are returned in the array and the option values set.
 
212
         */
 
213
        TEST_FEATURE ("with intermixed short options and arguments");
 
214
        argc = 0;
 
215
        argv[argc++] = "ignored";
 
216
        argv[argc++] = "foo";
 
217
        argv[argc++] = "-d";
 
218
        argv[argc++] = "bar";
 
219
        argv[argc++] = "-R";
 
220
        argv[argc++] = "baz";
 
221
        argv[argc] = NULL;
 
222
 
 
223
        daemonise = 0;
 
224
        recursive = 0;
 
225
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
226
 
 
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);
 
232
 
 
233
        TEST_TRUE (daemonise);
 
234
        TEST_TRUE (recursive);
 
235
 
 
236
        nih_free (args);
 
237
 
 
238
 
 
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
 
242
         * set.
 
243
         */
 
244
        TEST_FEATURE ("with command-mode short options and arguments");
 
245
        argc = 0;
 
246
        argv[argc++] = "ignored";
 
247
        argv[argc++] = "foo";
 
248
        argv[argc++] = "-d";
 
249
        argv[argc++] = "bar";
 
250
        argv[argc++] = "-R";
 
251
        argv[argc++] = "baz";
 
252
        argv[argc] = NULL;
 
253
 
303
254
        daemonise = 0;
304
255
        recursive = 0;
305
256
        args = nih_option_parser (NULL, argc, argv, options, TRUE);
306
257
 
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");
310
 
                ret = 1;
311
 
        }
312
 
 
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");
316
 
                ret = 1;
317
 
        }
318
 
 
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");
322
 
                ret = 1;
323
 
        }
324
 
 
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");
328
 
                ret = 1;
329
 
        }
330
 
 
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");
334
 
                ret = 1;
335
 
        }
336
 
 
337
 
        /* Return value should be a NULL array */
338
 
        if (args[5] != NULL) {
339
 
                printf ("BAD: return value wasn't what we expected.\n");
340
 
                ret = 1;
341
 
        }
342
 
 
343
 
        /* Daemonise variable should not have been set */
344
 
        if (daemonise) {
345
 
                printf ("BAD: daemonise value wasn't what we expected.\n");
346
 
                ret = 1;
347
 
        }
348
 
 
349
 
        /* Recursive variable should have been set */
350
 
        if (recursive) {
351
 
                printf ("BAD: recursive value wasn't what we expected.\n");
352
 
                ret = 1;
353
 
        }
 
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);
 
265
 
 
266
        TEST_FALSE (daemonise);
 
267
        TEST_FALSE (recursive);
354
268
 
355
269
        nih_free (args);
356
270
 
357
271
 
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
 
274
         * values NOT set.
 
275
         */
 
276
        TEST_FEATURE ("with short options and terminator");
359
277
        argc = 0;
360
278
        argv[argc++] = "ignored";
361
279
        argv[argc++] = "foo";
365
283
        argv[argc++] = "-R";
366
284
        argv[argc++] = "baz";
367
285
        argv[argc] = NULL;
 
286
 
368
287
        daemonise = 0;
369
288
        recursive = 0;
370
289
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
371
290
 
372
 
        /* First array entry should be first argument */
373
 
        if (strcmp (args[0], "foo")) {
374
 
                printf ("BAD: return value wasn't what we expected.\n");
375
 
                ret = 1;
376
 
        }
377
 
 
378
 
        /* Second array entry should be second argument */
379
 
        if (strcmp (args[1], "bar")) {
380
 
                printf ("BAD: return value wasn't what we expected.\n");
381
 
                ret = 1;
382
 
        }
383
 
 
384
 
        /* Third array entry should be the not-option */
385
 
        if (strcmp (args[2], "-R")) {
386
 
                printf ("BAD: return value wasn't what we expected.\n");
387
 
                ret = 1;
388
 
        }
389
 
 
390
 
        /* Fourth array entry should be third argument */
391
 
        if (strcmp (args[3], "baz")) {
392
 
                printf ("BAD: return value wasn't what we expected.\n");
393
 
                ret = 1;
394
 
        }
395
 
 
396
 
        /* Return value should be a NULL array */
397
 
        if (args[4] != NULL) {
398
 
                printf ("BAD: return value wasn't what we expected.\n");
399
 
                ret = 1;
400
 
        }
401
 
 
402
 
        /* Daemonise variable should have been set */
403
 
        if (! daemonise) {
404
 
                printf ("BAD: daemonise value wasn't what we expected.\n");
405
 
                ret = 1;
406
 
        }
407
 
 
408
 
        /* Recursive variable should NOT have been set */
409
 
        if (recursive) {
410
 
                printf ("BAD: recursive value wasn't what we expected.\n");
411
 
                ret = 1;
412
 
        }
 
291
        TEST_NE_P (args, NULL);
 
292
        TEST_EQ_STR (args[0], "foo");
 
293
        TEST_EQ_STR (args[1], "bar");
 
294
        TEST_EQ_STR (args[2], "-R");
 
295
        TEST_EQ_STR (args[3], "baz");
 
296
        TEST_EQ_P (args[4], NULL);
 
297
 
 
298
        TEST_TRUE (daemonise);
 
299
        TEST_FALSE (recursive);
413
300
 
414
301
        nih_free (args);
415
302
 
416
303
 
417
 
        printf ("...with short argument option\n");
 
304
        /* Check that a short option can eat the next non-option argument
 
305
         * as its own argument, which is stored in its value and not
 
306
         * returned in the array.
 
307
         */
 
308
        TEST_FEATURE ("with short argument option");
418
309
        argc = 0;
419
310
        argv[argc++] = "ignored";
420
311
        argv[argc++] = "-f";
421
312
        argv[argc++] = "foo";
422
313
        argv[argc] = NULL;
 
314
 
423
315
        filename = NULL;
424
316
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
425
317
 
426
 
        /* Return value should be a NULL array */
427
 
        if (args[0] != NULL) {
428
 
                printf ("BAD: return value wasn't what we expected.\n");
429
 
                ret = 1;
430
 
        }
 
318
        TEST_NE_P (args, NULL);
 
319
        TEST_EQ_P (args[0], NULL);
431
320
 
432
 
        /* Filename variable should be set to argument */
433
 
        if (strcmp (filename, "foo")) {
434
 
                printf ("BAD: filename value wasn't what we expected.\n");
435
 
                ret = 1;
436
 
        }
 
321
        TEST_EQ_STR (filename, "foo");
437
322
 
438
323
        nih_free (filename);
439
324
        nih_free (args);
440
325
 
441
326
 
442
 
 
443
 
        printf ("...with short argument option and other arguments\n");
 
327
        /* Check that only the next non-option argument is eaten, and the
 
328
         * rest of the arguments are returned in the array.
 
329
         */
 
330
        TEST_FEATURE ("with short argument option and other arguments");
444
331
        argc = 0;
445
332
        argv[argc++] = "ignored";
446
333
        argv[argc++] = "-f";
448
335
        argv[argc++] = "bar";
449
336
        argv[argc++] = "baz";
450
337
        argv[argc] = NULL;
 
338
 
451
339
        filename = NULL;
452
340
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
453
341
 
454
 
        /* First array entry should be first argument */
455
 
        if (strcmp (args[0], "bar")) {
456
 
                printf ("BAD: return value wasn't what we expected.\n");
457
 
                ret = 1;
458
 
        }
459
 
 
460
 
        /* Second array entry should be second argument */
461
 
        if (strcmp (args[1], "baz")) {
462
 
                printf ("BAD: return value wasn't what we expected.\n");
463
 
                ret = 1;
464
 
        }
465
 
 
466
 
        /* Return value should be a NULL array */
467
 
        if (args[2] != NULL) {
468
 
                printf ("BAD: return value wasn't what we expected.\n");
469
 
                ret = 1;
470
 
        }
471
 
 
472
 
        /* Filename variable should be set to argument */
473
 
        if (strcmp (filename, "foo")) {
474
 
                printf ("BAD: filename value wasn't what we expected.\n");
475
 
                ret = 1;
476
 
        }
 
342
        TEST_NE_P (args, NULL);
 
343
        TEST_EQ_STR (args[0], "bar");
 
344
        TEST_EQ_STR (args[1], "baz");
 
345
        TEST_EQ_P (args[2], NULL);
 
346
 
 
347
        TEST_EQ_STR (filename, "foo");
477
348
 
478
349
        nih_free (filename);
479
350
        nih_free (args);
480
351
 
481
352
 
482
 
        printf ("...with random mix of short options and arguments\n");
 
353
        /* Stress test all the various ways of dealing with short options
 
354
         * at once; in particular check that an option that takes an argument
 
355
         * eats the first argument after the terminator.
 
356
         */
 
357
        TEST_FEATURE ("with random mix of short options and arguments");
483
358
        argc = 0;
484
359
        argv[argc++] = "ignored";
485
360
        argv[argc++] = "wibble";
490
365
        argv[argc++] = "bar";
491
366
        argv[argc++] = "baz";
492
367
        argv[argc] = NULL;
 
368
 
493
369
        daemonise = 0;
494
370
        recursive = 0;
495
371
        filename = NULL;
496
372
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
497
373
 
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");
501
 
                ret = 1;
502
 
        }
503
 
 
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");
507
 
                ret = 1;
508
 
        }
509
 
 
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");
513
 
                ret = 1;
514
 
        }
515
 
 
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");
519
 
                ret = 1;
520
 
        }
521
 
 
522
 
        /* Return value should be a NULL array */
523
 
        if (args[4] != NULL) {
524
 
                printf ("BAD: return value wasn't what we expected.\n");
525
 
                ret = 1;
526
 
        }
527
 
 
528
 
        /* Daemonise variable should have been set */
529
 
        if (! daemonise) {
530
 
                printf ("BAD: daemonise value wasn't what we expected.\n");
531
 
                ret = 1;
532
 
        }
533
 
 
534
 
        /* Recursive variable should NOT have been set */
535
 
        if (recursive) {
536
 
                printf ("BAD: recursive value wasn't what we expected.\n");
537
 
                ret = 1;
538
 
        }
539
 
 
540
 
        /* Filename variable should be set to argument */
541
 
        if (strcmp (filename, "foo")) {
542
 
                printf ("BAD: filename value wasn't what we expected.\n");
543
 
                ret = 1;
544
 
        }
 
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);
 
380
 
 
381
        TEST_TRUE (daemonise);
 
382
        TEST_FALSE (recursive);
 
383
        TEST_EQ_STR (filename, "foo");
545
384
 
546
385
        nih_free (filename);
547
386
        nih_free (args);
548
387
 
549
388
 
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.
 
392
         */
 
393
        TEST_FEATURE ("with short option and embedded argument");
551
394
        argc = 0;
552
395
        argv[argc++] = "ignored";
553
396
        argv[argc++] = "-fROOT";
554
397
        argv[argc] = NULL;
 
398
 
555
399
        filename = NULL;
556
400
        recursive = 0;
557
401
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
558
402
 
559
 
        /* Return value should be a NULL array */
560
 
        if (args[0] != NULL) {
561
 
                printf ("BAD: return value wasn't what we expected.\n");
562
 
                ret = 1;
563
 
        }
564
 
 
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");
568
 
                ret = 1;
569
 
        }
570
 
 
571
 
        /* Recursive variable should not have been set */
572
 
        if (recursive) {
573
 
                printf ("BAD: recursive value wasn't what we expected.\n");
574
 
                ret = 1;
575
 
        }
 
403
        TEST_NE_P (args, NULL);
 
404
        TEST_EQ_P (args[0], NULL);
 
405
 
 
406
        TEST_EQ_STR (filename, "ROOT");
 
407
        TEST_FALSE (recursive);
576
408
 
577
409
        nih_free (filename);
578
410
        nih_free (args);
579
411
 
580
412
 
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.
 
416
         */
 
417
        TEST_FEATURE ("with short option and non-embedded argument");
582
418
        argc = 0;
583
419
        argv[argc++] = "ignored";
584
420
        argv[argc++] = "-dfR";
585
421
        argv[argc++] = "foo";
586
422
        argv[argc] = NULL;
 
423
 
587
424
        filename = NULL;
588
425
        daemonise = 0;
589
426
        recursive = 0;
590
427
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
591
428
 
592
 
        /* Return value should be a NULL array */
593
 
        if (args[0] != NULL) {
594
 
                printf ("BAD: return value wasn't what we expected.\n");
595
 
                ret = 1;
596
 
        }
597
 
 
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");
601
 
                ret = 1;
602
 
        }
603
 
 
604
 
        /* Daemonise variable should have been set */
605
 
        if (! daemonise) {
606
 
                printf ("BAD: daemonise value wasn't what we expected.\n");
607
 
                ret = 1;
608
 
        }
609
 
 
610
 
        /* Recursive variable should have been set */
611
 
        if (! recursive) {
612
 
                printf ("BAD: recursive value wasn't what we expected.\n");
613
 
                ret = 1;
614
 
        }
 
429
        TEST_NE_P (args, NULL);
 
430
        TEST_EQ_P (args[0], NULL);
 
431
 
 
432
        TEST_TRUE (daemonise);
 
433
        TEST_TRUE (recursive);
 
434
        TEST_EQ_STR (filename, "foo");
615
435
 
616
436
        nih_free (filename);
617
437
        nih_free (args);
618
438
 
619
439
 
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
 
442
         * argument.
 
443
         */
 
444
        TEST_FEATURE ("with multiple short argument options");
621
445
        argc = 0;
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;
 
452
 
628
453
        filename = NULL;
629
454
        option = NULL;
630
455
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
631
456
 
632
 
        /* Return value should be a NULL array */
633
 
        if (args[0] != NULL) {
634
 
                printf ("BAD: return value wasn't what we expected.\n");
635
 
                ret = 1;
636
 
        }
637
 
 
638
 
        /* Filename variable should be set to argument */
639
 
        if (strcmp (filename, "foo")) {
640
 
                printf ("BAD: filename value wasn't what we expected.\n");
641
 
                ret = 1;
642
 
        }
643
 
 
644
 
        /* Option variable should be set to argument */
645
 
        if (strcmp (option, "bar")) {
646
 
                printf ("BAD: option value wasn't what we expected.\n");
647
 
                ret = 1;
648
 
        }
 
457
        TEST_NE_P (args, NULL);
 
458
        TEST_EQ_P (args[0], NULL);
 
459
 
 
460
        TEST_EQ_STR (filename, "foo");
 
461
        TEST_EQ_STR (option, "bar");
649
462
 
650
463
        nih_free (filename);
651
464
        nih_free (option);
652
465
        nih_free (args);
653
466
 
654
467
 
655
 
        printf ("...with single long option\n");
656
 
        argc = 0;
657
 
        argv[argc++] = "ignored";
658
 
        argv[argc++] = "--wibble";
659
 
        argv[argc] = NULL;
660
 
        wibble = 0;
661
 
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
662
 
 
663
 
        /* Return value should be a NULL array */
664
 
        if (args[0] != NULL) {
665
 
                printf ("BAD: return value wasn't what we expected.\n");
666
 
                ret = 1;
667
 
        }
668
 
 
669
 
        /* Wibble variable should have been set */
670
 
        if (! wibble) {
671
 
                printf ("BAD: wibble value wasn't what we expected.\n");
672
 
                ret = 1;
673
 
        }
674
 
 
675
 
        nih_free (args);
676
 
 
677
 
 
678
 
        printf ("...with multiple long options\n");
679
 
        argc = 0;
680
 
        argv[argc++] = "ignored";
681
 
        argv[argc++] = "--wibble";
682
 
        argv[argc++] = "--recursive";
683
 
        argv[argc] = NULL;
684
 
        wibble = 0;
685
 
        recursive = 0;
686
 
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
687
 
 
688
 
        /* Return value should be a NULL array */
689
 
        if (args[0] != NULL) {
690
 
                printf ("BAD: return value wasn't what we expected.\n");
691
 
                ret = 1;
692
 
        }
693
 
 
694
 
        /* Wibble variable should have been set */
695
 
        if (! wibble) {
696
 
                printf ("BAD: wibble value wasn't what we expected.\n");
697
 
                ret = 1;
698
 
        }
699
 
 
700
 
        /* Recursive variable should have been set */
701
 
        if (! recursive) {
702
 
                printf ("BAD: recursive value wasn't what we expected.\n");
703
 
                ret = 1;
704
 
        }
705
 
 
706
 
        nih_free (args);
707
 
 
708
 
 
709
 
        printf ("...with intermixed long options and arguments\n");
710
 
        argc = 0;
711
 
        argv[argc++] = "ignored";
712
 
        argv[argc++] = "foo";
713
 
        argv[argc++] = "--wibble";
714
 
        argv[argc++] = "bar";
715
 
        argv[argc++] = "--recursive";
716
 
        argv[argc++] = "baz";
717
 
        argv[argc] = NULL;
718
 
        wibble = 0;
719
 
        recursive = 0;
720
 
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
721
 
 
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");
725
 
                ret = 1;
726
 
        }
727
 
 
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");
731
 
                ret = 1;
732
 
        }
733
 
 
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");
737
 
                ret = 1;
738
 
        }
739
 
 
740
 
        /* Return value should be a NULL array */
741
 
        if (args[3] != NULL) {
742
 
                printf ("BAD: return value wasn't what we expected.\n");
743
 
                ret = 1;
744
 
        }
745
 
 
746
 
        /* Wibble variable should have been set */
747
 
        if (! wibble) {
748
 
                printf ("BAD: wibble value wasn't what we expected.\n");
749
 
                ret = 1;
750
 
        }
751
 
 
752
 
        /* Recursive variable should have been set */
753
 
        if (! recursive) {
754
 
                printf ("BAD: recursive value wasn't what we expected.\n");
755
 
                ret = 1;
756
 
        }
757
 
 
758
 
        nih_free (args);
759
 
 
760
 
 
761
 
        printf ("...with command-mode long options and arguments\n");
762
 
        argc = 0;
763
 
        argv[argc++] = "ignored";
764
 
        argv[argc++] = "foo";
765
 
        argv[argc++] = "--wibble";
766
 
        argv[argc++] = "bar";
767
 
        argv[argc++] = "--recursive";
768
 
        argv[argc++] = "baz";
769
 
        argv[argc] = NULL;
 
468
        /* Check that a single long option is taken from the arguments and
 
469
         * the appropriate variable set.
 
470
         */
 
471
        TEST_FEATURE ("with single long option");
 
472
        argc = 0;
 
473
        argv[argc++] = "ignored";
 
474
        argv[argc++] = "--wibble";
 
475
        argv[argc] = NULL;
 
476
 
 
477
        wibble = 0;
 
478
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
479
 
 
480
        TEST_NE_P (args, NULL);
 
481
        TEST_EQ_P (args[0], NULL);
 
482
 
 
483
        TEST_TRUE (wibble);
 
484
 
 
485
        nih_free (args);
 
486
 
 
487
 
 
488
        /* Check that multiple long options are taken from the arguments
 
489
         * and the appropriate variables set.
 
490
         */
 
491
        TEST_FEATURE ("with multiple long options");
 
492
        argc = 0;
 
493
        argv[argc++] = "ignored";
 
494
        argv[argc++] = "--wibble";
 
495
        argv[argc++] = "--recursive";
 
496
        argv[argc] = NULL;
 
497
 
 
498
        wibble = 0;
 
499
        recursive = 0;
 
500
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
501
 
 
502
        TEST_NE_P (args, NULL);
 
503
        TEST_EQ_P (args[0], NULL);
 
504
 
 
505
        TEST_TRUE (wibble);
 
506
        TEST_TRUE (recursive);
 
507
 
 
508
        nih_free (args);
 
509
 
 
510
 
 
511
        /* Check that only the long options are taken from the arguments,
 
512
         * and the non-option arguments are returned in the array.
 
513
         */
 
514
        TEST_FEATURE ("with intermixed long options and arguments");
 
515
        argc = 0;
 
516
        argv[argc++] = "ignored";
 
517
        argv[argc++] = "foo";
 
518
        argv[argc++] = "--wibble";
 
519
        argv[argc++] = "bar";
 
520
        argv[argc++] = "--recursive";
 
521
        argv[argc++] = "baz";
 
522
        argv[argc] = NULL;
 
523
 
 
524
        wibble = 0;
 
525
        recursive = 0;
 
526
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
527
 
 
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);
 
533
 
 
534
        TEST_TRUE (wibble);
 
535
        TEST_TRUE (recursive);
 
536
 
 
537
        nih_free (args);
 
538
 
 
539
 
 
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.
 
543
         */
 
544
        TEST_FEATURE ("with command-mode long options and arguments");
 
545
        argc = 0;
 
546
        argv[argc++] = "ignored";
 
547
        argv[argc++] = "foo";
 
548
        argv[argc++] = "--wibble";
 
549
        argv[argc++] = "bar";
 
550
        argv[argc++] = "--recursive";
 
551
        argv[argc++] = "baz";
 
552
        argv[argc] = NULL;
 
553
 
770
554
        wibble = 0;
771
555
        recursive = 0;
772
556
        args = nih_option_parser (NULL, argc, argv, options, TRUE);
773
557
 
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");
777
 
                ret = 1;
778
 
        }
779
 
 
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");
783
 
                ret = 1;
784
 
        }
785
 
 
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");
789
 
                ret = 1;
790
 
        }
791
 
 
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");
795
 
                ret = 1;
796
 
        }
797
 
 
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");
801
 
                ret = 1;
802
 
        }
803
 
 
804
 
        /* Return value should be a NULL array */
805
 
        if (args[5] != NULL) {
806
 
                printf ("BAD: return value wasn't what we expected.\n");
807
 
                ret = 1;
808
 
        }
809
 
 
810
 
        /* Wibble variable should not have been set */
811
 
        if (wibble) {
812
 
                printf ("BAD: wibble value wasn't what we expected.\n");
813
 
                ret = 1;
814
 
        }
815
 
 
816
 
        /* Recursive variable should not have been set */
817
 
        if (recursive) {
818
 
                printf ("BAD: recursive value wasn't what we expected.\n");
819
 
                ret = 1;
820
 
        }
 
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);
 
565
 
 
566
        TEST_FALSE (wibble);
 
567
        TEST_FALSE (recursive);
821
568
 
822
569
        nih_free (args);
823
570
 
824
571
 
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.
 
574
         */
 
575
        TEST_FEATURE ("with long options and terminator");
826
576
        argc = 0;
827
577
        argv[argc++] = "ignored";
828
578
        argv[argc++] = "foo";
832
582
        argv[argc++] = "--recursive";
833
583
        argv[argc++] = "baz";
834
584
        argv[argc] = NULL;
 
585
 
835
586
        wibble = 0;
836
587
        recursive = 0;
837
588
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
838
589
 
839
 
        /* First array entry should be first argument */
840
 
        if (strcmp (args[0], "foo")) {
841
 
                printf ("BAD: return value wasn't what we expected.\n");
842
 
                ret = 1;
843
 
        }
844
 
 
845
 
        /* Second array entry should be second argument */
846
 
        if (strcmp (args[1], "bar")) {
847
 
                printf ("BAD: return value wasn't what we expected.\n");
848
 
                ret = 1;
849
 
        }
850
 
 
851
 
        /* Third array entry should be non-option */
852
 
        if (strcmp (args[2], "--recursive")) {
853
 
                printf ("BAD: return value wasn't what we expected.\n");
854
 
                ret = 1;
855
 
        }
856
 
 
857
 
        /* Fourth array entry should be third argument */
858
 
        if (strcmp (args[3], "baz")) {
859
 
                printf ("BAD: return value wasn't what we expected.\n");
860
 
                ret = 1;
861
 
        }
862
 
 
863
 
        /* Return value should be a NULL array */
864
 
        if (args[4] != NULL) {
865
 
                printf ("BAD: return value wasn't what we expected.\n");
866
 
                ret = 1;
867
 
        }
868
 
 
869
 
        /* Wibble variable should have been set */
870
 
        if (! wibble) {
871
 
                printf ("BAD: wibble value wasn't what we expected.\n");
872
 
                ret = 1;
873
 
        }
874
 
 
875
 
        /* Recursive variable should have NOT been set */
876
 
        if (recursive) {
877
 
                printf ("BAD: recursive value wasn't what we expected.\n");
878
 
                ret = 1;
879
 
        }
 
590
        TEST_NE_P (args, NULL);
 
591
        TEST_EQ_STR (args[0], "foo");
 
592
        TEST_EQ_STR (args[1], "bar");
 
593
        TEST_EQ_STR (args[2], "--recursive");
 
594
        TEST_EQ_STR (args[3], "baz");
 
595
        TEST_EQ_P (args[4], NULL);
 
596
 
 
597
        TEST_TRUE (wibble);
 
598
        TEST_FALSE (recursive);
880
599
 
881
600
        nih_free (args);
882
601
 
883
602
 
884
 
        printf ("...with long argument option\n");
 
603
        /* Check that a long option may take an argument, which eats the
 
604
         * next non-option argument and stores that in the value instead.
 
605
         */
 
606
        TEST_FEATURE ("with long argument option");
885
607
        argc = 0;
886
608
        argv[argc++] = "ignored";
887
609
        argv[argc++] = "--filename";
888
610
        argv[argc++] = "foo";
889
611
        argv[argc] = NULL;
 
612
 
890
613
        filename = NULL;
891
614
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
892
615
 
893
 
        /* Return value should be a NULL array */
894
 
        if (args[0] != NULL) {
895
 
                printf ("BAD: return value wasn't what we expected.\n");
896
 
                ret = 1;
897
 
        }
 
616
        TEST_NE_P (args, NULL);
 
617
        TEST_EQ_P (args[0], NULL);
898
618
 
899
 
        /* Filename variable should be set to argument */
900
 
        if (strcmp (filename, "foo")) {
901
 
                printf ("BAD: filename value wasn't what we expected.\n");
902
 
                ret = 1;
903
 
        }
 
619
        TEST_EQ_STR (filename, "foo");
904
620
 
905
621
        nih_free (filename);
906
622
        nih_free (args);
907
623
 
908
624
 
909
 
        printf ("...with long argument option and other arguments\n");
 
625
        /* Check that only the first non-option argument is eaten by a long
 
626
         * option, and subsequent arguments are stilled returned in the
 
627
         * array.
 
628
         */
 
629
        TEST_FEATURE ("with long argument option and other arguments");
910
630
        argc = 0;
911
631
        argv[argc++] = "ignored";
912
632
        argv[argc++] = "--filename";
914
634
        argv[argc++] = "bar";
915
635
        argv[argc++] = "baz";
916
636
        argv[argc] = NULL;
 
637
 
917
638
        filename = NULL;
918
639
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
919
640
 
920
 
        /* First array entry should be first argument */
921
 
        if (strcmp (args[0], "bar")) {
922
 
                printf ("BAD: return value wasn't what we expected.\n");
923
 
                ret = 1;
924
 
        }
925
 
 
926
 
        /* Second array entry should be second argument */
927
 
        if (strcmp (args[1], "baz")) {
928
 
                printf ("BAD: return value wasn't what we expected.\n");
929
 
                ret = 1;
930
 
        }
931
 
 
932
 
        /* Return value should be a NULL array */
933
 
        if (args[2] != NULL) {
934
 
                printf ("BAD: return value wasn't what we expected.\n");
935
 
                ret = 1;
936
 
        }
937
 
 
938
 
        /* Filename variable should be set to argument */
939
 
        if (strcmp (filename, "foo")) {
940
 
                printf ("BAD: filename value wasn't what we expected.\n");
941
 
                ret = 1;
942
 
        }
 
641
        TEST_NE_P (args, NULL);
 
642
        TEST_EQ_STR (args[0], "bar");
 
643
        TEST_EQ_STR (args[1], "baz");
 
644
        TEST_EQ_P (args[2], NULL);
 
645
 
 
646
        TEST_EQ_STR (filename, "foo");
943
647
 
944
648
        nih_free (filename);
945
649
        nih_free (args);
946
650
 
947
651
 
948
 
        printf ("...with random mix of long options and arguments\n");
 
652
        /* Stress test all the various ways of dealing with long options
 
653
         * at once; in particular check that an option that takes an argument
 
654
         * eats the first argument after the terminator.
 
655
         */
 
656
        TEST_FEATURE ("with random mix of long options and arguments");
949
657
        argc = 0;
950
658
        argv[argc++] = "ignored";
951
659
        argv[argc++] = "wibble";
957
665
        argv[argc++] = "bar";
958
666
        argv[argc++] = "baz";
959
667
        argv[argc] = NULL;
 
668
 
960
669
        wibble = 0;
961
670
        recursive = 0;
962
671
        filename = NULL;
963
672
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
964
673
 
965
 
        /* First array entry should be previous to options */
966
 
        if (strcmp (args[0], "wibble")) {
967
 
                printf ("BAD: return value wasn't what we expected.\n");
968
 
                ret = 1;
969
 
        }
970
 
 
971
 
        /* Second array entry should be non-option */
972
 
        if (strcmp (args[1], "--recursive")) {
973
 
                printf ("BAD: return value wasn't what we expected.\n");
974
 
                ret = 1;
975
 
        }
976
 
 
977
 
        /* Third array entry should be first argument */
978
 
        if (strcmp (args[2], "bar")) {
979
 
                printf ("BAD: return value wasn't what we expected.\n");
980
 
                ret = 1;
981
 
        }
982
 
 
983
 
        /* Fourth array entry should be second argument */
984
 
        if (strcmp (args[3], "baz")) {
985
 
                printf ("BAD: return value wasn't what we expected.\n");
986
 
                ret = 1;
987
 
        }
988
 
 
989
 
        /* Return value should be a NULL array */
990
 
        if (args[4] != NULL) {
991
 
                printf ("BAD: return value wasn't what we expected.\n");
992
 
                ret = 1;
993
 
        }
994
 
 
995
 
        /* Wibble variable should have been set */
996
 
        if (! wibble) {
997
 
                printf ("BAD: wibble value wasn't what we expected.\n");
998
 
                ret = 1;
999
 
        }
1000
 
 
1001
 
        /* Recursive variable should NOT have been set */
1002
 
        if (recursive) {
1003
 
                printf ("BAD: recursive value wasn't what we expected.\n");
1004
 
                ret = 1;
1005
 
        }
1006
 
 
1007
 
        /* Filename variable should be set to argument */
1008
 
        if (strcmp (filename, "foo")) {
1009
 
                printf ("BAD: filename value wasn't what we expected.\n");
1010
 
                ret = 1;
1011
 
        }
 
674
        TEST_NE_P (args, NULL);
 
675
        TEST_EQ_STR (args[0], "wibble");
 
676
        TEST_EQ_STR (args[1], "--recursive");
 
677
        TEST_EQ_STR (args[2], "bar");
 
678
        TEST_EQ_STR (args[3], "baz");
 
679
        TEST_EQ_P (args[4], NULL);
 
680
 
 
681
        TEST_TRUE (wibble);
 
682
        TEST_FALSE (recursive);
 
683
        TEST_EQ_STR (filename, "foo");
1012
684
 
1013
685
        nih_free (filename);
1014
686
        nih_free (args);
1015
687
 
1016
688
 
1017
 
        printf ("...with long option and embedded argument\n");
 
689
        /* Check that the argument to a long option may be embedded into
 
690
         * it, following an equals sign.
 
691
         */
 
692
        TEST_FEATURE ("with long option and embedded argument");
1018
693
        argc = 0;
1019
694
        argv[argc++] = "ignored";
1020
695
        argv[argc++] = "--filename=ROOT";
1021
696
        argv[argc] = NULL;
 
697
 
1022
698
        filename = NULL;
1023
699
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
1024
700
 
1025
 
        /* Return value should be a NULL array */
1026
 
        if (args[0] != NULL) {
1027
 
                printf ("BAD: return value wasn't what we expected.\n");
1028
 
                ret = 1;
1029
 
        }
 
701
        TEST_NE_P (args, NULL);
 
702
        TEST_EQ_P (args[0], NULL);
1030
703
 
1031
 
        /* Filename variable should be set to remainder of argument */
1032
 
        if (strcmp (filename, "ROOT")) {
1033
 
                printf ("BAD: filename value wasn't what we expected.\n");
1034
 
                ret = 1;
1035
 
        }
 
704
        TEST_EQ_STR (filename, "ROOT");
1036
705
 
1037
706
        nih_free (filename);
1038
707
        nih_free (args);
1039
708
 
1040
709
 
1041
 
        printf ("...with multiple long argument options\n");
 
710
        /* Check that multiple long options with arguments each eat the
 
711
         * next non-option argument, not the same one.
 
712
         */
 
713
        TEST_FEATURE ("with multiple long argument options");
1042
714
        argc = 0;
1043
715
        argv[argc++] = "ignored";
1044
716
        argv[argc++] = "--filename";
1046
718
        argv[argc++] = "foo";
1047
719
        argv[argc++] = "bar";
1048
720
        argv[argc] = NULL;
 
721
 
1049
722
        filename = NULL;
1050
723
        option = NULL;
1051
724
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
1052
725
 
1053
 
        /* Return value should be a NULL array */
1054
 
        if (args[0] != NULL) {
1055
 
                printf ("BAD: return value wasn't what we expected.\n");
1056
 
                ret = 1;
1057
 
        }
1058
 
 
1059
 
        /* Filename variable should be set to argument */
1060
 
        if (strcmp (filename, "foo")) {
1061
 
                printf ("BAD: filename value wasn't what we expected.\n");
1062
 
                ret = 1;
1063
 
        }
1064
 
 
1065
 
        /* Option variable should be set to argument */
1066
 
        if (strcmp (option, "bar")) {
1067
 
                printf ("BAD: option value wasn't what we expected.\n");
1068
 
                ret = 1;
1069
 
        }
 
726
        TEST_NE_P (args, NULL);
 
727
        TEST_EQ_P (args[0], NULL);
 
728
 
 
729
        TEST_EQ_STR (filename, "foo");
 
730
        TEST_EQ_STR (option, "bar");
1070
731
 
1071
732
        nih_free (filename);
1072
733
        nih_free (option);
1073
734
        nih_free (args);
1074
735
 
1075
736
 
1076
 
        printf ("...with invalid short option\n");
1077
 
        argc = 0;
1078
 
        argv[argc++] = "ignored";
1079
 
        argv[argc++] = "-z";
1080
 
        argv[argc] = NULL;
1081
 
 
1082
 
        dup2 (fileno (output), STDERR_FILENO);
1083
 
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
1084
 
        dup2 (oldstderr, STDERR_FILENO);
1085
 
 
1086
 
        rewind (output);
1087
 
 
1088
 
        /* Return value should be NULL */
1089
 
        if (args != NULL) {
1090
 
                printf ("BAD: return value wasn't what we expected.\n");
1091
 
                ret = 1;
1092
 
        }
1093
 
 
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");
1098
 
                ret = 1;
1099
 
        }
1100
 
 
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");
1105
 
                ret = 1;
1106
 
        }
1107
 
 
1108
 
        /* Should be no more output */
1109
 
        if (fgets (text, sizeof (text), output)) {
1110
 
                printf ("BAD: more output than we expected.\n");
1111
 
                ret = 1;
1112
 
        }
1113
 
 
1114
 
        rewind (output);
1115
 
        ftruncate (fileno (output), 0);
1116
 
 
1117
 
 
1118
 
        printf ("...with invalid short option and catch-all\n");
1119
 
        argc = 0;
1120
 
        argv[argc++] = "ignored";
1121
 
        argv[argc++] = "-z";
1122
 
        argv[argc] = NULL;
1123
 
        args = nih_option_parser (NULL, argc, argv, catch_options, FALSE);
1124
 
 
1125
 
        /* Return value should not be NULL */
1126
 
        if (args == NULL) {
1127
 
                printf ("BAD: return value wasn't what we expected.\n");
1128
 
                ret = 1;
1129
 
        }
1130
 
 
1131
 
 
1132
 
        printf ("...with invalid long option\n");
1133
 
        argc = 0;
1134
 
        argv[argc++] = "ignored";
1135
 
        argv[argc++] = "--zoiks";
1136
 
        argv[argc] = NULL;
1137
 
 
1138
 
        dup2 (fileno (output), STDERR_FILENO);
1139
 
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
1140
 
        dup2 (oldstderr, STDERR_FILENO);
1141
 
 
1142
 
        rewind (output);
1143
 
 
1144
 
        /* Return value should be NULL */
1145
 
        if (args != NULL) {
1146
 
                printf ("BAD: return value wasn't what we expected.\n");
1147
 
                ret = 1;
1148
 
        }
1149
 
 
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");
1154
 
                ret = 1;
1155
 
        }
1156
 
 
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");
1161
 
                ret = 1;
1162
 
        }
1163
 
 
1164
 
        /* Should be no more output */
1165
 
        if (fgets (text, sizeof (text), output)) {
1166
 
                printf ("BAD: more output than we expected.\n");
1167
 
                ret = 1;
1168
 
        }
1169
 
 
1170
 
        rewind (output);
1171
 
        ftruncate (fileno (output), 0);
1172
 
 
1173
 
 
1174
 
        printf ("...with invalid long option and catch-all\n");
1175
 
        argc = 0;
1176
 
        argv[argc++] = "ignored";
1177
 
        argv[argc++] = "--zoiks";
1178
 
        argv[argc] = NULL;
1179
 
        args = nih_option_parser (NULL, argc, argv, catch_options, FALSE);
1180
 
 
1181
 
        /* Return value should not be NULL */
1182
 
        if (args == NULL) {
1183
 
                printf ("BAD: return value wasn't what we expected.\n");
1184
 
                ret = 1;
1185
 
        }
1186
 
 
1187
 
 
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.
 
739
         */
 
740
        TEST_FEATURE ("with invalid short option");
 
741
        argc = 0;
 
742
        argv[argc++] = "ignored";
 
743
        argv[argc++] = "-z";
 
744
        argv[argc] = NULL;
 
745
 
 
746
        TEST_DIVERT_STDERR (output) {
 
747
                args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
748
        }
 
749
        rewind (output);
 
750
 
 
751
        TEST_EQ_P (args, NULL);
 
752
 
 
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);
 
756
 
 
757
        TEST_FILE_RESET (output);
 
758
 
 
759
 
 
760
        /* Check that an invalid short option is ignored if there's a
 
761
         * catch-all option in the list.
 
762
         */
 
763
        TEST_FEATURE ("with invalid short option and catch-all");
 
764
        argc = 0;
 
765
        argv[argc++] = "ignored";
 
766
        argv[argc++] = "-z";
 
767
        argv[argc] = NULL;
 
768
        args = nih_option_parser (NULL, argc, argv, catch_options, FALSE);
 
769
 
 
770
        TEST_NE_P (args, NULL);
 
771
 
 
772
        nih_free (args);
 
773
 
 
774
 
 
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.
 
777
         */
 
778
        TEST_FEATURE ("with invalid long option");
 
779
        argc = 0;
 
780
        argv[argc++] = "ignored";
 
781
        argv[argc++] = "--zoiks";
 
782
        argv[argc] = NULL;
 
783
 
 
784
        TEST_DIVERT_STDERR (output) {
 
785
                args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
786
        }
 
787
        rewind (output);
 
788
 
 
789
        TEST_EQ_P (args, NULL);
 
790
 
 
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);
 
794
 
 
795
        TEST_FILE_RESET (output);
 
796
 
 
797
 
 
798
        /* Check that an invalid long option is ignored if there's a
 
799
         * catch-all option in the list.
 
800
         */
 
801
        TEST_FEATURE ("with invalid long option and catch-all");
 
802
        argc = 0;
 
803
        argv[argc++] = "ignored";
 
804
        argv[argc++] = "--zoiks";
 
805
        argv[argc] = NULL;
 
806
        args = nih_option_parser (NULL, argc, argv, catch_options, FALSE);
 
807
 
 
808
        TEST_NE_P (args, NULL);
 
809
 
 
810
        nih_free (args);
 
811
 
 
812
 
 
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
 
815
         * to be returned.
 
816
         */
 
817
        TEST_FEATURE ("with unexpected long option argument");
1189
818
        argc = 0;
1190
819
        argv[argc++] = "ignored";
1191
820
        argv[argc++] = "--wibble=woo";
1192
821
        argv[argc] = NULL;
1193
822
 
1194
 
        dup2 (fileno (output), STDERR_FILENO);
1195
 
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
1196
 
        dup2 (oldstderr, STDERR_FILENO);
1197
 
 
1198
 
        rewind (output);
1199
 
 
1200
 
        /* Return value should be NULL */
1201
 
        if (args != NULL) {
1202
 
                printf ("BAD: return value wasn't what we expected.\n");
1203
 
                ret = 1;
1204
 
        }
1205
 
 
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");
1210
 
                ret = 1;
1211
 
        }
1212
 
 
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");
1217
 
                ret = 1;
1218
 
        }
1219
 
 
1220
 
        /* Should be no more output */
1221
 
        if (fgets (text, sizeof (text), output)) {
1222
 
                printf ("BAD: more output than we expected.\n");
1223
 
                ret = 1;
1224
 
        }
1225
 
 
1226
 
        rewind (output);
1227
 
        ftruncate (fileno (output), 0);
1228
 
 
1229
 
 
1230
 
        printf ("...with missing short option argument\n");
 
823
        TEST_DIVERT_STDERR (output) {
 
824
                args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
825
        }
 
826
        rewind (output);
 
827
 
 
828
        TEST_EQ_P (args, NULL);
 
829
 
 
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);
 
833
 
 
834
        TEST_FILE_RESET (output);
 
835
 
 
836
 
 
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
 
839
         * returned.
 
840
         */
 
841
        TEST_FEATURE ("with missing short option argument");
1231
842
        argc = 0;
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);
1238
 
 
1239
 
        rewind (output);
1240
 
 
1241
 
        /* Return value should be NULL */
1242
 
        if (args != NULL) {
1243
 
                printf ("BAD: return value wasn't what we expected.\n");
1244
 
                ret = 1;
1245
 
        }
1246
 
 
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");
1251
 
                ret = 1;
1252
 
        }
1253
 
 
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");
1258
 
                ret = 1;
1259
 
        }
1260
 
 
1261
 
        /* Should be no more output */
1262
 
        if (fgets (text, sizeof (text), output)) {
1263
 
                printf ("BAD: more output than we expected.\n");
1264
 
                ret = 1;
1265
 
        }
1266
 
 
1267
 
        rewind (output);
1268
 
        ftruncate (fileno (output), 0);
1269
 
 
1270
 
 
1271
 
        printf ("...with missing long option argument\n");
 
846
 
 
847
        TEST_DIVERT_STDERR (output) {
 
848
                args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
849
        }
 
850
        rewind (output);
 
851
 
 
852
        TEST_EQ_P (args, NULL);
 
853
 
 
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);
 
857
 
 
858
        TEST_FILE_RESET (output);
 
859
 
 
860
 
 
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
 
863
         * returned.
 
864
         */
 
865
        TEST_FEATURE ("with missing long option argument");
1272
866
        argc = 0;
1273
867
        argv[argc++] = "ignored";
1274
868
        argv[argc++] = "--filename";
1275
869
        argv[argc] = NULL;
1276
870
 
1277
 
        dup2 (fileno (output), STDERR_FILENO);
1278
 
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
1279
 
        dup2 (oldstderr, STDERR_FILENO);
1280
 
 
1281
 
        rewind (output);
1282
 
 
1283
 
        /* Return value should be NULL */
1284
 
        if (args != NULL) {
1285
 
                printf ("BAD: return value wasn't what we expected.\n");
1286
 
                ret = 1;
1287
 
        }
1288
 
 
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");
1293
 
                ret = 1;
1294
 
        }
1295
 
 
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");
1300
 
                ret = 1;
1301
 
        }
1302
 
 
1303
 
        /* Should be no more output */
1304
 
        if (fgets (text, sizeof (text), output)) {
1305
 
                printf ("BAD: more output than we expected.\n");
1306
 
                ret = 1;
1307
 
        }
1308
 
 
1309
 
        rewind (output);
1310
 
        ftruncate (fileno (output), 0);
1311
 
 
1312
 
 
1313
 
        printf ("...with short setter option\n");
 
871
        TEST_DIVERT_STDERR (output) {
 
872
                args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
873
        }
 
874
        rewind (output);
 
875
 
 
876
        TEST_EQ_P (args, NULL);
 
877
 
 
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);
 
881
 
 
882
        TEST_FILE_RESET (output);
 
883
 
 
884
 
 
885
        /* Check that a short option may result in a function call, and
 
886
         * that the arguments to that call are correct.
 
887
         */
 
888
        TEST_FEATURE ("with short setter option");
1314
889
        argc = 0;
1315
890
        argv[argc++] = "ignored";
1316
891
        argv[argc++] = "-x";
1317
892
        argv[argc++] = "foo";
1318
893
        argv[argc] = NULL;
 
894
 
1319
895
        was_called = 0;
1320
896
        last_option = NULL;
1321
897
        last_arg = NULL;
1322
898
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
1323
899
 
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");
1327
 
                ret = 1;
1328
 
        }
1329
 
 
1330
 
        /* Return value should be a NULL array */
1331
 
        if (args[1] != NULL) {
1332
 
                printf ("BAD: return value wasn't what we expected.\n");
1333
 
                ret = 1;
1334
 
        }
1335
 
 
1336
 
        /* Setter function should have been called */
1337
 
        if (! was_called) {
1338
 
                printf ("BAD: setter function was not called.\n");
1339
 
                ret = 1;
1340
 
        }
1341
 
 
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");
1345
 
                ret = 1;
1346
 
        }
1347
 
 
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");
1351
 
                ret = 1;
1352
 
        }
 
900
        TEST_NE_P (args, NULL);
 
901
        TEST_EQ_STR (args[0], "foo");
 
902
        TEST_EQ_P (args[1], NULL);
 
903
 
 
904
        TEST_TRUE (was_called);
 
905
        TEST_EQ (last_option->option, options[6].option);
 
906
        TEST_EQ_P (last_arg, NULL);
1353
907
 
1354
908
        nih_free (args);
1355
 
 
1356
 
 
1357
 
        printf ("...with short setter argument option\n");
 
909
        nih_free (last_option);
 
910
 
 
911
 
 
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
 
914
         * function call.
 
915
         */
 
916
        TEST_FEATURE ("with short setter argument option");
1358
917
        argc = 0;
1359
918
        argv[argc++] = "ignored";
1360
919
        argv[argc++] = "-s";
1361
920
        argv[argc++] = "foo";
1362
921
        argv[argc] = NULL;
 
922
 
1363
923
        was_called = 0;
1364
924
        last_option = NULL;
1365
925
        last_arg = NULL;
1366
926
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
1367
927
 
1368
 
        /* Return value should be a NULL array */
1369
 
        if (args[0] != NULL) {
1370
 
                printf ("BAD: return value wasn't what we expected.\n");
1371
 
                ret = 1;
1372
 
        }
1373
 
 
1374
 
        /* Setter function should have been called */
1375
 
        if (! was_called) {
1376
 
                printf ("BAD: setter function was not called.\n");
1377
 
                ret = 1;
1378
 
        }
1379
 
 
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");
1383
 
                ret = 1;
1384
 
        }
1385
 
 
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");
1389
 
                ret = 1;
1390
 
        }
 
928
        TEST_NE_P (args, NULL);
 
929
        TEST_EQ_P (args[0], NULL);
 
930
 
 
931
        TEST_TRUE (was_called);
 
932
        TEST_EQ (last_option->option, options[5].option);
 
933
        TEST_EQ_STR (last_arg, "foo");
1391
934
 
1392
935
        nih_free (args);
1393
 
 
1394
 
 
1395
 
        printf ("...with short setter embedded argument option\n");
 
936
        nih_free (last_option);
 
937
 
 
938
 
 
939
        /* Check that the setter function is stilled correctly if the
 
940
         * argument to the short option is embedded within it.
 
941
         */
 
942
        TEST_FEATURE ("with short setter embedded argument option");
1396
943
        argc = 0;
1397
944
        argv[argc++] = "ignored";
1398
945
        argv[argc++] = "-sfoo";
1399
946
        argv[argc] = NULL;
 
947
 
1400
948
        was_called = 0;
1401
949
        last_option = NULL;
1402
950
        last_arg = NULL;
1403
951
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
1404
952
 
1405
 
        /* Return value should be a NULL array */
1406
 
        if (args[0] != NULL) {
1407
 
                printf ("BAD: return value wasn't what we expected.\n");
1408
 
                ret = 1;
1409
 
        }
1410
 
 
1411
 
        /* Setter function should have been called */
1412
 
        if (! was_called) {
1413
 
                printf ("BAD: setter function was not called.\n");
1414
 
                ret = 1;
1415
 
        }
1416
 
 
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");
1420
 
                ret = 1;
1421
 
        }
1422
 
 
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");
1426
 
                ret = 1;
1427
 
        }
 
953
        TEST_NE_P (args, NULL);
 
954
        TEST_EQ_P (args[0], NULL);
 
955
 
 
956
        TEST_TRUE (was_called);
 
957
        TEST_EQ (last_option->option, options[5].option);
 
958
        TEST_EQ_STR (last_arg, "foo");
1428
959
 
1429
960
        nih_free (args);
1430
 
 
1431
 
 
1432
 
        printf ("...with long setter option\n");
 
961
        nih_free (last_option);
 
962
 
 
963
 
 
964
        /* Check that a long option may result in a function call, and
 
965
         * that the arguments to that call are correct.
 
966
         */
 
967
        TEST_FEATURE ("with long setter option");
1433
968
        argc = 0;
1434
969
        argv[argc++] = "ignored";
1435
970
        argv[argc++] = "--execute";
1436
971
        argv[argc++] = "foo";
1437
972
        argv[argc] = NULL;
 
973
 
1438
974
        was_called = 0;
1439
975
        last_option = NULL;
1440
976
        last_arg = NULL;
1441
977
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
1442
978
 
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");
1446
 
                ret = 1;
1447
 
        }
1448
 
 
1449
 
        /* Return value should be a NULL array */
1450
 
        if (args[1] != NULL) {
1451
 
                printf ("BAD: return value wasn't what we expected.\n");
1452
 
                ret = 1;
1453
 
        }
1454
 
 
1455
 
        /* Setter function should have been called */
1456
 
        if (! was_called) {
1457
 
                printf ("BAD: setter function was not called.\n");
1458
 
                ret = 1;
1459
 
        }
1460
 
 
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");
1464
 
                ret = 1;
1465
 
        }
1466
 
 
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");
1470
 
                ret = 1;
1471
 
        }
 
979
        TEST_NE_P (args, NULL);
 
980
        TEST_EQ_STR (args[0], "foo");
 
981
        TEST_EQ_P (args[1], NULL);
 
982
 
 
983
        TEST_TRUE (was_called);
 
984
        TEST_EQ (last_option->option, options[6].option);
 
985
        TEST_EQ_P (last_arg, NULL);
1472
986
 
1473
987
        nih_free (args);
1474
 
 
1475
 
 
1476
 
        printf ("...with long setter argument option\n");
 
988
        nih_free (last_option);
 
989
 
 
990
 
 
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
 
993
         * function call.
 
994
         */
 
995
        TEST_FEATURE ("with long setter argument option");
1477
996
        argc = 0;
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);
1486
1005
 
1487
 
        /* Return value should be a NULL array */
1488
 
        if (args[0] != NULL) {
1489
 
                printf ("BAD: return value wasn't what we expected.\n");
1490
 
                ret = 1;
1491
 
        }
1492
 
 
1493
 
        /* Setter function should have been called */
1494
 
        if (! was_called) {
1495
 
                printf ("BAD: setter function was not called.\n");
1496
 
                ret = 1;
1497
 
        }
1498
 
 
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");
1502
 
                ret = 1;
1503
 
        }
1504
 
 
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");
1508
 
                ret = 1;
1509
 
        }
 
1006
        TEST_NE_P (args, NULL);
 
1007
        TEST_EQ_P (args[0], NULL);
 
1008
 
 
1009
        TEST_TRUE (was_called);
 
1010
        TEST_EQ (last_option->option, options[5].option);
 
1011
        TEST_EQ_STR (last_arg, "foo");
1510
1012
 
1511
1013
        nih_free (args);
1512
 
 
1513
 
 
1514
 
        printf ("...with long setter embedded argument option\n");
 
1014
        nih_free (last_option);
 
1015
 
 
1016
 
 
1017
        /* Check that the setter function is stilled correctly if the
 
1018
         * argument to the short option is embedded within it.
 
1019
         */
 
1020
        TEST_FEATURE ("with long setter embedded argument option");
1515
1021
        argc = 0;
1516
1022
        argv[argc++] = "ignored";
1517
1023
        argv[argc++] = "--special=foo";
1518
1024
        argv[argc] = NULL;
 
1025
 
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);
1523
1030
 
1524
 
        /* Return value should be a NULL array */
1525
 
        if (args[0] != NULL) {
1526
 
                printf ("BAD: return value wasn't what we expected.\n");
1527
 
                ret = 1;
1528
 
        }
1529
 
 
1530
 
        /* Setter function should have been called */
1531
 
        if (! was_called) {
1532
 
                printf ("BAD: setter function was not called.\n");
1533
 
                ret = 1;
1534
 
        }
1535
 
 
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");
1539
 
                ret = 1;
1540
 
        }
1541
 
 
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");
1545
 
                ret = 1;
1546
 
        }
 
1031
        TEST_NE_P (args, NULL);
 
1032
        TEST_EQ_P (args[0], NULL);
 
1033
 
 
1034
        TEST_TRUE (was_called);
 
1035
        TEST_EQ (last_option->option, options[5].option);
 
1036
        TEST_EQ_STR (last_arg, "foo");
1547
1037
 
1548
1038
        nih_free (args);
1549
 
 
1550
 
 
1551
 
        printf ("...with short setter embedded argument error\n");
 
1039
        nih_free (last_option);
 
1040
 
 
1041
 
 
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).
 
1045
         */
 
1046
        TEST_FEATURE ("with short setter embedded argument error");
1552
1047
        argc = 0;
1553
1048
        argv[argc++] = "ignored";
1554
1049
        argv[argc++] = "-sfail";
1555
1050
        argv[argc] = NULL;
 
1051
 
1556
1052
        was_called = 0;
1557
1053
        last_option = NULL;
1558
1054
        last_arg = NULL;
1559
1055
 
1560
 
        dup2 (fileno (output), STDERR_FILENO);
1561
 
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
1562
 
        dup2 (oldstderr, STDERR_FILENO);
1563
 
 
1564
 
        rewind (output);
1565
 
 
1566
 
        /* Return value should be NULL */
1567
 
        if (args != NULL) {
1568
 
                printf ("BAD: return value wasn't what we expected.\n");
1569
 
                ret = 1;
1570
 
        }
1571
 
 
1572
 
        /* Setter function should have been called */
1573
 
        if (! was_called) {
1574
 
                printf ("BAD: setter function was not called.\n");
1575
 
                ret = 1;
1576
 
        }
1577
 
 
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");
1581
 
                ret = 1;
1582
 
        }
1583
 
 
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");
1587
 
                ret = 1;
1588
 
        }
1589
 
 
1590
 
        /* Should be no output */
1591
 
        if (fgets (text, sizeof (text), output)) {
1592
 
                printf ("BAD: more output than we expected.\n");
1593
 
                ret = 1;
1594
 
        }
1595
 
 
1596
 
        rewind (output);
1597
 
        ftruncate (fileno (output), 0);
1598
 
 
1599
 
 
1600
 
        printf ("...with long setter embedded argument error\n");
 
1056
        TEST_DIVERT_STDERR (output) {
 
1057
                args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
1058
        }
 
1059
        rewind (output);
 
1060
 
 
1061
        TEST_EQ_P (args, NULL);
 
1062
 
 
1063
        TEST_TRUE (was_called);
 
1064
        TEST_EQ (last_option->option, options[5].option);
 
1065
        TEST_EQ_STR (last_arg, "fail");
 
1066
 
 
1067
        TEST_FILE_END (output);
 
1068
 
 
1069
        TEST_FILE_RESET (output);
 
1070
        nih_free (last_option);
 
1071
 
 
1072
 
 
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).
 
1076
         */
 
1077
        TEST_FEATURE ("with long setter embedded argument error");
1601
1078
        argc = 0;
1602
1079
        argv[argc++] = "ignored";
1603
1080
        argv[argc++] = "--special=fail";
1604
1081
        argv[argc] = NULL;
 
1082
 
1605
1083
        was_called = 0;
1606
1084
        last_option = NULL;
1607
1085
        last_arg = NULL;
1608
1086
 
1609
 
        dup2 (fileno (output), STDERR_FILENO);
1610
 
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
1611
 
        dup2 (oldstderr, STDERR_FILENO);
1612
 
 
1613
 
        rewind (output);
1614
 
 
1615
 
        /* Return value should be NULL */
1616
 
        if (args != NULL) {
1617
 
                printf ("BAD: return value wasn't what we expected.\n");
1618
 
                ret = 1;
1619
 
        }
1620
 
 
1621
 
        /* Setter function should have been called */
1622
 
        if (! was_called) {
1623
 
                printf ("BAD: setter function was not called.\n");
1624
 
                ret = 1;
1625
 
        }
1626
 
 
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");
1630
 
                ret = 1;
1631
 
        }
1632
 
 
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");
1636
 
                ret = 1;
1637
 
        }
1638
 
 
1639
 
        /* Should be no output */
1640
 
        if (fgets (text, sizeof (text), output)) {
1641
 
                printf ("BAD: more output than we expected.\n");
1642
 
                ret = 1;
1643
 
        }
1644
 
 
1645
 
        rewind (output);
1646
 
        ftruncate (fileno (output), 0);
1647
 
 
 
1087
        TEST_DIVERT_STDERR (output) {
 
1088
                args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
1089
        }
 
1090
        rewind (output);
 
1091
 
 
1092
        TEST_EQ_P (args, NULL);
 
1093
 
 
1094
        TEST_TRUE (was_called);
 
1095
        TEST_EQ (last_option->option, options[5].option);
 
1096
        TEST_EQ_STR (last_arg, "fail");
 
1097
 
 
1098
        TEST_FILE_END (output);
 
1099
 
 
1100
        nih_free (last_option);
1648
1101
 
1649
1102
        fclose (output);
1650
 
        close (oldstderr);
1651
 
 
1652
 
        return ret;
1653
1103
}
1654
1104
 
1655
1105
 
1656
 
int
 
1106
void
1657
1107
test_count (void)
1658
1108
{
1659
1109
        NihOption opt;
1660
 
        int       ret = 0, value = 0, retval;
1661
 
 
1662
 
        printf ("Testing nih_option_count()\n");
 
1110
        int       ret, value = 0;
 
1111
 
 
1112
        TEST_FUNCTION ("nih_option_count");
 
1113
 
 
1114
        /* Check that the count function treats the option value as an
 
1115
         * integer pointer, and increments it.
 
1116
         */
 
1117
        TEST_FEATURE ("with zero value");
1663
1118
        opt.value = &value;
1664
 
        retval = nih_option_count (&opt, NULL);
1665
 
 
1666
 
        /* Return value should be zero */
1667
 
        if (retval != 0) {
1668
 
                printf ("BAD: return value wasn't what we expected.\n");
1669
 
                ret = 1;
1670
 
        }
1671
 
 
1672
 
        /* Value should be incremented */
1673
 
        if (value != 1) {
1674
 
                printf ("BAD: value wasn't what we expected.\n");
1675
 
                ret = 1;
1676
 
        }
1677
 
 
1678
 
 
1679
 
        retval = nih_option_count (&opt, NULL);
1680
 
 
1681
 
        /* Return value should be zero */
1682
 
        if (retval != 0) {
1683
 
                printf ("BAD: return value wasn't what we expected.\n");
1684
 
                ret = 1;
1685
 
        }
1686
 
 
1687
 
        /* Value should be incremented */
1688
 
        if (value != 2) {
1689
 
                printf ("BAD: value wasn't what we expected.\n");
1690
 
                ret = 1;
1691
 
        }
1692
 
 
1693
 
        return ret;
 
1119
        ret = nih_option_count (&opt, NULL);
 
1120
 
 
1121
        TEST_EQ (ret, 0);
 
1122
        TEST_EQ (value, 1);
 
1123
 
 
1124
 
 
1125
        /* Check that calling again increments the value to two. */
 
1126
        TEST_FEATURE ("with non-zero value");
 
1127
        ret = nih_option_count (&opt, NULL);
 
1128
 
 
1129
        TEST_EQ (ret, 0);
 
1130
        TEST_EQ (value, 2);
1694
1131
}
1695
1132
 
1696
1133
 
1705
1142
        return 0;
1706
1143
}
1707
1144
 
1708
 
 
1709
 
int
 
1145
void
1710
1146
test_quiet (void)
1711
1147
{
1712
1148
        char *argv[3], **args;
1713
 
        int   ret = 0, argc;
 
1149
        int   argc;
1714
1150
 
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);
1718
1154
 
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.
 
1157
         */
 
1158
        TEST_FEATURE ("with long option");
1720
1159
        argc = 0;
1721
1160
        argv[argc++] = "ignored";
1722
1161
        argv[argc++] = "--quiet";
1723
1162
        argv[argc] = NULL;
 
1163
 
 
1164
        logger_called = 0;
1724
1165
        nih_log_set_priority (NIH_LOG_WARN);
1725
 
        logger_called = 0;
 
1166
 
1726
1167
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
1168
 
 
1169
        nih_debug ("test message");
1727
1170
        nih_info ("test message");
1728
1171
        nih_warn ("test message");
1729
1172
        nih_error ("test message");
1730
1173
 
1731
 
        /* Return value should be a NULL array */
1732
 
        if (args[0] != NULL) {
1733
 
                printf ("BAD: return value wasn't what we expected.\n");
1734
 
                ret = 1;
1735
 
        }
1736
 
 
1737
 
        /* Logger should have been called only once */
1738
 
        if (logger_called != 1) {
1739
 
                printf ("BAD: priority wasn't what we expected.\n");
1740
 
                ret = 1;
1741
 
        }
 
1174
        TEST_NE_P (args, NULL);
 
1175
        TEST_EQ_P (args[0], NULL);
 
1176
        TEST_EQ (logger_called, 1);
1742
1177
 
1743
1178
        nih_free (args);
1744
1179
 
1745
1180
 
1746
 
        printf ("...with short option\n");
 
1181
        /* Check that the -q option has the same effect. */
 
1182
        TEST_FEATURE ("with short option");
1747
1183
        argc = 0;
1748
1184
        argv[argc++] = "ignored";
1749
1185
        argv[argc++] = "-q";
1750
1186
        argv[argc] = NULL;
 
1187
 
 
1188
        logger_called = 0;
1751
1189
        nih_log_set_priority (NIH_LOG_WARN);
1752
 
        logger_called = 0;
 
1190
 
1753
1191
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
1192
 
 
1193
        nih_debug ("test message");
1754
1194
        nih_info ("test message");
1755
1195
        nih_warn ("test message");
1756
1196
        nih_error ("test message");
1757
1197
 
1758
 
        /* Return value should be a NULL array */
1759
 
        if (args[0] != NULL) {
1760
 
                printf ("BAD: return value wasn't what we expected.\n");
1761
 
                ret = 1;
1762
 
        }
1763
 
 
1764
 
        /* Logger should have been called only once */
1765
 
        if (logger_called != 1) {
1766
 
                printf ("BAD: priority wasn't what we expected.\n");
1767
 
                ret = 1;
1768
 
        }
 
1198
        TEST_NE_P (args, NULL);
 
1199
        TEST_EQ_P (args[0], NULL);
 
1200
        TEST_EQ (logger_called, 1);
1769
1201
 
1770
1202
        nih_free (args);
1771
1203
 
1772
1204
 
1773
1205
        nih_log_set_priority (NIH_LOG_INFO);
1774
1206
        nih_log_set_logger (nih_logger_printf);
1775
 
 
1776
 
        return ret;
1777
1207
}
1778
1208
 
1779
 
int
 
1209
void
1780
1210
test_verbose (void)
1781
1211
{
1782
1212
        char *argv[3], **args;
1783
 
        int   ret = 0, argc;
 
1213
        int   argc;
1784
1214
 
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);
1788
1218
 
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.
 
1222
         */
 
1223
        TEST_FEATURE ("with long option");
1790
1224
        argc = 0;
1791
1225
        argv[argc++] = "ignored";
1792
1226
        argv[argc++] = "--verbose";
1793
1227
        argv[argc] = NULL;
 
1228
 
 
1229
        logger_called = 0;
1794
1230
        nih_log_set_priority (NIH_LOG_WARN);
1795
 
        logger_called = 0;
 
1231
 
1796
1232
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
1233
 
 
1234
        nih_debug ("test message");
1797
1235
        nih_info ("test message");
1798
1236
        nih_warn ("test message");
1799
1237
        nih_error ("test message");
1800
1238
 
1801
 
        /* Return value should be a NULL array */
1802
 
        if (args[0] != NULL) {
1803
 
                printf ("BAD: return value wasn't what we expected.\n");
1804
 
                ret = 1;
1805
 
        }
1806
 
 
1807
 
        /* Logger should have been called three times */
1808
 
        if (logger_called != 3) {
1809
 
                printf ("BAD: priority wasn't what we expected.\n");
1810
 
                ret = 1;
1811
 
        }
1812
 
 
1813
 
        nih_free (args);
1814
 
 
1815
 
 
1816
 
        printf ("...with short option\n");
1817
 
        argc = 0
1818
 
                ;
 
1239
        TEST_NE_P (args, NULL);
 
1240
        TEST_EQ_P (args[0], NULL);
 
1241
        TEST_EQ (logger_called, 3);
 
1242
 
 
1243
 
 
1244
        /* Check that the -v option has the same effect. */
 
1245
        TEST_FEATURE ("with short option");
 
1246
        argc = 0;
1819
1247
        argv[argc++] = "ignored";
1820
1248
        argv[argc++] = "-v";
1821
1249
        argv[argc] = NULL;
 
1250
 
 
1251
        logger_called = 0;
1822
1252
        nih_log_set_priority (NIH_LOG_WARN);
1823
 
        logger_called = 0;
 
1253
 
1824
1254
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
1255
 
 
1256
        nih_debug ("test message");
1825
1257
        nih_info ("test message");
1826
1258
        nih_warn ("test message");
1827
1259
        nih_error ("test message");
1828
1260
 
1829
 
        /* Return value should be a NULL array */
1830
 
        if (args[0] != NULL) {
1831
 
                printf ("BAD: return value wasn't what we expected.\n");
1832
 
                ret = 1;
1833
 
        }
1834
 
 
1835
 
        /* Logger should have been called three times */
1836
 
        if (logger_called != 3) {
1837
 
                printf ("BAD: priority wasn't what we expected.\n");
1838
 
                ret = 1;
1839
 
        }
 
1261
        TEST_NE_P (args, NULL);
 
1262
        TEST_EQ_P (args[0], NULL);
 
1263
        TEST_EQ (logger_called, 3);
1840
1264
 
1841
1265
        nih_free (args);
1842
1266
 
1843
1267
 
1844
1268
        nih_log_set_priority (NIH_LOG_INFO);
1845
1269
        nih_log_set_logger (nih_logger_printf);
1846
 
 
1847
 
        return ret;
1848
1270
}
1849
1271
 
1850
 
int
 
1272
void
1851
1273
test_debug (void)
1852
1274
{
1853
1275
        char *argv[3], **args;
1854
 
        int   ret = 0, argc;
 
1276
        int   argc;
1855
1277
 
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
 
1280
         * are output.
 
1281
         */
 
1282
        TEST_FUNCTION ("nih_option_debug");
1857
1283
        program_name = "test";
1858
1284
        nih_log_set_logger (my_logger);
1859
1285
 
1861
1287
        argv[argc++] = "ignored";
1862
1288
        argv[argc++] = "--debug";
1863
1289
        argv[argc] = NULL;
 
1290
 
 
1291
        logger_called = 0;
1864
1292
        nih_log_set_priority (NIH_LOG_WARN);
1865
 
        logger_called = 0;
 
1293
 
1866
1294
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
1295
 
1867
1296
        nih_debug ("test message");
1868
1297
        nih_info ("test message");
1869
1298
        nih_warn ("test message");
1870
1299
        nih_error ("test message");
1871
1300
 
1872
 
        /* Return value should be a NULL array */
1873
 
        if (args[0] != NULL) {
1874
 
                printf ("BAD: return value wasn't what we expected.\n");
1875
 
                ret = 1;
1876
 
        }
1877
 
 
1878
 
        /* Logger should have been called four times */
1879
 
        if (logger_called != 4) {
1880
 
                printf ("BAD: priority wasn't what we expected.\n");
1881
 
                ret = 1;
1882
 
        }
 
1301
        TEST_NE_P (args, NULL);
 
1302
        TEST_EQ_P (args[0], NULL);
 
1303
        TEST_EQ (logger_called, 4);
1883
1304
 
1884
1305
        nih_free (args);
1885
1306
 
1886
1307
        nih_log_set_priority (NIH_LOG_INFO);
1887
1308
        nih_log_set_logger (nih_logger_printf);
1888
 
 
1889
 
        return ret;
1890
1309
}
1891
1310
 
1892
 
 
1893
 
int
 
1311
void
1894
1312
test_version (void)
1895
1313
{
1896
1314
        FILE  *output;
1897
 
        char   text[81];
1898
1315
        char  *argv[3];
1899
1316
        pid_t  pid;
1900
 
        int    ret = 0, argc, status;
 
1317
        int    argc, status;
1901
1318
 
1902
 
        printf ("Testing nih_option_version()\n");
1903
 
        program_name = "test";
1904
 
        package_name = "wibble";
1905
 
        package_version = "1.0";
1906
 
        package_copyright = "Copyright Message";
 
1319
        /* Check that the --version option is caught, dealt with by outputting
 
1320
         * version information to standard output, and terminating the process
 
1321
         * with a zero exit code.
 
1322
         */
 
1323
        TEST_FUNCTION ("nih_option_version");
 
1324
        nih_main_init_full ("test", "wibble", "1.0",
 
1325
                            "foo@bar.com", "Copyright Message");
1907
1326
 
1908
1327
        argc = 0;
1909
1328
        argv[argc++] = "ignored";
1911
1330
        argv[argc] = NULL;
1912
1331
 
1913
1332
        output = tmpfile ();
1914
 
        pid = fork ();
1915
 
        if (pid == 0) {
1916
 
                dup2 (fileno (output), STDOUT_FILENO);
1917
 
                nih_option_parser (NULL, argc, argv, options, FALSE);
1918
 
                exit (1);
 
1333
        TEST_CHILD (pid) {
 
1334
                TEST_DIVERT_STDOUT (output) {
 
1335
                        nih_option_parser (NULL, argc, argv, options, FALSE);
 
1336
                        exit (1);
 
1337
                }
1919
1338
        }
1920
1339
 
1921
1340
        waitpid (pid, &status, 0);
1922
1341
        rewind (output);
1923
1342
 
1924
 
        /* Should have exited normally */
1925
 
        if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0)) {
1926
 
                printf ("BAD: process did not exit normally.\n");
1927
 
                ret = 1;
1928
 
        }
1929
 
 
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");
1934
 
                ret = 1;
1935
 
        }
1936
 
 
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");
1941
 
                ret = 1;
1942
 
        }
1943
 
 
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");
1948
 
                ret = 1;
1949
 
        }
1950
 
 
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");
1955
 
                ret = 1;
1956
 
        }
1957
 
 
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");
1962
 
                ret = 1;
1963
 
        }
1964
 
 
1965
 
        /* Should be no more output */
1966
 
        if (fgets (text, sizeof (text), output)) {
1967
 
                printf ("BAD: more output than we expected.\n");
1968
 
                ret = 1;
1969
 
        }
 
1343
        TEST_TRUE (WIFEXITED (status));
 
1344
        TEST_EQ (WEXITSTATUS (status), 0);
 
1345
 
 
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);
1970
1352
 
1971
1353
        fclose (output);
1972
 
 
1973
 
        return ret;
1974
1354
}
1975
1355
 
1976
 
int
 
1356
void
1977
1357
test_help (void)
1978
1358
{
1979
1359
        FILE  *output;
1980
 
        char   text[81];
1981
1360
        char  *argv[3];
1982
1361
        pid_t  pid;
1983
 
        int    ret = 0, argc, status;
1984
 
 
1985
 
        printf ("Testing nih_option_set_usage()\n");
1986
 
        nih_option_set_usage ("CMD [ARG]...\n");
1987
 
 
1988
 
 
1989
 
        printf ("Testing nih_option_help()\n");
1990
 
        program_name = "test";
1991
 
        package_bugreport = "foo@bar.com";
 
1362
        int    argc, status;
 
1363
 
 
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.
 
1367
         */
 
1368
        TEST_FUNCTION ("nih_option_set_usage_stem");
 
1369
        nih_option_set_usage_stem ("[OPT]...");
 
1370
 
 
1371
        TEST_FUNCTION ("nih_option_set_usage");
 
1372
        nih_option_set_usage ("CMD [ARG]...");
 
1373
 
 
1374
        TEST_FUNCTION ("nih_option_set_synopsis");
 
1375
        nih_option_set_synopsis ("Frobnicates bars carefully, taking into "
 
1376
                                 "account things that are important when "
 
1377
                                 "doing that");
 
1378
 
 
1379
        TEST_FUNCTION ("nih_option_set_help");
 
1380
        nih_option_set_help ("This is the help text for the bar frobnication "
 
1381
                             "program.\n\n"
 
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.");
 
1385
 
 
1386
        TEST_FUNCTION ("nih_option_set_footer");
 
1387
        nih_option_set_footer ("Go away!");
 
1388
 
 
1389
 
 
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.
 
1393
         */
 
1394
        TEST_FUNCTION ("nih_option_help");
 
1395
        nih_main_init_full ("test", "wibble", "1.0",
 
1396
                            "foo@bar.com", "Copyright Message");
1992
1397
 
1993
1398
        argc = 0;
1994
1399
        argv[argc++] = "ignored";
1996
1401
        argv[argc] = NULL;
1997
1402
 
1998
1403
        output = tmpfile ();
1999
 
        pid = fork ();
2000
 
        if (pid == 0) {
2001
 
                dup2 (fileno (output), STDOUT_FILENO);
2002
 
                nih_option_parser (NULL, argc, argv, options, FALSE);
2003
 
                exit (1);
 
1404
        TEST_CHILD (pid) {
 
1405
                unsetenv ("COLUMNS");
 
1406
 
 
1407
                TEST_DIVERT_STDOUT (output) {
 
1408
                        nih_option_parser (NULL, argc, argv, options, FALSE);
 
1409
                        exit (1);
 
1410
                }
2004
1411
        }
2005
1412
 
2006
1413
        waitpid (pid, &status, 0);
2007
1414
        rewind (output);
2008
1415
 
2009
 
        /* Should have exited normally */
2010
 
        if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0)) {
2011
 
                printf ("BAD: process did not exit normally.\n");
2012
 
                ret = 1;
2013
 
        }
2014
 
 
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");
2019
 
                ret = 1;
2020
 
        }
2021
 
 
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");
2026
 
                ret = 1;
2027
 
        }
2028
 
 
2029
 
 
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");
2034
 
                ret = 1;
2035
 
        }
2036
 
 
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");
2041
 
                ret = 1;
2042
 
        }
2043
 
 
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");
2048
 
                ret = 1;
2049
 
        }
2050
 
 
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");
2055
 
                ret = 1;
2056
 
        }
2057
 
 
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");
2062
 
                ret = 1;
2063
 
        }
2064
 
 
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");
2069
 
                ret = 1;
2070
 
        }
2071
 
 
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");
2076
 
                ret = 1;
2077
 
        }
2078
 
 
2079
 
 
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");
2084
 
                ret = 1;
2085
 
        }
2086
 
 
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");
2091
 
                ret = 1;
2092
 
        }
2093
 
 
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");
2098
 
                ret = 1;
2099
 
        }
2100
 
 
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");
2105
 
                ret = 1;
2106
 
        }
2107
 
 
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");
2111
 
                ret = 1;
2112
 
        }
2113
 
 
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");
2118
 
                ret = 1;
2119
 
        }
2120
 
 
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");
2125
 
                ret = 1;
2126
 
        }
2127
 
 
2128
 
 
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");
2133
 
                ret = 1;
2134
 
        }
2135
 
 
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");
2140
 
                ret = 1;
2141
 
        }
2142
 
 
2143
 
        fgets (text, sizeof (text), output);
2144
 
        if (strcmp (text, ("  -v, --verbose               "
2145
 
                           "increase output to include informational "
2146
 
                           "messages\n"))) {
2147
 
                printf ("BAD: output wasn't what we expected.\n");
2148
 
                ret = 1;
2149
 
        }
2150
 
 
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");
2155
 
                ret = 1;
2156
 
        }
2157
 
 
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");
2162
 
                ret = 1;
2163
 
        }
2164
 
 
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");
2169
 
                ret = 1;
2170
 
        }
2171
 
 
2172
 
 
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");
2177
 
                ret = 1;
2178
 
        }
2179
 
 
2180
 
        /* Should be no more output */
2181
 
        if (fgets (text, sizeof (text), output)) {
2182
 
                printf ("BAD: more output than we expected.\n");
2183
 
                ret = 1;
2184
 
        }
 
1416
        TEST_TRUE (WIFEXITED (status));
 
1417
        TEST_EQ (WEXITSTATUS (status), 0);
 
1418
 
 
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 "
 
1452
                               "messages\n"));
 
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 "
 
1459
                               "program.\n"));
 
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);
2185
1469
 
2186
1470
        fclose (output);
2187
 
 
2188
 
        return ret;
2189
1471
}
2190
1472
 
2191
1473
 
2193
1475
main (int   argc,
2194
1476
      char *argv[])
2195
1477
{
2196
 
        int ret = 0;
2197
 
 
2198
 
        ret |= test_parser ();
2199
 
        ret |= test_count ();
2200
 
        ret |= test_quiet ();
2201
 
        ret |= test_verbose ();
2202
 
        ret |= test_version ();
2203
 
        ret |= test_help ();
2204
 
 
2205
 
        return ret;
 
1478
        test_parser ();
 
1479
        test_count ();
 
1480
        test_quiet ();
 
1481
        test_verbose ();
 
1482
        test_debug ();
 
1483
        test_version ();
 
1484
        test_help ();
 
1485
 
 
1486
        return 0;
2206
1487
}