-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.cpp
More file actions
84 lines (74 loc) · 1.96 KB
/
demo.cpp
File metadata and controls
84 lines (74 loc) · 1.96 KB
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
#include "miniunit.h" // TEST, SKIP, TestSuite, TestCase
#include <cstdio> // printf
using namespace miniunit;
// Test PASS example
TEST(TEST1, "Test description") {
return true;
}
// Test FAIL example
TEST(TEST2, "This test should fail") {
return false;
}
// Custom test case
class MyTestCase : public TestSuite::TestCase {
public:
MyTestCase(const char* n, const char* d)
: TestSuite::TestCase(n, d) {
}
~MyTestCase() {
}
bool test() {
return true;
};
} MyTestCase_instance("TEST3", "Custom test case");
// Exception example
TEST(TEST4, "Throw exception") {
throw "Oh my god!";
return true;
}
// Custom test run function
class MyTestSuite : public TestSuite {
public:
static bool run() {
bool allResults = true;
printf("| %-8s | %-30s | %-6s | %-20s |\n",
"Test", "Description", "Result", "Comment");
printf("| %-8s | %-30s | %-6s | %-20s |\n",
std::string(8, '=').c_str(),
std::string(30, '=').c_str(),
std::string(6, '=').c_str(),
std::string(20, '=').c_str());
for (auto t : getTests()) {
printf("| %-8s | %-30s |", t->name.c_str(), t->description.c_str());
const char* resultStr = "OK";
const char* reasonStr = "";
if (!t->skip) {
bool result = false;
try {
result = t->test();
} catch (...) {
reasonStr = "Exception!";
}
if (!result) resultStr = "Fail!";
allResults = allResults && result;
} else {
resultStr = "Skip";
reasonStr = t->reason.c_str();
}
printf(" %-6s | %-20s |\n", resultStr, reasonStr);
}
return allResults;
};
};
// Test run example
int main(void) {
SKIP(TEST1, "testing skip");
printf("Default test run function:\n");
TestSuite::run();
printf("\n");
printf("Custom test run function:\n");
bool result = MyTestSuite::run();
printf("\n");
printf("Result: %s\n", (result ? "PASS" : "FAIL"));
return (result ? 0 : 1);
}