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.
26 * TestFixture module implementation.
29 #include <ccunit/CCUnitTestFixture.h>
30 #include <ccunit/CCUnitTestResult.h>
35 * run test fixtures exception.
37 jmp_buf _ccunit_run_env;
40 * run tests exception.
42 jmp_buf _ccunit_runTest_env;
44 extern void _ccunit_runTestCase (CCUnitTestResult* result,
45 CCUnitTestFixture* fixture);
47 inline void ccunit_addTestCase (CCUnitTestFixture* fixture,
48 CCUnitTestCase* testCase)
50 ccunit_addList (&fixture->testCases, testCase);
53 CCUnitTestCase* ccunit_addNewTestCase (CCUnitTestFixture* fixture,
58 CCUnitTestCase* c = ccunit_newTestCase (name, desc, runTest);
61 ccunit_addTestCase (fixture, c);
66 * Runs the test fixture and collects the results in CCUnitTestResult.
67 * @param test A test to run.
68 * @param result A result container.
70 static void run (CCUnitTest* test, CCUnitTestResult* result)
72 assert (test->type == ccunitTypeFixture);
73 _ccunit_runTestCase (result, (CCUnitTestFixture*)test);
77 * Destruct test fixture.
78 * @param test destruct test.
80 static void destroy (CCUnitTest* test)
82 CCUnitTestFixture* fixture;
83 assert (test->type == ccunitTypeFixture);
84 fixture = (CCUnitTestFixture*)test;
85 safe_free ((char*)fixture->name);
86 ccunit_deleteList (&fixture->testCases,
87 (void (*)(void*))ccunit_deleteTestCase);
90 CCUnitTestFixture* ccunit_newTestFixture (const char* name,
96 CCUnitTestFixture* fixture = calloc (1, sizeof (*fixture));
97 ccunit_initTest (&fixture->test, ccunitTypeFixture, run, destroy);
98 fixture->name = safe_strdup (name);
99 ccunit_initList (&fixture->testCases);
100 fixture->setUp = setUp;
101 fixture->tearDown = tearDown;
102 fixture->ctor = ctor;
103 fixture->dtor = dtor;
107 inline struct CCUnitTestResult* ccunit_runTestFixture (CCUnitTestFixture* f)
109 CCUnitTestResult* result;
110 result = ccunit_newTestResult ();
111 f->test.run (&f->test, result);
116 * Runs the bare test sequence.
118 * @param testFixture test fixture to run.
119 * @param runTest test run function.
120 * @param result test result.
121 * @throws _ccunit_runResult_env by longjmp.
123 void _ccunit_runBare (CCUnitTestFixture* fixture,
125 CCUnitTestResult* result)
129 assert (fixture->test.type == ccunitTypeFixture);
132 if ((failure = setjmp (_ccunit_runTest_env)) == 0)
135 ccunit_addFailure (result, (CCUnitTestFailure*)failure);
139 if ((runFailure = setjmp (_ccunit_runTest_env)) == 0)
144 if (fixture->tearDown)
146 if ((failure = setjmp (_ccunit_runTest_env)) == 0)
147 fixture->tearDown ();
149 ccunit_addFailure (result, (CCUnitTestFailure*)failure);
152 longjmp (_ccunit_run_env, runFailure);