2 * Copyright (c) 1999 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Test if agetarg works as expected
53 enum { GA_SUCCESS
= 0, GA_FAILURE
} retval
;
57 /* XXX TODO: aarg_negative_flag, manualpage generation ? */
64 test_simple_string(void)
69 {AARG_GNUSTYLE
, 2, {"string", "--string=foo", NULL
}},
70 {AARG_GNUSTYLE
, 3, {"string", "-s", "foo", NULL
}},
71 {AARG_AFSSTYLE
, 3, {"string", "-string", "foo", NULL
}},
72 {AARG_AFSSTYLE
, 3, {"string", "-strin", "foo", NULL
}},
73 {AARG_AFSSTYLE
, 3, {"string", "-st", "foo", NULL
}, GA_FAILURE
},
74 {AARG_AFSSTYLE
, 2, {"string", "--flag"}, GA_FAILURE
},
75 {AARG_AFSSTYLE
, 2, {"string", "foo", NULL
}}
78 struct agetargs args
[] = {
79 {"string", 's', aarg_string
, NULL
,
80 "string test", "stringfoo", aarg_mandatory
},
81 {"strip", 0, aarg_string
, NULL
,
82 "strip test", "stripfoo", aarg_optional
},
83 {NULL
, 0, aarg_end
, NULL
, NULL
}
88 for (i
= 0; i
< sizeof(tests
) / sizeof(*tests
); i
++) {
93 (args
, tests
[i
].argc
, tests
[i
].argv
, &optind
, tests
[i
].style
)) {
94 if (tests
[i
].retval
== GA_FAILURE
)
96 warnx("test_string: %s failed for test %d", tests
[i
].argv
[1], i
);
99 if (tests
[i
].retval
!= GA_SUCCESS
) {
100 warnx("test_string: %s failed to fail for test %d",
101 tests
[i
].argv
[1], i
);
106 if (optind
!= tests
[i
].argc
) {
107 warnx("argc != optind for test %s, %d", tests
[i
].argv
[1], i
);
111 if (string
== NULL
|| strcmp(string
, "foo") != 0) {
112 warnx("error parsing for test %d: string", i
);
123 test_simple_strings(void)
125 agetarg_strings strings
;
129 {AARG_GNUSTYLE
, 3, {"strings",
130 "--strings=foo", "--strings=bar", NULL
}},
131 {AARG_GNUSTYLE
, 5, {"strings", "-s", "foo", "-s", "bar", NULL
}},
132 {AARG_AFSSTYLE
, 4, {"strings", "-string", "foo", "bar", NULL
}}
134 {AARG_AFSSTYLE
, 3, {"strings", "foo", "bar", NULL
}}
138 struct agetargs args
[] = {
139 {"strings", 's', aarg_strings
, NULL
,
140 "strings test", "stringsfoo", aarg_optional
},
141 {NULL
, 0, aarg_end
, NULL
, NULL
}
146 for (i
= 0; i
< sizeof(tests
) / sizeof(*tests
); i
++) {
147 strings
.num_strings
= 0;
148 strings
.strings
= NULL
;
152 (args
, tests
[i
].argc
, tests
[i
].argv
, &optind
, tests
[i
].style
)) {
153 if (tests
[i
].retval
== GA_FAILURE
)
155 warnx("test_strings: %s failed for test %d", tests
[i
].argv
[1], i
);
158 if (tests
[i
].retval
!= GA_SUCCESS
) {
159 warnx("test_strings: %s failed to fail for test %d",
160 tests
[i
].argv
[1], i
);
165 if (optind
!= tests
[i
].argc
) {
166 warnx("argc != optind for test %s, %d", tests
[i
].argv
[1], i
);
170 if (strings
.num_strings
!= 2 || strcmp(strings
.strings
[0], "foo") != 0
171 || strcmp(strings
.strings
[1], "bar") != 0) {
172 warnx("error parsing for test %d: strings", i
);
183 test_simple_integer(void)
188 {AARG_GNUSTYLE
, 2, {"integer", "--integer=4711", NULL
}},
189 {AARG_GNUSTYLE
, 3, {"integer", "-i", "4711", NULL
}},
190 {AARG_AFSSTYLE
, 3, {"integer", "-integer", "4711", NULL
}},
191 {AARG_AFSSTYLE
, 2, {"integer", "4711", NULL
}}
194 struct agetargs args
[] = {
195 {"integer", 'i', aarg_integer
, NULL
,
196 "integer test", "integer", aarg_mandatory
},
197 {NULL
, 0, aarg_end
, NULL
, NULL
}
202 for (i
= 0; i
< sizeof(tests
) / sizeof(*tests
); i
++) {
207 (args
, tests
[i
].argc
, tests
[i
].argv
, &optind
, tests
[i
].style
)) {
208 if (tests
[i
].retval
== GA_FAILURE
)
210 warnx("test_integer: %s failed for test %d", tests
[i
].argv
[1], i
);
213 if (tests
[i
].retval
!= GA_SUCCESS
) {
214 warnx("test_integer: %s failed to fail for test %d",
215 tests
[i
].argv
[1], i
);
220 if (optind
!= tests
[i
].argc
) {
221 warnx("argc != optind for test %s, %d", tests
[i
].argv
[1], i
);
225 if (integer
!= 4711) {
226 warnx("error parsing for test %d: integer 4711", i
);
237 test_simple_flag(void)
242 {AARG_GNUSTYLE
, 2, {"flag", "--flag=yes", NULL
}, GA_SUCCESS
},
243 {AARG_GNUSTYLE
, 2, {"flag", "-g", NULL
}, GA_SUCCESS
},
244 {AARG_AFSSTYLE
, 2, {"flag", "--flag"}, GA_FAILURE
},
245 {AARG_AFSSTYLE
, 2, {"flag", "-flag", NULL
}, GA_SUCCESS
},
248 {AARG_AFSSTYLE
, 2, {"flag", "yes", NULL
}, GA_SUCCESS
},
250 {AARG_GNUSTYLE
, 2, {"flag", "--no-flag", NULL
}, GA_SUCCESS
}
253 struct agetargs args
[] = {
254 {"flag", 'g', aarg_flag
, NULL
,
255 "flag", "flag bar", aarg_optional
},
256 {NULL
, 0, aarg_end
, NULL
, NULL
}
261 for (i
= 0; i
< sizeof(tests
) / sizeof(*tests
); i
++) {
269 (args
, tests
[i
].argc
, tests
[i
].argv
, &optind
, tests
[i
].style
)) {
270 if (tests
[i
].retval
== GA_FAILURE
)
272 warnx("test_flag: %s failed for test %d", tests
[i
].argv
[1], i
);
275 if (tests
[i
].retval
!= GA_SUCCESS
) {
276 warnx("test_flag: %s failed to fail for test %d",
277 tests
[i
].argv
[1], i
);
282 if (optind
!= tests
[i
].argc
) {
283 warnx("argc != optind for test %s, %d", tests
[i
].argv
[1], i
);
289 warnx("error parsing for test %d: flag %s", i
,
295 warnx("error parsing test %d: flag %s", i
, tests
[i
].argv
[1]);
307 main(int argc
, char **argv
)
310 test_simple_string();
311 test_simple_strings();
312 test_simple_integer();