catchorg / catchorg/Catch2

Allow control over generated part of test case name for TEMPLATE_LIST_TEST_CASE

Open
#2,542 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

I would like to be able to control the generated part of the test case name when using TEMPLATE_LIST_TEST_CASE. At present it uses the name of the type list and the index into the list. For example:

```
using arithmetic_type_list = type_list;

TEMPLATE_LIST_TEST_CASE("test-case", "[tag]", arithmetic_type_list) {}
```

With this code we end up with test cases with names like:
```
test-case - arithmetic_type_list - 0
test-case - arithmetic_type_list - 1
test-case - arithmetic_type_list - 2
test-case - arithmetic_type_list - 3
```

I would like to be able to have:
```
test-case - int
test-case - long
test-case - float
test-case - double
```

As far as I can tell the relevant code is:

```
#define INTERNAL_CATCH_TEMPLATE_LIST_TEST_CASE_2(TestName, TestFunc, Name, Tags, TmplList)\
CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \
CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \
CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS \
CATCH_INTERNAL_SUPPRESS_UNUSED_VARIABLE_WARNINGS \
template static void TestFunc(); \
namespace {\
namespace INTERNAL_CATCH_MAKE_NAMESPACE(TestName){\
INTERNAL_CATCH_TYPE_GEN\
template \
struct TestName { \
void reg_tests() { \
size_t index = 0; \
using expander = size_t[]; \
(void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestFunc ), CATCH_INTERNAL_LINEINFO, Catch::StringRef(), Catch::NameAndTags{ Name " - " + std::string(INTERNAL_CATCH_STRINGIZE(TmplList)) + " - " + std::to_string(index), Tags } ), index++)... };/* NOLINT */\
} \
};\
static int INTERNAL_CATCH_UNIQUE_NAME( globalRegistrar ) = [](){ \
using TestInit = typename convert::type; \
TestInit t; \
t.reg_tests(); \
return 0; \
}(); \
}}\
CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \
template \
static void TestFunc()
```

Specifically:

```
Catch::NameAndTags{ Name " - " + std::string(INTERNAL_CATCH_STRINGIZE(TmplList)) + " - " + std::to_string(index), Tags }
```

To get the behaviour I would like would require something like:

```
Catch::NameAndTags{ Name " - " + std::string(typeid(Types).name()), Tags }
```
Would you be happy with a macro such as:

```
#ifndef CATCH_TEMPLATE_LIST_TEST_CASE_SUFFIX
#define CATCH_TEMPLATE_LIST_TEST_CASE_SUFFIX(TmplList) std::string(INTERNAL_CATCH_STRINGIZE(TmplList)) + " - " + std::to_string(index)
#endif
```

So that the relevant code becomes:

```
Catch::NameAndTags{ Name " - " + CATCH_TEMPLATE_LIST_TEST_CASE_SUFFIX(TmplList), Tags }
```

Then I could put in my code something like:

```
#define CATCH_TEMPLATE_LIST_TEST_CASE_SUFFIX(TL) \
std::string(typeid(Types).name())
```

Thoughts? Problems with the macro definition/usage?

If you are happy with this approach, let me know and I should be able to prepare a pull request based on the above.

Contributor guide

Open the contributing guide

Research direction

The relevant entry point is INTERNAL_CATCH_TEMPLATE_LIST_TEST_CASE_2, especially the NameAndTags construction shown in the issue. Start by tracing how TEMPLATE_LIST_TEST_CASE expands through this macro and determine a supported customization point; done means the requested per-type names are generated without relying on the current type-list name and index.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.