OSDN Git Service

Modified the example the test fails.
[ccunit/ccunit.git] / src / ccunit / CCUnitTestFixture.c
1 /* Copyright (C) 2003, 2010 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 /*
21  * $Id$
22  */
23
24 /**
25  * @file
26  * TestFixture module implementation.
27  */
28
29 #include <ccunit/CCUnitTestFixture.h>
30 #include <ccunit/CCUnitTestResult.h>
31 #include <assert.h>
32 #include <setjmp.h>
33
34 /**
35  * run tests exception environment.
36  */
37 jmp_buf _ccunit_runTest_env;
38
39 /**
40  * run tests exception.
41  */
42 CCUnitTestFailure* _ccunit_testFailure;
43
44 extern void _ccunit_startTest (CCUnitTestResult* result,
45                                struct CCUnitTestCase* test);
46 extern void _ccunit_endTest (CCUnitTestResult* result,
47                              struct CCUnitTestCase* test);
48
49 inline void ccunit_addTestCase (CCUnitTestFixture* fixture,
50                                 CCUnitTestCase* testCase)
51 {
52   ccunit_addList (&fixture->testCases, testCase);
53 }
54
55 CCUnitTestCase* ccunit_addNewTestCase (CCUnitTestFixture* fixture,
56                                        const char* name,
57                                        const char* desc,
58                                        void (*runTest)())
59 {
60   CCUnitTestCase* c;
61   if (!fixture)
62     return NULL;
63   c = ccunit_newTestCase (name, desc, runTest);
64   if (!c)
65     return NULL;
66   ccunit_addTestCase (fixture, c);
67   return c;
68 }
69
70 /**
71  * Runs the bare test sequence.
72  *
73  * @return failure
74  */
75 static CCUnitTestFailure* runTest (CCUnitTestCase* testCase,
76                                    CCUnitTestFunc* setUp,
77                                    CCUnitTestFunc* tearDown,
78                                    CCUnitTestResult* result)
79 {
80   CCUnitTestFailure* runFailure = NULL;
81   _ccunit_testFailure = NULL;
82   if (setUp && setUp->runTest)
83     {
84       if (setjmp (_ccunit_runTest_env) == 0)
85         setUp->runTest ();
86       else
87         {
88           _ccunit_testFailure->testCase = setUp;
89           ccunit_addFailure (result, _ccunit_testFailure);
90         }
91     }
92   if (_ccunit_testFailure)
93     ;
94   else if (setjmp (_ccunit_runTest_env) == 0)
95     {
96       result->runCount ++;
97       testCase->runTest ();
98     }
99   else
100     {
101       runFailure = _ccunit_testFailure;
102       runFailure->testCase = testCase;
103       _ccunit_testFailure = NULL;
104     }
105   if (tearDown && tearDown->runTest)
106     {
107       if (setjmp (_ccunit_runTest_env) == 0)
108         tearDown->runTest ();
109       else
110         {
111           _ccunit_testFailure->testCase = tearDown;
112           ccunit_addFailure (result, _ccunit_testFailure);
113         }
114     }
115   return runFailure;
116 }
117
118 /**
119  * Runs the test fixture and collects the results in CCUnitTestResult.
120  * @param test A test to run.
121  * @param result A result container.
122  */
123 static void run (CCUnitTest* test, CCUnitTestResult* result)
124 {
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)
131     {
132       if (setjmp (_ccunit_runTest_env) == 0)
133         fixture->case_setUp->runTest ();
134       else
135         {
136           _ccunit_testFailure->testCase = fixture->case_setUp;
137           ccunit_addFailure (result, _ccunit_testFailure);
138         }
139     }
140   if (!_ccunit_testFailure)
141     {
142       ccunit_initListIterator (&fixture->testCases, &itor);
143       while ((testCase = ccunit_nextListIterator (&itor)) != NULL)
144         {
145           CCUnitTestFailure* failure;
146           _ccunit_startTest (result, testCase);
147           failure = runTest (testCase, fixture->setUp, fixture->tearDown, result);
148           if (failure)
149             ccunit_addFailure (result, failure);
150           _ccunit_endTest (result, testCase);
151         }
152     }
153   if (fixture->case_tearDown && fixture->case_tearDown->runTest)
154     {
155       if (setjmp (_ccunit_runTest_env) == 0)
156         fixture->case_tearDown->runTest ();
157       else
158         {
159           _ccunit_testFailure->testCase = fixture->case_tearDown;
160           ccunit_addFailure (result, _ccunit_testFailure);
161         }
162     }
163 }
164
165 /**
166  * Destruct test fixture.
167  * @param test destruct test.
168  */
169 static void destroy (CCUnitTest* test)
170 {
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);
179 }
180
181 CCUnitTestFixture* ccunit_newTestFixture (const char* name,
182                                           CCUnitTestFunc* setUp,
183                                           CCUnitTestFunc* tearDown)
184 {
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;
191   return fixture;
192 }
193
194 inline void ccunit_setTestFixtureSetup (CCUnitTestFixture* fixture,
195                                         CCUnitTestFunc* case_setUp,
196                                         CCUnitTestFunc* case_tearDown)
197 {
198   if (!fixture)
199     return;
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;
206 }
207
208 inline struct CCUnitTestResult* ccunit_runTestFixture (CCUnitTestFixture* f)
209 {
210   CCUnitTestResult* result;
211   if (!f)
212     return NULL;
213   result = ccunit_newTestResult ();
214   if (!result)
215     return NULL;
216   f->test.run (&f->test, result);
217   return result;
218 }