X27-CapturedStdoutInTestCaseEvents.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 captured stdout/err in (partial) testCaseEnded events
  8. * is correct (e.g. that the partial test case event does not get accumulated
  9. * output).
  10. *
  11. * This is done by having a single test case that is entered multiple
  12. * times through generator, and a custom capturing reporter that knows
  13. * what it should expect captured from the test case.
  14. */
  15. #include <catch2/catch_test_macros.hpp>
  16. #include <catch2/generators/catch_generators.hpp>
  17. #include <catch2/generators/catch_generators_range.hpp>
  18. #include <catch2/reporters/catch_reporter_streaming_base.hpp>
  19. #include <catch2/reporters/catch_reporter_registrars.hpp>
  20. #include <iostream>
  21. #include <string>
  22. #include <utility>
  23. class TestReporter : public Catch::StreamingReporterBase {
  24. std::string stdOutString( uint64_t iter ){
  25. return "stdout " + std::to_string( iter ) + '\n';
  26. }
  27. std::string stdErrString(uint64_t iter) {
  28. return "stderr " + std::to_string( iter ) + '\n';
  29. }
  30. public:
  31. TestReporter( Catch::ReporterConfig&& _config ):
  32. StreamingReporterBase( std::move(_config) ) {
  33. m_preferences.shouldRedirectStdOut = true;
  34. std::cout << "X27 - TestReporter constructed\n";
  35. }
  36. static std::string getDescription() {
  37. return "X27 test reporter";
  38. }
  39. void testCasePartialEnded( Catch::TestCaseStats const& stats,
  40. uint64_t iter ) override {
  41. if ( stats.stdOut != stdOutString( iter ) ) {
  42. std::cerr << "X27 ERROR in partial stdout\n" << stats.stdOut;
  43. }
  44. if ( stats.stdErr != stdErrString( iter ) ) {
  45. std::cerr << "X27 ERROR in partial stderr\n" << stats.stdErr;
  46. }
  47. }
  48. void testCaseEnded( Catch::TestCaseStats const& stats ) override {
  49. if ( stats.stdOut != "stdout 0\nstdout 1\nstdout 2\nstdout 3\nstdout 4\nstdout 5\n" ) {
  50. std::cerr << "X27 ERROR in full stdout\n" << stats.stdOut;
  51. }
  52. if ( stats.stdErr != "stderr 0\nstderr 1\nstderr 2\nstderr 3\nstderr 4\nstderr 5\n" ) {
  53. std::cerr << "X27 ERROR in full stderr\n" << stats.stdErr;
  54. }
  55. }
  56. ~TestReporter() override;
  57. };
  58. TestReporter::~TestReporter() = default;
  59. CATCH_REGISTER_REPORTER( "test-reporter", TestReporter )
  60. TEST_CASE( "repeatedly entered test case" ) {
  61. auto i = GENERATE( range(0, 6) );
  62. std::cout << "stdout " << i << '\n';
  63. // Switch between writing to std::cerr and std::clog just to make sure
  64. // both are properly captured and redirected.
  65. ( ( i % 2 == 0 ) ? std::cerr : std::clog ) << "stderr " << i << '\n';
  66. }