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