-
Notifications
You must be signed in to change notification settings - Fork 1
/
Okra.h
254 lines (218 loc) · 6.96 KB
/
Okra.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
#pragma once
#include <chrono>
#include <functional>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
namespace okra
{
class IListener;
struct TestInfo;
namespace internals
{
std::chrono::high_resolution_clock::duration
duration_to_execute(const std::function<void()> &operation);
class Listeners
{
std::vector<std::shared_ptr<IListener>> listeners;
public:
Listeners() = default;
Listeners(std::initializer_list<std::shared_ptr<IListener>> l) { listeners = l; }
void Register(std::shared_ptr<IListener> &&listener) { listeners.push_back(move(listener)); }
void SendOnStart(const TestInfo &testInfo) const;
void SendOnEnd(const TestInfo &testInfo,
std::chrono::high_resolution_clock::duration duration) const;
void SendOnFail(const std::string &message) const;
} allListeners;
class AssertionFailedException
{
public:
const std::string message;
AssertionFailedException(const std::string &message)
: message(message)
{
}
};
} // namespace internals
class IListener
{
public:
virtual void OnStart(const TestInfo &testInfo) = 0;
virtual void OnEnd(const TestInfo &testInfo, std::chrono::high_resolution_clock::duration duration) = 0;
virtual void OnFail(const std::string &message) = 0;
};
struct TestInfo
{
const std::string name;
const std::function<void()> body;
bool Run(const internals::Listeners &listeners) const;
};
bool TestInfo::Run(const internals::Listeners &listeners) const
{
listeners.SendOnStart(*this);
bool pass = false;
std::chrono::high_resolution_clock::duration execution_duration;
execution_duration = internals::duration_to_execute([&]() {
try
{
body();
pass = true;
}
catch (const internals::AssertionFailedException &exception)
{
listeners.SendOnFail(exception.message);
}
catch (...)
{
pass = false;
}
});
listeners.SendOnEnd(*this, execution_duration);
return pass;
}
template <class T>
void RegisterListener()
{
internals::allListeners.Register(std::make_shared<T>());
}
namespace internals
{
void Fail(const std::string &message)
{
std::stringstream stringstream;
stringstream << ": "
<< " - assert FAILED - " << std::endl
<< message << std::endl;
throw AssertionFailedException(stringstream.str());
}
void AssertMessage(bool condition, const std::string &message)
{
if (!condition) { Fail(message); }
}
template <class T1, class T2>
void AssertEqual(const T1 &t1, const T2 &t2, const std::string &t1String, const std::string &t2String)
{
std::stringstream stringstream;
stringstream << "EXPECTED: " << t1 << "(" << t1String << ")" << std::endl;
stringstream << "ACTUAL : " << t2 << "(" << t2String << ")" << std::endl;
AssertMessage(t1 == t2, stringstream.str());
}
template <class TException>
void AssertThrows(const std::function<void()> action, const std::string &exceptionTypeString)
{
bool didThrow;
try
{
action();
didThrow = false;
}
catch (TException &)
{
didThrow = true;
}
AssertMessage(didThrow, "did not throw ");
}
std::chrono::high_resolution_clock::duration duration_to_execute(const std::function<void()> &operation)
{
auto begin = std::chrono::high_resolution_clock::now();
operation();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - begin);
return duration;
}
class OStreamListener : public IListener
{
std::ostream &ostream;
public:
OStreamListener(std::ostream &ostream)
: ostream(ostream)
{
}
void OnStart(const TestInfo &testInfo) override { ostream << testInfo.name; }
void OnEnd(const TestInfo &testInfo,
std::chrono::high_resolution_clock::duration execution_duration) override
{
using namespace std::chrono;
ostream << " (";
ostream << duration_cast<milliseconds>(execution_duration).count() << "ms";
ostream << ")";
ostream << std::endl;
}
void OnFail(const std::string &message) override { ostream << message; }
};
class ConsoleListener : public OStreamListener
{
public:
ConsoleListener()
: OStreamListener(std::cout)
{
}
};
class Tests
{
std::vector<TestInfo> tests;
public:
void Add(TestInfo testInfo) { tests.push_back(testInfo); }
bool RunAll(const Listeners &listeners) const
{
if (tests.empty()) { return false; }
bool pass = true;
for (const auto &testInfo : tests) { pass &= testInfo.Run(listeners); }
return pass;
}
};
void Listeners::SendOnStart(const TestInfo &testInfo) const
{
for (const auto &listener : listeners) { listener->OnStart(testInfo); }
}
void Listeners::SendOnEnd(const TestInfo &testInfo,
std::chrono::high_resolution_clock::duration duration) const
{
for (const auto &listener : listeners) { listener->OnEnd(testInfo, duration); }
}
void Listeners::SendOnFail(const std::string &message) const
{
for (const auto &listener : listeners) { listener->OnFail(message); }
}
Tests allTests;
bool RunAllTests() { return allTests.RunAll(okra::internals::allListeners); }
} // namespace internals
} // namespace okra
#define OKRA_REGISTER_LISTENER(name) OKRA_REGISTER_LISTENER_(name, __COUNTER__)
#define OKRA_REGISTER_LISTENER_(name, counter) OKRA_REGISTER_LISTENER__(name, counter)
#define OKRA_REGISTER_LISTENER__(name, counter) \
OKRA_REGISTER_LISTENER___(name, LISTENER_##counter, ListenerInitializer##counter)
#define OKRA_REGISTER_LISTENER___(name, bodyName, initializerName) \
struct initializerName \
{ \
initializerName() { okra::RegisterListener<name>(); } \
} initializerName##Instance
#define OKRA_TEST(name) OKRA_TEST_(name, __COUNTER__)
#define OKRA_TEST_(name, counter) OKRA_TEST__(name, counter)
#define OKRA_TEST__(name, counter) OKRA_TEST___(name, TEST_##counter, TestInitializer##counter)
#define OKRA_TEST___(name, bodyName, initializerName) \
void bodyName(); \
struct initializerName \
{ \
initializerName() { okra::internals::allTests.Add({name, bodyName}); } \
} initializerName##Instance; \
void bodyName()
#define OKRA_ASSERT_MESSAGE(condition, message) okra::internals::AssertMessage((condition), message)
#define OKRA_ASSERT(condition) OKRA_ASSERT_MESSAGE((condition), #condition)
#define OKRA_ASSERT_EQUAL(t1, t2) okra::internals::AssertEqual((t1), (t2), #t1, #t2)
#define OKRA_FAIL(message) okra::internals::Fail((message))
#define OKRA_ASSERT_THROWS(exception_type, action) \
okra::internals::AssertThrows<exception_type>(action, #exception_type)
#ifndef OKRA_DO_NOT_DEFINE_SHORT_NAMES
#define ASSERT_MESSAGE OKRA_ASSERT_MESSAGE
#define ASSERT_EQUAL OKRA_ASSERT_EQUAL
#define ASSERT OKRA_ASSERT
#define FAIL OKRA_FAIL
#define TEST OKRA_TEST
#define ASSERT_THROWS OKRA_ASSERT_THROWS
#define REGISTER_LISTENER OKRA_REGISTER_LISTENER
#endif
OKRA_REGISTER_LISTENER(okra::internals::ConsoleListener);
int main(int argc, char **argv) { return okra::internals::RunAllTests() ? 0 : 1; }