1 #ifndef WREPORT_TESTS_H 2 #define WREPORT_TESTS_H 70 std::string local_info;
73 : file(file_), line(line_), call(call_)
79 : file(file_), line(line_), call(call_), local_info(local_info_.str())
83 std::string format()
const;
85 void format(std::ostream& out)
const;
88 struct TestStack :
public std::vector<TestStackFrame>
110 template <
typename... Args>
113 add_stack_info(std::forward<Args>(args)...);
116 explicit 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>
void add_stack_info(Args&&... args)
129 stack.emplace_back(std::forward<Args>(args)...);
148 #define WREPORT_TEST_INFO(name) \ 149 wreport::tests::LocationInfo wreport_test_location_info; \ 150 wreport::tests::LocationInfo& name = wreport_test_location_info 159 template <
typename A>
void assert_true(
const A& actual)
164 std::stringstream ss;
165 ss <<
"actual value " << actual <<
" is not true";
169 [[noreturn]]
void assert_true(std::nullptr_t actual);
176 std::stringstream ss;
177 ss <<
"actual value " << actual <<
" is not false";
183 template <
typename LIST>
184 static inline void _format_list(std::ostream& o,
const LIST& list)
188 for (
const auto& v : list)
199 template <
typename T>
200 void assert_equal(
const std::vector<T>& actual,
const std::vector<T>& expected)
202 if (actual == expected)
204 std::stringstream ss;
206 _format_list(ss, actual);
207 ss <<
" is different than the expected ";
208 _format_list(ss, expected);
209 throw TestFailed(ss.str());
212 template <
typename T>
213 void assert_equal(
const std::vector<T>& actual,
214 const std::initializer_list<T>& expected)
216 if (actual == expected)
218 std::stringstream ss;
220 _format_list(ss, actual);
221 ss <<
" is different than the expected ";
222 _format_list(ss, expected);
223 throw TestFailed(ss.str());
230 template <
typename A,
typename E>
231 void assert_equal(
const A& actual,
const E& expected)
233 if (actual == expected)
235 std::stringstream ss;
236 ss <<
"value '" << actual <<
"' is different than the expected '" 245 template <
typename A,
typename E>
248 if (actual != expected)
250 std::stringstream ss;
251 ss <<
"value '" << actual <<
"' is not different than the expected '" 257 template <
typename A,
typename E>
260 if (actual < expected)
262 std::stringstream ss;
263 ss <<
"value '" << actual <<
"' is not less than the expected '" << expected
269 template <
typename A,
typename E>
272 if (actual <= expected)
274 std::stringstream ss;
275 ss <<
"value '" << actual
276 <<
"' is not less than or equals to the expected '" << expected <<
"'";
281 template <
typename A,
typename E>
284 if (actual > expected)
286 std::stringstream ss;
287 ss <<
"value '" << actual <<
"' is not greater than the expected '" 293 template <
typename A,
typename E>
296 if (actual >= expected)
298 std::stringstream ss;
299 ss <<
"value '" << actual
300 <<
"' is not greater than or equals to the expected '" << expected
309 void assert_endswith(
const std::string& actual,
const std::string& expected);
312 void assert_contains(
const std::string& actual,
const std::string& expected);
316 const std::string& expected);
333 const std::string& expected);
338 Actual(
const A& actual) : _actual(actual) {}
347 template <
typename E>
void operator==(
const E& expected)
const 349 assert_equal(_actual, expected);
351 template <
typename E>
void operator!=(
const E& expected)
const 355 template <
typename E>
void operator<(
const E& expected)
const 359 template <
typename E>
void operator<=(
const E& expected)
const 363 template <
typename E>
void operator>(
const E& expected)
const 367 template <
typename E>
void operator>=(
const E& expected)
const 373 template <
typename T>
inline Actual<int> actual_int(
const T& value)
377 template <
typename T>
inline Actual<unsigned> actual_unsigned(
const T& value)
379 return Actual<unsigned>(value);
381 template <
typename T>
inline Actual<double> actual_double(
const T& value)
383 return Actual<double>(value);
391 void istrue()
const {
return assert_true(_actual); }
393 void operator==(
const char* expected)
const;
394 void operator==(
const std::string& expected)
const;
395 void operator!=(
const char* expected)
const;
396 void operator!=(
const std::string& expected)
const;
397 void operator<(
const std::string& expected)
const;
398 void operator<=(
const std::string& expected)
const;
399 void operator>(
const std::string& expected)
const;
400 void operator>=(
const std::string& expected)
const;
401 void startswith(
const std::string& expected)
const;
402 void endswith(
const std::string& expected)
const;
403 void contains(
const std::string& expected)
const;
404 void not_contains(
const std::string& expected)
const;
405 void matches(
const std::string& re)
const;
406 void not_matches(
const std::string& re)
const;
414 void operator==(
const std::vector<uint8_t>& expected)
const;
416 void operator!=(
const std::vector<uint8_t>& expected)
const;
417 void startswith(
const std::string& expected)
const;
418 void endswith(
const std::string& expected)
const;
419 void contains(
const std::string& expected)
const;
420 void not_contains(
const std::string& expected)
const;
421 void matches(
const std::string& re)
const;
422 void not_matches(
const std::string& re)
const;
427 explicit ActualPath(
const std::filesystem::path& p)
436 void is(
const std::filesystem::path& expected)
const;
437 [[deprecated(
"Use path_startswith")]]
void 438 startswith(
const std::string& data)
const;
440 void path_startswith(
const std::filesystem::path& expected)
const;
441 void path_endswith(
const std::filesystem::path& expected)
const;
442 void path_contains(
const std::filesystem::path& expected)
const;
443 void path_not_contains(
const std::filesystem::path& expected)
const;
446 void not_exists()
const;
448 void not_empty()
const;
450 void contents_startwith(
const std::string& data)
const;
451 void contents_equal(
const std::string& data)
const;
452 void contents_equal(
const std::vector<uint8_t>& data)
const;
453 void contents_equal(
const std::initializer_list<std::string>& lines)
const;
454 void contents_match(
const std::string& data_re)
const;
456 contents_match(
const std::initializer_list<std::string>& lines_re)
const;
461 using Actual::Actual;
463 void almost_equal(
double expected,
unsigned places)
const;
464 void not_almost_equal(
double expected,
unsigned places)
const;
467 template <
typename A>
inline Actual<A> actual(
const A& actual)
471 inline ActualCString actual(
const char* actual)
473 return ActualCString(actual);
475 inline ActualCString actual(
char* actual) {
return ActualCString(actual); }
476 inline ActualStdString actual(
const std::string& actual)
478 return ActualStdString(actual);
480 inline ActualStdString actual(
const std::vector<uint8_t>& actual)
482 return ActualStdString(std::string(actual.begin(), actual.end()));
484 inline ActualPath actual(
const std::filesystem::path& actual)
486 return ActualPath(actual);
488 inline ActualDouble actual(
double actual) {
return ActualDouble(actual); }
492 using Actual::Actual;
494 void throws(
const std::string& what_match)
const;
497 inline ActualFunction actual_function(std::function<
void()> actual)
502 inline ActualPath actual_path(
const char* pathname)
504 return ActualPath(pathname);
506 inline ActualPath actual_path(
const std::string& pathname)
508 return ActualPath(pathname);
510 inline ActualPath actual_file(
const char* pathname)
512 return ActualPath(pathname);
514 inline ActualPath actual_file(
const std::string& pathname)
516 return ActualPath(pathname);
525 void _print_vector(std::ostream& o,
const std::vector<T>& value)
const 529 for (
const auto& el : value)
540 void operator==(
const std::vector<T>& expected)
542 if (expected == this->_actual)
544 std::stringstream ss;
546 _print_vector(ss, this->_actual);
547 ss <<
" is not the same as expected ";
548 _print_vector(ss, expected);
552 void operator!=(
const std::vector<T>& expected)
554 if (expected != this->_actual)
556 std::stringstream ss;
558 _print_vector(ss, this->_actual);
559 ss <<
" unexpectedly matches the value provided";
564 template <
typename T>
577 #define wassert(...) \ 584 catch (wreport::tests::TestFailed & e1) \ 586 e1.add_stack_info(__FILE__, __LINE__, #__VA_ARGS__, \ 587 wreport_test_location_info); \ 590 catch (std::exception & e2) \ 592 throw wreport::tests::TestFailed(e2, __FILE__, __LINE__, \ 594 wreport_test_location_info); \ 599 #define wassert_true(...) wassert(actual(__VA_ARGS__).istrue()) 602 #define wassert_false(...) wassert(actual(__VA_ARGS__).isfalse()) 609 #define wassert_throws(exc, ...) \ 614 wfail_test(#__VA_ARGS__ " did not throw " #exc); \ 616 catch (TestFailed & e1) \ 624 catch (std::exception & e3) \ 626 std::string msg(#__VA_ARGS__ " did not throw " #exc \ 628 msg += typeid(e3).name(); \ 641 #define wcallchecked(func) \ 647 catch (wreport::tests::TestFailed & e) \ 649 e.add_stack_info(__FILE__, __LINE__, #func, \ 650 wreport_test_location_info); \ 653 catch (std::exception & e) \ 655 throw wreport::tests::TestFailed(e, __FILE__, __LINE__, #func, \ 656 wreport_test_location_info); \ 663 #define wfail_test(msg) wassert(throw wreport::tests::TestFailed((msg))) 666 struct TestController;
668 struct TestCaseResult;
670 struct TestMethodResult;
692 TestMethod(
const std::string& name_, std::function<
void()> test_function_)
797 template <
typename... Args>
799 std::function<
void()> test_function)
801 methods.emplace_back(name_, test_function);
808 template <
typename... Args>
810 std::function<
void()> test_function)
812 methods.emplace_back(name_, test_function);
834 void test_teardown() {}
837 template <
typename Fixture,
typename... Args>
838 static inline Fixture* fixture_factory(Args... args)
849 typedef FIXTURE Fixture;
851 Fixture* fixture =
nullptr;
852 std::function<Fixture*()> make_fixture;
854 template <
typename... Args>
857 make_fixture = std::bind(fixture_factory<FIXTURE, Args...>, args...);
867 fixture = make_fixture();
881 fixture->test_setup();
887 fixture->test_teardown();
895 template <
typename... Args>
897 std::function<
void(FIXTURE&)> test_function)
906 template <
typename... Args>
908 std::function<
void(FIXTURE&)> test_function)
911 [=]() { test_function(*fixture); });
Utility functions for the unit tests.
Definition: tests.h:38
virtual TestCaseResult run_tests(TestController &controller)
Call setup(), run all the tests that have been registered, then call teardown().
Result of running a whole test case.
Definition: testrunner.h:90
Test case collecting several test methods, and self-registering with the singleton instance of TestRe...
Definition: utils/tests.h:702
std::string name
Name of the test case.
Definition: utils/tests.h:705
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
void assert_not_re_matches(const std::string &actual, const std::string &expected)
Ensure that the string actual does not match the extended regular expression expected.
Exception thrown when a test or a test case needs to be skipped.
Definition: utils/tests.h:136
Definition: utils/tests.h:519
void assert_endswith(const std::string &actual, const std::string &expected)
Ensure that the string actual ends with expected.
virtual void method_setup(TestMethodResult &)
Set up before the test method is run.
Definition: utils/tests.h:753
void register_tests_once()
Idempotent wrapper for register_tests()
virtual void test_teardown()
Clean up after each test method is run.
Definition: utils/tests.h:748
bool tests_registered
Set to true the first time register_tests_once is run.
Definition: utils/tests.h:711
Abstract interface for the objects that supervise test execution.
Definition: testrunner.h:155
void assert_true(const A &actual)
The following assert_* functions throw TestFailed without capturing file/line numbers, and need to be used inside wassert to give good error messages.
Definition: utils/tests.h:160
void assert_not_contains(const std::string &actual, const std::string &expected)
Ensure that the string actual does not contain expected.
Definition: utils/tests.h:335
virtual void test_setup()
Set up before each test method is run.
Definition: utils/tests.h:743
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:870
Exception thrown when a test assertion fails, normally by Location::fail_test.
Definition: utils/tests.h:103
Definition: utils/tests.h:425
void method_teardown(TestMethodResult &mr) override
Clean up after the test method is run.
Definition: utils/tests.h:884
std::vector< TestMethod > methods
All registered test methods.
Definition: utils/tests.h:708
std::string backtrace() const
Return the formatted backtrace for this location.
TestMethod & add_method(const std::string &name_, const std::string &doc, std::function< void()> test_function)
Register a new test method, including documentation.
Definition: utils/tests.h:809
Definition: utils/tests.h:88
virtual void setup()
Set up the test case before it is run.
Definition: utils/tests.h:733
Definition: utils/tests.h:386
std::string doc
Documentation attached to this test method.
Definition: utils/tests.h:681
Definition: utils/tests.h:490
Definition: utils/tests.h:459
Result of running a test method.
Definition: testrunner.h:23
TestMethod & add_method(const std::string &name_, const std::string &doc, std::function< void(FIXTURE &)> test_function)
Register a new test method that takes a reference to the fixture as argument, including documentation...
Definition: utils/tests.h:907
void setup() override
Set up the test case before it is run.
Definition: utils/tests.h:864
std::function< void()> test_function
Main body of the test method.
Definition: utils/tests.h:688
void method_setup(TestMethodResult &mr) override
Set up before the test method is run.
Definition: utils/tests.h:877
Definition: utils/tests.h:409
virtual void register_tests()=0
This will be called before running the test case, to populate it with its test methods.
TestMethod & add_method(const std::string &name_)
Register a new test method, with the actual test function to be added later.
Definition: utils/tests.h:788
void assert_greater_equal(const A &actual, const E &expected)
Ensure that the actual value is greather or equal than the reference value.
Definition: utils/tests.h:294
void assert_contains(const std::string &actual, const std::string &expected)
Ensure that the string actual contains expected.
void assert_not_equal(const A &actual, const E &expected)
Test function that ensures that the actual value is different than a reference one.
Definition: utils/tests.h:246
Test case that includes a fixture.
Definition: utils/tests.h:846
void assert_re_matches(const std::string &actual, const std::string &expected)
Ensure that the string actual matches the extended regular expression expected.
TestMethod & add_method(const std::string &name_, std::function< void()> test_function)
Register a new test method.
Definition: utils/tests.h:798
Test method information.
Definition: utils/tests.h:675
void assert_greater(const A &actual, const E &expected)
Ensure that the actual value is greater than the reference value.
Definition: utils/tests.h:282
virtual void method_teardown(TestMethodResult &)
Clean up after the test method is run.
Definition: utils/tests.h:758
void assert_false(const A &actual)
Test function that ensures that the actual value is false.
Definition: utils/tests.h:172
virtual void teardown()
Clean up after the test case is run.
Definition: utils/tests.h:738
TestMethod & add_method(const std::string &name_, std::function< void(FIXTURE &)> test_function)
Register a new test method that takes a reference to the fixture as argument.
Definition: utils/tests.h:896
void assert_less_equal(const A &actual, const E &expected)
Ensure that the actual value is less or equal than the reference value.
Definition: utils/tests.h:270
std::string name
Name of the test method.
Definition: utils/tests.h:678
Base class for test fixtures.
Definition: utils/tests.h:828
void assert_less(const A &actual, const E &expected)
Ensure that the actual value is less than the reference value.
Definition: utils/tests.h:258
std::ostream & operator()()
Clear the current information and return the output stream to which new information can be sent...
void assert_startswith(const std::string &actual, const std::string &expected)
Ensure that the string actual starts with expected.