DUNE-FEM (unstable)

Dune::TestSuite Class Reference

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

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

Public Member Functions

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

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.

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

◆ 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.

References name().

Referenced by checkNoThrow(), and checkThrow().

◆ 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

References check(), and name().

◆ 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

References check(), and name().

◆ 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.

References report().

◆ name()

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

Query name.

Returns
Name of this TestSuite

Referenced by check(), checkNoThrow(), checkThrow(), report(), require(), requireNoThrow(), and requireThrow().

◆ 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.

References name().

Referenced by exit().

◆ 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.

References name().

Referenced by requireNoThrow(), and requireThrow().

◆ 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

References name(), and require().

◆ 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

References name(), and require().

◆ 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.

References subTest().

Referenced by subTest().


The documentation for this class was generated from the following file:
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 20, 23:30, 2024)