@@ -15,6 +15,26 @@ struct TestData {
15
15
int result;
16
16
};
17
17
18
+ class TestSetup
19
+ {
20
+ public:
21
+ TestSetup () {
22
+ sInstance = this ;
23
+ }
24
+ virtual void run () {}
25
+ static TestSetup *sInstance ;
26
+ };
27
+
28
+ class TestTeardown
29
+ {
30
+ public:
31
+ TestTeardown () {
32
+ sInstance = this ;
33
+ }
34
+ virtual void run () {}
35
+ static TestTeardown *sInstance ;
36
+ };
37
+
18
38
class Test
19
39
{
20
40
public:
@@ -81,8 +101,6 @@ class Test
81
101
const char * mName ;
82
102
83
103
// linked list structure for active tests
84
- static Test* sSetup ;
85
- static Test* sTeardown ;
86
104
static Test* sRoot ;
87
105
Test* mNext ;
88
106
@@ -121,13 +139,7 @@ class Test
121
139
Test (const char * _name) : mName (_name) {
122
140
mResult = RESULT_NONE;
123
141
mReporter = 0 ;
124
- if (_name == " s e t u p" ) {
125
- sSetup = this ;
126
- } else if (_name == " t e a r d o w n" ) {
127
- sTeardown = this ;
128
- } else {
129
- append ();
130
- }
142
+ append ();
131
143
}
132
144
133
145
inline void fail () { mResult = RESULT_FAIL; }
@@ -160,9 +172,9 @@ class Test
160
172
static int run_and_report (int argc, char *argv[]) {
161
173
// TODO: pick a reporter based on args
162
174
ReporterTAP rep;
163
- if (sSetup ) sSetup -> task ();
175
+ if (TestSetup:: sInstance ) TestSetup:: sInstance -> run ();
164
176
Results results = run (&rep);
165
- if (sTeardown ) sTeardown -> task ();
177
+ if (TestTeardown:: sInstance ) TestTeardown:: sInstance -> run ();
166
178
return results.failed + results.skipped ;
167
179
}
168
180
@@ -233,25 +245,18 @@ class Test
233
245
* related to the functionality that you are testing, for instance a LCD.
234
246
*/
235
247
#define unittest_setup () \
236
- struct xtest_unittest_setup : Test \
237
- { \
238
- xtest_unittest_setup () : Test(" s e t u p" ){}; \
239
- void task (); \
240
- } xtest_unittest_setup_instance; \
241
- void xtest_unittest_setup::task ()
242
-
243
- #define unittest_teardown () \
244
- struct xtest_unittest_teardown : Test \
245
- { \
246
- xtest_unittest_teardown () : Test(" t e a r d o w n" ){}; \
247
- void task (); \
248
- } xtest_unittest_teardown_instance; \
249
- void xtest_unittest_teardown::task ()
250
-
251
- // To avoid potentionally breaking existing code by re-using the Test class for
252
- // setup and teardown, add a "x" prefix in front of names so that it will be
253
- // different from code generated by the unittest macro. Also using _name
254
- // arguments to Test that cannot be used in the unittest macro.
248
+ class unittest_setup_class : public TestSetup { \
249
+ public: \
250
+ virtual void run () override ; \
251
+ } unittest_setup_instance; \
252
+ void unittest_setup_class::run ()
253
+
254
+ #define unittest_teardown () \
255
+ class unittest_teardown_class : public TestTeardown { \
256
+ public: \
257
+ virtual void run () override ; \
258
+ } unittest_teardown_instance; \
259
+ void unittest_teardown_class::run ()
255
260
256
261
#define unittest_main () \
257
262
int main (int argc, char *argv[]) { \
0 commit comments