Files
boost_beast/extras/beast/unit_test/runner.hpp
T

289 lines
5.8 KiB
C++
Raw Normal View History

2017-07-20 08:01:46 -07:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-07-20 08:01:46 -07:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2016-05-06 19:14:17 -04:00
#ifndef BEAST_UNIT_TEST_RUNNER_H_INCLUDED
#define BEAST_UNIT_TEST_RUNNER_H_INCLUDED
2017-07-20 08:01:46 -07:00
2016-05-06 19:14:17 -04:00
#include <beast/unit_test/suite_info.hpp>
2016-09-25 12:17:32 -04:00
#include <boost/assert.hpp>
2017-07-20 08:01:46 -07:00
#include <mutex>
2016-05-10 07:09:57 -04:00
#include <ostream>
2017-07-20 08:01:46 -07:00
#include <string>
namespace beast {
namespace unit_test {
/** Unit test runner interface.
2016-08-29 10:43:56 -04:00
2017-07-20 08:01:46 -07:00
Derived classes can customize the reporting behavior. This interface is
injected into the unit_test class to receive the results of the tests.
*/
class runner
{
std::string arg_;
bool default_ = false;
bool failed_ = false;
bool cond_ = false;
std::recursive_mutex mutex_;
public:
2016-05-10 07:09:57 -04:00
runner() = default;
2017-07-20 08:01:46 -07:00
virtual ~runner() = default;
2016-05-10 07:09:57 -04:00
runner(runner const&) = delete;
runner& operator=(runner const&) = delete;
2017-07-20 08:01:46 -07:00
/** Set the argument string.
2016-05-10 07:09:57 -04:00
2017-07-20 08:01:46 -07:00
The argument string is available to suites and
allows for customization of the test. Each suite
defines its own syntax for the argumnet string.
The same argument is passed to all suites.
*/
void
2016-05-10 07:09:57 -04:00
arg(std::string const& s)
2017-07-20 08:01:46 -07:00
{
arg_ = s;
}
/** Returns the argument string. */
std::string const&
arg() const
{
return arg_;
}
/** Run the specified suite.
@return `true` if any conditions failed.
*/
2016-05-10 07:09:57 -04:00
template<class = void>
2017-07-20 08:01:46 -07:00
bool
2016-08-29 10:43:56 -04:00
run(suite_info const& s);
2017-07-20 08:01:46 -07:00
/** Run a sequence of suites.
The expression
`FwdIter::value_type`
must be convertible to `suite_info`.
@return `true` if any conditions failed.
*/
2016-08-26 08:01:44 -04:00
template<class FwdIter>
2017-07-20 08:01:46 -07:00
bool
2016-08-29 10:43:56 -04:00
run(FwdIter first, FwdIter last);
2017-07-20 08:01:46 -07:00
/** Conditionally run a sequence of suites.
pred will be called as:
@code
2016-08-29 10:43:56 -04:00
bool pred(suite_info const&);
2017-07-20 08:01:46 -07:00
@endcode
@return `true` if any conditions failed.
*/
2016-08-26 08:01:44 -04:00
template<class FwdIter, class Pred>
2017-07-20 08:01:46 -07:00
bool
2016-08-26 08:01:44 -04:00
run_if(FwdIter first, FwdIter last, Pred pred = Pred{});
2017-07-20 08:01:46 -07:00
/** Run all suites in a container.
@return `true` if any conditions failed.
*/
2016-08-26 08:01:44 -04:00
template<class SequenceContainer>
2017-07-20 08:01:46 -07:00
bool
2016-08-29 10:43:56 -04:00
run_each(SequenceContainer const& c);
2017-07-20 08:01:46 -07:00
/** Conditionally run suites in a container.
pred will be called as:
@code
2016-08-29 10:43:56 -04:00
bool pred(suite_info const&);
2017-07-20 08:01:46 -07:00
@endcode
@return `true` if any conditions failed.
*/
2016-08-26 08:01:44 -04:00
template<class SequenceContainer, class Pred>
2017-07-20 08:01:46 -07:00
bool
2016-08-26 08:01:44 -04:00
run_each_if(SequenceContainer const& c, Pred pred = Pred{});
2017-07-20 08:01:46 -07:00
2016-05-10 07:09:57 -04:00
protected:
/// Called when a new suite starts.
2017-07-20 08:01:46 -07:00
virtual
void
2016-05-10 07:09:57 -04:00
on_suite_begin(suite_info const&)
2017-07-20 08:01:46 -07:00
{
}
2016-05-10 07:09:57 -04:00
/// Called when a suite ends.
2017-07-20 08:01:46 -07:00
virtual
void
on_suite_end()
{
}
2016-05-10 07:09:57 -04:00
/// Called when a new case starts.
2017-07-20 08:01:46 -07:00
virtual
void
2016-05-10 07:09:57 -04:00
on_case_begin(std::string const&)
2017-07-20 08:01:46 -07:00
{
}
2016-05-10 07:09:57 -04:00
/// Called when a new case ends.
2017-07-20 08:01:46 -07:00
virtual
void
on_case_end()
{
}
2016-05-10 07:09:57 -04:00
/// Called for each passing condition.
2017-07-20 08:01:46 -07:00
virtual
void
2016-05-10 07:09:57 -04:00
on_pass()
2017-07-20 08:01:46 -07:00
{
}
2016-05-10 07:09:57 -04:00
/// Called for each failing condition.
2017-07-20 08:01:46 -07:00
virtual
void
2016-05-10 07:09:57 -04:00
on_fail(std::string const&)
2017-07-20 08:01:46 -07:00
{
}
2016-05-10 07:09:57 -04:00
/// Called when a test logs output.
2017-07-20 08:01:46 -07:00
virtual
void
2016-05-10 07:09:57 -04:00
on_log(std::string const&)
2017-07-20 08:01:46 -07:00
{
}
private:
friend class suite;
// Start a new testcase.
2016-08-26 08:01:44 -04:00
template<class = void>
2017-07-20 08:01:46 -07:00
void
2016-08-29 10:43:56 -04:00
testcase(std::string const& name);
2017-07-20 08:01:46 -07:00
2016-08-26 08:01:44 -04:00
template<class = void>
2017-07-20 08:01:46 -07:00
void
pass();
2016-08-26 08:01:44 -04:00
template<class = void>
2017-07-20 08:01:46 -07:00
void
2016-08-29 10:43:56 -04:00
fail(std::string const& reason);
2017-07-20 08:01:46 -07:00
2016-08-26 08:01:44 -04:00
template<class = void>
2017-07-20 08:01:46 -07:00
void
2016-08-29 10:43:56 -04:00
log(std::string const& s);
2017-07-20 08:01:46 -07:00
};
//------------------------------------------------------------------------------
2016-08-26 08:01:44 -04:00
template<class>
2017-07-20 08:01:46 -07:00
bool
2016-08-29 10:43:56 -04:00
runner::run(suite_info const& s)
2017-07-20 08:01:46 -07:00
{
// Enable 'default' testcase
default_ = true;
failed_ = false;
2016-08-29 10:43:56 -04:00
on_suite_begin(s);
s.run(*this);
2017-07-20 08:01:46 -07:00
// Forgot to call pass or fail.
2016-09-25 12:17:32 -04:00
BOOST_ASSERT(cond_);
2017-07-20 08:01:46 -07:00
on_case_end();
on_suite_end();
return failed_;
}
2016-08-26 08:01:44 -04:00
template<class FwdIter>
2017-07-20 08:01:46 -07:00
bool
2016-08-29 10:43:56 -04:00
runner::run(FwdIter first, FwdIter last)
2017-07-20 08:01:46 -07:00
{
2016-08-29 10:43:56 -04:00
bool failed(false);
2016-08-26 08:01:44 -04:00
for(;first != last; ++first)
2016-08-29 10:43:56 -04:00
failed = run(*first) || failed;
2017-07-20 08:01:46 -07:00
return failed;
}
2016-08-26 08:01:44 -04:00
template<class FwdIter, class Pred>
2017-07-20 08:01:46 -07:00
bool
2016-08-26 08:01:44 -04:00
runner::run_if(FwdIter first, FwdIter last, Pred pred)
2017-07-20 08:01:46 -07:00
{
2016-08-29 10:43:56 -04:00
bool failed(false);
2016-08-26 08:01:44 -04:00
for(;first != last; ++first)
2016-08-29 10:43:56 -04:00
if(pred(*first))
failed = run(*first) || failed;
2017-07-20 08:01:46 -07:00
return failed;
}
2016-08-26 08:01:44 -04:00
template<class SequenceContainer>
2017-07-20 08:01:46 -07:00
bool
2016-08-29 10:43:56 -04:00
runner::run_each(SequenceContainer const& c)
2017-07-20 08:01:46 -07:00
{
2016-08-29 10:43:56 -04:00
bool failed(false);
2016-08-26 08:01:44 -04:00
for(auto const& s : c)
2016-08-29 10:43:56 -04:00
failed = run(s) || failed;
2017-07-20 08:01:46 -07:00
return failed;
}
2016-08-26 08:01:44 -04:00
template<class SequenceContainer, class Pred>
2017-07-20 08:01:46 -07:00
bool
2016-08-26 08:01:44 -04:00
runner::run_each_if(SequenceContainer const& c, Pred pred)
2017-07-20 08:01:46 -07:00
{
2016-08-29 10:43:56 -04:00
bool failed(false);
2016-08-26 08:01:44 -04:00
for(auto const& s : c)
2016-08-29 10:43:56 -04:00
if(pred(s))
failed = run(s) || failed;
2017-07-20 08:01:46 -07:00
return failed;
}
2016-08-26 08:01:44 -04:00
template<class>
2017-07-20 08:01:46 -07:00
void
2016-08-29 10:43:56 -04:00
runner::testcase(std::string const& name)
2017-07-20 08:01:46 -07:00
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
// Name may not be empty
2016-09-25 12:17:32 -04:00
BOOST_ASSERT(default_ || ! name.empty());
2017-07-20 08:01:46 -07:00
// Forgot to call pass or fail
2016-09-25 12:17:32 -04:00
BOOST_ASSERT(default_ || cond_);
2016-08-26 08:01:44 -04:00
if(! default_)
2017-07-20 08:01:46 -07:00
on_case_end();
default_ = false;
cond_ = false;
2016-08-29 10:43:56 -04:00
on_case_begin(name);
2017-07-20 08:01:46 -07:00
}
2016-08-26 08:01:44 -04:00
template<class>
2017-07-20 08:01:46 -07:00
void
runner::pass()
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
2016-08-26 08:01:44 -04:00
if(default_)
2016-08-29 10:43:56 -04:00
testcase("");
2017-07-20 08:01:46 -07:00
on_pass();
cond_ = true;
}
2016-08-26 08:01:44 -04:00
template<class>
2017-07-20 08:01:46 -07:00
void
2016-08-29 10:43:56 -04:00
runner::fail(std::string const& reason)
2017-07-20 08:01:46 -07:00
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
2016-08-26 08:01:44 -04:00
if(default_)
2016-08-29 10:43:56 -04:00
testcase("");
on_fail(reason);
2017-07-20 08:01:46 -07:00
failed_ = true;
cond_ = true;
}
2016-08-26 08:01:44 -04:00
template<class>
2017-07-20 08:01:46 -07:00
void
2016-08-29 10:43:56 -04:00
runner::log(std::string const& s)
2017-07-20 08:01:46 -07:00
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
2016-08-26 08:01:44 -04:00
if(default_)
2016-08-29 10:43:56 -04:00
testcase("");
on_log(s);
2017-07-20 08:01:46 -07:00
}
} // unit_test
} // beast
#endif