-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestingFramework.cpp
158 lines (131 loc) · 4.07 KB
/
TestingFramework.cpp
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
#include "TestingFramework.h"
#include <utility>
#include <string>
TestRegistry& TestRegistry::Instance()
{
static TestRegistry registry;
return registry;
}
void TestRegistry::AddTest(ITest* test)
{
Tests_.emplace_back(test);
}
void TestRegistry::RunAllTests() const
{
const size_t total = Tests_.size();
int passed = 0;
for (const auto& test : Tests_)
{
const std::string testName = test->GetName();
OutputTestPreRun(testName);
test->Run();
OutputTestPostRun(test->Passed(), testName);
if (test->Passed())
{
passed++;
}
}
OutputTestResults(total, passed);
}
TestRegistry::TestRegistry() = default;
const char* TestRegistry::PluralEnding(const size_t total) const
{
return total != 1 ? "s" : "";
}
void TestRegistry::OutputTestResults(const size_t total, const int passed) const
{
const int failed = total - passed;
std::cout << std::endl << "[==========] " << total << " test" << PluralEnding(total) << " ran." << std::endl;
std::cout << "[ PASSED ] " << passed << " test" << PluralEnding(passed) << "." << std::endl;
std::cout << "[ FAILED ] " << failed << " test" << PluralEnding(failed) << "." << std::endl;
}
void TestRegistry::OutputTestPreRun(const std::string& testName) const
{
std::cout << "[ RUN ] " << testName << std::endl;
}
void TestRegistry::OutputTestPostRun(const bool bPassed, const std::string& testName) const
{
std::cout << (bPassed ? "[ PASSED ] " : "[ FAILED ] ") << testName << std::endl;
}
TestBase::TestBase(std::string name): Name(std::move(name))
{
TestRegistry::Instance().AddTest(this);
}
std::string TestBase::GetName()
{
return Name;
}
bool TestBase::Passed()
{
return FailureCount == 0;
}
void TestBase::SetShouldLog(const bool bShouldLog)
{
this->bShouldLog = bShouldLog;
}
void TestBase::ExpectTrue(const bool condition, const std::string& expression, const char* file, int line)
{
if (!condition)
{
FailTest(expression, file, line);
}
}
void TestBase::ExpectFalse(const bool condition, const std::string& expression, const char* file, int line)
{
if (condition)
{
FailTest(expression, file, line);
}
}
void TestBase::ExpectStringEquals(const std::string& expected, const std::string& actual, const std::string& expression, const char* file, int line)
{
if (expected != actual)
{
OutputExceptionFailed(expression, file, line);
// TODO: Refactor.
if (bShouldLog)
{
std::cerr << "Expected: " << expected << " but got " << actual << std::endl;
}
FailureCount++;
}
}
void TestBase::Fail(const char* file, int line)
{
FailTest("Fail()", file, line);
}
void TestBase::OutputExceptionFailed(const std::string& expression, const char* file, int line) const
{
if (bShouldLog)
{
std::cerr << file << ":" << line << ": Failure: expected " << expression << std::endl;
}
}
void TestBase::FailTest(const std::string& failureMessage, const std::string& expression, const char* file, int line)
{
// TODO: Refactor - combine or do something with OutputExceptionFailed.
if(bShouldLog)
{
std::cerr << file << ":" << line << ": Failure: " << failureMessage << std::endl;
}
FailTest(expression, file, line);
}
void TestBase::FailTest(const std::string& expression, const char* file, int line)
{
OutputExceptionFailed(expression, file, line);
FailureCount++;
}
void TestBase::AssertTrue(bool condition, const std::string &expression, const char *file, int line) {
if (!condition)
{
std::string message = "Assertion failed: (" + expression + ") in file " + file + ", line " + std::to_string(line);
FailTest(message, expression, file, line);
}
}
void TestBase::AssertFalse(bool condition, const std::string &expression, const char *file, int line) {
if (condition)
{
std::string message = "Assertion failed: (" + expression + ") in file " + file + ", line " + std::to_string(line);
FailTest(message, expression, file, line);
}
}