results.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_RESULTS_HPP
  10. #define BOOST_BEAST_UNIT_TEST_RESULTS_HPP
  11. #include <boost/beast/_experimental/unit_test/detail/const_container.hpp>
  12. #include <string>
  13. #include <vector>
  14. namespace boost {
  15. namespace beast {
  16. namespace unit_test {
  17. /** Holds a set of test condition outcomes in a testcase. */
  18. class case_results
  19. {
  20. public:
  21. /** Holds the result of evaluating one test condition. */
  22. struct test
  23. {
  24. explicit test(bool pass_)
  25. : pass(pass_)
  26. {
  27. }
  28. test(bool pass_, std::string const& reason_)
  29. : pass(pass_)
  30. , reason(reason_)
  31. {
  32. }
  33. bool pass;
  34. std::string reason;
  35. };
  36. private:
  37. class tests_t
  38. : public detail::const_container <std::vector <test>>
  39. {
  40. private:
  41. std::size_t failed_;
  42. public:
  43. tests_t()
  44. : failed_(0)
  45. {
  46. }
  47. /** Returns the total number of test conditions. */
  48. std::size_t
  49. total() const
  50. {
  51. return cont().size();
  52. }
  53. /** Returns the number of failed test conditions. */
  54. std::size_t
  55. failed() const
  56. {
  57. return failed_;
  58. }
  59. /** Register a successful test condition. */
  60. void
  61. pass()
  62. {
  63. cont().emplace_back(true);
  64. }
  65. /** Register a failed test condition. */
  66. void
  67. fail(std::string const& reason = "")
  68. {
  69. ++failed_;
  70. cont().emplace_back(false, reason);
  71. }
  72. };
  73. class log_t
  74. : public detail::const_container <std::vector <std::string>>
  75. {
  76. public:
  77. /** Insert a string into the log. */
  78. void
  79. insert(std::string const& s)
  80. {
  81. cont().push_back(s);
  82. }
  83. };
  84. std::string name_;
  85. public:
  86. explicit case_results(std::string const& name = "")
  87. : name_(name)
  88. {
  89. }
  90. /** Returns the name of this testcase. */
  91. std::string const&
  92. name() const
  93. {
  94. return name_;
  95. }
  96. /** Memberspace for a container of test condition outcomes. */
  97. tests_t tests;
  98. /** Memberspace for a container of testcase log messages. */
  99. log_t log;
  100. };
  101. //--------------------------------------------------------------------------
  102. /** Holds the set of testcase results in a suite. */
  103. class suite_results
  104. : public detail::const_container <std::vector <case_results>>
  105. {
  106. private:
  107. std::string name_;
  108. std::size_t total_ = 0;
  109. std::size_t failed_ = 0;
  110. public:
  111. explicit suite_results(std::string const& name = "")
  112. : name_(name)
  113. {
  114. }
  115. /** Returns the name of this suite. */
  116. std::string const&
  117. name() const
  118. {
  119. return name_;
  120. }
  121. /** Returns the total number of test conditions. */
  122. std::size_t
  123. total() const
  124. {
  125. return total_;
  126. }
  127. /** Returns the number of failures. */
  128. std::size_t
  129. failed() const
  130. {
  131. return failed_;
  132. }
  133. /** Insert a set of testcase results. */
  134. /** @{ */
  135. void
  136. insert(case_results&& r)
  137. {
  138. cont().emplace_back(std::move(r));
  139. total_ += r.tests.total();
  140. failed_ += r.tests.failed();
  141. }
  142. void
  143. insert(case_results const& r)
  144. {
  145. cont().push_back(r);
  146. total_ += r.tests.total();
  147. failed_ += r.tests.failed();
  148. }
  149. /** @} */
  150. };
  151. //------------------------------------------------------------------------------
  152. // VFALCO TODO Make this a template class using scoped allocators
  153. /** Holds the results of running a set of testsuites. */
  154. class results
  155. : public detail::const_container <std::vector <suite_results>>
  156. {
  157. private:
  158. std::size_t m_cases;
  159. std::size_t total_;
  160. std::size_t failed_;
  161. public:
  162. results()
  163. : m_cases(0)
  164. , total_(0)
  165. , failed_(0)
  166. {
  167. }
  168. /** Returns the total number of test cases. */
  169. std::size_t
  170. cases() const
  171. {
  172. return m_cases;
  173. }
  174. /** Returns the total number of test conditions. */
  175. std::size_t
  176. total() const
  177. {
  178. return total_;
  179. }
  180. /** Returns the number of failures. */
  181. std::size_t
  182. failed() const
  183. {
  184. return failed_;
  185. }
  186. /** Insert a set of suite results. */
  187. /** @{ */
  188. void
  189. insert(suite_results&& r)
  190. {
  191. m_cases += r.size();
  192. total_ += r.total();
  193. failed_ += r.failed();
  194. cont().emplace_back(std::move(r));
  195. }
  196. void
  197. insert(suite_results const& r)
  198. {
  199. m_cases += r.size();
  200. total_ += r.total();
  201. failed_ += r.failed();
  202. cont().push_back(r);
  203. }
  204. /** @} */
  205. };
  206. } // unit_test
  207. } // beast
  208. } // boost
  209. #endif