2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_UNIT_TEST_RUNNER_H_INCLUDED
11 #define BOOST_BEAST_UNIT_TEST_RUNNER_H_INCLUDED
13 #include <boost/beast/unit_test/suite_info.hpp>
14 #include <boost/assert.hpp>
23 /** Unit test runner interface.
25 Derived classes can customize the reporting behavior. This interface is
26 injected into the unit_test class to receive the results of the tests.
31 bool default_ = false;
34 std::recursive_mutex mutex_;
38 virtual ~runner() = default;
39 runner(runner const&) = delete;
40 runner& operator=(runner const&) = delete;
42 /** Set the argument string.
44 The argument string is available to suites and
45 allows for customization of the test. Each suite
46 defines its own syntax for the argumnet string.
47 The same argument is passed to all suites.
50 arg(std::string const& s)
55 /** Returns the argument string. */
62 /** Run the specified suite.
63 @return `true` if any conditions failed.
65 template<class = void>
67 run(suite_info const& s);
69 /** Run a sequence of suites.
72 must be convertible to `suite_info`.
73 @return `true` if any conditions failed.
75 template<class FwdIter>
77 run(FwdIter first, FwdIter last);
79 /** Conditionally run a sequence of suites.
80 pred will be called as:
82 bool pred(suite_info const&);
84 @return `true` if any conditions failed.
86 template<class FwdIter, class Pred>
88 run_if(FwdIter first, FwdIter last, Pred pred = Pred{});
90 /** Run all suites in a container.
91 @return `true` if any conditions failed.
93 template<class SequenceContainer>
95 run_each(SequenceContainer const& c);
97 /** Conditionally run suites in a container.
98 pred will be called as:
100 bool pred(suite_info const&);
102 @return `true` if any conditions failed.
104 template<class SequenceContainer, class Pred>
106 run_each_if(SequenceContainer const& c, Pred pred = Pred{});
109 /// Called when a new suite starts.
112 on_suite_begin(suite_info const&)
116 /// Called when a suite ends.
123 /// Called when a new case starts.
126 on_case_begin(std::string const&)
130 /// Called when a new case ends.
137 /// Called for each passing condition.
144 /// Called for each failing condition.
147 on_fail(std::string const&)
151 /// Called when a test logs output.
154 on_log(std::string const&)
161 // Start a new testcase.
162 template<class = void>
164 testcase(std::string const& name);
166 template<class = void>
170 template<class = void>
172 fail(std::string const& reason);
174 template<class = void>
176 log(std::string const& s);
179 //------------------------------------------------------------------------------
183 runner::run(suite_info const& s)
185 // Enable 'default' testcase
190 // Forgot to call pass or fail.
197 template<class FwdIter>
199 runner::run(FwdIter first, FwdIter last)
202 for(;first != last; ++first)
203 failed = run(*first) || failed;
207 template<class FwdIter, class Pred>
209 runner::run_if(FwdIter first, FwdIter last, Pred pred)
212 for(;first != last; ++first)
214 failed = run(*first) || failed;
218 template<class SequenceContainer>
220 runner::run_each(SequenceContainer const& c)
223 for(auto const& s : c)
224 failed = run(s) || failed;
228 template<class SequenceContainer, class Pred>
230 runner::run_each_if(SequenceContainer const& c, Pred pred)
233 for(auto const& s : c)
235 failed = run(s) || failed;
241 runner::testcase(std::string const& name)
243 std::lock_guard<std::recursive_mutex> lock(mutex_);
244 // Name may not be empty
245 BOOST_ASSERT(default_ || ! name.empty());
246 // Forgot to call pass or fail
247 BOOST_ASSERT(default_ || cond_);
259 std::lock_guard<std::recursive_mutex> lock(mutex_);
268 runner::fail(std::string const& reason)
270 std::lock_guard<std::recursive_mutex> lock(mutex_);
280 runner::log(std::string const& s)
282 std::lock_guard<std::recursive_mutex> lock(mutex_);