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

295 lines
6.0 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_REPORTER_HPP
#define BEAST_UNIT_TEST_REPORTER_HPP
2017-07-20 08:01:46 -07:00
2016-05-06 19:14:17 -04:00
#include <beast/unit_test/amount.hpp>
#include <beast/unit_test/recorder.hpp>
#include <boost/lexical_cast.hpp>
2017-07-20 08:01:46 -07:00
#include <boost/optional.hpp>
#include <algorithm>
#include <chrono>
#include <functional>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
namespace beast {
namespace unit_test {
namespace detail {
/** A simple test runner that writes everything to a stream in real time.
The totals are output when the object is destroyed.
*/
2016-05-10 07:09:57 -04:00
template<class = void>
2017-07-20 08:01:46 -07:00
class reporter : public runner
{
private:
using clock_type = std::chrono::steady_clock;
struct case_results
{
std::string name;
std::size_t total = 0;
std::size_t failed = 0;
2016-05-10 07:09:57 -04:00
explicit
case_results(std::string name_ = "")
: name(std::move(name_))
{
}
2017-07-20 08:01:46 -07:00
};
struct suite_results
{
std::string name;
std::size_t cases = 0;
std::size_t total = 0;
std::size_t failed = 0;
2016-05-10 07:09:57 -04:00
typename clock_type::time_point start = clock_type::now();
2017-07-20 08:01:46 -07:00
explicit
suite_results(std::string name_ = "")
2016-05-10 07:09:57 -04:00
: name(std::move(name_))
{
}
2017-07-20 08:01:46 -07:00
void
2016-05-10 07:09:57 -04:00
add(case_results const& r);
2017-07-20 08:01:46 -07:00
};
struct results
{
using run_time = std::pair<std::string,
typename clock_type::duration>;
enum
{
max_top = 10
};
std::size_t suites = 0;
std::size_t cases = 0;
std::size_t total = 0;
std::size_t failed = 0;
std::vector<run_time> top;
2016-05-10 07:09:57 -04:00
typename clock_type::time_point start = clock_type::now();
2017-07-20 08:01:46 -07:00
void
2016-08-29 10:43:56 -04:00
add(suite_results const& r);
2017-07-20 08:01:46 -07:00
};
2016-05-10 07:09:57 -04:00
std::ostream& os_;
2017-07-20 08:01:46 -07:00
results results_;
suite_results suite_results_;
case_results case_results_;
public:
2016-05-10 07:09:57 -04:00
reporter(reporter const&) = delete;
reporter& operator=(reporter const&) = delete;
2017-07-20 08:01:46 -07:00
~reporter();
explicit
2016-05-10 07:09:57 -04:00
reporter(std::ostream& os = std::cout);
2017-07-20 08:01:46 -07:00
private:
static
std::string
2016-05-10 07:09:57 -04:00
fmtdur(typename clock_type::duration const& d);
2017-07-20 08:01:46 -07:00
virtual
void
2016-08-29 10:43:56 -04:00
on_suite_begin(suite_info const& info) override;
2017-07-20 08:01:46 -07:00
virtual
void
on_suite_end() override;
virtual
void
2016-08-29 10:43:56 -04:00
on_case_begin(std::string const& name) override;
2017-07-20 08:01:46 -07:00
virtual
void
on_case_end() override;
virtual
void
on_pass() override;
virtual
void
2016-08-29 10:43:56 -04:00
on_fail(std::string const& reason) override;
2017-07-20 08:01:46 -07:00
virtual
void
2016-08-29 10:43:56 -04:00
on_log(std::string const& s) override;
2017-07-20 08:01:46 -07:00
};
//------------------------------------------------------------------------------
2016-05-10 07:09:57 -04:00
template<class _>
2017-07-20 08:01:46 -07:00
void
2016-05-10 07:09:57 -04:00
reporter<_>::
suite_results::add(case_results const& r)
2017-07-20 08:01:46 -07:00
{
++cases;
total += r.total;
failed += r.failed;
}
2016-05-10 07:09:57 -04:00
template<class _>
2017-07-20 08:01:46 -07:00
void
2016-05-10 07:09:57 -04:00
reporter<_>::
results::add(suite_results const& r)
2017-07-20 08:01:46 -07:00
{
++suites;
total += r.total;
cases += r.cases;
failed += r.failed;
2016-05-10 07:09:57 -04:00
auto const elapsed = clock_type::now() - r.start;
2016-08-26 08:01:44 -04:00
if(elapsed >= std::chrono::seconds{1})
2017-07-20 08:01:46 -07:00
{
auto const iter = std::lower_bound(top.begin(),
top.end(), elapsed,
[](run_time const& t1,
typename clock_type::duration const& t2)
{
return t1.second > t2;
});
2016-08-26 08:01:44 -04:00
if(iter != top.end())
2017-07-20 08:01:46 -07:00
{
2016-08-26 08:01:44 -04:00
if(top.size() == max_top)
2017-07-20 08:01:46 -07:00
top.resize(top.size() - 1);
top.emplace(iter, r.name, elapsed);
}
2016-08-26 08:01:44 -04:00
else if(top.size() < max_top)
2017-07-20 08:01:46 -07:00
{
top.emplace_back(r.name, elapsed);
}
}
}
//------------------------------------------------------------------------------
2016-05-10 07:09:57 -04:00
template<class _>
reporter<_>::
reporter(std::ostream& os)
: os_(os)
2017-07-20 08:01:46 -07:00
{
}
2016-05-10 07:09:57 -04:00
template<class _>
2017-07-20 08:01:46 -07:00
reporter<_>::~reporter()
{
2016-05-10 07:09:57 -04:00
if(results_.top.size() > 0)
2017-07-20 08:01:46 -07:00
{
2016-05-10 07:09:57 -04:00
os_ << "Longest suite times:\n";
for(auto const& i : results_.top)
os_ << std::setw(8) <<
fmtdur(i.second) << " " << i.first << '\n';
2017-07-20 08:01:46 -07:00
}
2016-05-10 07:09:57 -04:00
auto const elapsed = clock_type::now() - results_.start;
os_ <<
2017-07-20 08:01:46 -07:00
fmtdur(elapsed) << ", " <<
2016-05-10 07:09:57 -04:00
amount{results_.suites, "suite"} << ", " <<
amount{results_.cases, "case"} << ", " <<
amount{results_.total, "test"} << " total, " <<
amount{results_.failed, "failure"} <<
std::endl;
2017-07-20 08:01:46 -07:00
}
2016-05-10 07:09:57 -04:00
template<class _>
2017-07-20 08:01:46 -07:00
std::string
2016-05-10 07:09:57 -04:00
reporter<_>::fmtdur(typename clock_type::duration const& d)
2017-07-20 08:01:46 -07:00
{
using namespace std::chrono;
2016-05-10 07:09:57 -04:00
auto const ms = duration_cast<milliseconds>(d);
2016-08-26 08:01:44 -04:00
if(ms < seconds{1})
return boost::lexical_cast<std::string>(
ms.count()) + "ms";
2017-07-20 08:01:46 -07:00
std::stringstream ss;
ss << std::fixed << std::setprecision(1) <<
2016-08-29 10:43:56 -04:00
(ms.count()/1000.) << "s";
2017-07-20 08:01:46 -07:00
return ss.str();
}
2016-05-10 07:09:57 -04:00
template<class _>
2017-07-20 08:01:46 -07:00
void
2016-05-10 07:09:57 -04:00
reporter<_>::
on_suite_begin(suite_info const& info)
2017-07-20 08:01:46 -07:00
{
2016-05-10 07:09:57 -04:00
suite_results_ = suite_results{info.full_name()};
2017-07-20 08:01:46 -07:00
}
2016-05-10 07:09:57 -04:00
template<class _>
2017-07-20 08:01:46 -07:00
void
reporter<_>::on_suite_end()
{
2016-05-10 07:09:57 -04:00
results_.add(suite_results_);
2017-07-20 08:01:46 -07:00
}
2016-05-10 07:09:57 -04:00
template<class _>
2017-07-20 08:01:46 -07:00
void
2016-05-10 07:09:57 -04:00
reporter<_>::
on_case_begin(std::string const& name)
2017-07-20 08:01:46 -07:00
{
2016-08-29 10:43:56 -04:00
case_results_ = case_results(name);
os_ << suite_results_.name <<
2017-02-06 20:07:03 -05:00
(case_results_.name.empty() ? "" :
(" " + case_results_.name)) << std::endl;
2017-07-20 08:01:46 -07:00
}
2016-05-10 07:09:57 -04:00
template<class _>
2017-07-20 08:01:46 -07:00
void
2016-05-10 07:09:57 -04:00
reporter<_>::
on_case_end()
2017-07-20 08:01:46 -07:00
{
2016-05-10 07:09:57 -04:00
suite_results_.add(case_results_);
2017-07-20 08:01:46 -07:00
}
2016-05-10 07:09:57 -04:00
template<class _>
2017-07-20 08:01:46 -07:00
void
2016-05-10 07:09:57 -04:00
reporter<_>::
on_pass()
2017-07-20 08:01:46 -07:00
{
++case_results_.total;
}
2016-05-10 07:09:57 -04:00
template<class _>
2017-07-20 08:01:46 -07:00
void
2016-05-10 07:09:57 -04:00
reporter<_>::
on_fail(std::string const& reason)
2017-07-20 08:01:46 -07:00
{
++case_results_.failed;
++case_results_.total;
2016-05-10 07:09:57 -04:00
os_ <<
"#" << case_results_.total << " failed" <<
2016-08-29 10:43:56 -04:00
(reason.empty() ? "" : ": ") << reason << std::endl;
2017-07-20 08:01:46 -07:00
}
2016-05-10 07:09:57 -04:00
template<class _>
2017-07-20 08:01:46 -07:00
void
2016-05-10 07:09:57 -04:00
reporter<_>::
on_log(std::string const& s)
2017-07-20 08:01:46 -07:00
{
2016-05-10 07:09:57 -04:00
os_ << s;
2017-07-20 08:01:46 -07:00
}
} // detail
using reporter = detail::reporter<>;
} // unit_test
} // beast
#endif