1 /* Copyright (C) 2003 TSUTSUMI Kikuo.
2 This file is part of the CCUnit Library.
4 The CCUnit Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public License
6 as published by the Free Software Foundation; either version 2.1 of
7 the License, or (at your option) any later version.
9 The CCUnit Library is distributed in the hope that it will be
10 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
11 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the CCUnit Library; see the file COPYING.LESSER.
16 If not, write to the Free Software Foundation, Inc., 59 Temple
17 Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Make test suite object code from test source code.
26 #include <ccunit/CCUnitMakeSuite.h>
27 #include <ccunit/CCUnitLogMessage.h>
28 #if CCUNIT_HAVE_UNISTD_H
33 #if CCUNIT_STDC_HEADERS
38 * If `setvbuf' takes the buffering type as its second argument and
39 * the buffer pointer as the third, instead of the other way around.
41 #if CCUNIT_SETVBUF_REVERSED
42 # define SETVBUF(FP,PTR,TYP,SZ) setvbuf(FP,TYP,PTR,SZ)
44 # define SETVBUF(FP,PTR,TYP,SZ) setvbuf(FP,PTR,TYP,SZ)
47 #if defined(unix) || defined(__MACH__) || defined(__CYGWIN__)
48 #define DIRSEP '/' /**< directory separetor */
49 #elif defined (__WIN32__) || defined (__MSDOS__)
50 #define DIRSEP '\\' /**< directory separetor */
52 #define DIRSEP '/' /**< directory separetor */
55 #if CCUNIT_HAVE_GETOPT_LONG
56 # define LONGOPTNAME(LO) "--" #LO " | " /**< long name option */
58 # define LONGOPTNAME(LO) /* -- LO | */ /**< long name option */
62 * @addtogroup CCUnitMakeSuite
66 int ccunit_makeSuite (int ac, char** av)
68 CCUnitTestSuiteDef* suite;
69 char* outputFile = "-";
70 const char* functionName = "ccunit_suite";
73 char* progname = NULL;
74 #ifdef CCUNIT_HAVE_GETOPT_LONG
76 static struct option long_options[] = {
77 { "output", required_argument, NULL, 'o' },
78 { "function", required_argument, NULL, 'f' },
79 { "verbose", no_argument, NULL, 'v' },
80 { "debug", no_argument, NULL, 'd' },
81 { "version", no_argument, NULL, 'V' },
82 { "help", no_argument, NULL, 'h' },
88 progname = strrchr (av[0], DIRSEP);
92 # if defined (__WIN32__) || defined (__MSDOS__)
93 if (progname[2] == ':')
102 #ifdef CCUNIT_HAVE_GETOPT_LONG
103 opt = getopt_long (ac, av, "o:f:vdVh", long_options, &option_index);
105 opt = getopt (ac, av, "o:f:vdVh");
112 #ifdef CCUNIT_HAVE_GETOPT_LONG
113 puts (long_options[option_index].name);
120 functionName = optarg;
123 _ccunit_verbose_message = !_ccunit_verbose_message;
126 _ccunit_debug_message = !_ccunit_debug_message;
129 fprintf (stdout, "%s - %s\n", CCUNIT_PACKAGE_STRING, progname);
134 " USAGE: %s [OPTIONS] FILES...\n"
136 " " LONGOPTNAME(output) "-o OUTFILE\n"
137 " output file name (default stdout)\n"
138 " " LONGOPTNAME(function) "-f FUNCTION\n"
139 " created function name\n"
140 " " LONGOPTNAME(verbose) "-v\n"
141 " output verbose message\n"
142 " " LONGOPTNAME(debug) "-d\n"
143 " output debug message\n"
144 " " LONGOPTNAME(version) "-V\n"
146 " " LONGOPTNAME(help) "-h\n"
147 " print this message\n"
148 , CCUNIT_PACKAGE_STRING, progname);
151 fprintf (stderr, "unknown option %s\n", av[optind]);
154 fprintf (stderr, "bad option argument: %s\n", av[optind]);
158 suite = ccunit_newTestSuiteDef (NULL);
161 ccunit_err ("can't create test suite def. not enough memory.");
164 if (strcmp (outputFile, "-") == 0)
168 ofp = fopen (outputFile, "w");
171 fprintf (stderr, "can't open file '%s': %s.\n",
172 outputFile, strerror (errno));
176 SETVBUF(ofp, NULL, _IONBF, 0);
177 SETVBUF(stderr, NULL, _IONBF, 0);
178 /* process source files */
180 ccunit_readSuite ("-", suite);
182 for (; optind < ac; optind ++)
183 ccunit_readSuite (av[optind], suite);
184 ccunit_writeSuite (ofp, functionName, suite);
187 ccunit_deleteTestSuiteDef (suite);
191 int ccunit_va_makeSuite (const char* prg, ...)
194 size_t capacity = 10;
195 char** av = calloc (10, sizeof (char*));
198 va_start (args, prg);
205 av = realloc (av, capacity * sizeof (char*));
207 av[ac] = va_arg (args, char*);
210 return ccunit_makeSuite (ac, av);