X22-BenchmarksInCumulativeReporter.cpp 2.3 KB

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