forked from Arduino-CI/arduino_ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoUnitTests.h
298 lines (255 loc) · 7.91 KB
/
ArduinoUnitTests.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#pragma once
#include "OstreamHelpers.h"
#include "Assertion.h"
#include <iostream>
using namespace std;
struct Results {
int passed;
int failed;
int skipped; // TODO: not sure about this
};
struct TestData {
const char* name;
int result;
};
class TestSetup
{
public:
TestSetup() {
sInstance = this;
}
virtual void run() {}
static TestSetup *sInstance;
};
class TestTeardown
{
public:
TestTeardown() {
sInstance = this;
}
virtual void run() {}
static TestTeardown *sInstance;
};
class Test
{
public:
class ReporterTAP {
private:
int mTestCounter;
int mAssertCounter;
public:
ReporterTAP() {}
~ReporterTAP() {}
void onTestRunInit(int numTests) {
cout << "TAP version 13" << endl << flush;
cout << 1 << ".." << numTests << endl << flush; // we know how many tests, in advance
mTestCounter = 0;
}
void onTestStart(TestData td) {
mAssertCounter = 0;
++mTestCounter;
cout << "# Subtest: " << td.name << endl << flush;
}
void onTestEnd(TestData td) {
cout << " 1.." << mAssertCounter << endl << flush;
cout << (td.result == RESULT_PASS ? "ok " : "not ok ") << mTestCounter << " - " << td.name << endl << flush;
}
// non-comparative assert
void onAssert(
const char* file,
int line,
const char* description,
bool pass
) {
if (pass) {
cout << " ok " << ++mAssertCounter << " - " << description << endl << flush;
} else {
cout << " not ok " << ++mAssertCounter << " - " << description << endl << flush;
cerr << " ---" << endl;
cerr << " at:" << endl;
cerr << " file: " << file << endl;
cerr << " line: " << line << endl;
cerr << " ..." << endl;
}
}
template <typename A, typename B> void onAssert(
const char* file,
int line,
const char* description,
bool pass,
const char* lhsRelevance,
const char* lhsLabel,
const A &lhs,
const char* opLabel,
const char* rhsRelevance,
const char* rhsLabel,
const B &rhs
) {
if (pass) {
cout << " ok " << ++mAssertCounter << " - ";
cout << description << " " << lhsLabel << " " << opLabel << " " << rhsLabel << endl << flush;
} else {
cout << " not ok " << ++mAssertCounter << " - ";
cout << description << " " << lhsLabel << " " << opLabel << " " << rhsLabel << endl << flush;
cerr << " ---" << endl;
cerr << " operator: " << opLabel << endl;
cerr << " " << lhsRelevance << ": " << lhs << endl;
cerr << " " << rhsRelevance << ": " << rhs << endl;
cerr << " at:" << endl;
cerr << " file: " << file << endl;
cerr << " line: " << line << endl;
cerr << " ..." << endl;
}
}
};
private:
ReporterTAP* mReporter;
const char* mName;
// linked list structure for active tests
static Test* sRoot;
Test* mNext;
void append() {
if (!sRoot) return (void)(sRoot = this);
Test* p;
for (p = sRoot; p->mNext; p = p->mNext);
p->mNext = this;
}
void excise() {
for (Test **p = &sRoot; *p != 0; p=&((*p)->mNext)) {
if (*p == this) return (void)(*p = (*p)->mNext);
}
}
static int numTests() {
if (!sRoot) return 0;
int i = 1;
for (Test* p = sRoot; p->mNext; ++i && (p = p->mNext));
return i;
}
// current test result
int mResult;
public:
static const int RESULT_NONE = 0;
static const int RESULT_PASS = 1;
static const int RESULT_FAIL = 2;
static const int RESULT_SKIP = 3;
inline const char *name() const { return mName; }
inline int result() const { return mResult; }
Test(const char* _name) : mName(_name) {
mResult = RESULT_NONE;
mReporter = 0;
append();
}
inline void fail() { mResult = RESULT_FAIL; }
inline void skip() { mResult = RESULT_SKIP; }
static Results run(ReporterTAP* reporter) {
if (reporter) reporter->onTestRunInit(numTests());
Results results = {0, 0, 0};
for (Test *p = sRoot; p; p = p->mNext) {
p->prepare();
p->mReporter = reporter;
TestData t1 = {p->name(), p->result()};
if (reporter) reporter->onTestStart(t1);
if (TestSetup::sInstance) TestSetup::sInstance->run();
p->test();
if (TestTeardown::sInstance) TestTeardown::sInstance->run();
if (p->mResult == RESULT_PASS) ++results.passed;
if (p->mResult == RESULT_FAIL) ++results.failed;
if (p->mResult == RESULT_SKIP) ++results.skipped;
TestData t2 = {p->name(), p->result()};
if (reporter) reporter->onTestEnd(t2);
}
return results;
}
// TODO: figure out TAP output like
// https://api.travis-ci.org/v3/job/283745834/log.txt
// https://testanything.org/tap-specification.html
// parse input and decide how to report
static int run_and_report(int argc, char *argv[]) {
// TODO: pick a reporter based on args
ReporterTAP rep;
Results results = run(&rep);
return results.failed + results.skipped;
}
void prepare() {
mResult = RESULT_PASS; // not None, and not fail unless we hear otherwise
}
void test() {
// thin wrapper. nothing to do here for now
task();
}
virtual void task() = 0;
virtual ~Test() {
excise();
}
bool assertion(
const char *file,
int line,
const char *description,
bool ok)
{
if (mReporter) {
mReporter->onAssert(file, line, description, ok);
}
if (!ok)
fail();
return ok;
}
template <typename A, typename B>
bool assertion(
const char *file,
int line,
const char *description,
const char *lhsRelevance,
const char *lhsLabel,
const A &lhs,
const char *ops,
bool (*op)(
const A &lhs,
const B &rhs),
const char *rhsRelevance,
const char *rhsLabel,
const B &rhs)
{
bool ok = op(lhs, rhs);
if (mReporter) {
mReporter->onAssert(file, line, description, ok,
lhsRelevance, lhsLabel, lhs, ops, rhsRelevance, rhsLabel, rhs);
}
if (!ok)
fail();
return ok;
}
};
/**
* Extend the class into a struct.
* The implementation of task() will follow the macro
*
*/
#define unittest(name) \
struct test_##name : Test \
{ \
test_##name() : Test(#name){}; \
void task(); \
} test_##name##_instance; \
void test_##name ::task()
/**
* The unittest_setup and unittest_teardown functions are intended to be used
* to set up "external" dependencies that needs to present but are not directly
* related to the functionality that you are testing, for instance a LCD.
*/
#define unittest_setup() \
class unittest_setup_class : public TestSetup { \
public: \
virtual void run() override; \
} unittest_setup_instance; \
void unittest_setup_class::run()
#define unittest_teardown() \
class unittest_teardown_class : public TestTeardown { \
public: \
virtual void run() override; \
} unittest_teardown_instance; \
void unittest_teardown_class::run()
#define unittest_main() \
int main(int argc, char *argv[]) { \
return Test::run_and_report(argc, argv); \
}