rippled
Loading...
Searching...
No Matches
runner.h
1// Distributed under the Boost Software License, Version 1.0. (See accompanying
2// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
3//
4
5#ifndef BEAST_UNIT_TEST_RUNNER_H_INCLUDED
6#define BEAST_UNIT_TEST_RUNNER_H_INCLUDED
7
8#include <xrpl/beast/unit_test/suite_info.h>
9
10#include <boost/assert.hpp>
11
12#include <mutex>
13#include <string>
14
15namespace beast {
16namespace unit_test {
17
23class runner
24{
26 bool default_ = false;
27 bool failed_ = false;
28 bool cond_ = false;
30
31public:
32 runner() = default;
33 virtual ~runner() = default;
34 runner(runner const&) = delete;
35 runner&
36 operator=(runner const&) = delete;
37
45 void
46 arg(std::string const& s)
47 {
48 arg_ = s;
49 }
50
52 std::string const&
53 arg() const
54 {
55 return arg_;
56 }
57
61 template <class = void>
62 bool
63 run(suite_info const& s);
64
71 template <class FwdIter>
72 bool
73 run(FwdIter first, FwdIter last);
74
82 template <class FwdIter, class Pred>
83 bool
84 run_if(FwdIter first, FwdIter last, Pred pred = Pred{});
85
89 template <class SequenceContainer>
90 bool
91 run_each(SequenceContainer const& c);
92
100 template <class SequenceContainer, class Pred>
101 bool
102 run_each_if(SequenceContainer const& c, Pred pred = Pred{});
103
104protected:
106 virtual void
108 {
109 }
110
112 virtual void
114 {
115 }
116
118 virtual void
120 {
121 }
122
124 virtual void
126 {
127 }
128
130 virtual void
132 {
133 }
134
136 virtual void
138 {
139 }
140
142 virtual void
144 {
145 }
146
147private:
148 friend class suite;
149
150 // Start a new testcase.
151 template <class = void>
152 void
153 testcase(std::string const& name);
154
155 template <class = void>
156 void
157 pass();
158
159 template <class = void>
160 void
161 fail(std::string const& reason);
162
163 template <class = void>
164 void
165 log(std::string const& s);
166};
167
168//------------------------------------------------------------------------------
169
170template <class>
171bool
173{
174 // Enable 'default' testcase
175 default_ = true;
176 failed_ = false;
178 s.run(*this);
179 // Forgot to call pass or fail.
180 BOOST_ASSERT(cond_);
181 on_case_end();
182 on_suite_end();
183 return failed_;
184}
185
186template <class FwdIter>
187bool
188runner::run(FwdIter first, FwdIter last)
189{
190 bool failed(false);
191 for (; first != last; ++first)
192 failed = run(*first) || failed;
193 return failed;
194}
195
196template <class FwdIter, class Pred>
197bool
198runner::run_if(FwdIter first, FwdIter last, Pred pred)
199{
200 bool failed(false);
201 for (; first != last; ++first)
202 if (pred(*first))
203 failed = run(*first) || failed;
204 return failed;
205}
206
207template <class SequenceContainer>
208bool
209runner::run_each(SequenceContainer const& c)
210{
211 bool failed(false);
212 for (auto const& s : c)
213 failed = run(s) || failed;
214 return failed;
215}
216
217template <class SequenceContainer, class Pred>
218bool
219runner::run_each_if(SequenceContainer const& c, Pred pred)
220{
221 bool failed(false);
222 for (auto const& s : c)
223 if (pred(s))
224 failed = run(s) || failed;
225 return failed;
226}
227
228template <class>
229void
231{
233 // Name may not be empty
234 BOOST_ASSERT(default_ || !name.empty());
235 // Forgot to call pass or fail
236 BOOST_ASSERT(default_ || cond_);
237 if (!default_)
238 on_case_end();
239 default_ = false;
240 cond_ = false;
241 on_case_begin(name);
242}
243
244template <class>
245void
247{
249 if (default_)
250 testcase("");
251 on_pass();
252 cond_ = true;
253}
254
255template <class>
256void
258{
260 if (default_)
261 testcase("");
262 on_fail(reason);
263 failed_ = true;
264 cond_ = true;
265}
266
267template <class>
268void
270{
272 if (default_)
273 testcase("");
274 on_log(s);
275}
276
277} // namespace unit_test
278} // namespace beast
279
280#endif
Unit test runner interface.
Definition runner.h:24
virtual void on_suite_begin(suite_info const &)
Called when a new suite starts.
Definition runner.h:107
std::string const & arg() const
Returns the argument string.
Definition runner.h:53
virtual void on_pass()
Called for each passing condition.
Definition runner.h:131
bool run_each_if(SequenceContainer const &c, Pred pred=Pred{})
Conditionally run suites in a container.
Definition runner.h:219
virtual void on_fail(std::string const &)
Called for each failing condition.
Definition runner.h:137
void log(std::string const &s)
Definition runner.h:269
virtual ~runner()=default
runner & operator=(runner const &)=delete
void arg(std::string const &s)
Set the argument string.
Definition runner.h:46
runner(runner const &)=delete
virtual void on_case_end()
Called when a new case ends.
Definition runner.h:125
virtual void on_suite_end()
Called when a suite ends.
Definition runner.h:113
void fail(std::string const &reason)
Definition runner.h:257
std::recursive_mutex mutex_
Definition runner.h:29
void testcase(std::string const &name)
Definition runner.h:230
virtual void on_log(std::string const &)
Called when a test logs output.
Definition runner.h:143
bool run_if(FwdIter first, FwdIter last, Pred pred=Pred{})
Conditionally run a sequence of suites.
Definition runner.h:198
bool run_each(SequenceContainer const &c)
Run all suites in a container.
Definition runner.h:209
virtual void on_case_begin(std::string const &)
Called when a new case starts.
Definition runner.h:119
bool run(suite_info const &s)
Run the specified suite.
Definition runner.h:172
Associates a unit test type with metadata.
Definition suite_info.h:20
void run(runner &r) const
Run a new instance of the associated test suite.
Definition suite_info.h:81
A testsuite class.
Definition suite.h:52
T empty(T... args)