oos::unit_test Class Referenceabstract

A unit_test consists of serveral tests. More...

#include <unit_test.hpp>

Public Types

typedef std::tr1::function< void()> test_func
 A shortcut for the test function object. More...
 

Public Member Functions

 unit_test (const std::string &name, const std::string &caption)
 The constructor for a test_unit. More...
 
virtual void initialize ()=0
 Initializes a test_unit test. More...
 
virtual void finalize ()=0
 Cleans up a test_unit test. More...
 
std::string name () const
 
std::string caption () const
 
void execute ()
 Executes each test method. More...
 
void execute (const std::string &test)
 Executes a concrete test method. More...
 
void list (std::ostream &out)
 List all tests. More...
 
void add_test (const std::string &name, const test_func &test, const std::string &caption)
 Adds a test to the test_unit. More...
 
template<class X , class Y >
void assert_equal (const X &a, const Y &b, const std::string &msg, int line, const char *file)
 Checks if a is equal b. More...
 
template<class T >
void assert_not_equal (const T &a, const T &b, const std::string &msg, int line, const char *file)
 Checks if a is not equal b. More...
 
template<class T >
void assert_greater (const T &a, const T &b, const std::string &msg, int line, const char *file)
 Checks if a is greater b. More...
 
template<class T >
void assert_less (const T &a, const T &b, const std::string &msg, int line, const char *file)
 Checks if a is less b. More...
 
template<class T >
void assert_null (const T *a, const std::string &msg, int line, const char *file)
 Checks if the pointer to a is NULL. More...
 
template<class T >
void assert_not_null (const T *a, const std::string &msg, int line, const char *file)
 Checks if the pointer to a is not NULL. More...
 
void assert_true (bool a, const std::string &msg, int line, const char *file)
 Checks if a evaluates to true. More...
 
void assert_false (bool a, const std::string &msg, int line, const char *file)
 Checks if a evaluates to false. More...
 
void error (const std::string &msg, int line, const char *file)
 Throws an error. More...
 
void warn (const std::string &msg, int line, const char *file)
 Displays a warning. More...
 
void info (const std::string &msg)
 Displays an informatkon. More...
 

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 object all or one test method can be executed.

Member Typedef Documentation

typedef std::tr1::function<void ()> oos::unit_test::test_func

A shortcut for the test function object.

A shortcut for the test function object.

Constructor & Destructor Documentation

oos::unit_test::unit_test ( const std::string &  name,
const std::string &  caption 
)

The constructor for a test_unit.

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

Member Function Documentation

void oos::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 object.
captionA short description of the test.
template<class X , class Y >
void oos::unit_test::assert_equal ( const X &  a,
const Y &  b,
const std::string &  msg,
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 object to compare.
YThe type of the right hand object to compare.
Parameters
aThe left hand operand.
bThe right hand operand.
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.
void oos::unit_test::assert_false ( bool  a,
const std::string &  msg,
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.
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.
template<class T >
void oos::unit_test::assert_greater ( const T &  a,
const T &  b,
const std::string &  msg,
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.
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.
template<class T >
void oos::unit_test::assert_less ( const T &  a,
const T &  b,
const std::string &  msg,
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.
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.
template<class T >
void oos::unit_test::assert_not_equal ( const T &  a,
const T &  b,
const std::string &  msg,
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.
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.
template<class T >
void oos::unit_test::assert_not_null ( const T *  a,
const std::string &  msg,
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.
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.
template<class T >
void oos::unit_test::assert_null ( const T *  a,
const std::string &  msg,
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.
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.
void oos::unit_test::assert_true ( bool  a,
const std::string &  msg,
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.
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.
std::string oos::unit_test::caption ( ) const

Returns the caption of the test_unit.

Returns
The caption of the test_unit.
void oos::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.
void oos::unit_test::execute ( )

Executes each test method.

Each test method of the test_unit is executed.

void oos::unit_test::execute ( const std::string &  test)

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.
virtual void oos::unit_test::finalize ( )
pure virtual

Cleans up a test_unit test.

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

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

Displays an informatkon.

The test method displays an information with the given message.

Parameters
msgThe message to print if the check fails.
virtual void oos::unit_test::initialize ( )
pure virtual

Initializes a test_unit test.

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

void oos::unit_test::list ( std::ostream &  out)

List all tests.

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

Parameters
outThe stream to be written on.
std::string oos::unit_test::name ( ) const

Returns the name of the test_unit.

Returns
The name of the test_unit.
void oos::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: