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#include <boost/lexical_cast.hpp>
14#include <boost/optional.hpp>
15#include <algorithm>
16#include <chrono>
17#include <functional>
18#include <iomanip>
19#include <iostream>
20#include <sstream>
21#include <string>
22#include <utility>
23
24namespace beast {
25namespace unit_test {
26
27namespace detail {
28
32template <class = void>
33class reporter : public runner
34{
35private:
37
39 {
43
44 explicit case_results(std::string name_ = "") : name(std::move(name_))
45 {
46 }
47 };
48
50 {
55 typename clock_type::time_point start = clock_type::now();
56
57 explicit suite_results(std::string name_ = "") : name(std::move(name_))
58 {
59 }
60
61 void
62 add(case_results const& r);
63 };
64
65 struct results
66 {
68
69 enum { max_top = 10 };
70
76 typename clock_type::time_point start = clock_type::now();
77
78 void
79 add(suite_results const& r);
80 };
81
86
87public:
88 reporter(reporter const&) = delete;
90 operator=(reporter const&) = delete;
91
92 ~reporter();
93
94 explicit reporter(std::ostream& os = std::cout);
95
96private:
97 static std::string
98 fmtdur(typename clock_type::duration const& d);
99
100 virtual void
101 on_suite_begin(suite_info const& info) override;
102
103 virtual void
104 on_suite_end() override;
105
106 virtual void
107 on_case_begin(std::string const& name) override;
108
109 virtual void
110 on_case_end() override;
111
112 virtual void
113 on_pass() override;
114
115 virtual void
116 on_fail(std::string const& reason) override;
117
118 virtual void
119 on_log(std::string const& s) override;
120};
121
122//------------------------------------------------------------------------------
123
124template <class _>
125void
127{
128 ++cases;
129 total += r.total;
130 failed += r.failed;
131}
132
133template <class _>
134void
136{
137 ++suites;
138 total += r.total;
139 cases += r.cases;
140 failed += r.failed;
141 auto const elapsed = clock_type::now() - r.start;
142 if (elapsed >= std::chrono::seconds{1})
143 {
144 auto const iter = std::lower_bound(
145 top.begin(),
146 top.end(),
147 elapsed,
148 [](run_time const& t1, typename clock_type::duration const& t2) {
149 return t1.second > t2;
150 });
151 if (iter != top.end())
152 {
153 if (top.size() == max_top)
154 top.resize(top.size() - 1);
155 top.emplace(iter, r.name, elapsed);
156 }
157 else if (top.size() < max_top)
158 {
159 top.emplace_back(r.name, elapsed);
160 }
161 }
162}
163
164//------------------------------------------------------------------------------
165
166template <class _>
168{
169}
170
171template <class _>
173{
174 if (results_.top.size() > 0)
175 {
176 os_ << "Longest suite times:\n";
177 for (auto const& i : results_.top)
178 os_ << std::setw(8) << fmtdur(i.second) << " " << i.first << '\n';
179 }
180 auto const elapsed = clock_type::now() - results_.start;
181 os_ << fmtdur(elapsed) << ", " << amount{results_.suites, "suite"} << ", "
182 << amount{results_.cases, "case"} << ", "
183 << amount{results_.total, "test"} << " total, "
184 << amount{results_.failed, "failure"} << std::endl;
185}
186
187template <class _>
189reporter<_>::fmtdur(typename clock_type::duration const& d)
190{
191 using namespace std::chrono;
192 auto const ms = duration_cast<milliseconds>(d);
193 if (ms < seconds{1})
194 return boost::lexical_cast<std::string>(ms.count()) + "ms";
196 ss << std::fixed << std::setprecision(1) << (ms.count() / 1000.) << "s";
197 return ss.str();
198}
199
200template <class _>
201void
203{
204 suite_results_ = suite_results{info.full_name()};
205}
206
207template <class _>
208void
210{
211 results_.add(suite_results_);
212}
213
214template <class _>
215void
217{
218 case_results_ = case_results(name);
219 os_ << suite_results_.name
220 << (case_results_.name.empty() ? "" : (" " + case_results_.name))
221 << std::endl;
222}
223
224template <class _>
225void
227{
228 suite_results_.add(case_results_);
229}
230
231template <class _>
232void
234{
235 ++case_results_.total;
236}
237
238template <class _>
239void
241{
242 ++case_results_.failed;
243 ++case_results_.total;
244 os_ << "#" << case_results_.total << " failed"
245 << (reason.empty() ? "" : ": ") << reason << std::endl;
246}
247
248template <class _>
249void
251{
252 os_ << s;
253}
254
255} // namespace detail
256
258
259} // namespace unit_test
260} // namespace beast
261
262#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:34
virtual void on_log(std::string const &s) override
Called when a test logs output.
Definition: reporter.h:250
reporter(reporter const &)=delete
virtual void on_case_end() override
Called when a new case ends.
Definition: reporter.h:226
reporter & operator=(reporter const &)=delete
virtual void on_suite_begin(suite_info const &info) override
Called when a new suite starts.
Definition: reporter.h:202
virtual void on_case_begin(std::string const &name) override
Called when a new case starts.
Definition: reporter.h:216
virtual void on_fail(std::string const &reason) override
Called for each failing condition.
Definition: reporter.h:240
static std::string fmtdur(typename clock_type::duration const &d)
Definition: reporter.h:189
virtual void on_suite_end() override
Called when a suite ends.
Definition: reporter.h:209
virtual void on_pass() override
Called for each passing condition.
Definition: reporter.h:233
Unit test runner interface.
Definition: runner.h:26
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:135