// 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) // #pragma once #include #include #include #include namespace beast::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 reason_) : pass(pass_), reason(std::move(reason_)) { } bool pass; std::string reason; }; private: class tests_t : public detail::const_container> { private: std::size_t failed_{0}; public: tests_t() = default; /** Returns the total number of test conditions. */ [[nodiscard]] std::size_t total() const { return cont().size(); } /** Returns the number of failed test conditions. */ [[nodiscard]] 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> { 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 name = "") : name_(std::move(name)) { } /** Returns the name of this testcase. */ [[nodiscard]] 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> { private: std::string name_; std::size_t total_ = 0; std::size_t failed_ = 0; public: explicit suite_results(std::string name = "") : name_(std::move(name)) { } /** Returns the name of this suite. */ [[nodiscard]] std::string const& name() const { return name_; } /** Returns the total number of test conditions. */ [[nodiscard]] std::size_t total() const { return total_; } /** Returns the number of failures. */ [[nodiscard]] std::size_t failed() const { return failed_; } /** Insert a set of testcase results. */ /** @{ */ void insert(case_results&& r) { total_ += r.tests.total(); failed_ += r.tests.failed(); cont().emplace_back(std::move(r)); } 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> { private: std::size_t m_cases{0}; std::size_t total_{0}; std::size_t failed_{0}; public: results() = default; /** Returns the total number of test cases. */ [[nodiscard]] std::size_t cases() const { return m_cases; } /** Returns the total number of test conditions. */ [[nodiscard]] std::size_t total() const { return total_; } /** Returns the number of failures. */ [[nodiscard]] 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); } /** @} */ }; } // namespace beast::unit_test