dune-common 2.10
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
Dune::TestSuite Class Reference

A Simple helper class to organize your test suite. More...

#include <dune/common/test/testsuite.hh>

Public Types

enum  ThrowPolicy { AlwaysThrow , ThrowOnRequired }
 

Public Member Functions

 TestSuite (ThrowPolicy policy, std::string name="")
 Create TestSuite.
 
 TestSuite (std::string name="", ThrowPolicy policy=ThrowOnRequired)
 Create TestSuite.
 
CollectorStream check (bool condition, std::string name="")
 Check condition.
 
CollectorStream require (bool condition, std::string name="")
 Check a required condition.
 
template<class Exception = AnyException, class Expression >
CollectorStream checkThrow (Expression &&expr, std::string name="")
 Checks that the expression throws.
 
template<class Expression >
CollectorStream checkNoThrow (Expression &&expr, std::string name="")
 Checks that the expression doesn't throw.
 
template<class Exception = AnyException, class Expression >
CollectorStream requireThrow (Expression &&expr, std::string name="")
 Requires that the expression throws.
 
template<class Expression >
CollectorStream requireNoThrow (Expression &&expr, std::string name="")
 Requires that the expression doesn't throw.
 
void subTest (const TestSuite &subTest)
 Collect data from a sub-TestSuite.
 
 operator bool () const
 Check if this TestSuite failed.
 
std::string name () const
 Query name.
 
bool report () const
 Print a summary of this TestSuite.
 
int exit () const
 Exit the test.
 

Static Protected Member Functions

static std::string composeMessage (std::string type, std::string name, std::string reason)
 
static void announceCheckResult (bool throwException, std::string type, std::string name, std::string reason)
 

Protected Attributes

std::string name_
 
std::size_t checks_
 
std::size_t failedChecks_
 
bool throwPolicy_
 

Detailed Description

A Simple helper class to organize your test suite.

Usage: Construct a TestSuite and call check() or require() with the condition to check and probably a name for this check. These methods return a stream such that you can pipe in an explanation accompanied by respective data to give a reason for a test failure.

Member Enumeration Documentation

◆ ThrowPolicy

Enumerator
AlwaysThrow 
ThrowOnRequired 

Constructor & Destructor Documentation

◆ TestSuite() [1/2]

Dune::TestSuite::TestSuite ( ThrowPolicy  policy,
std::string  name = "" 
)
inline

Create TestSuite.

Parameters
nameA name to identify this TestSuite. Defaults to "".
policyIf AlwaysThrow any failing check will throw, otherwise only required checks will do.

◆ TestSuite() [2/2]

Dune::TestSuite::TestSuite ( std::string  name = "",
ThrowPolicy  policy = ThrowOnRequired 
)
inline

Create TestSuite.

Parameters
nameA name to identify this TestSuite. Defaults to "".
policyIf AlwaysThrow any failing check will throw, otherwise only required checks will do. Defaults to ThrowOnRequired

Member Function Documentation

◆ announceCheckResult()

static void Dune::TestSuite::announceCheckResult ( bool  throwException,
std::string  type,
std::string  name,
std::string  reason 
)
inlinestaticprotected

◆ check()

CollectorStream Dune::TestSuite::check ( bool  condition,
std::string  name = "" 
)
inline

Check condition.

This will throw an exception if the check fails and if the AlwaysThrow policy was used on creation.

Parameters
conditionChecks if this is true and increases the failure counter if not.
nameA name to identify this check. Defaults to ""
Returns
A CollectorStream that can be used to create a diagnostic message to be printed on failure.

◆ checkNoThrow()

template<class Expression >
CollectorStream Dune::TestSuite::checkNoThrow ( Expression &&  expr,
std::string  name = "" 
)
inline

Checks that the expression doesn't throw.

This will throw an exception if the check fails and if the AlwaysThrow policy was used on creation.

Parameters
exprA nullary functor that is expected not to throw an exception on evaluation
nameA name to identify this check. Defaults to ""
Returns
A CollectorStream that can be used to create a diagnostic message to be printed on failure.
Example:
checkNoThrow([]{ throw std::runtime_Error("error"); }, "Expected not to thrown"); //fails
CollectorStream checkNoThrow(Expression &&expr, std::string name="")
Checks that the expression doesn't throw.
Definition testsuite.hh:163

◆ checkThrow()

