diff options
| author | Leitwolf | 2026-10-11 10:09:19 +0300 |
|---|---|---|
| committer | GitHub | 2026-10-11 10:09:19 +0300 |
| commit | ce6fa1996a8e358efdcff53a7628584077d9855c (patch) | |
| tree | d76f5945311828511e3ea55712487f21fbf8f70f /tests | |
| parent | 5633c5c98ec073ae938253bcfc72430d5d9aee89 (diff) | |
| parent | 1f78d8903c2fb18afea8c3055f8df83d8c60d9b5 (diff) | |
| download | catch3-ce6fa1996a8e358efdcff53a7628584077d9855c.tar.gz | |
Merge pull request #4 from Progmasoft/backport/upstream-3.16.1
Backport Catch2 3.16.1
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ExtraTests/CMakeLists.txt | 35 | ||||
| -rw-r--r-- | tests/ExtraTests/X38-ThrowingReporter.cpp | 79 | ||||
| -rw-r--r-- | tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp | 14 |
3 files changed, 128 insertions, 0 deletions
diff --git a/tests/ExtraTests/CMakeLists.txt b/tests/ExtraTests/CMakeLists.txt index 168b739d..98f050d6 100644 --- a/tests/ExtraTests/CMakeLists.txt +++ b/tests/ExtraTests/CMakeLists.txt @@ -482,6 +482,41 @@ set_tests_properties(Validation::TestCaseTags ) +add_executable(ThrowingReporter ${TESTS_DIR}/X38-ThrowingReporter.cpp) +target_link_libraries(ThrowingReporter PRIVATE Catch2::Catch2WithMain) +add_test( + NAME Reporters::ThrowInConstructor + COMMAND + $<TARGET_FILE:ThrowingReporter> + --reporter ThrowingReporter::Xthrow-in-constructor=true +) +set_tests_properties(Reporters::ThrowInConstructor + PROPERTIES + PASS_REGULAR_EXPRESSION "Error when creating reporter: Exception in constructor" +) + +foreach(reporterName "Automake" + "compact" + "console" + "JUnit" + "SonarQube" + "TAP" + "TeamCity" + "XML" + "JSON") + add_test(NAME "Reporters::RejectUnknownArgs::${reporterName}" + COMMAND + $<TARGET_FILE:SelfTest> + --reporter ${reporterName}::Xfoo=bar + "[comparisons]" # Limit selection to avoid running full test binary if the error check doesn't trigger + ) + set_tests_properties("Reporters::RejectUnknownArgs::${reporterName}" + PROPERTIES + PASS_REGULAR_EXPRESSION "Error when creating reporter: Unknown reporter argument: Xfoo" + ) +endforeach() + + add_executable(QuickExitInTest ${TESTS_DIR}/X40-QuickExit.cpp) target_link_libraries(QuickExitInTest PRIVATE Catch2::Catch2WithMain) add_test( diff --git a/tests/ExtraTests/X38-ThrowingReporter.cpp b/tests/ExtraTests/X38-ThrowingReporter.cpp new file mode 100644 index 00000000..5f660322 --- /dev/null +++ b/tests/ExtraTests/X38-ThrowingReporter.cpp @@ -0,0 +1,79 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +/**\file + * Runs simple test cases with reporter that likes to throw. + * + * Used to analyze how Catch2 currently treats throwing reporters/listeners, + * and what changes make sense for the future. + */ + +#include <catch2/catch_test_macros.hpp> +#include <catch2/reporters/catch_reporter_helpers.hpp> +#include <catch2/reporters/catch_reporter_registrars.hpp> +#include <catch2/reporters/catch_reporter_streaming_base.hpp> + +namespace { + + class ThrowingReporter final : public Catch::StreamingReporterBase { + bool m_throwInConstructor = false; + bool m_throwInAssertionEnded = false; + + void processOptions() { + if ( m_customOptions.find( "Xthrow-in-constructor" ) != + m_customOptions.end() ) { + m_throwInConstructor = true; + } + if ( m_customOptions.find( "Xthrow-in-assertion-ended" ) != + m_customOptions.end() ) { + m_throwInAssertionEnded = true; + } + } + + public: + ThrowingReporter( Catch::ReporterConfig&& config ): + StreamingReporterBase( CATCH_MOVE( config ) ) { + m_preferences.shouldReportAllAssertions = true; + m_preferences.shouldReportAllAssertionStarts = false; + + Catch::rejectSuperfluousConfigKeys( + m_customOptions, + { "Xthrow-in-constructor", "Xthrow-in-assertion-ended" } ); + processOptions(); + + if ( m_throwInConstructor ) { + throw std::runtime_error( "Exception in constructor" ); + } + } + + void assertionEnded( Catch::AssertionStats const& stats ) override { + m_stream << "assertionEnded\n"; + if ( m_throwInAssertionEnded ) { + throw std::runtime_error( "Exception in assertionEnded" ); + } + } + + static std::string getDescription() { + using namespace std::string_literals; + return "Throws for different reporter events"s; + } + }; + + CATCH_REGISTER_REPORTER( "ThrowingReporter", ThrowingReporter ) + +} // namespace + +TEST_CASE( "REQUIRE - Pass" ) { REQUIRE( true ); } + +TEST_CASE( "REQUIRE - Fail" ) { REQUIRE( false ); } + +TEST_CASE( "REQUIRE_THROWS - Pass" ) { + REQUIRE_THROWS( [] { + throw std::runtime_error( "expected" ); + }() ); +} diff --git a/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp b/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp index 4ff793f0..7abfbb7f 100644 --- a/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp @@ -330,3 +330,17 @@ TEST_CASE("Registering multiple reporters with the same name fails", Catch::Detail::make_unique<TestReporterFactory>() ), "reporter using 'some-reporter-name' as name was already registered" ); } + +TEST_CASE( "rejectSuperfluousConfigKeys helper", + "[reporters][reporter-helpers][approvals]" ) { + using Catch::rejectSuperfluousConfigKeys; using Catch::Matchers::ContainsSubstring; + + std::map<std::string, std::string> options { { "Xfoo", "1" }, { "Xbar", "2" } }; + REQUIRE_THROWS_WITH( rejectSuperfluousConfigKeys( options, {} ), + ContainsSubstring( "Unknown reporter argument:" ) ); + REQUIRE_THROWS_WITH( + rejectSuperfluousConfigKeys( options, { "Xfoo", "Xbaz" } ), + ContainsSubstring( "Unknown reporter argument: Xbar" ) ); + REQUIRE_NOTHROW( + Catch::rejectSuperfluousConfigKeys( options, { "Xfoo", "Xbaz", "Xbar" } ) ); +} |