71 std::string local_info;
74 : 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;
88struct TestStack :
public std::vector<TestStackFrame>
110 template<
typename ...Args>
111 TestFailed(
const std::exception& e, Args&&... args)
114 add_stack_info(std::forward<Args>(args)...);
117 TestFailed(
const std::string& message) : message(message) {}
119 template<
typename ...Args>
120 TestFailed(
const std::string& message, Args&&... args)
123 add_stack_info(std::forward<Args>(args)...);
126 const char* what()
const noexcept override {
return message.c_str(); }
128 template<
typename ...Args>
129 void add_stack_info(Args&&... args) { stack.emplace_back(std::forward<Args>(args)...); }
147#define WREPORT_TEST_INFO(name) \
148 wreport::tests::LocationInfo wreport_test_location_info; \
149 wreport::tests::LocationInfo& name = wreport_test_location_info
161void assert_true(
const A& actual)
164 std::stringstream ss;
165 ss <<
"actual value " << actual <<
" is not true";
169void assert_true(std::nullptr_t actual);
173void assert_false(
const A& actual)
176 std::stringstream ss;
177 ss <<
"actual value " << actual <<
" is not false";
178 throw TestFailed(ss.str());
181void assert_false(std::nullptr_t actual);
183template<
typename LIST>
184static inline void _format_list(std::ostream& o,
const LIST& list) {
187 for (
const auto& v: list)
199void assert_equal(
const std::vector<T>& actual,
const std::vector<T>& expected)
201 if (actual == expected)
return;
202 std::stringstream ss;
204 _format_list(ss, actual);
205 ss <<
" is different than the expected ";
206 _format_list(ss, expected);
207 throw TestFailed(ss.str());
211void assert_equal(
const std::vector<T>& actual,
const std::initializer_list<T>& expected)
213 if (actual == expected)
return;
214 std::stringstream ss;
216 _format_list(ss, actual);
217 ss <<
" is different than the expected ";
218 _format_list(ss, expected);
219 throw TestFailed(ss.str());
226template<
typename A,
typename E>
227void assert_equal(
const A& actual,
const E& expected)
229 if (actual == expected)
return;
230 std::stringstream ss;
231 ss <<
"value '" << actual <<
"' is different than the expected '" << expected <<
"'";
232 throw TestFailed(ss.str());
239template<
typename A,
typename E>
240void assert_not_equal(
const A& actual,
const E& expected)
242 if (actual != expected)
return;
243 std::stringstream ss;
244 ss <<
"value '" << actual <<
"' is not different than the expected '" << expected <<
"'";
245 throw TestFailed(ss.str());
249template<
typename A,
typename E>
250void assert_less(
const A& actual,
const E& expected)
252 if (actual < expected)
return;
253 std::stringstream ss;
254 ss <<
"value '" << actual <<
"' is not less than the expected '" << expected <<
"'";
255 throw TestFailed(ss.str());
259template<
typename A,
typename E>
260void assert_less_equal(
const A& actual,
const E& expected)
262 if (actual <= expected)
return;
263 std::stringstream ss;
264 ss <<
"value '" << actual <<
"' is not less than or equals to the expected '" << expected <<
"'";
265 throw TestFailed(ss.str());
269template<
typename A,
typename E>
270void assert_greater(
const A& actual,
const E& expected)
272 if (actual > expected)
return;
273 std::stringstream ss;
274 ss <<
"value '" << actual <<
"' is not greater than the expected '" << expected <<
"'";
275 throw TestFailed(ss.str());
279template<
typename A,
typename E>
280void assert_greater_equal(
const A& actual,
const E& expected)
282 if (actual >= expected)
return;
283 std::stringstream ss;
284 ss <<
"value '" << actual <<
"' is not greater than or equals to the expected '" << expected <<
"'";
285 throw TestFailed(ss.str());
289void assert_startswith(
const std::string& actual,
const std::string& expected);
292void assert_endswith(
const std::string& actual,
const std::string& expected);
295void assert_contains(
const std::string& actual,
const std::string& expected);
298void assert_not_contains(
const std::string& actual,
const std::string& expected);
306void assert_re_matches(
const std::string& actual,
const std::string& expected);
314void assert_not_re_matches(
const std::string& actual,
const std::string& expected);
321 Actual(
const A& actual) : _actual(actual) {}
324 void istrue()
const { assert_true(_actual); }
325 void isfalse()
const { assert_false(_actual); }
326 template<
typename E>
void operator==(
const E& expected)
const { assert_equal(_actual, expected); }
327 template<
typename E>
void operator!=(
const E& expected)
const { assert_not_equal(_actual, expected); }
328 template<
typename E>
void operator<(
const E& expected)
const {
return assert_less(_actual, expected); }
329 template<
typename E>
void operator<=(
const E& expected)
const {
return assert_less_equal(_actual, expected); }
330 template<
typename E>
void operator>(
const E& expected)
const {
return assert_greater(_actual, expected); }
331 template<
typename E>
void operator>=(
const E& expected)
const {
return assert_greater_equal(_actual, expected); }
339 void istrue()
const {
return assert_true(_actual); }
340 void isfalse()
const {
return assert_false(_actual); }
341 void operator==(
const char* expected)
const;
342 void operator==(
const std::string& expected)
const;
343 void operator!=(
const char* expected)
const;
344 void operator!=(
const std::string& expected)
const;
345 void operator<(
const std::string& expected)
const;
346 void operator<=(
const std::string& expected)
const;
347 void operator>(
const std::string& expected)
const;
348 void operator>=(
const std::string& expected)
const;
349 void startswith(
const std::string& expected)
const;
350 void endswith(
const std::string& expected)
const;
351 void contains(
const std::string& expected)
const;
352 void not_contains(
const std::string& expected)
const;
353 void matches(
const std::string& re)
const;
354 void not_matches(
const std::string& re)
const;
361 using Actual<std::string>::operator==;
362 void operator==(
const std::vector<uint8_t>& expected)
const;
363 using Actual<std::string>::operator!=;
364 void operator!=(
const std::vector<uint8_t>& expected)
const;
365 void startswith(
const std::string& expected)
const;
366 void endswith(
const std::string& expected)
const;
367 void contains(
const std::string& expected)
const;
368 void not_contains(
const std::string& expected)
const;
369 void matches(
const std::string& re)
const;
370 void not_matches(
const std::string& re)
const;
375 using Actual::Actual;
377 void almost_equal(
double expected,
unsigned places)
const;
378 void not_almost_equal(
double expected,
unsigned places)
const;
383inline ActualCString actual(
const char* actual) {
return ActualCString(actual); }
384inline ActualCString actual(
char* actual) {
return ActualCString(actual); }
385inline ActualStdString actual(
const std::string& actual) {
return ActualStdString(actual); }
386inline ActualStdString actual(
const std::vector<uint8_t>& actual) {
return ActualStdString(std::string(actual.begin(), actual.end())); }
387inline ActualDouble actual(
double actual) {
return ActualDouble(actual); }
391 using Actual::Actual;
393 void throws(
const std::string& what_match)
const;
400 using Actual::Actual;
403 void not_exists()
const;
404 void startswith(
const std::string& data)
const;
406 void not_empty()
const;
407 void contents_equal(
const std::string& data)
const;
408 void contents_equal(
const std::vector<uint8_t>& data)
const;
409 void contents_equal(
const std::initializer_list<std::string>& lines)
const;
410 void contents_match(
const std::string& data_re)
const;
411 void contents_match(
const std::initializer_list<std::string>& lines_re)
const;
423#define wassert(...) \
426 } catch (wreport::tests::TestFailed& e) { \
427 e.add_stack_info(__FILE__, __LINE__, #__VA_ARGS__, wreport_test_location_info); \
429 } catch (std::exception& e) { \
430 throw wreport::tests::TestFailed(e, __FILE__, __LINE__, #__VA_ARGS__, wreport_test_location_info); \
434#define wassert_true(...) wassert(actual(__VA_ARGS__).istrue())
437#define wassert_false(...) wassert(actual(__VA_ARGS__).isfalse())
444#define wassert_throws(exc, ...) \
447 wfail_test(#__VA_ARGS__ " did not throw " #exc); \
448 } catch (TestFailed& e) { \
452 } catch (std::exception& e) { \
453 std::string msg(#__VA_ARGS__ " did not throw " #exc " but threw "); \
454 msg += typeid(e).name(); \
466#define wcallchecked(func) \
469 } catch (wreport::tests::TestFailed& e) { \
470 e.add_stack_info(__FILE__, __LINE__, #func, wreport_test_location_info); \
472 } catch (std::exception& e) { \
473 throw wreport::tests::TestFailed(e, __FILE__, __LINE__, #func, wreport_test_location_info); \
479#define wfail_test(msg) wassert(throw wreport::tests::TestFailed((msg)))
482struct TestController;
484struct TestCaseResult;
486struct TestMethodResult;
604 template<
typename ...Args>
614 template<
typename ...Args>
640 void test_teardown() {}
643template<
typename Fixture,
typename... Args>
644static inline Fixture* fixture_factory(Args... args)
652template<
typename FIXTURE>
656 typedef FIXTURE Fixture;
658 Fixture* fixture =
nullptr;
659 std::function<Fixture*()> make_fixture;
661 template<
typename... Args>
665 make_fixture = std::bind(fixture_factory<FIXTURE, Args...>, args...);
671 fixture = make_fixture();
684 if (fixture) fixture->test_setup();
689 if (fixture) fixture->test_teardown();
697 template<
typename ...Args>
707 template<
typename ...Args>
Test case that includes a fixture.
Definition utils/tests.h:654
void setup() override
Set up the test case before it is run.
Definition utils/tests.h:668
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:698
void method_teardown(TestMethodResult &mr) override
Clean up after the test method is run.
Definition utils/tests.h:687
void teardown() override
Clean up after the test case is run.
Definition utils/tests.h:674
void method_setup(TestMethodResult &mr) override
Set up before the test method is run.
Definition utils/tests.h:681
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:708
String functions.
Definition benchmark.h:13
Definition utils/tests.h:335
Definition utils/tests.h:374
Definition utils/tests.h:399
Definition utils/tests.h:390
Definition utils/tests.h:358
Definition utils/tests.h:319
Base class for test fixtures.
Definition utils/tests.h:635
Add information to the test backtrace for the tests run in the current scope.
Definition utils/tests.h:55
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:97
Test case collecting several test methods, and self-registering with the singleton instance of TestRe...
Definition utils/tests.h:520
virtual TestCaseResult run_tests(TestController &controller)
Call setup(), run all the tests that have been registered, then call teardown().
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:525
virtual void setup()
Set up the test case before it is run.
Definition utils/tests.h:551
virtual void method_teardown(TestMethodResult &)
Clean up after the test method is run.
Definition utils/tests.h:566
virtual void method_setup(TestMethodResult &)
Set up before the test method is run.
Definition utils/tests.h:561
std::string name
Name of the test case.
Definition utils/tests.h:522
bool tests_registered
Set to true the first time register_tests_once is run.
Definition utils/tests.h:528
void register_tests_once()
Idempotent wrapper for register_tests()
virtual void teardown()
Clean up after the test case is run.
Definition utils/tests.h:556
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:615
virtual TestMethodResult run_test(TestController &controller, TestMethod &method)
Run a test method.
TestMethod & add_method(const std::string &name, std::function< void()> test_function)
Register a new test method.
Definition utils/tests.h:605
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:595
Abstract interface for the objects that supervise test execution.
Definition testrunner.h:159
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:27
Test method information.
Definition utils/tests.h:493
std::string name
Name of the test method.
Definition utils/tests.h:495
std::function< void()> test_function
Main body of the test method.
Definition utils/tests.h:505
std::string doc
Documentation attached to this test method.
Definition utils/tests.h:498
Exception thrown when a test or a test case needs to be skipped.
Definition utils/tests.h:136
Information about one stack frame in the test execution stack.
Definition utils/tests.h:67
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.