rippled
Loading...
Searching...
No Matches
reporter.h
1//
2// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7
8#ifndef BEAST_UNIT_TEST_REPORTER_HPP
9#define BEAST_UNIT_TEST_REPORTER_HPP
10
11#include <xrpl/beast/unit_test/amount.h>
12#include <xrpl/beast/unit_test/recorder.h>
13
14#include <boost/lexical_cast.hpp>
15#include <boost/optional.hpp>
16
17#include <algorithm>
18#include <chrono>
19#include <functional>
20#include <iomanip>
21#include <iostream>
22#include <sstream>
23#include <string>
24#include <utility>
25
26namespace beast {
27namespace unit_test {
28
29namespace detail {
30
34template <class = void>
35class reporter : public runner
36{
37private:
39
41 {
45
46 explicit case_results(std::string name_ = "") : name(std::move(name_))
47 {
48 }
49 };
50
52 {
57 typename clock_type::time_point start = clock_type::now();
58
59 explicit suite_results(std::string name_ = "") : name(std::move(name_))
60 {
61 }
62
63 void
64 add(case_results const& r);
65 };
66
67 struct results
68 {
70
71 enum { max_top = 10 };
72
78 typename clock_type::time_point start = clock_type::now();
79
80 void
81 add(suite_results const& r);
82 };
83
88
89public:
90 reporter(reporter const&) = delete;
92 operator=(reporter const&) = delete;
93
94 ~reporter();
95
96 explicit reporter(std::ostream& os = std::cout);
97
98private:
99 static std::string
100 fmtdur(typename clock_type::duration const& d);
101
102 virtual void
103 on_suite_begin(suite_info const& info) override;
104
105 virtual void
106 on_suite_end() override;
107
108 virtual void
109 on_case_begin(std::string const& name) override;
110
111 virtual void
112 on_case_end() override;
113
114 virtual void
115 on_pass() override;
116
117 virtual void
118 on_fail(std::string const& reason) override;
119
120 virtual void
121 on_log(std::string const& s) override;
122};
123
124//------------------------------------------------------------------------------
125
126template <class _>
127void
129{
130 ++cases;
131 total += r.total;
132 failed += r.failed;
133}
134
135template <class _>
136void
138{
139 ++suites;
140 total += r.total;
141 cases += r.cases;
142 failed += r.failed;
143 auto const elapsed = clock_type::now() - r.start;
144 if (elapsed >= std::chrono::seconds{1})
145 {
146 auto const iter = std::lower_bound(
147 top.begin(),
148 top.end(),
149 elapsed,
150 [](run_time const& t1, typename clock_type::duration const& t2) {
151 return t1.second > t2;
152 });
153 if (iter != top.end())
154 {
155 if (top.size() == max_top)
156 top.resize(top.size() - 1);
157 top.emplace(iter, r.name, elapsed);
158 }
159 else if (top.size() < max_top)
160 {
161 top.emplace_back(r.name, elapsed);
162 }
163 }
164}
165
166//------------------------------------------------------------------------------
167
168template <class _>
170{
171}
172
173template <class _>
175{
176 if (results_.top.size() > 0)
177 {
178 os_ << "Longest suite times:\n";
179 for (auto const& i : results_.top)
180 os_ << std::setw(8) << fmtdur(i.second) << " " << i.first << '\n';
181 }
182 auto const elapsed = clock_type::now() - results_.start;
183 os_ << fmtdur(elapsed) << ", " << amount{results_.suites, "suite"} << ", "
184 << amount{results_.cases, "case"} << ", "
185 << amount{results_.total, "test"} << " total, "
186 << amount{results_.failed, "failure"} << std::endl;
187}
188
189template <class _>
191reporter<_>::fmtdur(typename clock_type::duration const& d)
192{
193 using namespace std::chrono;
194 auto const ms = duration_cast<milliseconds>(d);
195 if (ms < seconds{1})
196 return boost::lexical_cast<std::string>(ms.count()) + "ms";
198 ss << std::fixed << std::setprecision(1) << (ms.count() / 1000.) << "s";
199 return ss.str();
200}
201
202template <class _>
203void
205{
206 suite_results_ = suite_results{info.full_name()};
207}
208
209template <class _>
210void
212{
213 results_.add(suite_results_);
214}
215
216template <class _>
217void
219{
220 case_results_ = case_results(name);
221 os_ << suite_results_.name
222 << (case_results_.name.empty() ? "" : (" " + case_results_.name))
223 << std::endl;
224}
225
226template <class _>
227void
229{
230 suite_results_.add(case_results_);
231}
232
233template <class _>
234void
236{
237 ++case_results_.total;
238}
239
240template <class _>
241void
243{
244 ++case_results_.failed;
245 ++case_results_.total;
246 os_ << "#" << case_results_.total << " failed"
247 << (reason.empty() ? "" : ": ") << reason << std::endl;
248}
249
250template <class _>
251void
253{
254 os_ << s;
255}
256
257} // namespace detail
258
260
261} // namespace unit_test
262} // namespace beast
263
264#endif
Utility for producing nicely composed output of amounts with units.
A simple test runner that writes everything to a stream in real time.
Definition: reporter.h:36
virtual void on_log(std::string const &s) override
Called when a test logs output.
Definition: reporter.h:252
reporter(reporter const &)=delete
virtual void on_case_end() override
Called when a new case ends.
Definition: reporter.h:228
reporter & operator=(reporter const &)=delete
virtual void on_suite_begin(suite_info const &info) override
Called when a new suite starts.
Definition: reporter.h:204
virtual void on_case_begin(std::string const &name) override
Called when a new case starts.
Definition: reporter.h:218
virtual void on_fail(std::string const &reason) override
Called for each failing condition.
Definition: reporter.h:242
static std::string fmtdur(typename clock_type::duration const &d)
Definition: reporter.h:191
virtual void on_suite_end() override
Called when a suite ends.
Definition: reporter.h:211
virtual void on_pass() override
Called for each passing condition.
Definition: reporter.h:235
Unit test runner interface.
Definition: runner.h:28
Associates a unit test type with metadata.
Definition: suite_info.h:23
std::string full_name() const
Return the canonical suite name as a string.
Definition: suite_info.h:77
T empty(T... args)
T endl(T... args)
T fixed(T... args)
T lower_bound(T... args)
STL namespace.
T setprecision(T... args)
T setw(T... args)
T str(T... args)
void add(suite_results const &r)
Definition: reporter.h:137