X25-ListenerCanAskForCapturedStdout.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. * Registers custom listener that asks for captured stdout/err
  8. *
  9. * Running the binary with this listener, and asking for multiple _noncapturing_
  10. * reporters (one would be sufficient, but that would also mean depending on
  11. * implementation details inside Catch2's handling of listeners), we check that
  12. * the listener gets redirected stdout, even though the reporters didn't ask for
  13. * it.
  14. */
  15. #include <catch2/catch_test_macros.hpp>
  16. #include <catch2/reporters/catch_reporter_event_listener.hpp>
  17. #include <catch2/reporters/catch_reporter_registrars.hpp>
  18. #include <iostream>
  19. namespace {
  20. class CapturingListener : public Catch::EventListenerBase {
  21. public:
  22. CapturingListener( Catch::IConfig const* config ):
  23. EventListenerBase( config ) {
  24. m_preferences.shouldRedirectStdOut = true;
  25. std::cerr << "CapturingListener initialized\n";
  26. }
  27. void
  28. testCaseEnded( Catch::TestCaseStats const& testCaseStats ) override {
  29. if ( testCaseStats.stdOut.empty() ) {
  30. std::cerr << "X25 - ERROR: empty stdout\n";
  31. }
  32. }
  33. };
  34. }
  35. CATCH_REGISTER_LISTENER( CapturingListener )
  36. TEST_CASE( "Writes to stdout" ) {
  37. std::cout << "X25 - FooBarBaz\n";
  38. }