| 1 | |
| 2 | // Copyright Catch2 Authors |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE.txt or copy at |
| 5 | // https://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | |
| 9 | /**\file |
| 10 | * Test that the cumulative reporter base stores both assertions and |
| 11 | * benchmarks, and stores them in the right order. |
| 12 | * |
| 13 | * This is done through a custom reporter that writes out the assertions |
| 14 | * and benchmarks and checking that the output is in right order. |
| 15 | */ |
| 16 | |
| 17 | #include <catch2/catch_test_macros.hpp> |
| 18 | #include <catch2/benchmark/catch_benchmark.hpp> |
| 19 | #include <catch2/reporters/catch_reporter_cumulative_base.hpp> |
| 20 | #include <catch2/reporters/catch_reporter_registrars.hpp> |
| 21 | |
| 22 | #include <iostream> |
| 23 | #include <utility> |
| 24 | |
| 25 | class CumulativeBenchmarkReporter final : public Catch::CumulativeReporterBase { |
| 26 | |
| 27 | public: |
| 28 | CumulativeBenchmarkReporter(Catch::ReporterConfig&& _config) : |
| 29 | CumulativeReporterBase(std::move(_config)) { |
| 30 | m_preferences.shouldReportAllAssertions = true; |
| 31 | } |
| 32 | |
| 33 | static std::string getDescription() { |
| 34 | return "Custom reporter for testing cumulative reporter base"; |
| 35 | } |
| 36 | |
| 37 | virtual void testRunEndedCumulative() override; |
| 38 | }; |
| 39 | |
| 40 | CATCH_REGISTER_REPORTER("testReporter", CumulativeBenchmarkReporter) |
| 41 | |
| 42 | #include <chrono> |
| 43 | #include <thread> |
| 44 | |
| 45 | TEST_CASE("Some assertions and benchmarks") { |
| 46 | using namespace std::chrono_literals; |
| 47 | |
| 48 | REQUIRE(1); |
| 49 | BENCHMARK("2") { |
| 50 | std::this_thread::sleep_for(1ms); |
| 51 | }; |
| 52 | REQUIRE(3); |
| 53 | BENCHMARK("4") { |
| 54 | std::this_thread::sleep_for(1ms); |
| 55 | }; |
| 56 | REQUIRE(5); |
| 57 | } |
| 58 | |
| 59 | void CumulativeBenchmarkReporter::testRunEndedCumulative() { |
| 60 | auto const& testCases = m_testRun->children; |
| 61 | assert(testCases.size() == 1); |
| 62 | |
| 63 | auto const& testCase = *testCases.front(); |
| 64 | auto const& sections = testCase.children; |
| 65 | assert(sections.size() == 1); |
| 66 | |
| 67 | auto const& section = *sections.front(); |
| 68 | assert(section.childSections.empty()); |
| 69 | for (auto const& aob : section.assertionsAndBenchmarks) { |
| 70 | if (aob.isAssertion()) { |
| 71 | auto const& assertion = aob.asAssertion(); |
| 72 | std::cout << assertion.assertionResult.getExpandedExpression() << '\n'; |
| 73 | } |
| 74 | if (aob.isBenchmark()) { |
| 75 | auto const& bench = aob.asBenchmark(); |
| 76 | std::cout << bench.info.name << '\n'; |
| 77 | } |
| 78 | } |
| 79 | } |