Skip to content

Commit bc0e16d

Browse files
committed
Change implementation to distinct classes
1 parent ec2438e commit bc0e16d

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

cpp/unittest/ArduinoUnitTests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "ArduinoUnitTests.h"
22

3-
Test* Test::sSetup = 0;
4-
Test* Test::sTeardown = 0;
3+
TestSetup *TestSetup::sInstance;
4+
TestTeardown *TestTeardown::sInstance;
5+
56
Test* Test::sRoot = 0;

cpp/unittest/ArduinoUnitTests.h

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@ struct TestData {
1515
int result;
1616
};
1717

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+
1838
class Test
1939
{
2040
public:
@@ -81,8 +101,6 @@ class Test
81101
const char* mName;
82102

83103
// linked list structure for active tests
84-
static Test* sSetup;
85-
static Test* sTeardown;
86104
static Test* sRoot;
87105
Test* mNext;
88106

@@ -121,13 +139,7 @@ class Test
121139
Test(const char* _name) : mName(_name) {
122140
mResult = RESULT_NONE;
123141
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();
131143
}
132144

133145
inline void fail() { mResult = RESULT_FAIL; }
@@ -160,9 +172,9 @@ class Test
160172
static int run_and_report(int argc, char *argv[]) {
161173
// TODO: pick a reporter based on args
162174
ReporterTAP rep;
163-
if (sSetup) sSetup->task();
175+
if (TestSetup::sInstance) TestSetup::sInstance->run();
164176
Results results = run(&rep);
165-
if (sTeardown) sTeardown->task();
177+
if (TestTeardown::sInstance) TestTeardown::sInstance->run();
166178
return results.failed + results.skipped;
167179
}
168180

@@ -233,25 +245,18 @@ class Test
233245
* related to the functionality that you are testing, for instance a LCD.
234246
*/
235247
#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()
255260

256261
#define unittest_main() \
257262
int main(int argc, char *argv[]) { \

0 commit comments

Comments
 (0)