X28-ListenersGetEventsBeforeReporters.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 different events are sent to listeners before they are
  8. * sent to the reporters.
  9. *
  10. * We only do this for a subset of the events, as doing all of them would
  11. * be annoying, and we can assume that their implementation is roughly
  12. * the same, and thus if few work, all work.
  13. */
  14. #include <catch2/catch_test_macros.hpp>
  15. #include <catch2/reporters/catch_reporter_event_listener.hpp>
  16. #include <catch2/reporters/catch_reporter_registrars.hpp>
  17. #include <catch2/reporters/catch_reporter_streaming_base.hpp>
  18. #include <iostream>
  19. #include <utility>
  20. namespace {
  21. static bool testRunStartingReceivedByListener = false;
  22. static bool testRunEndedReceivedByListener = false;
  23. static bool assertionStartingReceivedByListener = false;
  24. static bool assertionEndedReceivedByListener = false;
  25. class TestListener : public Catch::EventListenerBase {
  26. public:
  27. TestListener( Catch::IConfig const* config ):
  28. EventListenerBase( config ) {
  29. std::cout << "X28 - TestListener constructed.\n";
  30. }
  31. void testRunStarting( Catch::TestRunInfo const& ) override {
  32. testRunStartingReceivedByListener = true;
  33. }
  34. void testRunEnded( Catch::TestRunStats const& ) override {
  35. testRunEndedReceivedByListener = true;
  36. }
  37. void assertionStarting( Catch::AssertionInfo const& ) override {
  38. assertionStartingReceivedByListener = true;
  39. }
  40. void assertionEnded( Catch::AssertionStats const& ) override {
  41. assertionEndedReceivedByListener = true;
  42. }
  43. };
  44. class TestReporter : public Catch::StreamingReporterBase {
  45. public:
  46. TestReporter( Catch::ReporterConfig&& _config ):
  47. StreamingReporterBase( std::move(_config) ) {
  48. std::cout << "X28 - TestReporter constructed\n";
  49. }
  50. void testRunStarting( Catch::TestRunInfo const& ) override {
  51. if ( !testRunStartingReceivedByListener ) {
  52. std::cout << "X28 - ERROR\n";
  53. }
  54. }
  55. void testRunEnded( Catch::TestRunStats const& ) override {
  56. if ( !testRunEndedReceivedByListener ) {
  57. std::cout << "X28 - ERROR\n";
  58. }
  59. }
  60. void assertionStarting( Catch::AssertionInfo const& ) override {
  61. if ( !assertionStartingReceivedByListener ) {
  62. std::cout << "X28 - ERROR\n";
  63. }
  64. }
  65. void assertionEnded( Catch::AssertionStats const& ) override {
  66. if ( !assertionEndedReceivedByListener ) {
  67. std::cout << "X28 - ERROR\n";
  68. }
  69. }
  70. static std::string getDescription() { return "X28 test reporter"; }
  71. ~TestReporter() override;
  72. };
  73. TestReporter::~TestReporter() = default;
  74. } // end unnamed namespace
  75. CATCH_REGISTER_REPORTER( "test-reporter", TestReporter )
  76. CATCH_REGISTER_LISTENER( TestListener )
  77. TEST_CASE( "Dummy test case" ) { REQUIRE( 1 == 1 ); }