A unit_test consists of serveral tests. More...
#include <unit_test.hpp>
Public Types | |
typedef std::function< void()> | test_func |
A shortcut for the test function serializable. | |
Public Member Functions | |
unit_test (std::string name, std::string caption) | |
The constructor for a test_unit. | |
virtual void | initialize () |
Initializes a test_unit test. | |
virtual void | finalize () |
Cleans up a test_unit test. | |
std::string | name () const |
std::string | caption () const |
bool | execute (bool quiet=false) |
Executes each test method. | |
bool | execute (const std::string &test, bool quiet=false) |
Executes a concrete test method. | |
void | list (std::ostream &out) const |
List all tests. | |
void | add_test (const std::string &name, const test_func &test, const std::string &caption) |
Adds a test to the test_unit. | |
template<class X > | |
void | assert_equal (const X &a, const X &b, int line, const char *file) |
Checks if a is equal b. | |
template<class X , class Y > | |
void | assert_equal (const X &a, const Y &b, int line, const char *file) |
Checks if a is equal b. | |
template<class T > | |
void | assert_not_equal (const T &a, const T &b, int line, const char *file) |
Checks if a is not equal b. | |
template<class T , class V > | |
void | assert_not_equal (const T &a, const V &b, int line, const char *file) |
Checks if a is not equal b. | |
template<class T > | |
void | assert_greater (const T &a, const T &b, int line, const char *file) |
Checks if a is greater b. | |
template<class T > | |
void | assert_less (const T &a, const T &b, int line, const char *file) |
Checks if a is less b. | |
template<class T > | |
void | assert_null (const T *a, int line, const char *file) |
Checks if the pointer to a is NULL. | |
template<class T > | |
void | assert_not_null (const T *a, int line, const char *file) |
Checks if the pointer to a is not NULL. | |
void | assert_true (bool a, int line, const char *file) |
Checks if a evaluates to true. | |
void | assert_false (bool a, int line, const char *file) |
Checks if a evaluates to false. | |
void | expect_true (bool a, int line, const char *file) |
Checks if a evaluates to true. | |
void | expect_false (bool a, int line, const char *file) |
Checks if a evaluates to false. | |
template<class X > | |
void | expect_equal (const X &a, const X &b, int line, const char *file) |
Checks if a is equal b. | |
template<class X , class Y > | |
void | expect_equal (const X &a, const Y &b, int line, const char *file) |
Checks if a is equal b. | |
template<class T > | |
void | expect_greater (const T &a, const T &b, int line, const char *file) |
Checks if a is greater b. | |
void | error (const std::string &msg, int line, const char *file) |
Throws an error. | |
void | warn (const std::string &msg, int line, const char *file) |
Displays a warning. | |
void | info (const std::string &msg) |
Displays an information. | |
Friends | |
class | test_suite |
A unit_test consists of serveral tests.
The unit_test class is the base class for all user defined test units. The user derives own test unit classes with several test methods. These methods must be added to the test_unit class with the contructor. With a concrete test_unit serializable all or one test method can be executed.
typedef std::function<void ()> matador::unit_test::test_func |
A shortcut for the test function serializable.
A shortcut for the test function serializable.
matador::unit_test::unit_test | ( | std::string | name, |
std::string | caption | ||
) |
The constructor for a test_unit.
name | The name of a test_unit serializable. |
caption | The caption of a test_unit serializable. |
void matador::unit_test::add_test | ( | const std::string & | name, |
const test_func & | test, | ||
const std::string & | caption | ||
) |
Adds a test to the test_unit.
This method adds a new test function identified by the given name to the test_unit. Additionaly a short description must be given.
name | unique name of the test to store. |
test | The test function serializable. |
caption | A short description of the test. |
|
inline |
Checks if a is equal b.
If a is not equal b the test method throws a unit_exception with the given message. The exception is caught by the test_suite and the message is displayed.
X | The type of the left and right hand serializable to compare. |
a | The left hand operand. |
b | The right hand operand. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
|
inline |
Checks if a is equal b.
If a is not equal b the test method throws a unit_exception with the given message. The exception is caught by the test_suite and the message is displayed.
X | The type of the left hand serializable to compare. |
Y | The type of the right hand serializable to compare. |
a | The left hand operand. |
b | The right hand operand. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
void matador::unit_test::assert_false | ( | bool | a, |
int | line, | ||
const char * | file | ||
) |
Checks if a evaluates to false.
If a doesn't evaluates to false the test method throws a unit_exception with the given message. The exception is caught by the test_suite and the message is displayed
T | The type of the objects to compare. |
a | The value to evaluate. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
|
inline |
Checks if a is greater b.
If a is not greater b the test method throws a unit_exception with the given message. The exception is caught by the test_suite and the message is displayed.
T | The type of the objects to compare. |
a | The left hand operand. |
b | The right hand operand. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
|
inline |
Checks if a is less b.
If a is greater equal b the test method throws a unit_exception with the given message. The exception is caught by the test_suite and the message is displayed.
T | The type of the objects to compare. |
a | The left hand operand. |
b | The right hand operand. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
|
inline |
Checks if a is not equal b.
If a is equal b the test method throws a unit_exception with the given message. The exception is caught by the test_suite and the message is displayed
T | The type of the objects to compare. |
a | The left hand operand. |
b | The right hand operand. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
|
inline |
Checks if a is not equal b.
If a is equal b the test method throws a unit_exception with the given message. The exception is caught by the test_suite and the message is displayed
T | The type of the left hand object to compare. |
V | The type of the right hand object to compare. |
a | The left hand operand. |
b | The right hand operand. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
|
inline |
Checks if the pointer to a is not NULL.
If the pointer to is NULL the test method throws a unit_exception with the given message. The exception is caught by the test_suite the and message is displayed
T | The type of the objects to compare. |
a | The value to evaluate. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
|
inline |
Checks if the pointer to a is NULL.
If the pointer to isn't NULL the test method throws a unit_exception with the given message. The exception is caught by the test_suite the and message is displayed
T | The type of the objects to compare. |
a | The value to evaluate. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
void matador::unit_test::assert_true | ( | bool | a, |
int | line, | ||
const char * | file | ||
) |
Checks if a evaluates to true.
If a doesn't evaluates to true the test method throws a unit_exception with the given message. The exception is caught by the test_suite and the message is displayed
T | The type of the objects to compare. |
a | The value to evaluate. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
std::string matador::unit_test::caption | ( | ) | const |
Returns the caption of the test_unit.
void matador::unit_test::error | ( | const std::string & | msg, |
int | line, | ||
const char * | file | ||
) |
Throws an error.
The test method throws an exception with the given error message and the current file and line number information.
msg | The message to print if the check fails. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
bool matador::unit_test::execute | ( | bool | quiet = false | ) |
Executes each test method.
Each test method of the test_unit is executed.
quiet | If true no output is written to stdout |
bool matador::unit_test::execute | ( | const std::string & | test, |
bool | quiet = false |
||
) |
Executes a concrete test method.
A single test method with the given name is executed. If the test method couldn't a warning is displayed.
test | Name of the test to execute. |
quiet | If true no output is written to stdout |
|
inline |
Checks if a is equal b.
If a is not equal b the test method prints the given message to stdout.
X | The type of the left and right hand serializable to compare. |
a | The left hand operand. |
b | The right hand operand. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
|
inline |
Checks if a is equal b.
If a is not equal b the test method prints the given message to stdout.
X | The type of the left hand serializable to compare. |
Y | The type of the right hand serializable to compare. |
a | The left hand operand. |
b | The right hand operand. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
void matador::unit_test::expect_false | ( | bool | a, |
int | line, | ||
const char * | file | ||
) |
Checks if a evaluates to false.
If a doesn't evaluates to false the test method prints the given message. The exception is caught by the test_suite and the message is displayed
T | The type of the objects to compare. |
a | The value to evaluate. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
|
inline |
Checks if a is greater b.
If a is not greater b the test method prints the given message to stdout.
T | The type of the objects to compare. |
a | The left hand operand. |
b | The right hand operand. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
void matador::unit_test::expect_true | ( | bool | a, |
int | line, | ||
const char * | file | ||
) |
Checks if a evaluates to true.
If a doesn't evaluates to true the test method prints the given message.
T | The type of the objects to compare. |
a | The value to evaluate. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |
|
inlinevirtual |
Cleans up a test_unit test.
Called after every executed test method. Can be overwritten by the derived test_unit class.
void matador::unit_test::info | ( | const std::string & | msg | ) |
Displays an information.
The test method displays an information with the given message.
msg | The message to print if the check fails. |
|
inlinevirtual |
Initializes a test_unit test.
Called before each executed test method. Can be overwritten by the derived test_unit class.
void matador::unit_test::list | ( | std::ostream & | out | ) | const |
List all tests.
All tests and their descriptions are printed to the given ostream.
out | The stream to be written on. |
std::string matador::unit_test::name | ( | ) | const |
Returns the name of the test_unit.
void matador::unit_test::warn | ( | const std::string & | msg, |
int | line, | ||
const char * | file | ||
) |
Displays a warning.
The test method displays a warning with the given message and the current file and line number information.
msg | The message to print if the check fails. |
line | The line number of this check in the source code. |
file | The file where this check can be found. |