Loading...
Searching...
No Matches
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

enum Dune::TestSuite::ThrowPolicy

Enumerator
AlwaysThrow |
ThrowOnRequired |

## Constructor & Destructor Documentation

## TestSuite() [1/2]

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

inline

Create TestSuite.

Parameters

  name | A name to identify this TestSuite. Defaults to "".
  policy | If 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

  name | A name to identify this TestSuite. Defaults to "".
  policy | If AlwaysThrow any failing check will throw, otherwise only required checks will do. Defaults to ThrowOnRequired

## Member Function Documentation

## announceCheckResult()

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

inline static protected

## 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

  condition | Checks if this is true and increases the failure counter if not.
  name | A 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

  expr | A nullary functor that is expected not to throw an exception on evaluation
  name | A 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
  Dune::TestSuite::checkNoThrow
  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

  expr | A 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.
  name | A 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");
  Dune::Exception
  Base class for Dune-Exceptions.
  Definition exceptions.hh:98
  Dune::TestSuite::checkThrow
  CollectorStream checkThrow(Expression &&expr, std::string name="")
  Checks that the expression throws.
  Definition testsuite.hh:144

## composeMessage()

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

inline static protected

## 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

inline explicit

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

  condition | Checks if this is true and increases the failure counter if not.
  name | A 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

  expr | A nullary functor that is expected not to throw an exception on evaluation
  name | A 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
  Dune::TestSuite::requireNoThrow
  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

  expr | A 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.
  name | A 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");
  Dune::TestSuite::requireThrow
  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:

- testsuite.hh