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