1 /* Copyright (C) 2003, 2010 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 tests exception environment.
37 jmp_buf _ccunit_runTest_env;
40 * run tests exception.
42 CCUnitTestFailure* _ccunit_testFailure;
44 extern void _ccunit_startTest (CCUnitTestResult* result,
45 struct CCUnitTestCase* test);
46 extern void _ccunit_endTest (CCUnitTestResult* result,
47 struct CCUnitTestCase* test);
49 inline void ccunit_addTestCase (CCUnitTestFixture* fixture,
50 CCUnitTestCase* testCase)
52 ccunit_addList (&fixture->testCases, testCase);
55 CCUnitTestCase* ccunit_addNewTestCase (CCUnitTestFixture* fixture,
63 c = ccunit_newTestCase (name, desc, runTest);
66 ccunit_addTestCase (fixture, c);
71 * Runs the bare test sequence.
75 static CCUnitTestFailure* runTest (CCUnitTestCase* testCase,
76 CCUnitTestFunc* setUp,
77 CCUnitTestFunc* tearDown,
78 CCUnitTestResult* result)
80 CCUnitTestFailure* runFailure = NULL;
81 _ccunit_testFailure = NULL;
82 if (setUp && setUp->runTest)
84 if (setjmp (_ccunit_runTest_env) == 0)
88 _ccunit_testFailure->testCase = setUp;
89 ccunit_addFailure (result, _ccunit_testFailure);
92 if (_ccunit_testFailure)
94 else if (setjmp (_ccunit_runTest_env) == 0)
101 runFailure = _ccunit_testFailure;
102 runFailure->testCase = testCase;
103 _ccunit_testFailure = NULL;
105 if (tearDown && tearDown->runTest)
107 if (setjmp (_ccunit_runTest_env) == 0)
108 tearDown->runTest ();
111 _ccunit_testFailure->testCase = tearDown;
112 ccunit_addFailure (result, _ccunit_testFailure);
119 * Runs the test fixture and collects the results in CCUnitTestResult.
120 * @param test A test to run.
121 * @param result A result container.
123 static void run (CCUnitTest* test, CCUnitTestResult* result)
125 CCUnitTestFixture* fixture = (CCUnitTestFixture*)test;
126 CCUnitListIterator itor;
127 CCUnitTestCase* testCase;
128 assert (test->type == ccunitTypeFixture);
129 _ccunit_testFailure = NULL;
130 if (fixture->case_setUp && fixture->case_setUp->runTest)
132 if (setjmp (_ccunit_runTest_env) == 0)
133 fixture->case_setUp->runTest ();
136 _ccunit_testFailure->testCase = fixture->case_setUp;
137 ccunit_addFailure (result, _ccunit_testFailure);
140 if (!_ccunit_testFailure)
142 ccunit_initListIterator (&fixture->testCases, &itor);
143 while ((testCase = ccunit_nextListIterator (&itor)) != NULL)
145 CCUnitTestFailure* failure;
146 _ccunit_startTest (result, testCase);
147 failure = runTest (testCase, fixture->setUp, fixture->tearDown, result);
149 ccunit_addFailure (result, failure);
150 _ccunit_endTest (result, testCase);
153 if (fixture->case_tearDown && fixture->case_tearDown->runTest)
155 if (setjmp (_ccunit_runTest_env) == 0)
156 fixture->case_tearDown->runTest ();
159 _ccunit_testFailure->testCase = fixture->case_tearDown;
160 ccunit_addFailure (result, _ccunit_testFailure);
166 * Destruct test fixture.
167 * @param test destruct test.
169 static void destroy (CCUnitTest* test)
171 CCUnitTestFixture* fixture;
172 assert (test->type == ccunitTypeFixture);
173 fixture = (CCUnitTestFixture*)test;
174 safe_free (fixture->name);
175 ccunit_deleteTestFunc (fixture->setUp);
176 ccunit_deleteTestFunc (fixture->tearDown);
177 ccunit_deleteList (&fixture->testCases,
178 (void (*)(void*))ccunit_deleteTestCase);
181 CCUnitTestFixture* ccunit_newTestFixture (const char* name,
182 CCUnitTestFunc* setUp,
183 CCUnitTestFunc* tearDown)
185 CCUnitTestFixture* fixture = calloc (1, sizeof (*fixture));
186 ccunit_initTest (&fixture->test, ccunitTypeFixture, run, destroy);
187 fixture->name = safe_strdup (name);
188 ccunit_initList (&fixture->testCases);
189 fixture->setUp = setUp;
190 fixture->tearDown = tearDown;
194 inline void ccunit_setTestFixtureSetup (CCUnitTestFixture* fixture,
195 CCUnitTestFunc* case_setUp,
196 CCUnitTestFunc* case_tearDown)
200 if (fixture->case_setUp)
201 ccunit_deleteTestCase (fixture->case_setUp);
202 fixture->case_setUp = case_setUp;
203 if (fixture->case_tearDown)
204 ccunit_deleteTestCase (fixture->case_tearDown);
205 fixture->case_tearDown = case_tearDown;
208 inline struct CCUnitTestResult* ccunit_runTestFixture (CCUnitTestFixture* f)
210 CCUnitTestResult* result;
213 result = ccunit_newTestResult ();
216 f->test.run (&f->test, result);