1 #ifndef WREPORT_TESTS_H
2 #define WREPORT_TESTS_H
70 std::string local_info;
73 : file(file), line(line), call(call)
78 : file(file), line(line), call(call), local_info(local_info.str())
82 std::string format()
const;
84 void format(std::ostream& out)
const;
87 struct TestStack :
public std::vector<TestStackFrame>
109 template<
typename ...Args>
110 TestFailed(
const std::exception& e, Args&&... args)
113 add_stack_info(std::forward<Args>(args)...);
116 TestFailed(
const std::string& message) : message(message) {}
118 template<
typename ...Args>
119 TestFailed(
const std::string& message, Args&&... args)
122 add_stack_info(std::forward<Args>(args)...);
125 const char* what()
const noexcept
override {
return message.c_str(); }
127 template<
typename ...Args>
128 void add_stack_info(Args&&... args) { stack.emplace_back(std::forward<Args>(args)...); }
135 #define WREPORT_TEST_INFO(name) \
136 wreport::tests::LocationInfo wreport_test_location_info; \
137 wreport::tests::LocationInfo& name = wreport_test_location_info
142 void assert_true(
const A& actual)
145 std::stringstream ss;
146 ss <<
"actual value " << actual <<
" is not true";
150 void assert_true(std::nullptr_t actual);
154 void assert_false(
const A& actual)
157 std::stringstream ss;
158 ss <<
"actual value " << actual <<
" is not false";
159 throw TestFailed(ss.str());
162 void assert_false(std::nullptr_t actual);
168 template<
typename A,
typename E>
169 void assert_equal(
const A& actual,
const E& expected)
171 if (actual == expected)
return;
172 std::stringstream ss;
173 ss <<
"value '" << actual <<
"' is different than the expected '" << expected <<
"'";
174 throw TestFailed(ss.str());
181 template<
typename A,
typename E>
182 void assert_not_equal(
const A& actual,
const E& expected)
184 if (actual != expected)
return;
185 std::stringstream ss;
186 ss <<
"value '" << actual <<
"' is not different than the expected '" << expected <<
"'";
187 throw TestFailed(ss.str());
191 template<
typename A,
typename E>
192 void assert_less(
const A& actual,
const E& expected)
194 if (actual < expected)
return;
195 std::stringstream ss;
196 ss <<
"value '" << actual <<
"' is not less than the expected '" << expected <<
"'";
197 throw TestFailed(ss.str());
201 template<
typename A,
typename E>
202 void assert_less_equal(
const A& actual,
const E& expected)
204 if (actual <= expected)
return;
205 std::stringstream ss;
206 ss <<
"value '" << actual <<
"' is not less than or equals to the expected '" << expected <<
"'";
207 throw TestFailed(ss.str());
211 template<
typename A,
typename E>
212 void assert_greater(
const A& actual,
const E& expected)
214 if (actual > expected)
return;
215 std::stringstream ss;
216 ss <<
"value '" << actual <<
"' is not greater than the expected '" << expected <<
"'";
217 throw TestFailed(ss.str());
221 template<
typename A,
typename E>
222 void assert_greater_equal(
const A& actual,
const E& expected)
224 if (actual >= expected)
return;
225 std::stringstream ss;
226 ss <<
"value '" << actual <<
"' is not greater than or equals to the expected '" << expected <<
"'";
227 throw TestFailed(ss.str());
231 void assert_startswith(
const std::string& actual,
const std::string& expected);
234 void assert_endswith(
const std::string& actual,
const std::string& expected);
237 void assert_contains(
const std::string& actual,
const std::string& expected);
240 void assert_not_contains(
const std::string& actual,
const std::string& expected);
248 void assert_re_matches(
const std::string& actual,
const std::string& expected);
256 void assert_not_re_matches(
const std::string& actual,
const std::string& expected);
263 Actual(
const A& actual) : _actual(actual) {}
266 void istrue()
const { assert_true(_actual); }
267 void isfalse()
const { assert_false(_actual); }
268 template<
typename E>
void operator==(
const E& expected)
const { assert_equal(_actual, expected); }
269 template<
typename E>
void operator!=(
const E& expected)
const { assert_not_equal(_actual, expected); }
270 template<
typename E>
void operator<(
const E& expected)
const {
return assert_less(_actual, expected); }
271 template<
typename E>
void operator<=(
const E& expected)
const {
return assert_less_equal(_actual, expected); }
272 template<
typename E>
void operator>(
const E& expected)
const {
return assert_greater(_actual, expected); }
273 template<
typename E>
void operator>=(
const E& expected)
const {
return assert_greater_equal(_actual, expected); }
281 void istrue()
const {
return assert_true(_actual); }
282 void isfalse()
const {
return assert_false(_actual); }
283 void operator==(
const char* expected)
const;
284 void operator==(
const std::string& expected)
const;
285 void operator!=(
const char* expected)
const;
286 void operator!=(
const std::string& expected)
const;
287 void operator<(
const std::string& expected)
const;
288 void operator<=(
const std::string& expected)
const;
289 void operator>(
const std::string& expected)
const;
290 void operator>=(
const std::string& expected)
const;
291 void startswith(
const std::string& expected)
const;
292 void endswith(
const std::string& expected)
const;
293 void contains(
const std::string& expected)
const;
294 void not_contains(
const std::string& expected)
const;
295 void matches(
const std::string& re)
const;
296 void not_matches(
const std::string& re)
const;
303 void startswith(
const std::string& expected)
const;
304 void endswith(
const std::string& expected)
const;
305 void contains(
const std::string& expected)
const;
306 void not_contains(
const std::string& expected)
const;
307 void matches(
const std::string& re)
const;
308 void not_matches(
const std::string& re)
const;
313 using Actual::Actual;
315 void almost_equal(
double expected,
unsigned places)
const;
316 void not_almost_equal(
double expected,
unsigned places)
const;
321 inline ActualCString actual(
const char* actual) {
return ActualCString(actual); }
322 inline ActualCString actual(
char* actual) {
return ActualCString(actual); }
323 inline ActualStdString actual(
const std::string& actual) {
return ActualStdString(actual); }
324 inline ActualDouble actual(
double actual) {
return ActualDouble(actual); }
328 using Actual::Actual;
330 void throws(
const std::string& what_match)
const;
343 #define wassert(...) \
346 } catch (TestFailed& e) { \
347 e.add_stack_info(__FILE__, __LINE__, #__VA_ARGS__, wreport_test_location_info); \
349 } catch (std::exception& e) { \
350 throw TestFailed(e, __FILE__, __LINE__, #__VA_ARGS__, wreport_test_location_info); \
354 #define wassert_true(...) wassert(actual(__VA_ARGS__).istrue())
357 #define wassert_false(...) wassert(actual(__VA_ARGS__).isfalse())
366 #define wcallchecked(func) \
369 } catch (TestFailed& e) { \
370 e.add_stack_info(__FILE__, __LINE__, #func, wreport_test_location_info); \
372 } catch (std::exception& e) { \
373 throw TestFailed(e, __FILE__, __LINE__, #func, wreport_test_location_info); \
403 TestMethodResult(
const std::string& test_case,
const std::string& test_method)
404 : test_case(test_case), test_method(test_method) {}
408 error_message = e.what();
409 error_stack = e.stack;
410 if (error_message.empty())
411 error_message =
"test failed with an empty error message";
414 void set_exception(std::exception& e)
416 error_message = e.what();
417 if (error_message.empty())
418 error_message =
"test threw an exception with an empty error message";
419 exception_typeid =
typeid(e).name();
422 void set_unknown_exception()
424 error_message =
"unknown exception caught";
427 void set_setup_exception(std::exception& e)
429 error_message =
"[setup failed: ";
430 error_message += e.what();
431 error_message +=
"]";
434 void set_teardown_exception(std::exception& e)
436 error_message =
"[teardown failed: ";
437 error_message += e.what();
438 error_message +=
"]";
441 bool is_success()
const
443 return error_message.empty();
462 bool skipped =
false;
464 TestCaseResult(
const std::string& test_case) : test_case(test_case) {}
466 void set_setup_failed()
468 fail_setup =
"test case setup method threw an unknown exception";
471 void set_setup_failed(std::exception& e)
473 fail_setup =
"test case setup method threw an exception: ";
474 fail_setup += e.what();
477 void set_teardown_failed()
479 fail_teardown =
"test case teardown method threw an unknown exception";
482 void set_teardown_failed(std::exception& e)
484 fail_teardown =
"test case teardown method threw an exception: ";
485 fail_teardown += e.what();
488 void add_test_method(TestMethodResult&& e)
490 methods.emplace_back(std::move(e));
493 bool is_success()
const
495 if (!fail_setup.empty() || !fail_teardown.empty())
return false;
496 for (
const auto& m: methods)
504 struct TestCaseResult;
506 struct TestMethodResult;
562 bool test_method_should_run(
const std::string& fullname)
const;
605 TestMethod(
const std::string& name, std::function<
void()> test_function)
606 : name(name), test_function(test_function) {}
684 template<
typename ...Args>
685 void add_method(
const std::string& name, std::function<
void()> test_function)
687 methods.emplace_back(name, test_function);
693 template<
typename ...Args>
694 void add_method(
const std::string& name, std::function<
void()> test_function, Args&&... args)
696 methods.emplace_back(name, test_function, std::forward<Args>(args)...);
704 template<
typename FUNC,
typename ...Args>
705 void add_method(
const std::string& name, FUNC test_function, Args&&... args)
707 auto f = std::bind(test_function, args...);
708 methods.emplace_back(name, f);
729 void test_teardown() {}
732 template<
typename Fixture,
typename... Args>
733 static inline Fixture* fixture_factory(Args... args)
741 template<
typename FIXTURE>
745 typedef FIXTURE Fixture;
747 Fixture* fixture =
nullptr;
748 std::function<Fixture*()> make_fixture;
750 template<
typename... Args>
754 make_fixture = std::bind(fixture_factory<FIXTURE, Args...>, args...);
760 fixture = make_fixture();
773 if (fixture) fixture->test_setup();
778 if (fixture) fixture->test_teardown();
788 template<
typename FUNC>
791 methods.emplace_back(name, [=]() {
792 test_function(*fixture);
801 std::function<void()> test_func;
805 virtual void add_tests() {}
Test registry.
Definition: utils/tests.h:572
virtual void test_method_end(const TestMethod &test_method, const TestMethodResult &test_method_result)
Called after running a test method.
Definition: utils/tests.h:540
virtual TestCaseResult run_tests(TestController &controller)
Call setup(), run all the tests that have been registered, then call teardown().
std::string exception_typeid
If non-empty, the test raised an exception and this is its type ID.
Definition: utils/tests.h:397
void test_method_end(const TestMethod &test_method, const TestMethodResult &test_method_result) override
Called after running a test method.
Result of running a whole test case.
Definition: utils/tests.h:450
Test case collecting several test methods, and self-registering with the singleton instance of TestRe...
Definition: utils/tests.h:614
std::string name
Name of the test case.
Definition: utils/tests.h:617
Information about one stack frame in the test execution stack.
Definition: utils/tests.h:65
Add information to the test backtrace for the tests run in the current scope.
Definition: utils/tests.h:53
std::string test_method
Name of the test method.
Definition: utils/tests.h:388
virtual bool test_method_begin(const TestMethod &test_method, const TestMethodResult &test_method_result)
Called before running a test method.
Definition: utils/tests.h:535
void add_method(const std::string &name, std::function< void()> test_function, Args &&...args)
Register a new test method.
Definition: utils/tests.h:694
virtual void method_setup(TestMethodResult &)
Set up before the test method is run.
Definition: utils/tests.h:651
std::vector< TestMethodResult > methods
Outcome of all the methods that have been run.
Definition: utils/tests.h:455
Simple default implementation of TestController.
Definition: utils/tests.h:549
TestStack error_stack
Stack frame of where the error happened.
Definition: utils/tests.h:394
Abstract interface for the objects that supervise test execution.
Definition: utils/tests.h:514
std::string fail_teardown
Set to a non-empty string if the teardown method of the test case failed.
Definition: utils/tests.h:460
Definition: utils/tests.h:260
virtual TestMethodResult run_test(TestController &controller, TestMethod &method)
Run a test method.
void teardown() override
Clean up after the test case is run.
Definition: utils/tests.h:763
std::vector< TestCaseResult > run_tests(TestController &controller)
Run all the registered tests using the given controller.
Exception raised when a test assertion fails, normally by Location::fail_test.
Definition: utils/tests.h:102
void method_teardown(TestMethodResult &mr) override
Clean up after the test method is run.
Definition: utils/tests.h:776
void register_test_case(TestCase &test_case)
Register a new test case.
std::string backtrace() const
Return the formatted backtrace for this location.
std::vector< TestMethod > methods
All registered test methods.
Definition: utils/tests.h:620
Definition: utils/tests.h:87
static TestRegistry & get()
Get the singleton instance of TestRegistry.
void add_method(const std::string &name, FUNC test_function, Args &&...args)
Register a new test metheod, with arguments.
Definition: utils/tests.h:705
virtual void setup()
Set up the test case before it is run.
Definition: utils/tests.h:641
std::vector< TestCase * > entries
All known test cases.
Definition: utils/tests.h:575
Definition: utils/tests.h:276
virtual void test_case_end(const TestCase &test_case, const TestCaseResult &test_case_result)
Called after running a test case.
Definition: utils/tests.h:528
std::string test_case
Name of the test case.
Definition: utils/tests.h:453
Definition: utils/tests.h:326
Definition: utils/tests.h:311
bool skipped
True if the test has been skipped.
Definition: utils/tests.h:400
std::string blacklist
Any method matching this glob expression will not be run.
Definition: utils/tests.h:555
Result of running a test method.
Definition: utils/tests.h:382
virtual bool test_case_begin(const TestCase &test_case, const TestCaseResult &test_case_result)
Called before running a test case.
Definition: utils/tests.h:523
void setup() override
Set up the test case before it is run.
Definition: utils/tests.h:757
std::function< void()> test_function
Main body of the test method.
Definition: utils/tests.h:603
void method_setup(TestMethodResult &mr) override
Set up before the test method is run.
Definition: utils/tests.h:770
Definition: utils/tests.h:299
void add_method(const std::string &name, FUNC test_function)
Add a method that takes a reference to the fixture as argument.
Definition: utils/tests.h:789
std::string whitelist
Any method not matching this glob expression will not be run.
Definition: utils/tests.h:552
virtual void register_tests()=0
This will be called before running the test case, to populate it with its test methods.
std::string error_message
If non-empty, the test failed with this error.
Definition: utils/tests.h:391
String functions.
Definition: benchmark.h:13
Test case that includes a fixture.
Definition: utils/tests.h:742
void add_method(const std::string &name, std::function< void()> test_function)
Register a new test method.
Definition: utils/tests.h:685
Test method information.
Definition: utils/tests.h:597
virtual void method_teardown(TestMethodResult &)
Clean up after the test method is run.
Definition: utils/tests.h:656
virtual void teardown()
Clean up after the test case is run.
Definition: utils/tests.h:646
std::string name
Name of the test method.
Definition: utils/tests.h:600
Base class for test fixtures.
Definition: utils/tests.h:723
std::string fail_setup
Set to a non-empty string if the setup method of the test case failed.
Definition: utils/tests.h:457
std::string test_case
Name of the test case.
Definition: utils/tests.h:385
bool test_method_begin(const TestMethod &test_method, const TestMethodResult &test_method_result) override
Called before running a test method.
std::ostream & operator()()
Clear the current information and return the output stream to which new information can be sent...
bool test_case_begin(const TestCase &test_case, const TestCaseResult &test_case_result) override
Called before running a test case.
void test_case_end(const TestCase &test_case, const TestCaseResult &test_case_result) override
Called after running a test case.