blob: 934da910195e8713a683b58428f7c2657a79ddb3 (
plain)
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
|
#include "../base/SRC_FIRST.hpp"
#include "testregister.hpp"
#include "testing.hpp"
#include "../base/logging.hpp"
#include "../std/algorithm.hpp"
#include "../std/iostream.hpp"
#include "../std/string.hpp"
#include "../std/vector.hpp"
static bool g_bLastTestOK = true;
int main()
{
my::g_LogLevel = LINFO;
vector<string> testNames;
vector<bool> testResults;
int numFailedTests = 0;
for (TestRegister * pTest = TestRegister::FirstRegister(); pTest; pTest = pTest->m_pNext)
{
string fileName(pTest->m_FileName);
string testName(pTest->m_TestName);
// Retrieve fine file name
{
int iFirstSlash = static_cast<int>(fileName.size()) - 1;
while (iFirstSlash >= 0 && fileName[iFirstSlash] != '\\' && fileName[iFirstSlash] != '/')
--iFirstSlash;
if (iFirstSlash >= 0)
fileName.erase(0, iFirstSlash + 1);
}
testNames.push_back(fileName + "::" + testName);
testResults.push_back(true);
}
int iTest = 0;
for (TestRegister * pTest = TestRegister::FirstRegister(); pTest; ++iTest, pTest = pTest->m_pNext)
{
cerr << "Running " << testNames[iTest] << endl << flush;
if (!g_bLastTestOK)
{
// Somewhere else global variables have been reset.
cerr << "\n\nSOMETHING IS REALLY WRONG IN THE UNIT TEST FRAMEWORK!!!" << endl;
return 5;
}
try
{
// Run the test.
pTest->m_Fn();
if (g_bLastTestOK)
{
cerr << "OK" << endl;
}
else
{
// You can set Break here if test failed,
// but it is already set in OnTestFail - to fail immediately.
testResults[iTest] = false;
++numFailedTests;
}
} catch (TestFailureException const & ex)
{
testResults[iTest] = false;
++numFailedTests;
} catch (std::exception const & ex)
{
cerr << "FAILED" << endl << "<<<Exception thrown [" << ex.what() << "].>>>" << endl;
testResults[iTest] = false;
++numFailedTests;
} catch (...)
{
cerr << "FAILED" << endl << "<<<Unknown exception thrown.>>>" << endl;
testResults[iTest] = false;
++numFailedTests;
}
g_bLastTestOK = true;
}
if (numFailedTests == 0)
{
cerr << endl << "All tests passed." << endl << flush;
return 0;
}
else
{
cerr << endl << numFailedTests << " tests failed:" << endl;
for (size_t i = 0; i < testNames.size(); ++i)
{
if (!testResults[i])
cerr << testNames[i] << endl;
}
cerr << "Some tests FAILED." << endl << flush;
return 1;
}
}
|