Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

**Contents**<br>
[Prefixing Catch macros](#prefixing-catch-macros)<br>
[Naming tests from type lists](#naming-tests-from-type-lists)<br>
[Terminal colour](#terminal-colour)<br>
[Console width](#console-width)<br>
[stdout](#stdout)<br>
Expand Down Expand Up @@ -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
` - <type list name> - <index>` 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<Type>()
#include <catch2/catch_template_test_macros.hpp>
#include <tuple>

template <typename T> char const* testTypeName();
template <> char const* testTypeName<int>() { return "int"; }
template <> char const* testTypeName<double>() { return "double"; }

using MyTypes = std::tuple<int, double>;
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
Expand Down
4 changes: 4 additions & 0 deletions docs/test-cases-and-sections.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, char, float>;
Expand Down
4 changes: 4 additions & 0 deletions docs/test-fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, char, double>;
Expand Down
2 changes: 2 additions & 0 deletions src/catch2/catch_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
}
Expand Down
22 changes: 18 additions & 4 deletions src/catch2/internal/catch_template_test_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@
#ifndef CATCH_TEMPLATE_TEST_REGISTRY_HPP_INCLUDED
#define CATCH_TEMPLATE_TEST_REGISTRY_HPP_INCLUDED

#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
#include <catch2/internal/catch_test_registry.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_preprocessor.hpp>
#include <catch2/internal/catch_meta.hpp>
#include <catch2/internal/catch_unique_name.hpp>

#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
Expand Down Expand Up @@ -182,13 +196,13 @@
void reg_tests() { \
size_t index = 0; \
using expander = size_t[]; \
(void)expander{(Catch::AutoReg( &TestFunc<Types>, CATCH_INTERNAL_LINEINFO, Catch::StringRef(), Catch::NameAndTags{ Name " - " INTERNAL_CATCH_STRINGIZE(TmplList) " - " + std::to_string(index), Tags } ), index++)... };/* NOLINT */\
(void)expander{(Catch::AutoReg( &TestFunc<Types>, 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<TestName, TmplList>::type; \
TestInit t; \
t.reg_tests(); \
INTERNAL_CATCH_REGISTER_TEMPLATE_LIST( t ) \
return 0; \
}(); \
}}\
Expand Down Expand Up @@ -318,13 +332,13 @@
void reg_tests(){\
size_t index = 0;\
using expander = size_t[];\
(void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestName<Types>::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<Types>::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<TestNameClass, TmplList>::type;\
TestInit t;\
t.reg_tests();\
INTERNAL_CATCH_REGISTER_TEMPLATE_LIST( t ) \
return 0;\
}(); \
}}\
Expand Down
11 changes: 10 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
5 changes: 5 additions & 0 deletions tests/ExtraTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,10 @@ 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)

# 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.
Expand Down Expand Up @@ -738,6 +742,7 @@ set(EXTRA_TEST_BINARIES
FastCompileMacros
InfiniteGenerators
ThreadSafetyTests
TemplateListTestCaseSuffixes
# DebugBreakMacros
)

Expand Down
70 changes: 70 additions & 0 deletions tests/ExtraTests/X41-TemplateListTestCaseSuffixes.cpp
Original file line number Diff line number Diff line change
@@ -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<Type>(ListName, Index)
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_case_info.hpp>
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
#include <catch2/interfaces/catch_interfaces_testcase.hpp>

#include <cstddef>
#include <string>
#include <vector>

namespace {
int suffixCalls = 0;

template <typename...> struct TypeList;
using Types = TypeList<int&, double, int&>;
using FixtureTypes = TypeList<int&, double>;

template <typename T> char const* typeName();
template <> char const* typeName<int&>() { return "int&"; }
template <> char const* typeName<double>() { return "double"; }

template <typename T>
std::string typeSuffix( Catch::StringRef listName, std::size_t index ) {
if ( listName == "Types" && index == 2 ) {
return std::string( typeName<T>() ) + " again";
}
return typeName<T>();
}

template <typename T> struct Fixture {
Catch::StringRef expectedSuffix = typeName<T>();
};
} // namespace

TEMPLATE_LIST_TEST_CASE( "List test", "[list]", Types ) {
auto const name = Catch::getResultCapture().getCurrentTestName();
CHECK( name.find( typeName<TestType>() ) != 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<std::string> names;
for ( auto const& test : Catch::getRegistryHub().getTestCaseRegistry().getAllTests() ) {
names.push_back( test.getTestCaseInfo().name );
}

std::vector<std::string> 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 );
}
30 changes: 30 additions & 0 deletions tests/ThrowingTemplateListTestCaseSuffix.cpp
Original file line number Diff line number Diff line change
@@ -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 <catch2/catch_template_test_macros.hpp>

#include <stdexcept>
#include <tuple>

namespace {
char const* throwingSuffix( Catch::StringRef listName ) {
if ( listName == "Types" ) {
throw std::runtime_error( "Could not name type list test" );
}
throw 1;
}

template <typename> struct Fixture {};
} // namespace

using Types = std::tuple<int>;
TEMPLATE_LIST_TEST_CASE( "Test with a throwing suffix", "", Types ) {}

using FixtureTypes = std::tuple<int>;
TEMPLATE_LIST_TEST_CASE_METHOD( Fixture, "Fixture with a throwing suffix", "", FixtureTypes ) {}