matador::unit_test Class Reference

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
 

Detailed Description

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.

Member Typedef Documentation

◆ test_func

typedef std::function<void ()> matador::unit_test::test_func

A shortcut for the test function serializable.

A shortcut for the test function serializable.

Constructor & Destructor Documentation

◆ unit_test()

matador::unit_test::unit_test ( std::string  name,
std::string  caption 
)

The constructor for a test_unit.

Parameters
nameThe name of a test_unit serializable.
captionThe caption of a test_unit serializable.

Member Function Documentation

◆ add_test()

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.

Parameters
nameunique name of the test to store.
testThe test function serializable.
captionA short description of the test.

◆ assert_equal() [1/2]

template<class X >
void matador::unit_test::assert_equal ( const X &  a,
const X &  b,
int  line,
const char *  file 
)
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.

Template Parameters
XThe type of the left and right hand serializable to compare.
Parameters
aThe left hand operand.
bThe right hand operand.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ assert_equal() [2/2]

template<class X , class Y >
void matador::unit_test::assert_equal ( const X &  a,
const Y &  b,
int  line,
const char *  file 
)
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.

Template Parameters
XThe type of the left hand serializable to compare.
YThe type of the right hand serializable to compare.
Parameters
aThe left hand operand.
bThe right hand operand.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ assert_false()

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

Template Parameters
TThe type of the objects to compare.
Parameters
aThe value to evaluate.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ assert_greater()

template<class T >
void matador::unit_test::assert_greater ( const T &  a,
const T &  b,
int  line,
const char *  file 
)
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.

Template Parameters
TThe type of the objects to compare.
Parameters
aThe left hand operand.
bThe right hand operand.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ assert_less()

template<class T >
void matador::unit_test::assert_less ( const T &  a,
const T &  b,
int  line,
const char *  file 
)
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.

Template Parameters
TThe type of the objects to compare.
Parameters
aThe left hand operand.
bThe right hand operand.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ assert_not_equal() [1/2]

template<class T >
void matador::unit_test::assert_not_equal ( const T &  a,
const T &  b,
int  line,
const char *  file 
)
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

Template Parameters
TThe type of the objects to compare.
Parameters
aThe left hand operand.
bThe right hand operand.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ assert_not_equal() [2/2]

template<class T , class V >
void matador::unit_test::assert_not_equal ( const T &  a,
const V &  b,
int  line,
const char *  file 
)
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

Template Parameters
TThe type of the left hand object to compare.
VThe type of the right hand object to compare.
Parameters
aThe left hand operand.
bThe right hand operand.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ assert_not_null()

template<class T >
void matador::unit_test::assert_not_null ( const T *  a,
int  line,
const char *  file 
)
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

Template Parameters
TThe type of the objects to compare.
Parameters
aThe value to evaluate.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ assert_null()

template<class T >
void matador::unit_test::assert_null ( const T *  a,
int  line,
const char *  file 
)
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

Template Parameters
TThe type of the objects to compare.
Parameters
aThe value to evaluate.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ assert_true()

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

Template Parameters
TThe type of the objects to compare.
Parameters
aThe value to evaluate.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ caption()

std::string matador::unit_test::caption ( ) const

Returns the caption of the test_unit.

Returns
The caption of the test_unit.

◆ error()

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.

Parameters
msgThe message to print if the check fails.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ execute() [1/2]

bool matador::unit_test::execute ( bool  quiet = false)

Executes each test method.

Each test method of the test_unit is executed.

Parameters
quietIf true no output is written to stdout

◆ execute() [2/2]

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.

Parameters
testName of the test to execute.
quietIf true no output is written to stdout

◆ expect_equal() [1/2]

template<class X >
void matador::unit_test::expect_equal ( const X &  a,
const X &  b,
int  line,
const char *  file 
)
inline

Checks if a is equal b.

If a is not equal b the test method prints the given message to stdout.

Template Parameters
XThe type of the left and right hand serializable to compare.
Parameters
aThe left hand operand.
bThe right hand operand.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ expect_equal() [2/2]

template<class X , class Y >
void matador::unit_test::expect_equal ( const X &  a,
const Y &  b,
int  line,
const char *  file 
)
inline

Checks if a is equal b.

If a is not equal b the test method prints the given message to stdout.

Template Parameters
XThe type of the left hand serializable to compare.
YThe type of the right hand serializable to compare.
Parameters
aThe left hand operand.
bThe right hand operand.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ expect_false()

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

Template Parameters
TThe type of the objects to compare.
Parameters
aThe value to evaluate.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ expect_greater()

template<class T >
void matador::unit_test::expect_greater ( const T &  a,
const T &  b,
int  line,
const char *  file 
)
inline

Checks if a is greater b.

If a is not greater b the test method prints the given message to stdout.

Template Parameters
TThe type of the objects to compare.
Parameters
aThe left hand operand.
bThe right hand operand.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ expect_true()

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.

Template Parameters
TThe type of the objects to compare.
Parameters
aThe value to evaluate.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

◆ finalize()

virtual void matador::unit_test::finalize ( )
inlinevirtual

Cleans up a test_unit test.

Called after every executed test method. Can be overwritten by the derived test_unit class.

◆ info()

void matador::unit_test::info ( const std::string &  msg)

Displays an information.

The test method displays an information with the given message.

Parameters
msgThe message to print if the check fails.

◆ initialize()

virtual void matador::unit_test::initialize ( )
inlinevirtual

Initializes a test_unit test.

Called before each executed test method. Can be overwritten by the derived test_unit class.

◆ list()

void matador::unit_test::list ( std::ostream &  out) const

List all tests.

All tests and their descriptions are printed to the given ostream.

Parameters
outThe stream to be written on.

◆ name()

std::string matador::unit_test::name ( ) const

Returns the name of the test_unit.

Returns
The name of the test_unit.

◆ warn()

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.

Parameters
msgThe message to print if the check fails.
lineThe line number of this check in the source code.
fileThe file where this check can be found.

The documentation for this class was generated from the following file: