-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.hxx
330 lines (255 loc) · 7.96 KB
/
index.hxx
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#ifndef TAP_H
#define TAP_H
#include <string>
#include <vector>
#include <map>
#include <exception>
#include <iostream>
#include <regex>
#include <sstream>
#include "deps/datcxx/timers/index.hxx"
#include "deps/datcxx/cxx-eventemitter/index.hxx"
namespace TAP {
typedef std::string String;
typedef std::exception Ex;
typedef std::stringstream StringStream;
template<typename T>
String stringify(const T& t) {
StringStream ss;
String s;
ss << t;
ss >> s;
return s;
}
class Test : public EventEmitter {
bool fails = false;
int testsExpected = 0;
int testsActual = 0;
int testsSkip = 0;
int hasPlan = false;
int assertionsExpected = 0;
int assertionsMade = 0;
int assertionsPassed = 0;
int id = 0;
int ended = 0;
String name = "(unnamed test)";
Test* parent;
static bool finished;
static bool started;
static int idIndex;
static int assertionsPassing;
static int assertionsTotal;
bool asserts (bool value, const String& oper, const String& actual,
const String& expected, const String& message);
template<typename L, typename R, typename BinaryPredicate>
void assert_equal(const L&, const R&, const String&,
BinaryPredicate);
template<typename L, typename R, typename BinaryPredicate>
void assert_notEqual(const L&, const R&, const String&,
BinaryPredicate);
public:
template<typename Lambda>
void test(const String&, Lambda);
void summarize();
void bailout(const String&);
void end();
void plan(int);
void timeoutAfter(int);
void comment(const String&);
void pass(const String&);
void fail(const String&);
void skip(int number, const String& reason);
void ok();
void ok(bool);
void ok(bool, const String&);
template<typename Left, typename Right>
void equal(const Left& l, const Right& r);
template<typename Left, typename Right>
void equal(const Left& l, const Right& r, const String& msg);
template<typename Left, typename Right>
void notEqual(const Left& l, const Right& r);
template<typename Left, typename Right>
void notEqual(const Left& l, const Right& r, const String& msg);
~Test () {
if (this->ended == 0) {
this->end();
}
}
};
inline bool Test::finished = false;
inline bool Test::started = false;
inline int Test::idIndex = 0;
inline int Test::assertionsTotal = 0;
inline int Test::assertionsPassing = 0;
inline void failedFinalEnd () {
std::cout << "End of tests never reached." << std::endl;
exit(1);
}
template<typename Lambda>
inline void Test::test (const String& name, Lambda callback) {
std::shared_ptr<Test> t = std::make_shared<Test>();
if (!Test::started) {
Test::started = true;
std::cout << "TAP version 13";
atexit([]{
if (!Test::finished) failedFinalEnd();
});
}
t->name = name;
t->parent = this;
t->id = ++Test::idIndex;
this->comment(name);
callback(t);
if (t->ended == 0) {
this->fail("End never called.");
return;
}
if (t->ended > 1) {
this->fail("End called " + std::to_string(t->ended) + " times.");
return;
}
bool assertionsMatch = t->assertionsPassed != t->assertionsExpected;
bool assertionsFailed = t->assertionsMade - t->assertionsPassed;
if (t->hasPlan == true && assertionsMatch) {
this->fail("plan != count");
return;
}
if (assertionsFailed > 0) {
return;
}
}
inline void Test::end () {
this->ended++;
if (this->id == 0 && this->ended == 1) {
Test::finished = true;
this->summarize();
}
}
inline void Test::ok () {
this->ok(true, "");
}
inline void Test::ok (bool value) {
this->ok(value, "");
}
inline void Test::ok (bool value, const String& msg) {
this->asserts(value, "ok", "true", "false", msg);
}
inline void Test::pass (const String& msg) {
this->asserts(true, "pass", "", "", msg);
}
inline void Test::fail (const String& msg) {
this->asserts(false, "fail", "", "", msg);
}
inline void Test::skip (int number, const String& msg) {
this->testsSkip = number;
this->comment(msg);
}
inline void Test::bailout (const String& msg = "") {
std::cout << "Bail out! " << msg << std::endl;
}
inline void Test::comment (const String& msg) {
String comment = msg;
std::regex makeComment = std::regex("^#\\s*");
std::regex makeTrim("^[\\s\\uFEFF\\xA0]+|[\\s\\uFEFF\\xA0]+$");
comment = regex_replace(comment, makeComment, String(""));
comment = regex_replace(comment, makeTrim, String(""));
std::cout << std::endl << "# " << comment;
}
inline void Test::plan (int n) {
this->hasPlan = true;
this->assertionsExpected = n;
}
inline void Test::summarize () {
using namespace std;
cout
<< endl
<< "1.." << Test::assertionsTotal << endl
<< "# tests " << Test::assertionsTotal << endl
<< "# pass " << Test::assertionsPassing;
if (Test::assertionsPassing != Test::assertionsTotal) {
const auto failing = Test::assertionsTotal - Test::assertionsPassing;
cout << endl << "# fail " << failing << endl;
} else {
cout << endl << "# ok\n" << endl;
}
}
inline bool Test::asserts (
bool value,
const String& oper,
const String& actual,
const String& expected,
const String& message) {
using namespace std;
const int id = (Test::assertionsTotal++) + 1;
String status = "not ok";
this->assertionsMade++;
if (this->testsSkip > 0) {
this->testsSkip -= 1;
this->assertionsPassed += 1;
cout << endl << "ok " << id << " - # SKIP" << message;
return true;
}
if (value == true) {
status = "ok";
this->assertionsPassed++;
Test::assertionsPassing++;
}
cout << endl << status << " " << id << " - " << message;
if (value == false) {
StringStream ss;
ss << endl;
ss << " ---" << endl;
ss << " operator: " << oper << endl;
ss << " expected: " << expected << endl;
ss << " actual: " << actual << endl;
ss << " ...";
cout << ss.str();
}
return value;
}
template<typename L, typename R, typename BinaryPredicate>
inline void Test::assert_equal(const L& l, const R& r, const String& msg, BinaryPredicate p) {
String message = !msg.empty() ? msg : "should be equal";
String actual = stringify(l);
String expected = stringify(r);
try {
this->asserts(p(l, r), "equal", actual, expected, message);
} catch(const std::exception& e) {
this->fail(e.what());
} catch(...) {
this->fail("unknown exception");
}
}
template<typename Left, typename Right>
inline void Test::equal (const Left& l, const Right& r) {
String msg;
this->assert_equal(l, r, msg, std::equal_to<Left>());
}
template<typename Left, typename Right>
inline void Test::equal(const Left& l, const Right& r, const String& msg) {
this->assert_equal (l, r, msg, std::equal_to<Left>());
}
template<typename L, typename R, typename BinaryPredicate>
inline void Test::assert_notEqual (const L& l, const R& r, const String& msg, BinaryPredicate p) {
String message = !msg.empty() ? msg : "should not be equal";
String actual = stringify(l);
String expected = stringify(r);
try {
this->asserts(!p(l, r), "notEqual", actual, expected, message);
} catch(const Ex& e) {
this->fail(e.what());
} catch(...) {
this->fail("unknown exception");
}
}
template<typename Left, typename Right>
inline void Test::notEqual (const Left& l, const Right& r) {
String msg;
this->assert_notEqual(l, r, msg, std::equal_to<Left>());
}
template<typename Left, typename Right>
inline void Test::notEqual (const Left& l, const Right& r, const String& msg) {
this->assert_notEqual(l, r, msg, std::equal_to<Left>());
}
}
#endif