runner.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // Copyright (c) 2016-2019 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. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_UNIT_TEST_RUNNER_H_INCLUDED
  10. #define BOOST_BEAST_UNIT_TEST_RUNNER_H_INCLUDED
  11. #include <boost/beast/_experimental/unit_test/suite_info.hpp>
  12. #include <boost/assert.hpp>
  13. #include <mutex>
  14. #include <ostream>
  15. #include <string>
  16. namespace boost {
  17. namespace beast {
  18. namespace unit_test {
  19. /** Unit test runner interface.
  20. Derived classes can customize the reporting behavior. This interface is
  21. injected into the unit_test class to receive the results of the tests.
  22. */
  23. class runner
  24. {
  25. std::string arg_;
  26. bool default_ = false;
  27. bool failed_ = false;
  28. bool cond_ = false;
  29. std::recursive_mutex mutex_;
  30. public:
  31. runner() = default;
  32. virtual ~runner() = default;
  33. runner(runner const&) = delete;
  34. runner& operator=(runner const&) = delete;
  35. /** Set the argument string.
  36. The argument string is available to suites and
  37. allows for customization of the test. Each suite
  38. defines its own syntax for the argument string.
  39. The same argument is passed to all suites.
  40. */
  41. void
  42. arg(std::string const& s)
  43. {
  44. arg_ = s;
  45. }
  46. /** Returns the argument string. */
  47. std::string const&
  48. arg() const
  49. {
  50. return arg_;
  51. }
  52. /** Run the specified suite.
  53. @return `true` if any conditions failed.
  54. */
  55. template<class = void>
  56. bool
  57. run(suite_info const& s);
  58. /** Run a sequence of suites.
  59. The expression
  60. `FwdIter::value_type`
  61. must be convertible to `suite_info`.
  62. @return `true` if any conditions failed.
  63. */
  64. template<class FwdIter>
  65. bool
  66. run(FwdIter first, FwdIter last);
  67. /** Conditionally run a sequence of suites.
  68. pred will be called as:
  69. @code
  70. bool pred(suite_info const&);
  71. @endcode
  72. @return `true` if any conditions failed.
  73. */
  74. template<class FwdIter, class Pred>
  75. bool
  76. run_if(FwdIter first, FwdIter last, Pred pred = Pred{});
  77. /** Run all suites in a container.
  78. @return `true` if any conditions failed.
  79. */
  80. template<class SequenceContainer>
  81. bool
  82. run_each(SequenceContainer const& c);
  83. /** Conditionally run suites in a container.
  84. pred will be called as:
  85. @code
  86. bool pred(suite_info const&);
  87. @endcode
  88. @return `true` if any conditions failed.
  89. */
  90. template<class SequenceContainer, class Pred>
  91. bool
  92. run_each_if(SequenceContainer const& c, Pred pred = Pred{});
  93. protected:
  94. /// Called when a new suite starts.
  95. virtual
  96. void
  97. on_suite_begin(suite_info const&)
  98. {
  99. }
  100. /// Called when a suite ends.
  101. virtual
  102. void
  103. on_suite_end()
  104. {
  105. }
  106. /// Called when a new case starts.
  107. virtual
  108. void
  109. on_case_begin(std::string const&)
  110. {
  111. }
  112. /// Called when a new case ends.
  113. virtual
  114. void
  115. on_case_end()
  116. {
  117. }
  118. /// Called for each passing condition.
  119. virtual
  120. void
  121. on_pass()
  122. {
  123. }
  124. /// Called for each failing condition.
  125. virtual
  126. void
  127. on_fail(std::string const&)
  128. {
  129. }
  130. /// Called when a test logs output.
  131. virtual
  132. void
  133. on_log(std::string const&)
  134. {
  135. }
  136. private:
  137. friend class suite;
  138. // Start a new testcase.
  139. template<class = void>
  140. void
  141. testcase(std::string const& name);
  142. template<class = void>
  143. void
  144. pass();
  145. template<class = void>
  146. void
  147. fail(std::string const& reason);
  148. template<class = void>
  149. void
  150. log(std::string const& s);
  151. };
  152. //------------------------------------------------------------------------------
  153. template<class>
  154. bool
  155. runner::run(suite_info const& s)
  156. {
  157. // Enable 'default' testcase
  158. default_ = true;
  159. failed_ = false;
  160. on_suite_begin(s);
  161. s.run(*this);
  162. // Forgot to call pass or fail.
  163. BOOST_ASSERT(cond_);
  164. on_case_end();
  165. on_suite_end();
  166. return failed_;
  167. }
  168. template<class FwdIter>
  169. bool
  170. runner::run(FwdIter first, FwdIter last)
  171. {
  172. bool failed(false);
  173. for(;first != last; ++first)
  174. failed = run(*first) || failed;
  175. return failed;
  176. }
  177. template<class FwdIter, class Pred>
  178. bool
  179. runner::run_if(FwdIter first, FwdIter last, Pred pred)
  180. {
  181. bool failed(false);
  182. for(;first != last; ++first)
  183. if(pred(*first))
  184. failed = run(*first) || failed;
  185. return failed;
  186. }
  187. template<class SequenceContainer>
  188. bool
  189. runner::run_each(SequenceContainer const& c)
  190. {
  191. bool failed(false);
  192. for(auto const& s : c)
  193. failed = run(s) || failed;
  194. return failed;
  195. }
  196. template<class SequenceContainer, class Pred>
  197. bool
  198. runner::run_each_if(SequenceContainer const& c, Pred pred)
  199. {
  200. bool failed(false);
  201. for(auto const& s : c)
  202. if(pred(s))
  203. failed = run(s) || failed;
  204. return failed;
  205. }
  206. template<class>
  207. void
  208. runner::testcase(std::string const& name)
  209. {
  210. std::lock_guard<std::recursive_mutex> lock(mutex_);
  211. // Name may not be empty
  212. BOOST_ASSERT(default_ || ! name.empty());
  213. // Forgot to call pass or fail
  214. BOOST_ASSERT(default_ || cond_);
  215. if(! default_)
  216. on_case_end();
  217. default_ = false;
  218. cond_ = false;
  219. on_case_begin(name);
  220. }
  221. template<class>
  222. void
  223. runner::pass()
  224. {
  225. std::lock_guard<std::recursive_mutex> lock(mutex_);
  226. if(default_)
  227. testcase("");
  228. on_pass();
  229. cond_ = true;
  230. }
  231. template<class>
  232. void
  233. runner::fail(std::string const& reason)
  234. {
  235. std::lock_guard<std::recursive_mutex> lock(mutex_);
  236. if(default_)
  237. testcase("");
  238. on_fail(reason);
  239. failed_ = true;
  240. cond_ = true;
  241. }
  242. template<class>
  243. void
  244. runner::log(std::string const& s)
  245. {
  246. std::lock_guard<std::recursive_mutex> lock(mutex_);
  247. if(default_)
  248. testcase("");
  249. on_log(s);
  250. }
  251. } // unit_test
  252. } // beast
  253. } // boost
  254. #endif