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)...); }
146 #define WREPORT_TEST_INFO(name) \
147 wreport::tests::LocationInfo wreport_test_location_info; \
148 wreport::tests::LocationInfo& name = wreport_test_location_info
160 void assert_true(
const A& actual)
163 std::stringstream ss;
164 ss <<
"actual value " << actual <<
" is not true";
168 void assert_true(std::nullptr_t actual);
172 void assert_false(
const A& actual)
175 std::stringstream ss;
176 ss <<
"actual value " << actual <<
" is not false";
177 throw TestFailed(ss.str());
180 void assert_false(std::nullptr_t actual);
186 template<
typename A,
typename E>
187 void assert_equal(
const A& actual,
const E& expected)
189 if (actual == expected)
return;
190 std::stringstream ss;
191 ss <<
"value '" << actual <<
"' is different than the expected '" << expected <<
"'";
192 throw TestFailed(ss.str());
199 template<
typename A,
typename E>
200 void assert_not_equal(
const A& actual,
const E& expected)
202 if (actual != expected)
return;
203 std::stringstream ss;
204 ss <<
"value '" << actual <<
"' is not different than the expected '" << expected <<
"'";
205 throw TestFailed(ss.str());
209 template<
typename A,
typename E>
210 void assert_less(
const A& actual,
const E& expected)
212 if (actual < expected)
return;
213 std::stringstream ss;
214 ss <<
"value '" << actual <<
"' is not less than the expected '" << expected <<
"'";
215 throw TestFailed(ss.str());
219 template<
typename A,
typename E>
220 void assert_less_equal(
const A& actual,
const E& expected)
222 if (actual <= expected)
return;
223 std::stringstream ss;
224 ss <<
"value '" << actual <<
"' is not less than or equals to the expected '" << expected <<
"'";
225 throw TestFailed(ss.str());
229 template<
typename A,
typename E>
230 void assert_greater(
const A& actual,
const E& expected)
232 if (actual > expected)
return;
233 std::stringstream ss;
234 ss <<
"value '" << actual <<
"' is not greater than the expected '" << expected <<
"'";
235 throw TestFailed(ss.str());
239 template<
typename A,
typename E>
240 void assert_greater_equal(
const A& actual,
const E& expected)
242 if (actual >= expected)
return;
243 std::stringstream ss;
244 ss <<
"value '" << actual <<
"' is not greater than or equals to the expected '" << expected <<
"'";
245 throw TestFailed(ss.str());
249 void assert_startswith(
const std::string& actual,
const std::string& expected);
252 void assert_endswith(
const std::string& actual,
const std::string& expected);
255 void assert_contains(
const std::string& actual,
const std::string& expected);
258 void assert_not_contains(
const std::string& actual,
const std::string& expected);
266 void assert_re_matches(
const std::string& actual,
const std::string& expected);
274 void assert_not_re_matches(
const std::string& actual,
const std::string& expected);
281 Actual(
const A& actual) : _actual(actual) {}
284 void istrue()
const { assert_true(_actual); }
285 void isfalse()
const { assert_false(_actual); }
286 template<
typename E>
void operator==(
const E& expected)
const { assert_equal(_actual, expected); }
287 template<
typename E>
void operator!=(
const E& expected)
const { assert_not_equal(_actual, expected); }
288 template<
typename E>
void operator<(
const E& expected)
const {
return assert_less(_actual, expected); }
289 template<
typename E>
void operator<=(
const E& expected)
const {
return assert_less_equal(_actual, expected); }
290 template<
typename E>
void operator>(
const E& expected)
const {
return assert_greater(_actual, expected); }
291 template<
typename E>
void operator>=(
const E& expected)
const {
return assert_greater_equal(_actual, expected); }
299 void istrue()
const {
return assert_true(_actual); }
300 void isfalse()
const {
return assert_false(_actual); }
301 void operator==(
const char* expected)
const;
302 void operator==(
const std::string& expected)
const;
303 void operator!=(
const char* expected)
const;
304 void operator!=(
const std::string& expected)
const;
305 void operator<(
const std::string& expected)
const;
306 void operator<=(
const std::string& expected)
const;
307 void operator>(
const std::string& expected)
const;
308 void operator>=(
const std::string& expected)
const;
309 void startswith(
const std::string& expected)
const;
310 void endswith(
const std::string& expected)
const;
311 void contains(
const std::string& expected)
const;
312 void not_contains(
const std::string& expected)
const;
313 void matches(
const std::string& re)
const;
314 void not_matches(
const std::string& re)
const;
322 void operator==(
const std::vector<uint8_t>& expected)
const;
324 void operator!=(
const std::vector<uint8_t>& expected)
const;
325 void startswith(
const std::string& expected)
const;
326 void endswith(
const std::string& expected)
const;
327 void contains(
const std::string& expected)
const;
328 void not_contains(
const std::string& expected)
const;
329 void matches(
const std::string& re)
const;
330 void not_matches(
const std::string& re)
const;
335 using Actual::Actual;
337 void almost_equal(
double expected,
unsigned places)
const;
338 void not_almost_equal(
double expected,
unsigned places)
const;
343 inline ActualCString actual(
const char* actual) {
return ActualCString(actual); }
344 inline ActualCString actual(
char* actual) {
return ActualCString(actual); }
345 inline ActualStdString actual(
const std::string& actual) {
return ActualStdString(actual); }
346 inline ActualStdString actual(
const std::vector<uint8_t>& actual) {
return ActualStdString(std::string(actual.begin(), actual.end())); }
347 inline ActualDouble actual(
double actual) {
return ActualDouble(actual); }
351 using Actual::Actual;
353 void throws(
const std::string& what_match)
const;
360 using Actual::Actual;
363 void not_exists()
const;
364 void startswith(
const std::string& data)
const;
366 void not_empty()
const;
367 void contents_equal(
const std::string& data)
const;
368 void contents_equal(
const std::vector<uint8_t>& data)
const;
369 void contents_equal(
const std::initializer_list<std::string>& lines)
const;
370 void contents_match(
const std::string& data_re)
const;
371 void contents_match(
const std::initializer_list<std::string>& lines_re)
const;
383 #define wassert(...) \
386 } catch (wreport::tests::TestFailed& e) { \
387 e.add_stack_info(__FILE__, __LINE__, #__VA_ARGS__, wreport_test_location_info); \
389 } catch (std::exception& e) { \
390 throw wreport::tests::TestFailed(e, __FILE__, __LINE__, #__VA_ARGS__, wreport_test_location_info); \
394 #define wassert_true(...) wassert(actual(__VA_ARGS__).istrue())
397 #define wassert_false(...) wassert(actual(__VA_ARGS__).isfalse())
404 #define wassert_throws(exc, ...) \
407 wfail_test(#__VA_ARGS__ " did not throw " #exc); \
408 } catch (TestFailed& e) { \
412 } catch (std::exception& e) { \
413 std::string msg(#__VA_ARGS__ " did not throw " #exc " but threw "); \
414 msg += typeid(e).name(); \
426 #define wcallchecked(func) \
429 } catch (wreport::tests::TestFailed& e) { \
430 e.add_stack_info(__FILE__, __LINE__, #func, wreport_test_location_info); \
432 } catch (std::exception& e) { \
433 throw wreport::tests::TestFailed(e, __FILE__, __LINE__, #func, wreport_test_location_info); \
439 #define wfail_test(msg) wassert(throw wreport::tests::TestFailed((msg)))
442 struct TestController;
444 struct TestCaseResult;
446 struct TestMethodResult;
564 template<
typename ...Args>
574 template<
typename ...Args>
600 void test_teardown() {}
603 template<
typename Fixture,
typename... Args>
604 static inline Fixture* fixture_factory(Args... args)
612 template<
typename FIXTURE>
616 typedef FIXTURE Fixture;
618 Fixture* fixture =
nullptr;
619 std::function<Fixture*()> make_fixture;
621 template<
typename... Args>
625 make_fixture = std::bind(fixture_factory<FIXTURE, Args...>, args...);
631 fixture = make_fixture();
644 if (fixture) fixture->test_setup();
649 if (fixture) fixture->test_teardown();
657 template<
typename ...Args>
667 template<
typename ...Args>
Test case that includes a fixture.
Definition: utils/tests.h:614
void setup() override
Set up the test case before it is run.
Definition: utils/tests.h:628
void method_teardown(TestMethodResult &mr) override
Clean up after the test method is run.
Definition: utils/tests.h:647
void teardown() override
Clean up after the test case is run.
Definition: utils/tests.h:634
void method_setup(TestMethodResult &mr) override
Set up before the test method is run.
Definition: utils/tests.h:641
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: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:658
String functions.
Definition: benchmark.h:13
Definition: utils/tests.h:295
Definition: utils/tests.h:334
Definition: utils/tests.h:359
Definition: utils/tests.h:350
Definition: utils/tests.h:318
Definition: utils/tests.h:279
Base class for test fixtures.
Definition: utils/tests.h:595
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:97
Test case collecting several test methods, and self-registering with the singleton instance of TestRe...
Definition: utils/tests.h:480
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:485
TestMethod & add_method(const std::string &name, std::function< void()> test_function)
Register a new test method.
Definition: utils/tests.h:565
virtual void setup()
Set up the test case before it is run.
Definition: utils/tests.h:511
virtual void method_teardown(TestMethodResult &)
Clean up after the test method is run.
Definition: utils/tests.h:526
virtual void method_setup(TestMethodResult &)
Set up before the test method is run.
Definition: utils/tests.h:521
std::string name
Name of the test case.
Definition: utils/tests.h:482
bool tests_registered
Set to true the first time register_tests_once is run.
Definition: utils/tests.h:488
void register_tests_once()
Idempotent wrapper for register_tests()
virtual void teardown()
Clean up after the test case is run.
Definition: utils/tests.h:516
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:555
virtual TestMethodResult run_test(TestController &controller, TestMethod &method)
Run a test method.
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:575
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:103
Result of running a test method.
Definition: testrunner.h:27
Test method information.
Definition: utils/tests.h:453
std::string name
Name of the test method.
Definition: utils/tests.h:455
std::function< void()> test_function
Main body of the test method.
Definition: utils/tests.h:465
std::string doc
Documentation attached to this test method.
Definition: utils/tests.h:458
Exception thrown when a test or a test case needs to be skipped.
Definition: utils/tests.h:135
Information about one stack frame in the test execution stack.
Definition: utils/tests.h:66
Definition: utils/tests.h:88
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.