From 0c00b4b9428fdf273c9455387df02f666f509c75 Mon Sep 17 00:00:00 2001 From: Michael B Date: Fri, 20 Nov 2026 00:30:29 +0300 Subject: [PATCH 1/2] Customize type-list test names Add a suffix hook for both type-list test macros. Document it and cover custom names and startup errors. --- docs/configuration.md | 35 ++++++++++ docs/test-cases-and-sections.md | 4 ++ docs/test-fixtures.md | 4 ++ src/catch2/catch_session.cpp | 2 + .../internal/catch_template_test_registry.hpp | 22 ++++-- tests/ExtraTests/CMakeLists.txt | 13 ++++ .../X41-TemplateListTestCaseSuffixes.cpp | 70 +++++++++++++++++++ ...X42-ThrowingTemplateListTestCaseSuffix.cpp | 30 ++++++++ 8 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 tests/ExtraTests/X41-TemplateListTestCaseSuffixes.cpp create mode 100644 tests/ExtraTests/X42-ThrowingTemplateListTestCaseSuffix.cpp diff --git a/docs/configuration.md b/docs/configuration.md index 358671041b..7f14123658 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -3,6 +3,7 @@ **Contents**
[Prefixing Catch macros](#prefixing-catch-macros)
+[Naming tests from type lists](#naming-tests-from-type-lists)
[Terminal colour](#terminal-colour)
[Console width](#console-width)
[stdout](#stdout)
@@ -34,6 +35,40 @@ with the same name. To keep test code clean and uncluttered Catch uses short macro names (e.g. ```TEST_CASE``` and ```REQUIRE```). Occasionally these may conflict with identifiers from platform headers or the system under test. In this case the above identifier can be defined. This will cause all the Catch user macros to be prefixed with ```CATCH_``` (e.g. ```CATCH_TEST_CASE``` and ```CATCH_REQUIRE```). +## Naming tests from type lists + +> [Introduced](https://github.com/catchorg/Catch2/issues/2542) in Catch2 X.Y.Z + +`TEMPLATE_LIST_TEST_CASE` and `TEMPLATE_LIST_TEST_CASE_METHOD` add +` - - ` to each test name by default. + +To customize the suffix, define +`CATCH_CONFIG_TEMPLATE_LIST_TEST_CASE_SUFFIX(Type, ListName, Index)` before +including Catch2 headers in the translation unit that defines the tests. +`Type` is the current C++ type, `ListName` is the stringified type list +argument, and `Index` is the zero-based position as a `std::size_t` value. +The macro expands to an expression from which Catch2 constructs a `std::string` +during static test registration. Catch2 appends the resulting string after ` - `. +Use distinct suffixes for entries in the same test, including repeated types. + +```cpp +#define CATCH_CONFIG_TEMPLATE_LIST_TEST_CASE_SUFFIX(Type, ListName, Index) \ + testTypeName() +#include +#include + +template char const* testTypeName(); +template <> char const* testTypeName() { return "int"; } +template <> char const* testTypeName() { return "double"; } + +using MyTypes = std::tuple; +TEMPLATE_LIST_TEST_CASE("Arithmetic", "[arithmetic]", MyTypes) { + REQUIRE(TestType{2} + TestType{2} == TestType{4}); +} +``` + +This registers `Arithmetic - int` and `Arithmetic - double`. + ## Terminal colour CATCH_CONFIG_COLOUR_WIN32 // Force enables compiling colouring impl based on Win32 console API diff --git a/docs/test-cases-and-sections.md b/docs/test-cases-and-sections.md index e32ffa2a6d..8b5a1bb0e9 100644 --- a/docs/test-cases-and-sections.md +++ b/docs/test-cases-and-sections.md @@ -280,6 +280,10 @@ List can be `std::tuple`, `boost::mpl::list`, `boost::mp11::mp_list` or anything This allows you to reuse the _type list_ in multiple test cases. +See the "Naming tests from type lists" section of the +[compile-time configuration guide](configuration.md#top) to customize the +suffix added to each registered test name. + Example: ```cpp using MyTypes = std::tuple; diff --git a/docs/test-fixtures.md b/docs/test-fixtures.md index 9496dd1656..aece9df711 100644 --- a/docs/test-fixtures.md +++ b/docs/test-fixtures.md @@ -275,6 +275,10 @@ Catch2 also provides `TEMPLATE_LIST_TEST_CASE_METHOD` to support template fixtur template type lists like `std::tuple`, `boost::mpl::list` or `boost::mp11::mp_list`. This test case works the same as `TEMPLATE_TEST_CASE_METHOD`, only difference is the source of types. This allows you to reuse the template type list in multiple test cases. +See the "Naming tests from type lists" section of the +[compile-time configuration guide](configuration.md#top) to customize the +suffix added to each registered test name. + Example: ```cpp using MyTypes = std::tuple; diff --git a/src/catch2/catch_session.cpp b/src/catch2/catch_session.cpp index bd1fb5ade8..a3ea34ab08 100644 --- a/src/catch2/catch_session.cpp +++ b/src/catch2/catch_session.cpp @@ -222,6 +222,8 @@ namespace Catch { std::rethrow_exception(ex_ptr); } catch ( std::exception const& ex ) { errStream->stream() << TextFlow::Column( ex.what() ).indent(2) << '\n'; + } catch ( ... ) { + errStream->stream() << " Unknown exception\n"; } } } diff --git a/src/catch2/internal/catch_template_test_registry.hpp b/src/catch2/internal/catch_template_test_registry.hpp index 47da95bbfa..fe43166650 100644 --- a/src/catch2/internal/catch_template_test_registry.hpp +++ b/src/catch2/internal/catch_template_test_registry.hpp @@ -8,12 +8,26 @@ #ifndef CATCH_TEMPLATE_TEST_REGISTRY_HPP_INCLUDED #define CATCH_TEMPLATE_TEST_REGISTRY_HPP_INCLUDED +#include #include #include #include #include #include +#if defined( CATCH_CONFIG_TEMPLATE_LIST_TEST_CASE_SUFFIX ) +# define INTERNAL_CATCH_TEMPLATE_LIST_TEST_CASE_NAME( Name, Type, TmplList, Index ) \ + Name " - " + std::string( ( CATCH_CONFIG_TEMPLATE_LIST_TEST_CASE_SUFFIX( \ + Type, INTERNAL_CATCH_STRINGIZE( TmplList ), std::size_t{ Index } ) ) ) +# define INTERNAL_CATCH_REGISTER_TEMPLATE_LIST( Registrar ) \ + CATCH_TRY { Registrar.reg_tests(); } \ + CATCH_CATCH_ALL { ::Catch::getMutableRegistryHub().registerStartupException(); } +#else +# define INTERNAL_CATCH_TEMPLATE_LIST_TEST_CASE_NAME( Name, Type, TmplList, Index ) \ + Name " - " INTERNAL_CATCH_STRINGIZE( TmplList ) " - " + std::to_string( Index ) +# define INTERNAL_CATCH_REGISTER_TEMPLATE_LIST( Registrar ) Registrar.reg_tests(); +#endif + // GCC 5 and older do not properly handle disabling unused-variable warning // with a _Pragma. This means that we have to leak the suppression to the @@ -182,13 +196,13 @@ void reg_tests() { \ size_t index = 0; \ using expander = size_t[]; \ - (void)expander{(Catch::AutoReg( &TestFunc, CATCH_INTERNAL_LINEINFO, Catch::StringRef(), Catch::NameAndTags{ Name " - " INTERNAL_CATCH_STRINGIZE(TmplList) " - " + std::to_string(index), Tags } ), index++)... };/* NOLINT */\ + (void)expander{(Catch::AutoReg( &TestFunc, CATCH_INTERNAL_LINEINFO, Catch::StringRef(), Catch::NameAndTags{ INTERNAL_CATCH_TEMPLATE_LIST_TEST_CASE_NAME( Name, Types, TmplList, index ), Tags } ), index++)... };/* NOLINT */\ } \ };\ static const int INTERNAL_CATCH_UNIQUE_NAME( globalRegistrar ) = [](){ \ using TestInit = typename convert::type; \ TestInit t; \ - t.reg_tests(); \ + INTERNAL_CATCH_REGISTER_TEMPLATE_LIST( t ) \ return 0; \ }(); \ }}\ @@ -318,13 +332,13 @@ void reg_tests(){\ size_t index = 0;\ using expander = size_t[];\ - (void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestName::test ), CATCH_INTERNAL_LINEINFO, #ClassName##_catch_sr, Catch::NameAndTags{ Name " - " INTERNAL_CATCH_STRINGIZE(TmplList) " - " + std::to_string(index), Tags } ), index++)... };/* NOLINT */ \ + (void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestName::test ), CATCH_INTERNAL_LINEINFO, #ClassName##_catch_sr, Catch::NameAndTags{ INTERNAL_CATCH_TEMPLATE_LIST_TEST_CASE_NAME( Name, Types, TmplList, index ), Tags } ), index++)... };/* NOLINT */ \ }\ };\ static const int INTERNAL_CATCH_UNIQUE_NAME( globalRegistrar ) = [](){\ using TestInit = typename convert::type;\ TestInit t;\ - t.reg_tests();\ + INTERNAL_CATCH_REGISTER_TEMPLATE_LIST( t ) \ return 0;\ }(); \ }}\ diff --git a/tests/ExtraTests/CMakeLists.txt b/tests/ExtraTests/CMakeLists.txt index 98f050d6d4..b54f6af6e5 100644 --- a/tests/ExtraTests/CMakeLists.txt +++ b/tests/ExtraTests/CMakeLists.txt @@ -626,6 +626,17 @@ set_tests_properties(CompileConfiguration::FastCompile PASS_REGULAR_EXPRESSION "test cases: 6 \\| 1 passed \\| 1 failed \\| 4 failed as expected\nassertions: 13 \\| 6 passed \\| 2 failed \\| 5 failed as expected" ) +add_executable(TemplateListTestCaseSuffixes ${TESTS_DIR}/X41-TemplateListTestCaseSuffixes.cpp) +target_link_libraries(TemplateListTestCaseSuffixes PRIVATE Catch2::Catch2WithMain) +add_test(NAME TemplateListTestCaseSuffixes COMMAND TemplateListTestCaseSuffixes) + +add_executable(ThrowingTemplateListTestCaseSuffix ${TESTS_DIR}/X42-ThrowingTemplateListTestCaseSuffix.cpp) +target_link_libraries(ThrowingTemplateListTestCaseSuffix PRIVATE Catch2::Catch2WithMain) +add_test(NAME ThrowingTemplateListTestCaseSuffix COMMAND ThrowingTemplateListTestCaseSuffix) +set_tests_properties(ThrowingTemplateListTestCaseSuffix PROPERTIES + PASS_REGULAR_EXPRESSION "Errors occurred during startup![\r\n ]+Could not name type list test[\r\n ]+Unknown exception" +) + # This sets up a one-off executable that compiles against the amalgamated # files, and then runs it for a super simple check that the amalgamated # files are usable. @@ -738,6 +749,8 @@ set(EXTRA_TEST_BINARIES FastCompileMacros InfiniteGenerators ThreadSafetyTests + TemplateListTestCaseSuffixes + ThrowingTemplateListTestCaseSuffix # DebugBreakMacros ) diff --git a/tests/ExtraTests/X41-TemplateListTestCaseSuffixes.cpp b/tests/ExtraTests/X41-TemplateListTestCaseSuffixes.cpp new file mode 100644 index 0000000000..c53bb86444 --- /dev/null +++ b/tests/ExtraTests/X41-TemplateListTestCaseSuffixes.cpp @@ -0,0 +1,70 @@ +// 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 + +#define CATCH_CONFIG_TEMPLATE_LIST_TEST_CASE_SUFFIX(Type, ListName, Index) \ + ++suffixCalls, typeSuffix(ListName, Index) +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace { + int suffixCalls = 0; + + template struct TypeList; + using Types = TypeList; + using FixtureTypes = TypeList; + + template char const* typeName(); + template <> char const* typeName() { return "int&"; } + template <> char const* typeName() { return "double"; } + + template + std::string typeSuffix( Catch::StringRef listName, std::size_t index ) { + if ( listName == "Types" && index == 2 ) { + return std::string( typeName() ) + " again"; + } + return typeName(); + } + + template struct Fixture { + Catch::StringRef expectedSuffix = typeName(); + }; +} // namespace + +TEMPLATE_LIST_TEST_CASE( "List test", "[list]", Types ) { + auto const name = Catch::getResultCapture().getCurrentTestName(); + CHECK( name.find( typeName() ) != std::string::npos ); +} + +TEMPLATE_LIST_TEST_CASE_METHOD( Fixture, "List fixture test", "[list]", FixtureTypes ) { + auto const name = Catch::getResultCapture().getCurrentTestName(); + CHECK( name == "List fixture test - " + this->expectedSuffix ); +} + +TEST_CASE( "Type list suffixes form the registered names" ) { + std::vector names; + for ( auto const& test : Catch::getRegistryHub().getTestCaseRegistry().getAllTests() ) { + names.push_back( test.getTestCaseInfo().name ); + } + + std::vector const expected{ + "List test - int&", + "List test - double", + "List test - int& again", + "List fixture test - int&", + "List fixture test - double", + "Type list suffixes form the registered names", + }; + CHECK( names == expected ); + CHECK( suffixCalls == 5 ); +} diff --git a/tests/ExtraTests/X42-ThrowingTemplateListTestCaseSuffix.cpp b/tests/ExtraTests/X42-ThrowingTemplateListTestCaseSuffix.cpp new file mode 100644 index 0000000000..abb79bc062 --- /dev/null +++ b/tests/ExtraTests/X42-ThrowingTemplateListTestCaseSuffix.cpp @@ -0,0 +1,30 @@ +// 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 + +#define CATCH_CONFIG_TEMPLATE_LIST_TEST_CASE_SUFFIX(Type, ListName, Index) \ + throwingSuffix(ListName) +#include + +#include +#include + +namespace { + char const* throwingSuffix( Catch::StringRef listName ) { + if ( listName == "Types" ) { + throw std::runtime_error( "Could not name type list test" ); + } + throw 1; + } + + template struct Fixture {}; +} // namespace + +using Types = std::tuple; +TEMPLATE_LIST_TEST_CASE( "Test with a throwing suffix", "", Types ) {} + +using FixtureTypes = std::tuple; +TEMPLATE_LIST_TEST_CASE_METHOD( Fixture, "Fixture with a throwing suffix", "", FixtureTypes ) {} From 9f988df1f1f9080590c85ab8000618fc730d4db1 Mon Sep 17 00:00:00 2001 From: Michael B Date: Fri, 20 Nov 2026 01:40:53 +0300 Subject: [PATCH 2/2] Include startup suffix test in coverage Run the throwing suffix test from the basic CTest suite so AppVeyor collects its startup coverage. --- tests/CMakeLists.txt | 11 ++++++++++- tests/ExtraTests/CMakeLists.txt | 8 -------- ...fix.cpp => ThrowingTemplateListTestCaseSuffix.cpp} | 0 3 files changed, 10 insertions(+), 9 deletions(-) rename tests/{ExtraTests/X42-ThrowingTemplateListTestCaseSuffix.cpp => ThrowingTemplateListTestCaseSuffix.cpp} (100%) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 33daefb891..18ac96ab31 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -176,6 +176,15 @@ set_tests_properties(RunTests PROPERTIES COST 15 ) +# Startup errors require a separate executable. Keep it in the basic suite +# so its coverage is collected alongside SelfTest's. +add_executable(ThrowingTemplateListTestCaseSuffix ThrowingTemplateListTestCaseSuffix.cpp) +target_link_libraries(ThrowingTemplateListTestCaseSuffix PRIVATE Catch2::Catch2WithMain) +add_test(NAME ThrowingTemplateListTestCaseSuffix COMMAND ThrowingTemplateListTestCaseSuffix) +set_tests_properties(ThrowingTemplateListTestCaseSuffix PROPERTIES + PASS_REGULAR_EXPRESSION "Errors occurred during startup![\r\n ]+Could not name type list test[\r\n ]+Unknown exception" +) + # Because CTest does not allow us to check both return code _and_ expected # output in one test, we run these commands twice. First time we check # the output, the second time we check the exit code. @@ -750,5 +759,5 @@ set_tests_properties("Bazel::RngSeedEnvVar::MalformedValueIsIgnored" PASS_REGULAR_EXPRESSION "Randomness seeded to: 17171717" ) -list(APPEND CATCH_TEST_TARGETS SelfTest) +list(APPEND CATCH_TEST_TARGETS SelfTest ThrowingTemplateListTestCaseSuffix) set(CATCH_TEST_TARGETS ${CATCH_TEST_TARGETS} PARENT_SCOPE) diff --git a/tests/ExtraTests/CMakeLists.txt b/tests/ExtraTests/CMakeLists.txt index b54f6af6e5..0269032006 100644 --- a/tests/ExtraTests/CMakeLists.txt +++ b/tests/ExtraTests/CMakeLists.txt @@ -630,13 +630,6 @@ add_executable(TemplateListTestCaseSuffixes ${TESTS_DIR}/X41-TemplateListTestCas target_link_libraries(TemplateListTestCaseSuffixes PRIVATE Catch2::Catch2WithMain) add_test(NAME TemplateListTestCaseSuffixes COMMAND TemplateListTestCaseSuffixes) -add_executable(ThrowingTemplateListTestCaseSuffix ${TESTS_DIR}/X42-ThrowingTemplateListTestCaseSuffix.cpp) -target_link_libraries(ThrowingTemplateListTestCaseSuffix PRIVATE Catch2::Catch2WithMain) -add_test(NAME ThrowingTemplateListTestCaseSuffix COMMAND ThrowingTemplateListTestCaseSuffix) -set_tests_properties(ThrowingTemplateListTestCaseSuffix PROPERTIES - PASS_REGULAR_EXPRESSION "Errors occurred during startup![\r\n ]+Could not name type list test[\r\n ]+Unknown exception" -) - # This sets up a one-off executable that compiles against the amalgamated # files, and then runs it for a super simple check that the amalgamated # files are usable. @@ -750,7 +743,6 @@ set(EXTRA_TEST_BINARIES InfiniteGenerators ThreadSafetyTests TemplateListTestCaseSuffixes - ThrowingTemplateListTestCaseSuffix # DebugBreakMacros ) diff --git a/tests/ExtraTests/X42-ThrowingTemplateListTestCaseSuffix.cpp b/tests/ThrowingTemplateListTestCaseSuffix.cpp similarity index 100% rename from tests/ExtraTests/X42-ThrowingTemplateListTestCaseSuffix.cpp rename to tests/ThrowingTemplateListTestCaseSuffix.cpp