1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "apr_errno.h"
18 #include "apr_general.h"
19 #include "apr_getopt.h"
20 #include "apr_strings.h"
23 static void format_arg(char *str, char option, const char *arg)
26 apr_snprintf(str, 8196, "%soption: %c with %s\n", str, option, arg);
29 apr_snprintf(str, 8196, "%soption: %c\n", str, option);
33 static void unknown_arg(void *str, const char *err, ...)
38 apr_vsnprintf(str, 8196, err, va);
42 static void no_options_found(CuTest *tc)
45 const char * const largv[] = {"testprog", "-a", "-b", "-c", "-d"};
53 rv = apr_getopt_init(&opt, p, largc, largv);
54 CuAssertIntEquals(tc, APR_SUCCESS, rv);
56 while (apr_getopt(opt, "abcd", &data, &optarg) == APR_SUCCESS) {
63 format_arg(str, data, optarg);
66 CuAssertStrEquals(tc, "option: a\n"
72 static void no_options(CuTest *tc)
75 const char * const largv[] = {"testprog", "-a", "-b", "-c", "-d"};
83 rv = apr_getopt_init(&opt, p, largc, largv);
84 CuAssertIntEquals(tc, APR_SUCCESS, rv);
86 opt->errfn = unknown_arg;
89 while (apr_getopt(opt, "efgh", &data, &optarg) == APR_SUCCESS) {
95 format_arg(str, data, optarg);
101 CuAssertStrEquals(tc, "testprog: illegal option -- a\n", str);
104 static void required_option(CuTest *tc)
107 const char * const largv[] = {"testprog", "-a", "foo"};
115 rv = apr_getopt_init(&opt, p, largc, largv);
116 CuAssertIntEquals(tc, APR_SUCCESS, rv);
118 opt->errfn = unknown_arg;
121 while (apr_getopt(opt, "a:", &data, &optarg) == APR_SUCCESS) {
124 format_arg(str, data, optarg);
130 CuAssertStrEquals(tc, "option: a with foo\n", str);
133 static void required_option_notgiven(CuTest *tc)
136 const char * const largv[] = {"testprog", "-a"};
144 rv = apr_getopt_init(&opt, p, largc, largv);
145 CuAssertIntEquals(tc, APR_SUCCESS, rv);
147 opt->errfn = unknown_arg;
150 while (apr_getopt(opt, "a:", &data, &optarg) == APR_SUCCESS) {
153 format_arg(str, data, optarg);
159 CuAssertStrEquals(tc, "testprog: option requires an argument -- a\n", str);
162 static void optional_option(CuTest *tc)
165 const char * const largv[] = {"testprog", "-a", "foo"};
173 rv = apr_getopt_init(&opt, p, largc, largv);
174 CuAssertIntEquals(tc, APR_SUCCESS, rv);
176 opt->errfn = unknown_arg;
179 while (apr_getopt(opt, "a::", &data, &optarg) == APR_SUCCESS) {
182 format_arg(str, data, optarg);
188 CuAssertStrEquals(tc, "option: a with foo\n", str);
191 static void optional_option_notgiven(CuTest *tc)
194 const char * const largv[] = {"testprog", "-a"};
202 rv = apr_getopt_init(&opt, p, largc, largv);
203 CuAssertIntEquals(tc, APR_SUCCESS, rv);
205 opt->errfn = unknown_arg;
208 while (apr_getopt(opt, "a::", &data, &optarg) == APR_SUCCESS) {
211 format_arg(str, data, optarg);
218 /* Our version of getopt doesn't allow for optional arguments. */
219 CuAssertStrEquals(tc, "option: a\n", str);
221 CuAssertStrEquals(tc, "testprog: option requires an argument -- a\n", str);
224 CuSuite *testgetopt(void)
226 CuSuite *suite = CuSuiteNew("Getopt");
228 SUITE_ADD_TEST(suite, no_options);
229 SUITE_ADD_TEST(suite, no_options_found);
230 SUITE_ADD_TEST(suite, required_option);
231 SUITE_ADD_TEST(suite, required_option_notgiven);
232 SUITE_ADD_TEST(suite, optional_option);
233 SUITE_ADD_TEST(suite, optional_option_notgiven);