53struct LocationInfo :
public std::stringstream
70 std::string local_info;
72 TestStackFrame(
const char* file_,
int line_,
const char* call_)
73 : file(file_), line(line_), call(call_)
77 TestStackFrame(
const char* file_,
int line_,
const char* 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;
88struct TestStack :
public std::vector<TestStackFrame>
103struct TestFailed :
public std::exception
108 explicit TestFailed(
const std::exception& e);
110 template <
typename... Args>
111 TestFailed(
const std::exception& e, Args&&... args) : TestFailed(e)
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)
120 : TestFailed(message_)
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)...);
136struct TestSkipped :
public std::exception
141 explicit TestSkipped(
const std::string& reason);
148#define WREPORT_TEST_INFO(name) \
149 wreport::tests::LocationInfo wreport_test_location_info; \
150 wreport::tests::LocationInfo& name = wreport_test_location_info
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";
183template <
typename LIST>
184static inline void _format_list(std::ostream& o,
const LIST& list)
188 for (
const auto& v : list)
200void 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);
213void 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);
230template <
typename A,
typename E>
231void 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 '"
245template <
typename A,
typename E>
248 if (actual != expected)
250 std::stringstream ss;
251 ss <<
"value '" << actual <<
"' is not different than the expected '"
257template <
typename A,
typename E>
260 if (actual < expected)
262 std::stringstream ss;
263 ss <<
"value '" << actual <<
"' is not less than the expected '" << expected
269template <
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 <<
"'";
281template <
typename A,
typename E>
284 if (actual > expected)
286 std::stringstream ss;
287 ss <<
"value '" << actual <<
"' is not greater than the expected '"
293template <
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
316 const std::string& expected);
333 const std::string& expected);
335template <
class A>
struct Actual
338 Actual(
const A& actual) : _actual(actual) {}
339 Actual(
const Actual&) =
default;
340 Actual(Actual&&) =
default;
342 Actual& operator=(
const Actual&) =
delete;
343 Actual& operator=(Actual&&) =
delete;
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
373template <
typename T>
inline Actual<int> actual_int(
const T& value)
377template <
typename T>
inline Actual<unsigned> actual_unsigned(
const T& value)
379 return Actual<unsigned>(value);
381template <
typename T>
inline Actual<double> actual_double(
const T& value)
383 return Actual<double>(value);
389 ActualCString(
const char* s) : _actual(s) {}
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;
409struct ActualStdString :
public Actual<std::string>
411 explicit ActualStdString(
const std::string& s) : Actual<std::string>(s) {}
413 using Actual<std::string>::operator==;
414 void operator==(
const std::vector<uint8_t>& expected)
const;
415 using Actual<std::string>::operator!=;
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;
425struct ActualPath :
public Actual<std::filesystem::path>
427 explicit ActualPath(
const std::filesystem::path& p)
428 : Actual<std::filesystem::path>(p)
432 using Actual<std::filesystem::path>::operator==;
433 using Actual<std::filesystem::path>::operator!=;
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;
467template <
typename A>
inline Actual<A> actual(
const A& actual)
471inline ActualCString actual(
const char* actual)
473 return ActualCString(actual);
475inline ActualCString actual(
char* actual) {
return ActualCString(actual); }
484inline ActualPath actual(
const std::filesystem::path& actual)
492 using Actual::Actual;
494 void throws(
const std::string& what_match)
const;
497inline ActualFunction actual_function(std::function<
void()> actual)
502inline ActualPath actual_path(
const char* pathname)
504 return ActualPath(pathname);
506inline ActualPath actual_path(
const std::string& pathname)
508 return ActualPath(pathname);
510inline ActualPath actual_file(
const char* pathname)
514inline ActualPath actual_file(
const std::string& pathname)
519template <
typename T>
struct ActualVector :
public Actual<std::vector<T>>
521 explicit ActualVector(
const std::vector<T>& v) : Actual<std::vector<T>>(v)
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";
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)))
666struct TestController;
668struct TestCaseResult;
670struct TestMethodResult;
692 TestMethod(
const std::string& name_, std::function<
void()> test_function_)
713 TestCase(
const std::string&
name);
714 virtual ~TestCase() {}
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() {}
837template <
typename Fixture,
typename... Args>
838static inline Fixture* fixture_factory(Args... args)
846template <
typename FIXTURE>
class FixtureTestCase :
public TestCase
849 typedef FIXTURE Fixture;
851 Fixture* fixture =
nullptr;
852 std::function<Fixture*()> make_fixture;
854 template <
typename... Args>
855 FixtureTestCase(
const std::string& name_, Args... args) : TestCase(name_)
857 make_fixture = std::bind(fixture_factory<FIXTURE, Args...>, args...);
859 FixtureTestCase(
const FixtureTestCase&) =
delete;
860 FixtureTestCase(FixtureTestCase&&) =
delete;
861 FixtureTestCase& operator=(
const FixtureTestCase&) =
delete;
862 FixtureTestCase& operator=(FixtureTestCase&) =
delete;
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); });
void setup() override
Set up the test case before it is run.
Definition utils/tests.h:864
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 method_teardown(TestMethodResult &mr) override
Clean up after the test method is run.
Definition utils/tests.h:884
void teardown() override
Clean up after the test case is run.
Definition utils/tests.h:870
void method_setup(TestMethodResult &mr) override
Set up before the test method is run.
Definition utils/tests.h:877
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
Utility functions for the unit tests.
Definition tests.h:38
void assert_false(const A &actual)
Test function that ensures that the actual value is false.
Definition utils/tests.h:172
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
void assert_contains(const std::string &actual, const std::string &expected)
Ensure that the string actual contains expected.
void assert_endswith(const std::string &actual, const std::string &expected)
Ensure that the string actual ends with expected.
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
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_re_matches(const std::string &actual, const std::string &expected)
Ensure that the string actual matches the extended regular expression expected.
void assert_true(const A &actual)
The following assert_* functions throw TestFailed without capturing file/line numbers,...
Definition utils/tests.h:160
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
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
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.
void assert_not_contains(const std::string &actual, const std::string &expected)
Ensure that the string actual does not contain expected.
void assert_startswith(const std::string &actual, const std::string &expected)
Ensure that the string actual starts with expected.
Definition utils/tests.h:460
Definition utils/tests.h:491
Definition utils/tests.h:426
Definition utils/tests.h:410
Definition utils/tests.h:520
Definition utils/tests.h:336
Base class for test fixtures.
Definition utils/tests.h:829
Add information to the test backtrace for the tests run in the current scope.
Definition utils/tests.h:54
std::ostream & operator()()
Clear the current information and return the output stream to which new information can be sent.
Result of running a whole test case.
Definition testrunner.h:91
virtual void test_setup()
Set up before each test method is run.
Definition utils/tests.h:743
virtual TestCaseResult run_tests(TestController &controller)
Call setup(), run all the tests that have been registered, then call teardown().
TestMethod & add_method(const std::string &name_, std::function< void()> test_function)
Register a new test method.
Definition utils/tests.h:798
virtual void register_tests()=0
This will be called before running the test case, to populate it with its test methods.
std::vector< TestMethod > methods
All registered test methods.
Definition utils/tests.h:708
virtual void setup()
Set up the test case before it is run.
Definition utils/tests.h:733
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
virtual void method_teardown(TestMethodResult &)
Clean up after the test method is run.
Definition utils/tests.h:758
virtual void method_setup(TestMethodResult &)
Set up before the test method is run.
Definition utils/tests.h:753
std::string name
Name of the test case.
Definition utils/tests.h:705
bool tests_registered
Set to true the first time register_tests_once is run.
Definition utils/tests.h:711
void register_tests_once()
Idempotent wrapper for register_tests()
virtual void teardown()
Clean up after the test case is run.
Definition utils/tests.h:738
virtual TestMethodResult run_test(TestController &controller, TestMethod &method)
Run a test method.
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
virtual void test_teardown()
Clean up after each test method is run.
Definition utils/tests.h:748
Abstract interface for the objects that supervise test execution.
Definition testrunner.h:156
Exception thrown when a test assertion fails, normally by Location::fail_test.
Definition utils/tests.h:104
Result of running a test method.
Definition testrunner.h:24
Test method information.
Definition utils/tests.h:676
std::string name
Name of the test method.
Definition utils/tests.h:678
std::function< void()> test_function
Main body of the test method.
Definition utils/tests.h:688
std::string doc
Documentation attached to this test method.
Definition utils/tests.h:681
Definition utils/tests.h:89
std::string backtrace() const
Return the formatted backtrace for this location.
void backtrace(std::ostream &out) const
Write the formatted backtrace for this location to out.