OSDN Git Service

rename from WriteTestAdd
authortsutsumi <>
Thu, 11 Sep 2003 02:00:46 +0000 (02:00 +0000)
committertsutsumi <>
Thu, 11 Sep 2003 02:00:46 +0000 (02:00 +0000)
src/ccunit/CCUnitWriteSuite.c [new file with mode: 0644]

diff --git a/src/ccunit/CCUnitWriteSuite.c b/src/ccunit/CCUnitWriteSuite.c
new file mode 100644 (file)
index 0000000..54ccc54
--- /dev/null
@@ -0,0 +1,201 @@
+/* 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$
+ */
+
+#include <ccunit/CCUnitMakeSuite.h>
+#include <ccunit/CCUnitLogMessage.h>
+#include <ccunit/CCUnitList.h>
+#include <stdio.h>
+
+/**
+ * print test case prototypes.
+ *
+ * @param ofp output stream.
+ * @param suite test suite object to print.
+ */
+static void printPrototypes (FILE* ofp, CCUnitTestSuiteDef* suite)
+{
+  CCUnitListIterator itor;
+  CCUnitTestDef* test;
+  CCUnitTestFixtureDef* fixture;
+  static unsigned suiteid = 0;
+  char sname[32];
+  sprintf (sname, "newSuite_%03u", ++ suiteid);
+  fprintf (ofp, "static CCUnitTestSuite* %s (const char* name);\n", sname);
+  ccunit_log ("testSuite: '%s'", sname);
+  ccunit_initListIterator (&suite->testdefs, &itor);
+  while ((test = (CCUnitTestDef*)ccunit_nextListIterator (&itor)) != NULL)
+    {
+      if (test->type == ccunitTypeSuite)
+       {
+         printPrototypes (ofp, (CCUnitTestSuiteDef*)test);
+       }
+      else if (test->type == ccunitTypeCase)
+       {
+         CCUnitTestCaseDef* testCase = (CCUnitTestCaseDef*)test;
+         CCUnitListIterator fitor;
+         ccunit_log ("testCase: %s '%s'",
+                     testCase->testdef.name, testCase->desc);
+         if (testCase->desc)
+           fprintf (ofp, "/** test case: %s */\n", testCase->desc);
+         if (testCase->setUp)
+           {
+             fprintf (ofp,
+                      "/** %s */\n"
+                      "extern %s %s ();\n",
+                      testCase->setUp->desc,
+                      testCase->setUp->type,
+                      testCase->setUp->name);
+             ccunit_log ("setUp: %s %s",
+                         testCase->setUp->type, testCase->setUp->name);
+           }
+         if (testCase->tearDown)
+           {
+             fprintf (ofp,
+                      "/** %s */\n"
+                      "extern %s %s ();\n",
+                      testCase->tearDown->desc,
+                      testCase->tearDown->type,
+                      testCase->tearDown->name);
+             ccunit_log ("tearDown: %s %s",
+                         testCase->tearDown->type, testCase->tearDown->name);
+           }
+         ccunit_initListIterator (&testCase->fixtures, &fitor);
+         while ((fixture = ccunit_nextListIterator (&fitor)) != NULL)
+           {
+             fprintf (ofp,
+                      "/** %s */\n"
+                      "extern %s %s ();\n",
+                      fixture->desc,
+                      fixture->type,
+                      fixture->name);
+             ccunit_log ("runTest: %s %s", fixture->type, fixture->name);
+           }
+       }
+    }
+  fputc ('\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 unsigned int suiteid = 0;
+  unsigned int callid;
+  suiteid ++;
+  fprintf (ofp,
+          "static CCUnitTestSuite* newSuite_%03u (const char* name)\n"
+          "{\n"
+          "  CCUnitTestSuite* suite;\n"
+          "  suite = ccunit_newTestSuite (name);\n",
+          suiteid);
+  callid = suiteid;
+  ccunit_initListIterator (&suite->testdefs, &itor);
+  while ((testdef = (CCUnitTestDef*)ccunit_nextListIterator (&itor)) != NULL)
+    {
+      if (testdef->type == ccunitTypeSuite)
+       {
+         callid ++;
+         fprintf (ofp,
+                  "  {\n"
+                  "    CCUnitTestSuite* newSuite;\n"
+                  "    newSuite = newSuite_%03d (name);\n"
+                  "    if (newSuite != NULL)\n"
+                  "      ccunit_addTestSuite (suite, newSuite);\n"
+                  "  }\n",
+                  callid);
+       }
+      else if (testdef->type == ccunitTypeCase)
+       {
+         CCUnitListIterator fitor;
+         CCUnitTestFixtureDef* fixture;
+         CCUnitTestCaseDef* testCase = (CCUnitTestCaseDef*)testdef;
+         fprintf (ofp,
+                  "  {\n"
+                  "    CCUnitTestCase* testCase;\n"
+                  "    testCase = ccunit_newTestCase (\"%s\",\n"
+                  "                                   \"%s\",\n"
+                  "                                   %s,\n"
+                  "                                   %s);\n"
+                  "    if (testCase != NULL)\n" 
+                  "      ccunit_addTestCase (suite, testCase);\n",
+                  testdef->name,
+                  testCase->desc,
+                  !testCase->setUp ? "NULL" : testCase->setUp->name,
+                  !testCase->tearDown ? "NULL" : testCase->tearDown->name);
+         ccunit_initListIterator (&testCase->fixtures, &fitor);
+         while ((fixture = ccunit_nextListIterator (&fitor)) != NULL)
+           {
+             fprintf (ofp,
+                      "    {\n"
+                      "      CCUnitTestFixture* newFixture;\n"
+                      "      newFixture = ccunit_newTestFixture (\"%s\",\n"
+                      "                                          \"%s\",\n"
+                      "                                          %s);\n"
+                      "      if (newFixture != NULL)\n"
+                      "        ccunit_addTestFixture (testCase, newFixture);\n"
+                      "    }\n",
+                      fixture->name, fixture->desc, fixture->name);
+           }
+         fprintf (ofp, "  }\n");
+       }
+    }
+  fprintf (ofp, "\n  return suite;\n}\n\n");
+  ccunit_initListIterator (&suite->testdefs, &itor);
+  while ((testdef = (CCUnitTestDef*)ccunit_nextListIterator (&itor)) != NULL)
+    {
+      if (testdef->type == ccunitTypeSuite)
+       printSuite (ofp, testdef->name, (CCUnitTestSuiteDef*)testdef);
+      else
+       ;
+    }
+}
+
+/*
+ * print test suite.
+ */
+void ccunit_writeSuite (FILE* ofp, const char* name, CCUnitTestSuiteDef* suite)
+{
+  fprintf (ofp,
+          "#include <ccunit/CCUnit.h>\n"
+          "#include <stdio.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"
+          "  return newSuite_001 (name);\n"
+          "}\n",
+          name);
+}