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