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