Loading...
Searching...
No Matches
testsuite.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_TEST_TESTSUITE_HH
#define DUNE_COMMON_TEST_TESTSUITE_HH
#include <iostream>
#include <sstream>
#include <string>
#include <dune/common/exceptions.hh>
#include <dune/common/test/collectorstream.hh>
namespace Dune {
class TestSuite
{
public:
enum ThrowPolicy
{
AlwaysThrow,
ThrowOnRequired
};
TestSuite(ThrowPolicy policy, std::string name="") :
name_(name),
checks_(0),
failedChecks_(0),
throwPolicy_(policy==AlwaysThrow)
{}
TestSuite(std::string name="", ThrowPolicy policy=ThrowOnRequired) :
name_(name),
checks_(0),
failedChecks_(0),
throwPolicy_(policy==AlwaysThrow)
{}
CollectorStream check(bool condition, std::string name="")
{
++checks_;
if (not condition)
++failedChecks_;
return CollectorStream([condition, name, this](std::string reason) {
if (not condition)
this->announceCheckResult(throwPolicy_, "CHECK ", name, reason);
});
}
CollectorStream require(bool condition, std::string name="")
{
++checks_;
if (not condition)
++failedChecks_;
return CollectorStream([condition, name, this](std::string reason) {
if (not condition)
this->announceCheckResult(true, "REQUIRED CHECK", name, reason);
});
}
private:
struct AnyException{};
struct NoException{};
template <class Exception= AnyException,class Expression>
bool evaluateThrowCondition(Expression&& expr)
{
try { expr(); }
catch (const Exception& e)
{
return true;
}
catch (...) //If we end up here, but we wanted to catch a specific Exception, we consider this failed, but if Exception is AnyExpection, we pass.
{
return std::is_same_v<Exception,AnyException>;
}
// If we end up here we didn't catch any exception. Thus, if this is on purpose, i.e. 'Exception= NoException', we return true.
return std::is_same_v<Exception,NoException>;
}
public:
template <class Exception= AnyException,class Expression>
CollectorStream checkThrow(Expression&& expr, std::string name = "")
{
return check(evaluateThrowCondition<Exception>(expr),name);
}
template <class Expression>
CollectorStream checkNoThrow(Expression&& expr, std::string name = "")
{
return check(evaluateThrowCondition<NoException>(expr),name);
}
template <class Exception= AnyException,class Expression>
CollectorStream requireThrow(Expression&& expr, std::string name = "")
{
return require(evaluateThrowCondition<Exception>(expr),name);
}
template <class Expression>
CollectorStream requireNoThrow(Expression&& expr, std::string name = "")
{
return require(evaluateThrowCondition<NoException>(expr),name);
}
void subTest(const TestSuite& subTest)
{
checks_ += subTest.checks_;
failedChecks_ += subTest.failedChecks_;
if (not subTest)
announceCheckResult(throwPolicy_, "SUBTEST", subTest.name(), std::to_string(subTest.failedChecks_)+"/"+std::to_string(subTest.checks_) + " checks failed in this subtest.");
}
explicit operator bool () const
{
return (failedChecks_==0);
}
std::string name() const
{
return name_;
}
bool report() const
{
if (failedChecks_>0)
std::cout << composeMessage("TEST ", name(), std::to_string(failedChecks_)+"/"+std::to_string(checks_) + " checks failed in this test.") << std::endl;
return (failedChecks_==0);
}
int exit() const
{
return (report() ? 0: 1);
}
protected:
// Compose a diagnostic message
static std::string composeMessage(std::string type, std::string name, std::string reason)
{
std::ostringstream s;
s << type << " FAILED";
if (name!="")
s << "(" << name << ")";
s << ": ";
if (reason!="")
s << reason;
return s.str();
}
// Announce check results. To be called on failed checks
static void announceCheckResult(bool throwException, std::string type, std::string name, std::string reason)
{
std::string message = composeMessage(type, name, reason);
std::cout << message << std::endl;
if (throwException)
{
Dune::Exception ex;
ex.message(message);
throw ex;
}
}
std::string name_;
std::size_t checks_;
std::size_t failedChecks_;
bool throwPolicy_;
};
} // namespace Dune
#endif // DUNE_COMMON_TEST_TESTSUITE_HH
exceptions.hh
A few common exception classes.
Dune::Exception::message
void message(const std::string &msg)
store string in internal message buffer
Definition exceptions.cc:32
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::Exception
Base class for Dune-Exceptions.
Definition exceptions.hh:98
Dune::type
Dune::TestSuite::name
std::string name() const
Query name.
Definition testsuite.hh:238
Dune::TestSuite::announceCheckResult
static void announceCheckResult(bool throwException, std::string type, std::string name, std::string reason)
Definition testsuite.hh:284
Dune::TestSuite::checkNoThrow
CollectorStream checkNoThrow(Expression &&expr, std::string name="")
Checks that the expression doesn't throw.
Definition testsuite.hh:163
Dune::TestSuite::requireNoThrow
CollectorStream requireNoThrow(Expression &&expr, std::string name="")
Requires that the expression doesn't throw.
Definition testsuite.hh:202
Dune::TestSuite::name_
std::string name_
Definition testsuite.hh:296
Dune::TestSuite::composeMessage
static std::string composeMessage(std::string type, std::string name, std::string reason)
Definition testsuite.hh:271
Dune::TestSuite::TestSuite
TestSuite(std::string name="", ThrowPolicy policy=ThrowOnRequired)
Create TestSuite.
Definition testsuite.hh:58
Dune::TestSuite::subTest
void subTest(const TestSuite &subTest)
Collect data from a sub-TestSuite.
Definition testsuite.hh:214
Dune::TestSuite::TestSuite
TestSuite(ThrowPolicy policy, std::string name="")
Create TestSuite.
Definition testsuite.hh:45
Dune::TestSuite::checks_
std::size_t checks_
Definition testsuite.hh:297
Dune::TestSuite::exit
int exit() const
Exit the test.
Definition testsuite.hh:263
Dune::TestSuite::require
CollectorStream require(bool condition, std::string name="")
Check a required condition.
Definition testsuite.hh:95
Dune::TestSuite::check
CollectorStream check(bool condition, std::string name="")
Check condition.
Definition testsuite.hh:74
Dune::TestSuite::throwPolicy_
bool throwPolicy_
Definition testsuite.hh:299
Dune::TestSuite::checkThrow
CollectorStream checkThrow(Expression &&expr, std::string name="")
Checks that the expression throws.
Definition testsuite.hh:144
Dune::TestSuite::requireThrow
CollectorStream requireThrow(Expression &&expr, std::string name="")
Requires that the expression throws.
Definition testsuite.hh:183
Dune::TestSuite::failedChecks_
std::size_t failedChecks_
Definition testsuite.hh:298
Dune::TestSuite::report
bool report() const
Print a summary of this TestSuite.
Definition testsuite.hh:248
Dune::TestSuite::ThrowPolicy
ThrowPolicy
Definition testsuite.hh:34
Dune::TestSuite::ThrowOnRequired
@ ThrowOnRequired
Definition testsuite.hh:36
Dune::TestSuite::AlwaysThrow
@ AlwaysThrow
Definition testsuite.hh:35