2 This file is part of GNUnet.
3 (C) 2003, 2004, 2005, 2006, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file util/test_getopt.c
22 * @brief testcase for util/getopt.c
25 #include "gnunet_common.h"
26 #include "gnunet_configuration_lib.h"
27 #include "gnunet_getopt_lib.h"
34 struct GNUNET_CONFIGURATION_Handle *cfg;
35 char *const emptyargv[] = {
39 const struct GNUNET_GETOPT_CommandLineOption emptyoptionlist[] = {
40 GNUNET_GETOPT_OPTION_END
43 cfg = GNUNET_CONFIGURATION_create ();
44 if (1 != GNUNET_GETOPT_run ("test", cfg, emptyoptionlist, 1, emptyargv))
46 GNUNET_CONFIGURATION_destroy (cfg);
49 GNUNET_CONFIGURATION_destroy (cfg);
57 struct GNUNET_CONFIGURATION_Handle *cfg;
58 char *const myargv[] = {
65 unsigned int vflags = 0;
66 const struct GNUNET_GETOPT_CommandLineOption verboseoptionlist[] = {
67 GNUNET_GETOPT_OPTION_VERBOSE (&vflags),
68 GNUNET_GETOPT_OPTION_END
71 cfg = GNUNET_CONFIGURATION_create ();
72 if (3 != GNUNET_GETOPT_run ("test", cfg, verboseoptionlist, 4, myargv))
75 GNUNET_CONFIGURATION_destroy (cfg);
78 GNUNET_CONFIGURATION_destroy (cfg);
90 struct GNUNET_CONFIGURATION_Handle *cfg;
91 char *const myargv[] = {
96 const struct GNUNET_GETOPT_CommandLineOption versionoptionlist[] = {
97 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
98 GNUNET_GETOPT_OPTION_END
101 cfg = GNUNET_CONFIGURATION_create ();
102 if (-1 != GNUNET_GETOPT_run ("test_getopt",
103 cfg, versionoptionlist, 2, myargv))
106 GNUNET_CONFIGURATION_destroy (cfg);
109 GNUNET_CONFIGURATION_destroy (cfg);
116 struct GNUNET_CONFIGURATION_Handle *cfg;
117 char *const myargv[] = {
122 const struct GNUNET_GETOPT_CommandLineOption aboutoptionlist[] = {
123 GNUNET_GETOPT_OPTION_HELP ("Testing"),
124 GNUNET_GETOPT_OPTION_END
127 cfg = GNUNET_CONFIGURATION_create ();
128 if (-1 != GNUNET_GETOPT_run ("test_getopt",
129 cfg, aboutoptionlist, 2, myargv))
132 GNUNET_CONFIGURATION_destroy (cfg);
135 GNUNET_CONFIGURATION_destroy (cfg);
142 struct GNUNET_CONFIGURATION_Handle *cfg;
143 char *const myargv[] = {
149 char *level = GNUNET_strdup ("stuff");
151 const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = {
152 GNUNET_GETOPT_OPTION_LOGFILE (&fn),
153 GNUNET_GETOPT_OPTION_LOGLEVEL (&level),
154 GNUNET_GETOPT_OPTION_END
157 cfg = GNUNET_CONFIGURATION_create ();
158 if (5 != GNUNET_GETOPT_run ("test_getopt", cfg, logoptionlist, 5, myargv))
161 GNUNET_CONFIGURATION_destroy (cfg);
164 GNUNET_assert (fn != NULL);
165 GNUNET_CONFIGURATION_destroy (cfg);
166 if ((0 != strcmp (level, "WARNING")) || (0 != strcmp (fn, "filename")))
181 struct GNUNET_CONFIGURATION_Handle *cfg;
182 char *const myargv[] = {
190 unsigned int num = 0;
191 unsigned long long lnum = 0;
192 const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = {
193 {'f', "--flag", NULL, "helptext", 0, &GNUNET_GETOPT_set_one,
195 {'n', "--num", "ARG", "helptext", 1, &GNUNET_GETOPT_set_uint,
197 {'N', "--lnum", "ARG", "helptext", 1, &GNUNET_GETOPT_set_ulong,
199 GNUNET_GETOPT_OPTION_END
202 cfg = GNUNET_CONFIGURATION_create ();
203 if (6 != GNUNET_GETOPT_run ("test_getopt", cfg, logoptionlist, 6, myargv))
206 GNUNET_CONFIGURATION_destroy (cfg);
209 GNUNET_CONFIGURATION_destroy (cfg);
210 if ((1 != flag) || (42 != num) || (42 != lnum))
219 main (int argc, char *argv[])
223 GNUNET_log_setup ("test_getopt", "WARNING", NULL);
224 /* suppress output from -h, -v options */
225 GNUNET_break (0 == CLOSE (1));
226 if (0 != testMinimal ())
228 if (0 != testVerbose ())
230 if (0 != testVersion ())
232 if (0 != testAbout ())
234 if (0 != testLogOpts ())
236 if (0 != testFlagNum ())