template<class Exception = AnyException, class Expression >
CollectorStream Dune::TestSuite::checkThrow ( Expression &&  expr,
std::string  name = "" 
)
inline

Checks that the expression throws.

This will throw an exception if the check fails and if the AlwaysThrow policy was used on creation.

Parameters
exprA nullary functor that is expected to throw an exception on evaluation that is of type Exception or any exception if the template parameter is omitted.
nameA name to identify this check. Defaults to ""
Returns
A CollectorStream that can be used to create a diagnostic message to be printed on failure.
Example:
checkThrow<Exception>([]{ throw Exception; }, "Expected an 'Exception' to be thrown");
checkThrow([]{ throw std::runtime_Error("error"); }, "Expected any exception to be thrown");
Base class for Dune-Exceptions.
Definition exceptions.hh:96
CollectorStream checkThrow(Expression &&expr, std::string name="")
Checks that the expression throws.
Definition testsuite.hh:144

◆ composeMessage()

static std::string Dune::TestSuite::composeMessage ( std::string  type,
std::string  name,
std::string  reason 
)
inlinestaticprotected

◆ exit()

int Dune::TestSuite::exit ( ) const
inline

Exit the test.

This will print a summary of the test and return an integer to be used on program exit.

Returns
1 if any of the executed tests failed, otherwise 0.

◆ name()

std::string Dune::TestSuite::name ( ) const
inline

Query name.

Returns
Name of this TestSuite

◆ operator bool()

Dune::TestSuite::operator bool ( ) const
inlineexplicit

Check if this TestSuite failed.

Returns
False if any of the executed tests failed, otherwise true.

◆ report()

bool Dune::TestSuite::report ( ) const
inline

Print a summary of this TestSuite.

Returns
False if any of the executed tests failed, otherwise true.

◆ require()

CollectorStream Dune::TestSuite::require ( bool  condition,
std::string  name = "" 
)
inline

Check a required condition.

This will always throw an exception if the check fails.

Parameters
conditionChecks if this is true and increases the failure counter if not.
nameA name to identify this check. Defaults to ""
Returns
A CollectorStream that can be used to create a diagnostic message to be printed on failure.

◆ requireNoThrow()

template<class Expression >
CollectorStream Dune::TestSuite::requireNoThrow ( Expression &&  expr,
std::string  name = "" 
)
inline

Requires that the expression doesn't throw.

This will throw an exception if the check fails.

Parameters
exprA nullary functor that is expected not to throw an exception on evaluation
nameA name to identify this check. Defaults to ""
Returns
A CollectorStream that can be used to create a diagnostic message to be printed on failure.
Example:
requireNoThrow([]{ throw std::runtime_Error("error"); }, "Expected not to thrown"); //fails
CollectorStream requireNoThrow(Expression &&expr, std::string name="")
Requires that the expression doesn't throw.
Definition testsuite.hh:202

◆ requireThrow()

template<class Exception = AnyException, class Expression >
CollectorStream Dune::TestSuite::requireThrow ( Expression &&  expr,
std::string  name = "" 
)
inline

Requires that the expression throws.

This will throw an exception if the check fails.

Parameters
exprA nullary functor that is expected to throw an exception on evaluation that is of type Exception or any exception if the template parameter is omitted.
nameA name to identify this check. Defaults to ""
Returns
A CollectorStream that can be used to create a diagnostic message to be printed on failure.
Example:
requireThrow<Exception>([]{ throw Exception; }, "Expected an 'Exception' to be thrown");
requireThrow([]{ throw std::runtime_Error("error"); }, "Expected any exception to be thrown");
CollectorStream requireThrow(Expression &&expr, std::string name="")
Requires that the expression throws.
Definition testsuite.hh:183

◆ subTest()

void Dune::TestSuite::subTest ( const TestSuite subTest)
inline

Collect data from a sub-TestSuite.

This will incorporate the accumulated results of the sub-TestSuite into this one. If the sub-TestSuite failed, i.e., contained failed checks, a summary will be printed.

Member Data Documentation

◆ checks_

std::size_t Dune::TestSuite::checks_
protected

◆ failedChecks_

std::size_t Dune::TestSuite::failedChecks_
protected

◆ name_

std::string Dune::TestSuite::name_
protected

◆ throwPolicy_

bool Dune::TestSuite::throwPolicy_
protected

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