wibble 1.1
tests.h
Go to the documentation of this file.
1#ifndef WIBBLE_TESTS_H
2#define WIBBLE_TESTS_H
3
13#include <string>
14#include <sstream>
15
16#include <wibble/tests/tut.h>
18
19namespace wibble {
20namespace tests {
21struct Location;
22struct LocationInfo;
23}
24}
25
26/*
27 * These global arguments will be shadowed by local variables in functions that
28 * implement tests.
29 *
30 * They are here to act as default root nodes to fulfill method signatures when
31 * tests are called from outside other tests.
32 */
35
36#define TESTGRP(name) \
37typedef test_group<name ## _shar> tg; \
38typedef tg::object to; \
39tg name ## _tg (#name);
40
41
42namespace wibble {
43namespace tests {
44
45#define WIBBLE_TESTS_ALWAYS_THROWS __attribute__ ((noreturn))
46
48{
49 const Location* parent;
51 const char* file;
52 int line;
53 const char* args;
54
55 Location(const Location* parent, const wibble::tests::LocationInfo& info, const char* file, int line, const char* args);
56
57public:
58 Location();
59 // legacy
60 Location(const char* file, int line, const char* args);
61 // legacy
62 Location(const Location& parent, const char* file, int line, const char* args);
63 Location nest(const wibble::tests::LocationInfo& info, const char* file, int line, const char* args=0) const;
64
65 std::string locstr() const;
66 std::string msg(const std::string m) const;
67 void fail_test(const std::string& msg) const WIBBLE_TESTS_ALWAYS_THROWS;
68 void fail_test(const wibble::tests::LocationInfo& info, const char* file, int line, const char* args, const std::string& msg) const WIBBLE_TESTS_ALWAYS_THROWS;
69 void backtrace(std::ostream& out) const;
70};
71
72struct LocationInfo : public std::stringstream
73{
89 std::ostream& operator()();
91};
92
93#define WIBBLE_TEST_LOCPRM wibble::tests::Location wibble_test_location
94
97#define WIBBLE_TEST_INFO(name) \
98 wibble::tests::LocationInfo wibble_test_location_info; \
99 wibble::tests::LocationInfo& name = wibble_test_location_info
100
101#define ensure(x) wibble::tests::impl_ensure(wibble::tests::Location(__FILE__, __LINE__, #x), (x))
102#define inner_ensure(x) wibble::tests::impl_ensure(wibble::tests::Location(loc, __FILE__, __LINE__, #x), (x))
103void impl_ensure(const Location& loc, bool res);
104
105#define ensure_equals(x, y) wibble::tests::impl_ensure_equals(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
106#define inner_ensure_equals(x, y) wibble::tests::impl_ensure_equals(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
107
108template <class Actual,class Expected>
109void impl_ensure_equals(const Location& loc, const Actual& actual, const Expected& expected)
110{
111 if( expected != actual )
112 {
113 std::stringstream ss;
114 ss << "expected '" << expected << "' actual '" << actual << "'";
115 loc.fail_test(ss.str());
116 }
117}
118
119#define ensure_similar(x, y, prec) wibble::tests::impl_ensure_similar(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y), (prec))
120#define inner_ensure_similar(x, y, prec) wibble::tests::impl_ensure_similar(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y), (prec))
121
122template <class Actual, class Expected, class Precision>
123void impl_ensure_similar(const Location& loc, const Actual& actual, const Expected& expected, const Precision& precision)
124{
125 if( actual < expected - precision || expected + precision < actual )
126 {
127 std::stringstream ss;
128 ss << "expected '" << expected << "' actual '" << actual << "'";
129 loc.fail_test(ss.str());
130 }
131}
132
133#define ensure_contains(x, y) wibble::tests::impl_ensure_contains(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
134#define inner_ensure_contains(x, y) wibblwibblempl_ensure_contains(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
135void impl_ensure_contains(const wibble::tests::Location& loc, const std::string& haystack, const std::string& needle);
136
137#define ensure_not_contains(x, y) wibble::tests::impl_ensure_not_contains(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
138#define inner_ensure_not_contains(x, y) wibble::tests::impl_ensure_not_contains(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
139void impl_ensure_not_contains(const wibble::tests::Location& loc, const std::string& haystack, const std::string& needle);
140
141
142template<typename A>
144{
145 const A& actual;
147 TestBool(const A& actual, bool inverted=false) : actual(actual), inverted(inverted) {}
148
150
152 {
153 if (!inverted)
154 {
155 if (actual) return;
156 wibble_test_location.fail_test("actual value is false");
157 } else {
158 if (!actual) return;
159 wibble_test_location.fail_test("actual value is true");
160 }
161 }
162};
163
164template<typename A, typename E>
166{
170 TestEquals(const A& actual, const E& expected, bool inverted=false)
172
175 {
176 if (!inverted)
177 {
178 if (actual == expected) return;
179 std::stringstream ss;
180 ss << "value '" << actual << "' is different than the expected '" << expected << "'";
182 } else {
183 if (actual != expected) return;
184 std::stringstream ss;
185 ss << "value '" << actual << "' is not different than the expected '" << expected << "'";
187 }
188 }
189};
190
191template<typename A, typename E>
193{
197 TestIsLt(const A& actual, const E& expected, bool inverted=false)
199
202 {
203 if (!inverted)
204 {
205 if (actual < expected) return;
206 std::stringstream ss;
207 ss << "value '" << actual << "' is not less than the expected '" << expected << "'";
209 } else {
210 if (!(actual < expected)) return;
211 std::stringstream ss;
212 ss << "value '" << actual << "' is less than the expected '" << expected << "'";
214 }
215 }
216};
217
218template<typename A, typename E>
220{
225
228 {
229 if (!inverted)
230 {
231 if (actual <= expected) return;
232 std::stringstream ss;
233 ss << "value '" << actual << "' is not less than or equals to the expected '" << expected << "'";
235 } else {
236 if (!(actual <= expected)) return;
237 std::stringstream ss;
238 ss << "value '" << actual << "' is less than or equals to the expected '" << expected << "'";
240 }
241 }
242};
243
244template<typename A, typename E>
246{
251
254 {
255 if (!inverted)
256 {
257 if (actual > expected) return;
258 std::stringstream ss;
259 ss << "value '" << actual << "' is not greater than the expected '" << expected << "'";
261 } else {
262 if (!(actual > expected)) return;
263 std::stringstream ss;
264 ss << "value '" << actual << "' is greater than the expected '" << expected << "'";
266 }
267 }
268};
269
270template<typename A, typename E>
272{
277
280 {
281 if (!inverted)
282 {
283 if (actual >= expected) return;
284 std::stringstream ss;
285 ss << "value '" << actual << "' is not greater than or equals to the expected '" << expected << "'";
287 } else {
288 if (!(actual >= expected)) return;
289 std::stringstream ss;
290 ss << "value '" << actual << "' is greater than or equals to the expected '" << expected << "'";
292 }
293 }
294};
295
297{
298 std::string actual;
299 std::string expected;
301 TestStartsWith(const std::string& actual, const std::string& expected, bool inverted=false) : actual(actual), expected(expected), inverted(inverted) {}
302
304 void check(WIBBLE_TEST_LOCPRM) const;
305};
306
308{
309 std::string actual;
310 std::string expected;
312 TestEndsWith(const std::string& actual, const std::string& expected, bool inverted=false) : actual(actual), expected(expected), inverted(inverted) {}
313
315 void check(WIBBLE_TEST_LOCPRM) const;
316};
317
319{
320 std::string actual;
321 std::string expected;
323 TestContains(const std::string& actual, const std::string& expected, bool inverted=false) : actual(actual), expected(expected), inverted(inverted) {}
324
326 void check(WIBBLE_TEST_LOCPRM) const;
327};
328
330{
331 std::string actual;
332 std::string regexp;
334 TestRegexp(const std::string& actual, const std::string& regexp, bool inverted=false) : actual(actual), regexp(regexp), inverted(inverted) {}
335
337 void check(WIBBLE_TEST_LOCPRM) const;
338};
339
341{
342 std::string pathname;
344 TestFileExists(const std::string& pathname, bool inverted=false) : pathname(pathname), inverted(inverted) {}
346 void check(WIBBLE_TEST_LOCPRM) const;
347};
348
349
350template<class A>
351struct Actual
352{
354 Actual(const A& actual) : actual(actual) {}
356
357 template<typename E> TestEquals<A, E> operator==(const E& expected) const { return TestEquals<A, E>(actual, expected); }
358 template<typename E> TestEquals<A, E> operator!=(const E& expected) const { return !TestEquals<A, E>(actual, expected); }
359 template<typename E> TestIsLt<A, E> operator<(const E& expected) const { return TestIsLt<A, E>(actual, expected); }
360 template<typename E> TestIsLte<A, E> operator<=(const E& expected) const { return TestIsLte<A, E>(actual, expected); }
361 template<typename E> TestIsGt<A, E> operator>(const E& expected) const { return TestIsGt<A, E>(actual, expected); }
362 template<typename E> TestIsGte<A, E> operator>=(const E& expected) const { return TestIsGte<A, E>(actual, expected); }
364 TestBool<A> isfalse() const { return TestBool<A>(actual, true); }
365};
366
367struct ActualString : public Actual<std::string>
368{
369 ActualString(const std::string& s) : Actual<std::string>(s) {}
370 TestEquals<std::string, std::string> operator==(const std::string& expected) const { return TestEquals<std::string, std::string>(actual, expected); }
371 TestEquals<std::string, std::string> operator!=(const std::string& expected) const { return !TestEquals<std::string, std::string>(actual, expected); }
372 TestIsLt<std::string, std::string> operator<(const std::string& expected) const { return TestIsLt<std::string, std::string>(actual, expected); }
373 TestIsLte<std::string, std::string> operator<=(const std::string& expected) const { return TestIsLte<std::string, std::string>(actual, expected); }
374 TestIsGt<std::string, std::string> operator>(const std::string& expected) const { return TestIsGt<std::string, std::string>(actual, expected); }
375 TestIsGte<std::string, std::string> operator>=(const std::string& expected) const { return TestIsGte<std::string, std::string>(actual, expected); }
376 TestStartsWith startswith(const std::string& expected) const { return TestStartsWith(actual, expected); }
377 TestEndsWith endswith(const std::string& expected) const { return TestEndsWith(actual, expected); }
378 TestContains contains(const std::string& expected) const { return TestContains(actual, expected); }
379 TestRegexp matches(const std::string& regexp) const { return TestRegexp(actual, regexp); }
381};
382
383template<typename A>
384inline Actual<A> actual(const A& actual) { return Actual<A>(actual); }
385inline ActualString actual(const std::string& actual) { return ActualString(actual); }
386inline ActualString actual(const char* actual) { return ActualString(actual ? actual : ""); }
387inline ActualString actual(char* actual) { return ActualString(actual ? actual : ""); }
388
389/*
390template<typename T, typename P>
391void _wassert(WIBBLE_TEST_LOCPRM, T& a, P& op)
392{
393 op.invoke(wibble_test_location, a);
394}
395*/
396
397template<typename T>
398static inline void _wassert(WIBBLE_TEST_LOCPRM, const T& expr)
399{
401}
402
403
404#define wibble_test_runner(loc, func, ...) \
405 do { try { \
406 func(loc, ##__VA_ARGS__); \
407 } catch (tut::failure) { \
408 throw; \
409 } catch (std::exception& e) { \
410 loc.fail_test(e.what()); \
411 } } while(0)
412
413#define wrunchecked(func) \
414 do { try { \
415 func; \
416 } catch (tut::failure) { \
417 throw; \
418 } catch (std::exception& e) { \
419 wibble_test_location.fail_test(wibble_test_location_info, __FILE__, __LINE__, #func, e.what()); \
420 } } while(0)
421
422// function test, just runs the function without mangling its name
423#define wruntest(test, ...) wibble_test_runner(wibble_test_location.nest(wibble_test_location_info, __FILE__, __LINE__, "function: " #test "(" #__VA_ARGS__ ")"), test, ##__VA_ARGS__)
424
425#define wassert(...) wibble_test_runner(wibble_test_location.nest(wibble_test_location_info, __FILE__, __LINE__, #__VA_ARGS__), _wassert, ##__VA_ARGS__)
426
427}
428}
429
430#endif
Definition tests.h:48
std::string locstr() const
Definition tests.cpp:58
Location nest(const wibble::tests::LocationInfo &info, const char *file, int line, const char *args=0) const
Definition tests.cpp:43
void backtrace(std::ostream &out) const
Definition tests.cpp:48
std::string msg(const std::string m) const
Definition tests.cpp:65
Location()
Definition tests.cpp:22
void fail_test(const std::string &msg) const WIBBLE_TESTS_ALWAYS_THROWS
Definition tests.cpp:74
void impl_ensure_not_contains(const wibble::tests::Location &loc, const std::string &haystack, const std::string &needle)
Definition tests.cpp:176
void impl_ensure_contains(const wibble::tests::Location &loc, const std::string &haystack, const std::string &needle)
Definition tests.cpp:166
void impl_ensure_similar(const Location &loc, const Actual &actual, const Expected &expected, const Precision &precision)
Definition tests.h:123
Actual< A > actual(const A &actual)
Definition tests.h:384
void impl_ensure(const Location &loc, bool res)
Definition tests.cpp:160
void impl_ensure_equals(const Location &loc, const Actual &actual, const Expected &expected)
Definition tests.h:109
Definition amorph.h:17
Definition amorph.h:30
Definition tests.h:368
TestContains contains(const std::string &expected) const
Definition tests.h:378
TestIsGt< std::string, std::string > operator>(const std::string &expected) const
Definition tests.h:374
TestIsLt< std::string, std::string > operator<(const std::string &expected) const
Definition tests.h:372
TestEquals< std::string, std::string > operator!=(const std::string &expected) const
Definition tests.h:371
TestIsLte< std::string, std::string > operator<=(const std::string &expected) const
Definition tests.h:373
TestIsGte< std::string, std::string > operator>=(const std::string &expected) const
Definition tests.h:375
TestEndsWith endswith(const std::string &expected) const
Definition tests.h:377
TestRegexp matches(const std::string &regexp) const
Definition tests.h:379
TestStartsWith startswith(const std::string &expected) const
Definition tests.h:376
TestEquals< std::string, std::string > operator==(const std::string &expected) const
Definition tests.h:370
ActualString(const std::string &s)
Definition tests.h:369
TestFileExists fileexists() const
Definition tests.h:380
Definition tests.h:352
TestEquals< A, E > operator==(const E &expected) const
Definition tests.h:357
TestIsLt< A, E > operator<(const E &expected) const
Definition tests.h:359
~Actual()
Definition tests.h:355
A actual
Definition tests.h:353
TestIsLte< A, E > operator<=(const E &expected) const
Definition tests.h:360
TestBool< A > istrue() const
Definition tests.h:363
TestIsGt< A, E > operator>(const E &expected) const
Definition tests.h:361
TestBool< A > isfalse() const
Definition tests.h:364
TestEquals< A, E > operator!=(const E &expected) const
Definition tests.h:358
TestIsGte< A, E > operator>=(const E &expected) const
Definition tests.h:362
Actual(const A &actual)
Definition tests.h:354
Definition tests.h:73
std::ostream & operator()()
Clear the stringstream and return self.
Definition tests.cpp:85
LocationInfo()
Definition tests.h:90
Definition tests.h:144
TestBool< A > operator!()
Definition tests.h:149
void check(WIBBLE_TEST_LOCPRM) const
Definition tests.h:151
bool inverted
Definition tests.h:146
TestBool(const A &actual, bool inverted=false)
Definition tests.h:147
const A & actual
Definition tests.h:145
Definition tests.h:319
void check(WIBBLE_TEST_LOCPRM) const
Definition tests.cpp:218
bool inverted
Definition tests.h:322
TestContains(const std::string &actual, const std::string &expected, bool inverted=false)
Definition tests.h:323
std::string expected
Definition tests.h:321
std::string actual
Definition tests.h:320
TestContains operator!()
Definition tests.h:325
Definition tests.h:308
bool inverted
Definition tests.h:311
std::string expected
Definition tests.h:310
TestEndsWith operator!()
Definition tests.h:314
void check(WIBBLE_TEST_LOCPRM) const
Definition tests.cpp:202
TestEndsWith(const std::string &actual, const std::string &expected, bool inverted=false)
Definition tests.h:312
std::string actual
Definition tests.h:309
Definition tests.h:166
bool inverted
Definition tests.h:169
TestEquals< A, E > operator!()
Definition tests.h:173
TestEquals(const A &actual, const E &expected, bool inverted=false)
Definition tests.h:170
void check(WIBBLE_TEST_LOCPRM) const
Definition tests.h:174
A actual
Definition tests.h:167
E expected
Definition tests.h:168
Definition tests.h:341
void check(WIBBLE_TEST_LOCPRM) const
Definition tests.cpp:251
bool inverted
Definition tests.h:343
TestFileExists(const std::string &pathname, bool inverted=false)
Definition tests.h:344
std::string pathname
Definition tests.h:342
TestFileExists operator!()
Definition tests.h:345
Definition tests.h:246
TestIsGt(const A &actual, const E &expected, bool inverted=false)
Definition tests.h:250
A actual
Definition tests.h:247
bool inverted
Definition tests.h:249
TestIsGt< A, E > operator!()
Definition tests.h:252
E expected
Definition tests.h:248
void check(WIBBLE_TEST_LOCPRM) const
Definition tests.h:253
Definition tests.h:272
bool inverted
Definition tests.h:275
E expected
Definition tests.h:274
TestIsGte(const A &actual, const E &expected, bool inverted=false)
Definition tests.h:276
TestIsGte< A, E > operator!()
Definition tests.h:278
A actual
Definition tests.h:273
void check(WIBBLE_TEST_LOCPRM) const
Definition tests.h:279
Definition tests.h:193
E expected
Definition tests.h:195
bool inverted
Definition tests.h:196
void check(WIBBLE_TEST_LOCPRM) const
Definition tests.h:201
A actual
Definition tests.h:194
TestIsLt< A, E > operator!()
Definition tests.h:200
TestIsLt(const A &actual, const E &expected, bool inverted=false)
Definition tests.h:197
Definition tests.h:220
bool inverted
Definition tests.h:223
void check(WIBBLE_TEST_LOCPRM) const
Definition tests.h:227
A actual
Definition tests.h:221
TestIsLte< A, E > operator!()
Definition tests.h:226
TestIsLte(const A &actual, const E &expected, bool inverted=false)
Definition tests.h:224
E expected
Definition tests.h:222
Definition tests.h:330
TestRegexp operator!()
Definition tests.h:336
void check(WIBBLE_TEST_LOCPRM) const
Definition tests.cpp:234
std::string actual
Definition tests.h:331
bool inverted
Definition tests.h:333
TestRegexp(const std::string &actual, const std::string &regexp, bool inverted=false)
Definition tests.h:334
std::string regexp
Definition tests.h:332
Definition tests.h:297
void check(WIBBLE_TEST_LOCPRM) const
Definition tests.cpp:186
bool inverted
Definition tests.h:300
TestStartsWith operator!()
Definition tests.h:303
std::string expected
Definition tests.h:299
TestStartsWith(const std::string &actual, const std::string &expected, bool inverted=false)
Definition tests.h:301
std::string actual
Definition tests.h:298
const wibble::tests::Location wibble_test_location
Definition tests.cpp:16
const wibble::tests::Location wibble_test_location
Definition tests.cpp:16
#define WIBBLE_TEST_LOCPRM
Definition tests.h:93
const wibble::tests::LocationInfo wibble_test_location_info
Definition tests.cpp:17
#define WIBBLE_TESTS_ALWAYS_THROWS
Definition tests.h:45