1 #ifndef WREPORT_TESTS_H 2 #define WREPORT_TESTS_H 61 std::ostream& operator()();
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>
92 std::string backtrace()
const;
95 void backtrace(std::ostream& out)
const;
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)...); }
142 #define WREPORT_TEST_INFO(name) \ 143 wreport::tests::LocationInfo wreport_test_location_info; \ 144 wreport::tests::LocationInfo& name = wreport_test_location_info 149 void assert_true(
const A& actual)
152 std::stringstream ss;
153 ss <<
"actual value " << actual <<
" is not true";
157 void assert_true(std::nullptr_t actual);
161 void assert_false(
const A& actual)
164 std::stringstream ss;
165 ss <<
"actual value " << actual <<
" is not false";
169 void assert_false(std::nullptr_t actual);
175 template<
typename A,
typename E>
176 void assert_equal(
const A& actual,
const E& expected)
178 if (actual == expected)
return;
179 std::stringstream ss;
180 ss <<
"value '" << actual <<
"' is different than the expected '" << expected <<
"'";
188 template<
typename A,
typename E>
189 void assert_not_equal(
const A& actual,
const E& expected)
191 if (actual != expected)
return;
192 std::stringstream ss;
193 ss <<
"value '" << actual <<
"' is not different than the expected '" << expected <<
"'";
198 template<
typename A,
typename E>
199 void assert_less(
const A& actual,
const E& expected)
201 if (actual < expected)
return;
202 std::stringstream ss;
203 ss <<
"value '" << actual <<
"' is not less than the expected '" << expected <<
"'";
208 template<
typename A,
typename E>
209 void assert_less_equal(
const A& actual,
const E& expected)
211 if (actual <= expected)
return;
212 std::stringstream ss;
213 ss <<
"value '" << actual <<
"' is not less than or equals to the expected '" << expected <<
"'";
218 template<
typename A,
typename E>
219 void assert_greater(
const A& actual,
const E& expected)
221 if (actual > expected)
return;
222 std::stringstream ss;
223 ss <<
"value '" << actual <<
"' is not greater than the expected '" << expected <<
"'";
228 template<
typename A,
typename E>
229 void assert_greater_equal(
const A& actual,
const E& expected)
231 if (actual >= expected)
return;
232 std::stringstream ss;
233 ss <<
"value '" << actual <<
"' is not greater than or equals to the expected '" << expected <<
"'";
238 void assert_startswith(
const std::string& actual,
const std::string& expected);
241 void assert_endswith(
const std::string& actual,
const std::string& expected);
244 void assert_contains(
const std::string& actual,
const std::string& expected);
247 void assert_not_contains(
const std::string& actual,
const std::string& expected);
255 void assert_re_matches(
const std::string& actual,
const std::string& expected);
263 void assert_not_re_matches(
const std::string& actual,
const std::string& expected);
270 Actual(
const A& actual) : _actual(actual) {}
273 void istrue()
const { assert_true(_actual); }
274 void isfalse()
const { assert_false(_actual); }
275 template<
typename E>
void operator==(
const E& expected)
const { assert_equal(_actual, expected); }
276 template<
typename E>
void operator!=(
const E& expected)
const { assert_not_equal(_actual, expected); }
277 template<
typename E>
void operator<(
const E& expected)
const {
return assert_less(_actual, expected); }
278 template<
typename E>
void operator<=(
const E& expected)
const {
return assert_less_equal(_actual, expected); }
279 template<
typename E>
void operator>(
const E& expected)
const {
return assert_greater(_actual, expected); }
280 template<
typename E>
void operator>=(
const E& expected)
const {
return assert_greater_equal(_actual, expected); }
288 void istrue()
const {
return assert_true(_actual); }
289 void isfalse()
const {
return assert_false(_actual); }
290 void operator==(
const char* expected)
const;
291 void operator==(
const std::string& expected)
const;
292 void operator!=(
const char* expected)
const;
293 void operator!=(
const std::string& expected)
const;
294 void operator<(
const std::string& expected)
const;
295 void operator<=(
const std::string& expected)
const;
296 void operator>(
const std::string& expected)
const;
297 void operator>=(
const std::string& expected)
const;
298 void startswith(
const std::string& expected)
const;
299 void endswith(
const std::string& expected)
const;
300 void contains(
const std::string& expected)
const;
301 void not_contains(
const std::string& expected)
const;
302 void matches(
const std::string& re)
const;
303 void not_matches(
const std::string& re)
const;
310 void startswith(
const std::string& expected)
const;
311 void endswith(
const std::string& expected)
const;
312 void contains(
const std::string& expected)
const;
313 void not_contains(
const std::string& expected)
const;
314 void matches(
const std::string& re)
const;
315 void not_matches(
const std::string& re)
const;
320 using Actual::Actual;
322 void almost_equal(
double expected,
unsigned places)
const;
323 void not_almost_equal(
double expected,
unsigned places)
const;
335 using Actual::Actual;
337 void throws(
const std::string& what_match)
const;
344 using Actual::Actual;
347 void not_exists()
const;
359 #define wassert(...) \ 362 } catch (wreport::tests::TestFailed& e) { \ 363 e.add_stack_info(__FILE__, __LINE__, #__VA_ARGS__, wreport_test_location_info); \ 365 } catch (std::exception& e) { \ 366 throw wreport::tests::TestFailed(e, __FILE__, __LINE__, #__VA_ARGS__, wreport_test_location_info); \ 370 #define wassert_true(...) wassert(actual(__VA_ARGS__).istrue()) 373 #define wassert_false(...) wassert(actual(__VA_ARGS__).isfalse()) 382 #define wcallchecked(func) \ 385 } catch (wreport::tests::TestFailed& e) { \ 386 e.add_stack_info(__FILE__, __LINE__, #func, wreport_test_location_info); \ 388 } catch (std::exception& e) { \ 389 throw wreport::tests::TestFailed(e, __FILE__, __LINE__, #func, wreport_test_location_info); \ 416 bool skipped =
false;
419 TestMethodResult(
const std::string& test_case,
const std::string& test_method)
420 : test_case(test_case), test_method(test_method) {}
424 error_message = e.what();
425 error_stack = e.stack;
426 if (error_message.empty())
427 error_message =
"test failed with an empty error message";
430 void set_exception(std::exception& e)
432 error_message = e.what();
433 if (error_message.empty())
434 error_message =
"test threw an exception with an empty error message";
435 exception_typeid =
typeid(e).name();
438 void set_unknown_exception()
440 error_message =
"unknown exception caught";
443 void set_setup_exception(std::exception& e)
445 error_message =
"[setup failed: ";
446 error_message += e.what();
447 error_message +=
"]";
450 void set_teardown_exception(std::exception& e)
452 error_message =
"[teardown failed: ";
453 error_message += e.what();
454 error_message +=
"]";
457 bool is_success()
const 459 return error_message.empty();
478 bool skipped =
false;
480 TestCaseResult(
const std::string& test_case) : test_case(test_case) {}
482 void set_setup_failed()
484 fail_setup =
"test case setup method threw an unknown exception";
487 void set_setup_failed(std::exception& e)
489 fail_setup =
"test case setup method threw an exception: ";
490 fail_setup += e.what();
493 void set_teardown_failed()
495 fail_teardown =
"test case teardown method threw an unknown exception";
498 void set_teardown_failed(std::exception& e)
500 fail_teardown =
"test case teardown method threw an exception: ";
501 fail_teardown += e.what();
506 methods.emplace_back(std::move(e));
509 bool is_success()
const 511 if (!fail_setup.empty() || !fail_teardown.empty())
return false;
512 for (
const auto& m: methods)
578 bool test_method_should_run(
const std::string& fullname)
const;
599 void register_test_case(
TestCase& test_case);
604 std::vector<TestCaseResult> run_tests(
TestController& controller);
621 TestMethod(
const std::string& name, std::function<
void()> test_function)
622 : name(name), test_function(test_function) {}
652 virtual void register_tests() = 0;
700 template<
typename ...Args>
701 void add_method(
const std::string& name, std::function<
void()> test_function)
703 methods.emplace_back(name, test_function);
709 template<
typename ...Args>
710 void add_method(
const std::string& name, std::function<
void()> test_function, Args&&... args)
712 methods.emplace_back(name, test_function, std::forward<Args>(args)...);
720 template<
typename FUNC,
typename ...Args>
721 void add_method(
const std::string& name, FUNC test_function, Args&&... args)
723 auto f = std::bind(test_function, args...);
724 methods.emplace_back(name, f);
745 void test_teardown() {}
748 template<
typename Fixture,
typename... Args>
749 static inline Fixture* fixture_factory(Args... args)
757 template<
typename FIXTURE>
761 typedef FIXTURE Fixture;
763 Fixture* fixture =
nullptr;
764 std::function<Fixture*()> make_fixture;
766 template<
typename... Args>
770 make_fixture = std::bind(fixture_factory<FIXTURE, Args...>, args...);
776 fixture = make_fixture();
789 if (fixture) fixture->test_setup();
794 if (fixture) fixture->test_teardown();
804 template<
typename FUNC>
807 methods.emplace_back(name, [=]() {
808 test_function(*fixture);
817 std::function<void()> test_func;
821 virtual void add_tests() {}
Test registry.
Definition: utils/tests.h:588
virtual void test_method_end(const TestMethod &test_method, const TestMethodResult &test_method_result)
Called after running a test method.
Definition: utils/tests.h:556
std::string exception_typeid
If non-empty, the test threw an exception and this is its type ID.
Definition: utils/tests.h:413
Result of running a whole test case.
Definition: utils/tests.h:466
Test case collecting several test methods, and self-registering with the singleton instance of TestRe...
Definition: utils/tests.h:630
std::string name
Name of the test case.
Definition: utils/tests.h:633
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:404
Exception thrown when a test or a test case needs to be skipped.
Definition: utils/tests.h:134
virtual bool test_method_begin(const TestMethod &test_method, const TestMethodResult &test_method_result)
Called before running a test method.
Definition: utils/tests.h:551
void add_method(const std::string &name, std::function< void()> test_function, Args &&...args)
Register a new test method.
Definition: utils/tests.h:710
virtual void method_setup(TestMethodResult &)
Set up before the test method is run.
Definition: utils/tests.h:667
std::vector< TestMethodResult > methods
Outcome of all the methods that have been run.
Definition: utils/tests.h:471
Simple default implementation of TestController.
Definition: utils/tests.h:565
TestStack error_stack
Stack frame of where the error happened.
Definition: utils/tests.h:410
Abstract interface for the objects that supervise test execution.
Definition: utils/tests.h:530
std::string fail_teardown
Set to a non-empty string if the teardown method of the test case failed.
Definition: utils/tests.h:476
Definition: utils/tests.h:267
void teardown() override
Clean up after the test case is run.
Definition: utils/tests.h:779
Exception thrown 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:792
void register_test_case(TestCase &test_case)
Register a new test case.
std::vector< TestMethod > methods
All registered test methods.
Definition: utils/tests.h:636
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:721
virtual void setup()
Set up the test case before it is run.
Definition: utils/tests.h:657
std::vector< TestCase * > entries
All known test cases.
Definition: utils/tests.h:591
Definition: utils/tests.h:283
virtual void test_case_end(const TestCase &test_case, const TestCaseResult &test_case_result)
Called after running a test case.
Definition: utils/tests.h:544
std::string test_case
Name of the test case.
Definition: utils/tests.h:469
Definition: utils/tests.h:333
Definition: utils/tests.h:318
std::string blacklist
Any method matching this glob expression will not be run.
Definition: utils/tests.h:571
Result of running a test method.
Definition: utils/tests.h:398
virtual bool test_case_begin(const TestCase &test_case, const TestCaseResult &test_case_result)
Called before running a test case.
Definition: utils/tests.h:539
Definition: utils/tests.h:342
void setup() override
Set up the test case before it is run.
Definition: utils/tests.h:773
std::function< void()> test_function
Main body of the test method.
Definition: utils/tests.h:619
void method_setup(TestMethodResult &mr) override
Set up before the test method is run.
Definition: utils/tests.h:786
Definition: utils/tests.h:306
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:805
std::string whitelist
Any method not matching this glob expression will not be run.
Definition: utils/tests.h:568
std::string error_message
If non-empty, the test failed with this error.
Definition: utils/tests.h:407
String functions.
Definition: benchmark.h:13
Test case that includes a fixture.
Definition: utils/tests.h:758
void add_method(const std::string &name, std::function< void()> test_function)
Register a new test method.
Definition: utils/tests.h:701
Test method information.
Definition: utils/tests.h:613
virtual void method_teardown(TestMethodResult &)
Clean up after the test method is run.
Definition: utils/tests.h:672
virtual void teardown()
Clean up after the test case is run.
Definition: utils/tests.h:662
std::string name
Name of the test method.
Definition: utils/tests.h:616
Base class for test fixtures.
Definition: utils/tests.h:739
std::string fail_setup
Set to a non-empty string if the setup method of the test case failed.
Definition: utils/tests.h:473
std::string test_case
Name of the test case.
Definition: utils/tests.h:401