rippled
Loading...
Searching...
No Matches
runner.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_RUNNER_H_INCLUDED
9#define BEAST_UNIT_TEST_RUNNER_H_INCLUDED
10
11#include <xrpl/beast/unit_test/suite_info.h>
12#include <boost/assert.hpp>
13#include <mutex>
14#include <ostream>
15#include <string>
16
17namespace beast {
18namespace unit_test {
19
25class runner
26{
28 bool default_ = false;
29 bool failed_ = false;
30 bool cond_ = false;
32
33public:
34 runner() = default;
35 virtual ~runner() = default;
36 runner(runner const&) = delete;
37 runner&
38 operator=(runner const&) = delete;
39
47 void
48 arg(std::string const& s)
49 {
50 arg_ = s;
51 }
52
54 std::string const&
55 arg() const
56 {
57 return arg_;
58 }
59
63 template <class = void>
64 bool
65 run(suite_info const& s);
66
73 template <class FwdIter>
74 bool
75 run(FwdIter first, FwdIter last);
76
84 template <class FwdIter, class Pred>
85 bool
86 run_if(FwdIter first, FwdIter last, Pred pred = Pred{});
87
91 template <class SequenceContainer>
92 bool
93 run_each(SequenceContainer const& c);
94
102 template <class SequenceContainer, class Pred>
103 bool
104 run_each_if(SequenceContainer const& c, Pred pred = Pred{});
105
106protected:
108 virtual void
110 {
111 }
112
114 virtual void
116 {
117 }
118
120 virtual void
122 {
123 }
124
126 virtual void
128 {
129 }
130
132 virtual void
134 {
135 }
136
138 virtual void
140 {
141 }
142
144 virtual void
146 {
147 }
148
149private:
150 friend class suite;
151
152 // Start a new testcase.
153 template <class = void>
154 void
155 testcase(std::string const& name);
156
157 template <class = void>
158 void
159 pass();
160
161 template <class = void>
162 void
163 fail(std::string const& reason);
164
165 template <class = void>
166 void
167 log(std::string const& s);
168};
169
170//------------------------------------------------------------------------------
171
172template <class>
173bool
175{
176 // Enable 'default' testcase
177 default_ = true;
178 failed_ = false;
180 s.run(*this);
181 // Forgot to call pass or fail.
182 BOOST_ASSERT(cond_);
183 on_case_end();
184 on_suite_end();
185 return failed_;
186}
187
188template <class FwdIter>
189bool
190runner::run(FwdIter first, FwdIter last)
191{
192 bool failed(false);
193 for (; first != last; ++first)
194 failed = run(*first) || failed;
195 return failed;
196}
197
198template <class FwdIter, class Pred>
199bool
200runner::run_if(FwdIter first, FwdIter last, Pred pred)
201{
202 bool failed(false);
203 for (; first != last; ++first)
204 if (pred(*first))
205 failed = run(*first) || failed;
206 return failed;
207}
208
209template <class SequenceContainer>
210bool
211runner::run_each(SequenceContainer const& c)
212{
213 bool failed(false);
214 for (auto const& s : c)
215 failed = run(s) || failed;
216 return failed;
217}
218
219template <class SequenceContainer, class Pred>
220bool
221runner::run_each_if(SequenceContainer const& c, Pred pred)
222{
223 bool failed(false);
224 for (auto const& s : c)
225 if (pred(s))
226 failed = run(s) || failed;
227 return failed;
228}
229
230template <class>
231void
233{
235 // Name may not be empty
236 BOOST_ASSERT(default_ || !name.empty());
237 // Forgot to call pass or fail
238 BOOST_ASSERT(default_ || cond_);
239 if (!default_)
240 on_case_end();
241 default_ = false;
242 cond_ = false;
243 on_case_begin(name);
244}
245
246template <class>
247void
249{
251 if (default_)
252 testcase("");
253 on_pass();
254 cond_ = true;
255}
256
257template <class>
258void
260{
262 if (default_)
263 testcase("");
264 on_fail(reason);
265 failed_ = true;
266 cond_ = true;
267}
268
269template <class>
270void
272{
274 if (default_)
275 testcase("");
276 on_log(s);
277}
278
279} // namespace unit_test
280} // namespace beast
281
282#endif
Unit test runner interface.
Definition: runner.h:26
virtual void on_suite_begin(suite_info const &)
Called when a new suite starts.
Definition: runner.h:109
std::string const & arg() const
Returns the argument string.
Definition: runner.h:55
virtual void on_pass()
Called for each passing condition.
Definition: runner.h:133
bool run_each_if(SequenceContainer const &c, Pred pred=Pred{})
Conditionally run suites in a container.
Definition: runner.h:221
virtual void on_fail(std::string const &)
Called for each failing condition.
Definition: runner.h:139
void log(std::string const &s)
Definition: runner.h:271
virtual ~runner()=default
runner & operator=(runner const &)=delete
void arg(std::string const &s)
Set the argument string.
Definition: runner.h:48
runner(runner const &)=delete
virtual void on_case_end()
Called when a new case ends.
Definition: runner.h:127
std::string arg_
Definition: runner.h:27
virtual void on_suite_end()
Called when a suite ends.
Definition: runner.h:115
void fail(std::string const &reason)
Definition: runner.h:259
std::recursive_mutex mutex_
Definition: runner.h:31
void testcase(std::string const &name)
Definition: runner.h:232
virtual void on_log(std::string const &)
Called when a test logs output.
Definition: runner.h:145
bool run_if(FwdIter first, FwdIter last, Pred pred=Pred{})
Conditionally run a sequence of suites.
Definition: runner.h:200
bool run_each(SequenceContainer const &c)
Run all suites in a container.
Definition: runner.h:211
virtual void on_case_begin(std::string const &)
Called when a new case starts.
Definition: runner.h:121
bool run(suite_info const &s)
Run the specified suite.
Definition: runner.h:174
Associates a unit test type with metadata.
Definition: suite_info.h:23
void run(runner &r) const
Run a new instance of the associated test suite.
Definition: suite_info.h:84
A testsuite class.
Definition: suite.h:53
T empty(T... args)