-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlemon.h
415 lines (379 loc) · 13.2 KB
/
lemon.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
///////////////////////////////////////////////////////////////////////////////
// lemon.h - A minimal unit-testing framework based on Test::More
//
// Description:
// lemon is a minimal unit-testing framework designed to be simple to drop
// in and use without require external dependencies or linking. In this way
// lemon hopes to promote testing on projects of all sizes by reducing the
// barrier of setup that comes with most unit-testing frameworks. To find
// the latest version visit http://github.com/etscrivner/lemon.
//
// Copyright (c) 2010 lemon team
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
///////////////////////////////////////////////////////////////////////////////
#ifndef LEMON_H_
#define LEMON_H_
// C++ includes
#include <iostream>
#include <string>
///////////////////////////////////////////////////////////////////////////////
// Macro: LEMON_SKIP
//
// Begins a block of tests that will be skipped, creating a more convenient
// syntax for test skipping. For example:
//
// <code>
// lemon::test<> lemon(..);
// // ...
// LEMON_SKIP(lemon, "These tests are currently broken") {
// lemon.is(foo(), true, "foo true");
// lemon.is(bar(), false, "bar false");
// }
// </code>
#define LEMON_SKIP(lemon_inst, reason) \
for (bool __skip_enabled__ = (lemon_inst).enable_skip(); \
__skip_enabled__; __skip_enabled__ = (lemon_inst).disable_skip())
///////////////////////////////////////////////////////////////////////////////
// Macro: LEMON_TODO
//
// Begins a block of tests for features which have yet to be completed. For
// example:
//
// <code>
// lemon::test<> lemon(..);
// // ...
// LEMON_TODO(lemon) {
// lemon.is(tumtum(), fizbaz(), "tumtum is fizbaz");
// lemon.not_ok(cannibalism(), "cannibalism is not ok");
// }
// </code>
#define LEMON_TODO(lemon_inst) \
for (bool __todo_enabled__ = (lemon_inst).enable_todo(); \
__todo_enabled__; __todo_enabled__ = (lemon_inst).disable_todo())
namespace lemon {
namespace output {
///////////////////////////////////////////////////////////////////////////
// Class: cout
//
// Implements output to standard out
struct cout {
template<typename T>
lemon::output::cout& operator << (const T& val) {
std::cout << val;
return *this;
}
};
///////////////////////////////////////////////////////////////////////////
// Class: cerr
//
// Implements output to standard error
struct cerr {
template<typename T>
lemon::output::cerr& operator << (const T& val) {
std::cerr << val;
return *this;
}
};
///////////////////////////////////////////////////////////////////////////
// Class: clog
//
// Implements output to standard logging
struct clog {
template<typename T>
lemon::output::clog& operator << (const T& val) {
std::clog << val;
return *this;
}
};
///////////////////////////////////////////////////////////////////////////
// Class: nothing
//
// Implements a null output policy
struct nothing {
template<typename T>
lemon::output::nothing& operator << (const T& val) {
return *this;
}
};
}
/////////////////////////////////////////////////////////////////////////////
// Class: test
//
// Policy-based class for doing testing. For example a simple test might be:
//
// <code>
// bool always_true() { return true; }
// //...
// lemon::test<> lemon(2);
// lemon.is(always_true(), true, "always_true is true");
// lemon.isnt(always_true(), false, "always_true isn't false");
// lemon.done();
// </code>
template <class output_policy_t = lemon::output::cout>
class test {
public:
/////////////////////////////////////////////////////////////////////////////
// Function: test
//
// Parameters:
// num_planned_tests - The total number of tests you plan to execute
//
// This simply lets lemon know how many tests you're planning to run so that
// it can properly output the diagnostic information and doesn't have to
// count by hand (which can be tricky as one test can have many assertions).
test (unsigned int num_planned_tests = 0)
: num_tests_(0),
test_number_(0),
num_skipped_(0),
num_failed_(0),
num_planned_(num_planned_tests),
skip_enabled_(false),
todo_enabled_(false)
{
if (num_planned_tests > 0) {
output_ << "1.." << num_planned_tests << "\n";
}
}
/////////////////////////////////////////////////////////////////////////////
// Function: done
//
// Returns true if all unskipped tests passed, false if there were failures.
bool done () {
// If there was no test plan specified
if (num_planned_ == 0) {
output_ << "1.." << num_tests_ << "\n";
}
// Compute the total number of tests without skipped tests counted
unsigned int total_tests = num_tests_ - num_skipped_;
// If any tests were skipped
if (num_planned_ > 0 && num_skipped_ > 0) {
// Display information about the skipped tests
output_ << "# Looks like you planned " << num_planned_;
output_ << " but only ran " << total_tests << "\n";
}
// If any tests were failed
if (num_failed_ > 0) {
// Display test failure statistics
output_ << "# Looks like you failed " << num_failed_;
output_ << " of " << total_tests << "\n";
return false;
} else if(num_planned_ > 0 && total_tests > num_planned_) {
output_ << "# Looks like you ran " << total_tests << " tests, ";
output_ << "but only planned " << num_planned_ << "\n";
return false;
} else {
// Otherwise display success message
output_ << "# Looks like you passed all " << total_tests << " tests.\n";
return true;
}
}
/////////////////////////////////////////////////////////////////////////////
// Function: diag
//
// Parameters:
// message - A string to be written out to the display
//
// Used to display diagnostic information which is not a unit test.
void diag (const std::string& message) {
output_ << "# " << message << "\n";
}
/////////////////////////////////////////////////////////////////////////////
// Function: ok
//
// Parameters:
// passed - True indicates a passing condition, false indicates failure
// test_name - A short, descriptive name for this test
//
// Marks this test as passed if pass is true. The test is marked as
// failing otherwise.
bool ok (bool passed, const std::string& test_name) {
// Increment the number of tests run
num_tests_++;
// If this is a skip or todo message
std::string test_name_out = test_name;
if (test_name[0] != '#') {
// Not the safest thing, but append a dash to the front
test_name_out = "- " + test_name_out;
}
// If we're currently skipping tests
if (skip_enabled_) {
num_skipped_++;
output_ << "skipping " << num_tests_ << " " << test_name_out << "\n";
return false;
} else if (todo_enabled_) {
num_skipped_++;
output_ << "todo " << num_tests_ << " " << test_name_out << "\n";
return false;
} else if (passed) {
// Inform you that the test passed
output_ << "ok " << num_tests_ << " " << test_name_out << "\n";
} else {
// Otherwise increment the number of failed tests.
num_failed_++;
// Inform you that the test failed
output_ << "not ok " << num_tests_ << " " << test_name_out << "\n";
diag(" Failed test '" + test_name + "'");
}
return passed;
}
/////////////////////////////////////////////////////////////////////////////
// Function: not_ok
//
// Parameters:
// failed - False indicates a passing condition, true indicates failure
// test_name - A short, descriptive name for this test
//
// Marks this test as passed if the boolean parameter is false. The test is
// marked as failing otherwise.
bool not_ok (bool failed, const std::string& test_name) {
return ok(!failed, test_name);
}
/////////////////////////////////////////////////////////////////////////////
// Function: is
//
// Parameters:
// this_one - The left hand of the equality operator
// that_one - The right hand of the equality operator
// test_name - A short, descriptive name for this test
//
// Checks whether the two values are equal using the == operator. If
// they are equal the test passes, otherwise it fails.
template<typename T1, typename T2>
bool is (const T1& this_one,
const T2& that_one,
const std::string& test_name)
{
bool passed = (this_one == that_one);
ok(passed, test_name);
if (!passed && !skip_enabled_ && !todo_enabled_) {
output_ << "# got: '" << this_one << "'\n";
output_ << "# expected: '" << that_one << "'\n";
}
return passed;
}
/////////////////////////////////////////////////////////////////////////////
// Function: isnt
//
// Parameters:
// this_one - The left hand of the inequality operator
// that_one - The right hand of the inequality operator
// test_name - A short, descriptive name for this test
//
// Checks whether the two values are equal using the != operator. If
// they are not equal the test passes, otherwise the test fails.
template<typename T1, typename T2>
bool isnt (const T1& this_one,
const T2& that_one,
const std::string& test_name)
{
bool passed = (this_one != that_one);
ok (passed, test_name);
if (!passed && !skip_enabled_ && !todo_enabled_) {
output_ << "# '" << this_one << "'\n";
output_ << "# !=\n";
output_ << "# '" << that_one << "'\n";
}
return passed;
}
/////////////////////////////////////////////////////////////////////////////
// Function: pass
//
// Parameters:
// test_name - A short, descriptive name for this test
//
// Marks the given test as trivially passing.
bool pass (const std::string& test_name) {
return ok(true, test_name);
}
/////////////////////////////////////////////////////////////////////////////
// Function: fail
//
// Parameters:
// test_name - A short, descriptive name for this test
//
// Marks the given test as trivially failing.
bool fail (const std::string& test_name) {
return ok(false, test_name);
}
/////////////////////////////////////////////////////////////////////////////
// Function: enable_todo
//
// Enables todo mode causing no tests to be run until todo is disabled.
// Always returns true.
bool enable_todo () {
todo_enabled_ = true;
return true;
}
/////////////////////////////////////////////////////////////////////////////
// Function: disable_todo
//
// Disables todo mode causing any further tests to be run. Always returns
// false.
bool disable_todo () {
todo_enabled_ = false;
return false;
}
/////////////////////////////////////////////////////////////////////////////
// Function: enable_skip
//
// Parameters:
// reason - The reason printed for skipping tests
//
// Enables skipping causing no tests to be run until skipping is disabled.
// Always returns true.
bool enable_skip () {
skip_enabled_ = true;
return true;
}
/////////////////////////////////////////////////////////////////////////////
// Function: disable_skip
//
// Disables skipping allowing tests to be run again. Always returns false.
bool disable_skip () {
skip_enabled_ = false;
return false;
}
/////////////////////////////////////////////////////////////////////////////
// Function: num_failed
//
// Returns the number of failed tests
unsigned int num_failed () const {
return num_failed_;
}
/////////////////////////////////////////////////////////////////////////////
// Function: num_skipped
//
// Returns the number of skipped tests
unsigned int num_skipped() const {
return num_skipped_;
}
private:
unsigned int num_tests_; // The total number of tests to be executed
unsigned int test_number_; // The number of the current test
unsigned int num_skipped_; // The number of tests marked as skipped
unsigned int num_failed_; // The number of tests marked as failing
unsigned int num_planned_; // The number of tests planned to be run
bool skip_enabled_; // Are tests being skipped
bool todo_enabled_; // Are these tests incomplete
output_policy_t output_; // The place where output will be sent
};
}
#endif // LEMON_H_