Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/tests/ExtraTests
diff options
context:
space:
mode:
authorLeitwolf2026-10-11 10:09:19 +0300
committerGitHub2026-10-11 10:09:19 +0300
commitce6fa1996a8e358efdcff53a7628584077d9855c (patch)
treed76f5945311828511e3ea55712487f21fbf8f70f /tests/ExtraTests
parent5633c5c98ec073ae938253bcfc72430d5d9aee89 (diff)
parent1f78d8903c2fb18afea8c3055f8df83d8c60d9b5 (diff)
downloadcatch3-ce6fa1996a8e358efdcff53a7628584077d9855c.tar.gz
Merge pull request #4 from Progmasoft/backport/upstream-3.16.1
Backport Catch2 3.16.1
Diffstat (limited to 'tests/ExtraTests')
-rw-r--r--tests/ExtraTests/CMakeLists.txt35
-rw-r--r--tests/ExtraTests/X38-ThrowingReporter.cpp79
2 files changed, 114 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" );
+ }() );
+}