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
|
#include <stdlib.h>
#if WINDOWS
#include <io.h>
#endif
#include "unit/TestRunner.h"
void addTestToTestSuite(TestSuite testSuite, TestCase testCase)
{
linkedListAppend(testSuite->testCases, testCase);
}
const LogColor getLogColor(TestLogEventType eventType)
{
switch (eventType) {
case kTestLogEventSection:
return isatty(1) ? COLOR_FG_CYAN : COLOR_NONE;
case kTestLogEventPass:
return isatty(1) ? COLOR_FG_GREEN : COLOR_NONE;
case kTestLogEventFail:
return isatty(1) ? COLOR_BG_MAROON : COLOR_NONE;
case kTestLogEventSkip:
return isatty(1) ? COLOR_FG_YELLOW : COLOR_NONE;
case kTestLogEventReset:
return isatty(1) ? COLOR_RESET : COLOR_NONE;
default:
return COLOR_NONE;
}
}
void printTestSuccess(void)
{
printToLog(getLogColor(kTestLogEventPass), NULL, "OK");
flushLog(NULL);
}
void printTestFail(void)
{
printToLog(getLogColor(kTestLogEventFail), NULL, "FAIL");
flushLog(NULL);
}
static void _printTestSkipped(void)
{
printToLog(getLogColor(kTestLogEventSkip), NULL, "Skipped");
flushLog(NULL);
}
void printTestName(const char *testName)
{
fprintf(stderr, " %s: ", testName);
// Flush standard output in case the test crashes. That way at least the
// crashing test name is seen.
fflush(stderr);
}
void runTestCase(void *item, void *extraData)
{
TestCase testCase = (TestCase)item;
TestSuite testSuite = (TestSuite)extraData;
int result;
if (!testSuite->onlyPrintFailing) {
printTestName(testCase->name);
}
if (testCase->testCaseFunc != NULL) {
if (testSuite->setup != NULL) {
testSuite->setup();
}
result = testCase->testCaseFunc();
if (result == 0) {
if (!testSuite->onlyPrintFailing) {
printTestSuccess();
}
testSuite->numSuccess++;
} else {
printTestFail();
testSuite->numFail++;
}
if (testSuite->teardown != NULL) {
testSuite->teardown();
}
} else {
if (!testSuite->onlyPrintFailing) {
_printTestSkipped();
}
testSuite->numSkips++;
}
}
void runTestSuite(void *testSuitePtr, void *extraData)
{
TestSuite testSuite = (TestSuite)testSuitePtr;
printToLog(getLogColor(kTestLogEventReset), NULL, "Running tests in ");
printToLog(getLogColor(kTestLogEventSection), NULL, testSuite->name);
flushLog(NULL);
linkedListForeach(testSuite->testCases, runTestCase, testSuite);
}
// In both the TestSuite and TestCase objects we assume that we do not need ownership of
// the strings passed in, since they should be allocated on the heap and live for the
// lifetime of the program.
TestSuite newTestSuite(char *name, TestCaseSetupFunc setup, TestCaseTeardownFunc teardown)
{
TestSuite testSuite = (TestSuite)malloc(sizeof(TestSuiteMembers));
testSuite->name = name;
testSuite->numSuccess = 0;
testSuite->numFail = 0;
testSuite->numSkips = 0;
testSuite->testCases = newLinkedList();
testSuite->setup = setup;
testSuite->teardown = teardown;
testSuite->onlyPrintFailing = false;
testSuite->keepFiles = false;
return testSuite;
}
TestCase newTestCase(char *name, char *filename, int lineNumber, TestCaseExecFunc testCaseFunc)
{
TestCase testCase = (TestCase)malloc(sizeof(TestCaseMembers));
testCase->name = name;
testCase->filename = filename;
testCase->lineNumber = lineNumber;
testCase->testCaseFunc = testCaseFunc;
return testCase;
}
void freeTestCase(TestCase self)
{
free(self);
}
void freeTestSuite(TestSuite self)
{
if (self != NULL) {
freeLinkedListAndItems(self->testCases, (LinkedListFreeItemFunc)freeTestCase);
free(self);
}
}
|