Go to the documentation of this file.
82 std::string
format()
const;
84 void format(std::ostream& out)
const;
87 struct TestStack :
public std::vector<TestStackFrame>
109 template<
typename ...Args>
118 template<
typename ...Args>
125 const char*
what() const noexcept
override {
return message.c_str(); }
127 template<
typename ...Args>
135 #define EPT_TEST_INFO(name) \
136 ept::tests::LocationInfo ept_test_location_info; \
137 ept::tests::LocationInfo& name = ept_test_location_info
145 std::stringstream ss;
146 ss <<
"actual value " <<
actual <<
" is not true";
157 std::stringstream ss;
158 ss <<
"actual value " <<
actual <<
" is not false";
168 template<
typename A,
typename E>
171 if (
actual == expected)
return;
172 std::stringstream ss;
173 ss <<
"value '" <<
actual <<
"' is different than the expected '" << expected <<
"'";
181 template<
typename A,
typename E>
184 if (
actual != expected)
return;
185 std::stringstream ss;
186 ss <<
"value '" <<
actual <<
"' is not different than the expected '" << expected <<
"'";
191 template<
typename A,
typename E>
194 if (
actual < expected)
return;
195 std::stringstream ss;
196 ss <<
"value '" <<
actual <<
"' is not less than the expected '" << expected <<
"'";
201 template<
typename A,
typename E>
204 if (
actual <= expected)
return;
205 std::stringstream ss;
206 ss <<
"value '" <<
actual <<
"' is not less than or equals to the expected '" << expected <<
"'";
211 template<
typename A,
typename E>
214 if (
actual > expected)
return;
215 std::stringstream ss;
216 ss <<
"value '" <<
actual <<
"' is not greater than the expected '" << expected <<
"'";
221 template<
typename A,
typename E>
224 if (
actual >= expected)
return;
225 std::stringstream ss;
226 ss <<
"value '" <<
actual <<
"' is not greater than or equals to the expected '" << expected <<
"'";
284 void operator==(
const std::string& expected)
const;
286 void operator!=(
const std::string& expected)
const;
287 void operator<(
const std::string& expected)
const;
288 void operator<=(
const std::string& expected)
const;
289 void operator>(
const std::string& expected)
const;
290 void operator>=(
const std::string& expected)
const;
291 void startswith(
const std::string& expected)
const;
292 void endswith(
const std::string& expected)
const;
293 void contains(
const std::string& expected)
const;
295 void matches(
const std::string& re)
const;
303 void startswith(
const std::string& expected)
const;
304 void endswith(
const std::string& expected)
const;
305 void contains(
const std::string& expected)
const;
307 void matches(
const std::string& re)
const;
315 void almost_equal(
double expected,
unsigned places)
const;
330 void throws(
const std::string& what_match)
const;
343 #define wassert(...) \
346 } catch (TestFailed& e) { \
347 e.add_stack_info(__FILE__, __LINE__, #__VA_ARGS__, ept_test_location_info); \
349 } catch (std::exception& e) { \
350 throw TestFailed(e, __FILE__, __LINE__, #__VA_ARGS__, ept_test_location_info); \
354 #define wassert_true(...) wassert(actual(__VA_ARGS__).istrue())
357 #define wassert_false(...) wassert(actual(__VA_ARGS__).isfalse())
366 #define wcallchecked(func) \
369 } catch (TestFailed& e) { \
370 e.add_stack_info(__FILE__, __LINE__, #func, ept_test_location_info); \
372 } catch (std::exception& e) { \
373 throw TestFailed(e, __FILE__, __LINE__, #func, ept_test_location_info); \
418 error_message =
"test threw an exception with an empty error message";
468 fail_setup =
"test case setup method threw an unknown exception";
473 fail_setup =
"test case setup method threw an exception: ";
479 fail_teardown =
"test case teardown method threw an unknown exception";
484 fail_teardown =
"test case teardown method threw an exception: ";
490 methods.emplace_back(std::move(e));
504 struct TestCaseResult;
506 struct TestMethodResult;
684 template<
typename ...Args>
693 template<
typename ...Args>
694 void add_method(
const std::string&
name, std::function<
void()> test_function, Args&&... args)
696 methods.emplace_back(
name, test_function, std::forward<Args>(args)...);
704 template<
typename FUNC,
typename ...Args>
707 methods.emplace_back(
name, [test_function, args...]() { test_function(args...); });
736 template<
typename FIXTURE>
744 template<
typename... Args>
782 template<
typename FUNC,
typename ...Args>
785 methods.emplace_back(
name, [
this, test_function, args...] { test_function(*fixture, args...); });
793 std::function<void()> test_func;
797 virtual void add_tests() {}
void not_contains(const std::string &expected) const
Definition: tests.cc:325
void set_unknown_exception()
Definition: tests.h:422
TestFailed(const std::exception &e, Args &&... args)
Definition: tests.h:110
void operator<(const E &expected) const
Definition: tests.h:270
bool test_case_begin(const TestCase &test_case, const TestCaseResult &test_case_result) override
Called before running a test case.
Definition: tests.cc:536
const char * _actual
Definition: tests.h:278
void assert_not_contains(const std::string &actual, const std::string &expected)
Ensure that the string actual does not contain expected.
Definition: tests.cc:132
std::ostream & operator()()
Clear the current information and return the output stream to which new information can be sent.
Definition: tests.cc:97
void assert_false(std::nullptr_t actual)
Definition: tests.cc:199
std::function< Fixture *()> make_fixture
Definition: tests.h:742
bool skipped
Set to true if this test case has been skipped.
Definition: tests.h:462
Add information to the test backtrace for the tests run in the current scope.
Definition: tests.h:53
void startswith(const std::string &expected) const
Definition: tests.cc:286
void operator!=(const char *expected) const
Definition: tests.cc:236
void signal_to_exception(int)
Definition: tests-main.cc:7
LocationInfo()
Definition: tests.h:55
Information about one stack frame in the test execution stack.
Definition: tests.h:65
std::string test_case
Name of the test case.
Definition: tests.h:453
bool is_success() const
Definition: tests.h:493
virtual bool test_case_begin(const TestCase &test_case, const TestCaseResult &test_case_result)
Called before running a test case.
Definition: tests.h:523
Abstract interface for the objects that supervise test execution.
Definition: tests.h:514
std::function< void()> test_function
Main body of the test method.
Definition: tests.h:603
std::string format() const
Definition: tests.cc:29
void throws(const std::string &what_match) const
Definition: tests.cc:358
void isfalse() const
Definition: tests.h:267
void add_stack_info(Args &&... args)
Definition: tests.h:128
void startswith(const std::string &expected) const
Definition: tests.cc:310
void matches(const std::string &re) const
Definition: tests.cc:330
const ept::tests::LocationInfo ept_test_location_info
Definition: tests.cc:20
void not_matches(const std::string &re) const
Definition: tests.cc:280
void operator<(const std::string &expected) const
Definition: tests.cc:250
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.
Definition: tests.cc:185
Simple default implementation of TestController.
Definition: tests.h:549
TestStack error_stack
Stack frame of where the error happened.
Definition: tests.h:394
virtual void test_teardown()
Definition: tests.h:730
virtual void register_tests()=0
This will be called before running the test case, to populate it with its test methods.
FIXTURE Fixture
Definition: tests.h:739
void operator!=(const E &expected) const
Definition: tests.h:269
void operator>=(const E &expected) const
Definition: tests.h:273
void set_teardown_exception(std::exception &e)
Definition: tests.h:434
void istrue() const
Definition: tests.h:266
void set_teardown_failed()
Definition: tests.h:477
ActualStdString(const std::string &s)
Definition: tests.h:301
void add_method(const std::string &name, std::function< void()> test_function)
Register a new test method.
Definition: tests.h:685
static TestRegistry & get()
Get the singleton instance of TestRegistry.
Definition: tests.cc:420
const char * what() const noexcept override
Definition: tests.h:125
void set_setup_failed()
Definition: tests.h:466
std::string encode_cstring(const std::string &str)
Escape the string so it can safely used as a C string inside double quotes.
Definition: string.cc:253
std::string exception_typeid
If non-empty, the test raised an exception and this is its type ID.
Definition: tests.h:397
void assert_re_matches(const std::string &actual, const std::string &expected)
Ensure that the string actual matches the extended regular expression expected.
Definition: tests.cc:176
TestFailed(const std::string &message, Args &&... args)
Definition: tests.h:119
bool is_success() const
Definition: tests.h:441
Result of running a test method.
Definition: tests.h:382
virtual bool test_method_begin(const TestMethod &test_method, const TestMethodResult &test_method_result)
Called before running a test method.
Definition: tests.h:535
void test_case_end(const TestCase &test_case, const TestCaseResult &test_case_result) override
Called after running a test case.
Definition: tests.cc:549
TestMethodResult(const std::string &test_case, const std::string &test_method)
Definition: tests.h:403
void set_teardown_failed(std::exception &e)
Definition: tests.h:482
bool skipped
True if the test has been skipped.
Definition: tests.h:400
void operator<=(const E &expected) const
Definition: tests.h:271
void almost_equal(double expected, unsigned places) const
Definition: tests.cc:340
TestStackFrame(const char *file, int line, const char *call, const LocationInfo &local_info)
Definition: tests.h:77
virtual void test_setup()
Definition: tests.h:727
std::string fail_teardown
Set to a non-empty string if the teardown method of the test case failed.
Definition: tests.h:460
void register_test_case(TestCase &test_case)
Register a new test case.
Definition: tests.cc:428
void contains(const std::string &expected) const
Definition: tests.cc:298
std::string fail_setup
Set to a non-empty string if the setup method of the test case failed.
Definition: tests.h:457
void operator==(const char *expected) const
Definition: tests.cc:210
void set_failed(TestFailed &e)
Definition: tests.h:406
void test_method_end(const TestMethod &test_method, const TestMethodResult &test_method_result) override
Called after running a test method.
Definition: tests.cc:566
void method_teardown(TestMethodResult &mr) override
Clean up after the test method is run.
Definition: tests.h:770
String functions.
Definition: apt.cc:40
set< string > & res
Definition: packagerecord.cc:73
std::string blacklist
Any method matching this glob expression will not be run.
Definition: tests.h:555
std::string error_message
If non-empty, the test failed with this error.
Definition: tests.h:391
std::string whitelist
Any method not matching this glob expression will not be run.
Definition: tests.h:552
void isfalse() const
Definition: tests.h:282
std::vector< TestMethodResult > methods
Outcome of all the methods that have been run.
Definition: tests.h:455
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: tests.h:182
void set_setup_failed(std::exception &e)
Definition: tests.h:471
bool exists(const std::string &file)
Same as access(s, F_OK);.
Definition: sys.cc:148
Base class for test fixtures.
Definition: tests.h:722
const char * file
Definition: tests.h:67
const char * call
Definition: tests.h:69
Test method information.
Definition: tests.h:597
const ept::tests::LocationInfo ept_test_location_info
Definition: tests.cc:20
void add_method(const std::string &name, FUNC test_function, Args &&... args)
Register a new test metheod, with arguments.
Definition: tests.h:705
struct ept::apt::AptImplementation tests
Actual< A > actual(const A &actual)
Definition: tests.h:320
virtual TestCaseResult run_tests(TestController &controller)
Call setup(), run all the tests that have been registered, then call teardown().
Definition: tests.cc:445
void assert_greater(const A &actual, const E &expected)
Ensure that the actual value is greater than the reference value.
Definition: tests.h:212
std::string test_case
Name of the test case.
Definition: tests.h:385
std::string _actual
Definition: tests.h:262
Fixture * fixture
Definition: tests.h:741
void add_method(const std::string &name, std::function< void()> test_function, Args &&... args)
Register a new test method.
Definition: tests.h:694
void endswith(const std::string &expected) const
Definition: tests.cc:315
bool test_method_should_run(const std::string &fullname) const
Definition: tests.cc:525
TestFailed(const std::string &message)
Definition: tests.h:116
virtual void test_method_end(const TestMethod &test_method, const TestMethodResult &test_method_result)
Called after running a test method.
Definition: tests.h:540
virtual ~Fixture()
Definition: tests.h:724
std::string local_info
Definition: tests.h:70
void endswith(const std::string &expected) const
Definition: tests.cc:292
void assert_contains(const std::string &actual, const std::string &expected)
Ensure that the string actual contains expected.
Definition: tests.cc:124
TestMethod(const std::string &name, std::function< void()> test_function)
Definition: tests.h:605
Test case collecting several test methods, and self-registering with the singleton instance of TestRe...
Definition: tests.h:614
TestFailed(const std::exception &e)
Definition: tests.cc:67
bool endswith(const std::string &str, const std::string &part)
Check if a string ends with the given substring.
Definition: string.h:28
void assert_endswith(const std::string &actual, const std::string &expected)
Ensure that the string actual ends with expected.
Definition: tests.cc:116
void add_method(const std::string &name, FUNC test_function, Args &&... args)
Add a method that takes a reference to the fixture as argument.
Definition: tests.h:783
void assert_less_equal(const A &actual, const E &expected)
Ensure that the actual value is less or equal than the reference value.
Definition: tests.h:202
virtual ~TestController()
Definition: tests.h:516
void operator>=(const std::string &expected) const
Definition: tests.cc:268
void assert_greater_equal(const A &actual, const E &expected)
Ensure that the actual value is greather or equal than the reference value.
Definition: tests.h:222
void not_contains(const std::string &expected) const
Definition: tests.cc:304
size_t size(const std::string &file)
File size.
Definition: sys.cc:116
bool test_method_begin(const TestMethod &test_method, const TestMethodResult &test_method_result) override
Called before running a test method.
Definition: tests.cc:560
void operator<=(const std::string &expected) const
Definition: tests.cc:256
TestStack stack
Definition: tests.h:105
ActualFunction actual_function(std::function< void()> actual)
Definition: tests.h:333
int main(int argc, const char *argv[])
Definition: tests-main.cc:12
std::string name
Name of the test method.
Definition: tests.h:600
bool startswith(const std::string &str, const std::string &part)
Check if a string starts with the given substring.
Definition: string.h:20
void method_setup(TestMethodResult &mr) override
Set up before the test method is run.
Definition: tests.h:764
void set_exception(std::exception &e)
Definition: tests.h:414
~Actual()
Definition: tests.h:264
std::string backtrace() const
Return the formatted backtrace for this location.
Definition: tests.cc:55
void not_matches(const std::string &re) const
Definition: tests.cc:335
void add_test_method(TestMethodResult &&e)
Definition: tests.h:488
TestStackFrame(const char *file, int line, const char *call)
Definition: tests.h:72
void teardown() override
Clean up after the test case is run.
Definition: tests.h:757
std::string message
Definition: tests.h:104
Definition: packagerecord-test.cc:4
void set_setup_exception(std::exception &e)
Definition: tests.h:427
void operator==(const E &expected) const
Definition: tests.h:268
ActualCString(const char *s)
Definition: tests.h:279
virtual void method_teardown(TestMethodResult &)
Clean up after the test method is run.
Definition: tests.h:656
void assert_equal(const A &actual, const E &expected)
Test function that ensures that the actual value is the same as a reference one.
Definition: tests.h:169
void matches(const std::string &re) const
Definition: tests.cc:274
std::vector< TestCase * > entries
All known test cases.
Definition: tests.h:575
virtual void test_case_end(const TestCase &test_case, const TestCaseResult &test_case_result)
Called after running a test case.
Definition: tests.h:528
Actual(const A &actual)
Definition: tests.h:263
virtual ~TestCase()
Definition: tests.h:627
std::vector< TestMethod > methods
All registered test methods.
Definition: tests.h:620
void istrue() const
Definition: tests.h:281
TestCase(const std::string &name)
Definition: tests.h:622
Result of running a whole test case.
Definition: tests.h:450
void setup() override
Set up the test case before it is run.
Definition: tests.h:751
TestCaseResult(const std::string &test_case)
Definition: tests.h:464
int line
Definition: tests.h:68
void not_almost_equal(double expected, unsigned places) const
Definition: tests.cc:349
void assert_true(std::nullptr_t actual)
Definition: tests.cc:194
#define wassert(...)
Run the given command, raising TestFailed with the appropriate backtrace information if it threw an e...
Definition: tests.h:343
Exception raised when a test assertion fails, normally by Location::fail_test.
Definition: tests.h:102
Test registry.
Definition: tests.h:572
std::string name
Name of the test case.
Definition: tests.h:617
void assert_startswith(const std::string &actual, const std::string &expected)
Ensure that the string actual starts with expected.
Definition: tests.cc:108
virtual void setup()
Set up the test case before it is run.
Definition: tests.h:641
FixtureTestCase(const std::string &name, Args... args)
Definition: tests.h:745
void operator>(const std::string &expected) const
Definition: tests.cc:262
regex_t compiled
Definition: tests.cc:144
virtual TestMethodResult run_test(TestController &controller, TestMethod &method)
Run a test method.
Definition: tests.cc:480
std::string test_method
Name of the test method.
Definition: tests.h:388
virtual void teardown()
Clean up after the test case is run.
Definition: tests.h:646
void contains(const std::string &expected) const
Definition: tests.cc:320
virtual void method_setup(TestMethodResult &)
Set up before the test method is run.
Definition: tests.h:651
Test case that includes a fixture.
Definition: tests.h:737
void operator>(const E &expected) const
Definition: tests.h:272
void assert_less(const A &actual, const E &expected)
Ensure that the actual value is less than the reference value.
Definition: tests.h:192
std::vector< TestCaseResult > run_tests(TestController &controller)
Run all the registered tests using the given controller.
Definition: tests.cc:433