OSDN Git Service

change private struct name
[ccunit/ccunit.git] / src / ccunit / CCUnitWriteSuite.c
1 /* Copyright (C) 2003 TSUTSUMI Kikuo.
2    This file is part of the CCUnit Library.
3
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.
8
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.
13
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.  
18 */
19 /*
20  * $Id$
21  */
22 /**@file
23  * WriteSuite module implementation.
24  */
25
26 #include <ccunit/CCUnitMakeSuite.h>
27 #include <ccunit/CCUnitLogMessage.h>
28 #include <stdio.h>
29
30 /**
31  * @ingroup CCunitMakeSuite
32  * @addtogroup CCUnitWriteSuite
33  * @{
34  */
35
36 /**
37  * print test fixture prototypes.
38  *
39  * @param ofp output stream.
40  * @param suite test suite object to print.
41  */
42 static void writePrototypes (FILE* ofp, _CCUnitTestSuiteDef* suite)
43 {
44   CCUnitListIterator itor;
45   _CCUnitTestDef* test;
46   _CCUnitFuncDef* testCase;
47   static unsigned suiteid = 0;
48   char sname[32];
49   sprintf (sname, "newSuite_%03u", ++ suiteid);
50   fprintf (ofp, "static CCUnitTestSuite* %s (const char* name);\n", sname);
51   ccunit_log ("testSuite: '%s'", sname);
52   ccunit_initListIterator (&suite->testdefs, &itor);
53   while ((test = (_CCUnitTestDef*)ccunit_nextListIterator (&itor)) != NULL)
54     {
55       if (test->type == ccunitTypeSuite)
56         {
57           writePrototypes (ofp, (_CCUnitTestSuiteDef*)test);
58         }
59       else if (test->type == ccunitTypeFixture)
60         {
61           _CCUnitTestFixtureDef* testFixture = (_CCUnitTestFixtureDef*)test;
62           CCUnitListIterator fitor;
63           ccunit_log ("testFixture: %s", testFixture->testdef.name);
64           if (testFixture->testdef.name)
65             fprintf (ofp, "/* test fixture: %s */\n", testFixture->testdef.name);
66           if (testFixture->setUp)
67             {
68               fprintf (ofp,
69                        "/* %s */\n"
70                        "extern %s %s ();\n",
71                        testFixture->setUp->desc,
72                        testFixture->setUp->type,
73                        testFixture->setUp->name);
74               ccunit_log ("setUp: %s %s",
75                           testFixture->setUp->type, testFixture->setUp->name);
76             }
77           if (testFixture->tearDown)
78             {
79               fprintf (ofp,
80                        "/* %s */\n"
81                        "extern %s %s ();\n",
82                        testFixture->tearDown->desc,
83                        testFixture->tearDown->type,
84                        testFixture->tearDown->name);
85               ccunit_log ("tearDown: %s %s",
86                           testFixture->tearDown->type, testFixture->tearDown->name);
87             }
88           ccunit_initListIterator (&testFixture->testCases, &fitor);
89           while ((testCase = ccunit_nextListIterator (&fitor)) != NULL)
90             {
91               fprintf (ofp,
92                        "/* %s */\n"
93                        "extern %s %s ();\n",
94                        testCase->desc,
95                        testCase->type,
96                        testCase->name);
97               ccunit_log ("runTest: %s %s", testCase->type, testCase->name);
98             }
99         }
100     }
101   fputc ('\n', ofp);
102 }
103
104 /**
105  * print test suite adding function.
106  *
107  * @param ofp output stream.
108  * @param name function name.
109  * @param suite test suite to print.
110  */
111 static void writeSuite (FILE* ofp,
112                         const char* name,
113                         _CCUnitTestSuiteDef* suite)
114 {
115   CCUnitListIterator itor;
116   _CCUnitTestDef* testdef;
117   static unsigned int suiteid = 0;
118   unsigned int callid;
119   suiteid ++;
120   fprintf (ofp,
121            "static CCUnitTestSuite* newSuite_%03u (const char* name)\n"
122            "{\n"
123            "  CCUnitTestSuite* suite;\n"
124            "  suite = ccunit_newTestSuite (name);\n",
125            suiteid);
126   callid = suiteid;
127   ccunit_initListIterator (&suite->testdefs, &itor);
128   while ((testdef = (_CCUnitTestDef*)ccunit_nextListIterator (&itor)) != NULL)
129     {
130       if (testdef->type == ccunitTypeSuite)
131         {
132           callid ++;
133           fprintf (ofp,
134                    "  {\n"
135                    "    CCUnitTestSuite* newSuite;\n"
136                    "    newSuite = newSuite_%03d (name);\n"
137                    "    if (newSuite != NULL)\n"
138                    "      ccunit_addTestSuite (suite, newSuite);\n"
139                    "  }\n",
140                    callid);
141         }
142       else if (testdef->type == ccunitTypeFixture)
143         {
144           CCUnitListIterator fitor;
145           _CCUnitFuncDef* testCase;
146           _CCUnitTestFixtureDef* testFixture = (_CCUnitTestFixtureDef*)testdef;
147           fprintf (ofp,
148                    "  {\n"
149                    "    CCUnitTestFixture* testFixture;\n"
150                    "    testFixture = ccunit_newTestFixture (\"%s\",\n"
151                    "                                         %s,\n"
152                    "                                         %s);\n"
153                    "    if (testFixture != NULL)\n" 
154                    "      ccunit_addTestFixture (suite, testFixture);\n",
155                    testdef->name,
156                    !testFixture->setUp ? "NULL" : testFixture->setUp->name,
157                    !testFixture->tearDown ? "NULL" : testFixture->tearDown->name);
158           ccunit_initListIterator (&testFixture->testCases, &fitor);
159           while ((testCase = ccunit_nextListIterator (&fitor)) != NULL)
160             {
161               fprintf (ofp,
162                        "    {\n"
163                        "      CCUnitTestCase* newCase;\n"
164                        "      newCase = ccunit_newTestCase (\"%s\",\n"
165                        "                                    \"%s\",\n"
166                        "                                    %s);\n"
167                        "      if (newCase != NULL)\n"
168                        "        ccunit_addTestCase (testFixture, newCase);\n"
169                        "    }\n",
170                        testCase->name, testCase->desc, testCase->name);
171             }
172           fprintf (ofp, "  }\n");
173         }
174     }
175   fprintf (ofp, "\n  return suite;\n}\n\n");
176   ccunit_initListIterator (&suite->testdefs, &itor);
177   while ((testdef = (_CCUnitTestDef*)ccunit_nextListIterator (&itor)) != NULL)
178     {
179       if (testdef->type == ccunitTypeSuite)
180         writeSuite (ofp, testdef->name, (_CCUnitTestSuiteDef*)testdef);
181       else
182         ;
183     }
184 }
185
186 /*
187  * print test suite.
188  */
189 void ccunit_writeSuite (FILE* ofp, const char* name, _CCUnitTestSuiteDef* suite)
190 {
191   fprintf (ofp,
192            "#include <ccunit/CCUnitTestSuite.h>\n"
193            "\n");
194   writePrototypes (ofp, suite);
195   writeSuite (ofp, NULL, suite);
196   if (!name)
197     name = "ccunit_suite";
198   fprintf (ofp,
199            "\n"
200            "CCUnitTestSuite* %s (const char* name)\n"
201            "{\n"
202            "  return newSuite_001 (name);\n"
203            "}\n",
204            name);
205 }
206
207 /** @} */