--- /dev/null
+/* Copyright (C) 2003 TSUTSUMI Kikuo.
+ This file is part of the CCUnit Library.
+
+ The CCUnit Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public License
+ as published by the Free Software Foundation; either version 2.1 of
+ the License, or (at your option) any later version.
+
+ The CCUnit Library is distributed in the hope that it will be
+ useful, but WITHOUT ANY WARRANTY; without even the implied warranty
+ of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the CCUnit Library; see the file COPYING.LESSER.
+ If not, write to the Free Software Foundation, Inc., 59 Temple
+ Place - Suite 330, Boston, MA 02111-1307, USA.
+*/
+/*
+ * $Id$
+ */
+/**@file
+ * WriteSuite module implementation.
+ */
+
+#include <ccunit/CCUnitMakeSuite.h>
+#include <ccunit/CCUnitLogMessage.h>
+#include <stdio.h>
+
+/**
+ * @addtogroup CCUnitWriteSuite
+ * @{
+ */
+
+/**
+ * CCUnitTestType_t name string.
+ */
+static const char* ccunitTypeNames[] = {
+ "ccunitTypeTest", /**< Test class */
+ "ccunitTypeSuite", /**< TestSuite class */
+ "ccunitTypeFixture", /**< TestFixture class */
+ "ccunitTypeCase", /**< TestCase class */
+};
+
+/**
+ * print function prototype.
+ *
+ * @param ofp output stream.
+ * @param type function type; ctor, dtor, setUp, tearDown, testCase.
+ * @param fdef funcdef object to print.
+ */
+static void printPrototype (FILE* ofp, const char* type, CCUnitFuncDef* fdef)
+{
+ fprintf (ofp,
+ "/* %s */\n"
+ "%s %s %s ();\n",
+ fdef->desc,
+ !fdef->scope ? "extern" : fdef->scope, fdef->type, fdef->name);
+ ccunit_log ("%s: %s %s", type, fdef->type, fdef->name);
+}
+
+/**
+ * print test fixture prototypes.
+ *
+ * @param ofp output stream.
+ * @param suite test suite object to print.
+ */
+static void printPrototypes (FILE* ofp, CCUnitTestSuiteDef* suitedef)
+{
+ CCUnitListIterator itor;
+ CCUnitTestDef* testdef;
+ ccunit_initListIterator (&suitedef->testdefs, &itor);
+ while ((testdef = ccunit_nextListIterator (&itor)) != NULL)
+ {
+ if (testdef->type == ccunitTypeSuite)
+ printPrototypes (ofp, (CCUnitTestSuiteDef*)testdef);
+ else if (testdef->type == ccunitTypeFixture)
+ {
+ CCUnitTestFixtureDef* fdef = (CCUnitTestFixtureDef*)testdef;
+ ccunit_log ("fixturdef: %s", fdef->testdef.name);
+ if (fdef->testdef.name)
+ fprintf (ofp, "/* test fixture: %s */\n", fdef->testdef.name);
+ if (fdef->ctor)
+ printPrototype (ofp, "ctor", fdef->ctor);
+ if (fdef->dtor)
+ printPrototype (ofp, "dtor", fdef->dtor);
+ if (fdef->setUp)
+ printPrototype (ofp, "setUp", fdef->setUp);
+ if (fdef->tearDown)
+ printPrototype (ofp, "tearDown", fdef->tearDown);
+ /* print testCase prototypes */
+ {
+ CCUnitListIterator fitor;
+ CCUnitFuncDef* casedef;
+ ccunit_initListIterator (&fdef->testCases, &fitor);
+ while ((casedef = ccunit_nextListIterator (&fitor)) != NULL)
+ printPrototype (ofp, "testCase", casedef);
+ }
+ }
+ }
+ fputc ('\n', ofp);
+}
+
+#define GETNAME(P) ((!(P) || !((P)->name)) ? "NULL" : (P)->name)
+
+/**
+ * print test fixture adding function.
+ *
+ * @param ofp output stream.
+ * @param fxdef test suite to print.
+ */
+static void printFixture (FILE* ofp, CCUnitTestFixtureDef* fxdef)
+{
+ static int fxid = 0;
+ if (!fxdef->testdef.idname)
+ {
+ fxdef->testdef.idname = calloc (1, 8);
+ fxid ++;
+ sprintf (fxdef->testdef.idname, "fx_%03d", fxid);
+ }
+ fprintf (ofp,
+ "static CCUnitTestFixtureDfn %s = {\n"
+ " { %s },\n"
+ " \"%s\",\n"
+ " %s,\n"
+ " %s,\n"
+ " %s,\n"
+ " %s,\n"
+ " {\n",
+ fxdef->testdef.idname,
+ ccunitTypeNames[fxdef->testdef.type],
+ GETNAME (&fxdef->testdef),
+ GETNAME (fxdef->ctor),
+ GETNAME (fxdef->dtor),
+ GETNAME (fxdef->setUp),
+ GETNAME (fxdef->tearDown));
+ {
+ CCUnitListIterator fnitor;
+ CCUnitFuncDef* fndef;
+ ccunit_initListIterator (&fxdef->testCases, &fnitor);
+ while ((fndef = ccunit_nextListIterator (&fnitor)) != NULL)
+ {
+ fputs (" {\n", ofp);
+ fprintf (ofp,
+ " \"%s\",\n"
+ " \"%s\",\n"
+ " %s,\n",
+ fndef->name, fndef->desc, fndef->name);
+ fputs (" },\n", ofp);
+ }
+ fputs (" { NULL, NULL, NULL }\n", ofp);
+ }
+ fputs (" }\n};\n\n", ofp);
+}
+
+/**
+ * print test suite adding function.
+ *
+ * @param ofp output stream.
+ * @param name function name.
+ * @param suite test suite to print.
+ */
+static void printSuite (FILE* ofp, const char* name, CCUnitTestSuiteDef* suite)
+{
+ CCUnitListIterator itor;
+ CCUnitTestDef* testdef;
+ static int suiteid = 0;
+ if (!suite->testdef.idname)
+ {
+ suiteid ++;
+ suite->testdef.idname = calloc (1, 12);
+ sprintf (suite->testdef.idname, "suite_%03d", suiteid);
+ }
+ ccunit_initListIterator (&suite->testdefs, &itor);
+ while ((testdef = ccunit_nextListIterator (&itor)) != NULL)
+ {
+ if (testdef->type == ccunitTypeSuite)
+ printSuite (ofp, testdef->name, (CCUnitTestSuiteDef*)testdef);
+ else if (testdef->type == ccunitTypeFixture)
+ printFixture (ofp, (CCUnitTestFixtureDef*)testdef);
+ else
+ ;
+ }
+ fprintf (ofp,
+ "static CCUnitTestSuiteDfn %s = {\n"
+ " { %s },\n"
+ " \"%s\",\n"
+ " {\n",
+ suite->testdef.idname,
+ ccunitTypeNames[suite->testdef.type],
+ !suite->testdef.name ? "" : suite->testdef.name);
+ ccunit_initListIterator (&suite->testdefs, &itor);
+ while ((testdef = ccunit_nextListIterator (&itor)) != NULL)
+ {
+ fprintf (ofp, " &%s.test,\n", testdef->idname);
+ }
+ fputs (" NULL,\n },\n", ofp);
+ fputs ("};\n", ofp);
+}
+
+/*
+ * print test suite.
+ */
+void ccunit_printSuite (FILE* ofp, const char* name, CCUnitTestSuiteDef* suite)
+{
+ fprintf (ofp,
+ "#include <ccunit/CCUnitTestSuite.h>\n"
+ "#include <ccunit/CCUnitTestFixture.h>\n"
+ "#include <ccunit/CCUnitTestCase.h>\n"
+ "\n");
+ printPrototypes (ofp, suite);
+ printSuite (ofp, NULL, suite);
+ if (!name)
+ name = "ccunit_suite";
+ fprintf (ofp,
+ "\n"
+ "CCUnitTestSuite* %s (const char* name)\n"
+ "{\n"
+ " if (!%s.name[0])\n"
+ " %s.name = name;\n"
+ " return ccunit_newTestSuiteFromDfn (&%s);\n"
+ "}\n",
+ name,
+ suite->testdef.idname, suite->testdef.idname, suite->testdef.idname);
+}
+
+/** @} */