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.
27 #include <ccunit/CCUnitMakeSuite.h>
28 #include <ccunit/CCUnitReadSuite.h>
29 #include <ccunit/CCUnitWriteSuite.h>
30 #include <ccunit/CCUnitLogMessage.h>
36 * If `setvbuf' takes the buffering type as its second argument and
37 * the buffer pointer as the third, instead of the other way around.
40 # define SETVBUF(FP,PTR,TYP,SZ) setvbuf(FP,TYP,PTR,SZ)
42 # define SETVBUF(FP,PTR,TYP,SZ) setvbuf(FP,PTR,TYP,SZ)
45 #if defined(unix) || defined(__MACH__) || defined(__CYGWIN__)
46 #define DIRSEP '/' /**< directory separetor */
47 #elif defined (__WIN32__) || defined (__MSDOS__)
48 #define DIRSEP '\\' /**< directory separetor */
50 #define DIRSEP '/' /**< directory separetor */
54 # define LONGOPTNAME(LO) "--" #LO " | " /**< long name option */
56 # define LONGOPTNAME(LO) /* -- LO | */ /**< long name option */
60 * @addtogroup CCUnitMakeSuite
64 int ccunit_makeSuite (int ac, char** av)
66 CCUnitTestSuiteDef* suite;
67 char* outputFile = "-";
68 const char* functionName = "ccunit_suite";
71 char* progname = NULL;
72 #ifdef HAVE_GETOPT_LONG
74 static struct option long_options[] = {
75 { "output", required_argument, NULL, 'o' },
76 { "function", required_argument, NULL, 'f' },
77 { "verbose", no_argument, NULL, 'v' },
78 { "debug", no_argument, NULL, 'd' },
79 { "version", no_argument, NULL, 'V' },
80 { "help", no_argument, NULL, 'h' },
86 progname = strrchr (av[0], DIRSEP);
90 # if defined (__WIN32__) || defined (__MSDOS__)
91 if (progname[2] == ':')
100 #ifdef HAVE_GETOPT_LONG
101 opt = getopt_long (ac, av, "o:f:vdVh", long_options, &option_index);
103 opt = getopt (ac, av, "o:f:vdVh");
110 #ifdef HAVE_GETOPT_LONG
111 puts (long_options[option_index].name);
118 functionName = optarg;
121 _ccunit_verbose_message = !_ccunit_verbose_message;
124 _ccunit_debug_message = !_ccunit_debug_message;
127 fprintf (stdout, "%s - %s\n", PACKAGE_STRING, progname);
132 " USAGE: %s [OPTIONS] FILES...\n"
134 " " LONGOPTNAME(output) "-o OUTFILE\n"
135 " output file name (default stdout)\n"
136 " " LONGOPTNAME(function) "-f FUNCTION\n"
137 " created function name\n"
138 " " LONGOPTNAME(verbose) "-v\n"
139 " output verbose message\n"
140 " " LONGOPTNAME(debug) "-d\n"
141 " output debug message\n"
142 " " LONGOPTNAME(version) "-V\n"
144 " " LONGOPTNAME(help) "-h\n"
145 " print this message\n"
146 , PACKAGE_STRING, progname);
149 fprintf (stderr, "unknown option %s\n", av[optind]);
152 fprintf (stderr, "bad option argument: %s\n", av[optind]);
156 suite = ccunit_newTestSuiteDef (NULL);
159 ccunit_err ("can't create test suite def. not enough memory.");
162 if (strcmp (outputFile, "-") == 0)
166 ofp = fopen (outputFile, "w");
169 fprintf (stderr, "can't open file '%s': %s.\n",
170 outputFile, strerror (errno));
174 SETVBUF(ofp, NULL, _IONBF, 0);
175 SETVBUF(stderr, NULL, _IONBF, 0);
176 /* process source files */
178 ccunit_readSuite ("-", suite);
180 for (; optind < ac; optind ++)
181 ccunit_readSuite (av[optind], suite);
182 ccunit_writeSuite (ofp, functionName, suite);
185 ccunit_deleteTestSuiteDef (suite);
189 int ccunit_va_makeSuite (const char* prg, ...)
192 size_t capacity = 10;
193 char** av = calloc (10, sizeof (char*));
196 va_start (args, prg);
203 av = realloc (av, capacity * sizeof (char*));
205 av[ac] = va_arg (args, char*);
208 return ccunit_makeSuite (ac, av);