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