Skip to content

Commit 3d075f9

Browse files
committed
Enable configuration of Catch2 test naming
Catch2 produces tests names with spaces by default when using type-parametric macros like `TEMPLATE_TEST_CASE` and `TEMPLATE_PRODUCT_TEST_CASE`. For some test-runners that are incompatible with spaces in test names, this presents a significant problem: It's possible to define a test that's impossible to run. This adds a `CATCH_CONFIG_NAME_SEPARATOR` configuration value that can be used to override the separator string used during test naming.
1 parent 0520ff4 commit 3d075f9

22 files changed

+1148
-24
lines changed

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ expand_template(
3030
"#cmakedefine CATCH_CONFIG_FAST_COMPILE": "",
3131
"#cmakedefine CATCH_CONFIG_GETENV": "",
3232
"#cmakedefine CATCH_CONFIG_GLOBAL_NEXTAFTER": "",
33+
"#cmakedefine CATCH_CONFIG_NAME_SEPARATOR": "",
3334
"#cmakedefine CATCH_CONFIG_NO_ANDROID_LOGWRITE": "",
3435
"#cmakedefine CATCH_CONFIG_NO_COLOUR_WIN32": "",
3536
"#cmakedefine CATCH_CONFIG_NO_COUNTER": "",

src/catch2/catch_user_config.hpp.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210

211211
#define CATCH_CONFIG_DEFAULT_REPORTER "@CATCH_CONFIG_DEFAULT_REPORTER@"
212212
#define CATCH_CONFIG_CONSOLE_WIDTH @CATCH_CONFIG_CONSOLE_WIDTH@
213+
#cmakedefine CATCH_CONFIG_NAME_SEPARATOR
213214

214215
// Unlike the macros above, CATCH_CONFIG_FALLBACK_STRINGIFIER does not
215216
// have a good default value, so we cannot always define it, and cannot

src/catch2/internal/catch_template_test_registry.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
#pragma GCC diagnostic ignored "-Wunused-variable"
2323
#endif
2424

25+
#ifdef CATCH_CONFIG_NAME_SEPARATOR
26+
#define INTERNAL_CATCH_CONFIG_NAME_SEPARATOR CATCH_CONFIG_NAME_SEPARATOR
27+
#else
28+
#define INTERNAL_CATCH_CONFIG_NAME_SEPARATOR " - "
29+
#endif
30+
2531
#if defined(CATCH_CONFIG_DISABLE)
2632
#define INTERNAL_CATCH_TEMPLATE_TEST_CASE_NO_REGISTRATION_2( TestName, TestFunc, Name, Tags, Signature, ... ) \
2733
INTERNAL_CATCH_DEFINE_SIG_TEST(TestFunc, INTERNAL_CATCH_REMOVE_PARENS(Signature))
@@ -87,7 +93,7 @@
8793
size_t index = 0; \
8894
constexpr char const* tmpl_types[] = {CATCH_REC_LIST(INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS, __VA_ARGS__)}; /* NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,hicpp-avoid-c-arrays) */\
8995
using expander = size_t[]; /* NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,hicpp-avoid-c-arrays) */\
90-
(void)expander{(reg_test(Types{}, Catch::NameAndTags{ Name " - " + std::string(tmpl_types[index]), Tags } ), index++)... };/* NOLINT */ \
96+
(void)expander{(reg_test(Types{}, Catch::NameAndTags{ Name INTERNAL_CATCH_CONFIG_NAME_SEPARATOR + std::string(tmpl_types[index]), Tags } ), index++)... };/* NOLINT */ \
9197
}\
9298
};\
9399
static const int INTERNAL_CATCH_UNIQUE_NAME( globalRegistrar ) = [](){\
@@ -135,7 +141,7 @@
135141
constexpr char const* tmpl_types[] = {CATCH_REC_LIST(INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS, INTERNAL_CATCH_REMOVE_PARENS(TmplTypes))};\
136142
constexpr char const* types_list[] = {CATCH_REC_LIST(INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS, INTERNAL_CATCH_REMOVE_PARENS(TypesList))};\
137143
constexpr auto num_types = sizeof(types_list) / sizeof(types_list[0]);\
138-
(void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestFuncName<Types> ), CATCH_INTERNAL_LINEINFO, Catch::StringRef(), Catch::NameAndTags{ Name " - " + std::string(tmpl_types[index / num_types]) + '<' + std::string(types_list[index % num_types]) + '>', Tags } ), index++)... };/* NOLINT */\
144+
(void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestFuncName<Types> ), CATCH_INTERNAL_LINEINFO, Catch::StringRef(), Catch::NameAndTags{ Name INTERNAL_CATCH_CONFIG_NAME_SEPARATOR + std::string(tmpl_types[index / num_types]) + '<' + std::string(types_list[index % num_types]) + '>', Tags } ), index++)... };/* NOLINT */\
139145
} \
140146
}; \
141147
static int INTERNAL_CATCH_UNIQUE_NAME( globalRegistrar ) = [](){ \
@@ -181,7 +187,7 @@
181187
void reg_tests() { \
182188
size_t index = 0; \
183189
using expander = size_t[]; \
184-
(void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestFunc<Types> ), CATCH_INTERNAL_LINEINFO, Catch::StringRef(), Catch::NameAndTags{ Name " - " INTERNAL_CATCH_STRINGIZE(TmplList) " - " + std::to_string(index), Tags } ), index++)... };/* NOLINT */\
190+
(void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestFunc<Types> ), CATCH_INTERNAL_LINEINFO, Catch::StringRef(), Catch::NameAndTags{ Name INTERNAL_CATCH_CONFIG_NAME_SEPARATOR INTERNAL_CATCH_STRINGIZE(TmplList) INTERNAL_CATCH_CONFIG_NAME_SEPARATOR + std::to_string(index), Tags } ), index++)... };/* NOLINT */\
185191
} \
186192
};\
187193
static int INTERNAL_CATCH_UNIQUE_NAME( globalRegistrar ) = [](){ \
@@ -217,7 +223,7 @@
217223
size_t index = 0; \
218224
constexpr char const* tmpl_types[] = {CATCH_REC_LIST(INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS, __VA_ARGS__)};\
219225
using expander = size_t[];\
220-
(void)expander{(reg_test(Types{}, #ClassName, Catch::NameAndTags{ Name " - " + std::string(tmpl_types[index]), Tags } ), index++)... };/* NOLINT */ \
226+
(void)expander{(reg_test(Types{}, #ClassName, Catch::NameAndTags{ Name INTERNAL_CATCH_CONFIG_NAME_SEPARATOR + std::string(tmpl_types[index]), Tags } ), index++)... };/* NOLINT */ \
221227
}\
222228
};\
223229
static int INTERNAL_CATCH_UNIQUE_NAME( globalRegistrar ) = [](){\
@@ -267,7 +273,7 @@
267273
constexpr char const* tmpl_types[] = {CATCH_REC_LIST(INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS, INTERNAL_CATCH_REMOVE_PARENS(TmplTypes))};\
268274
constexpr char const* types_list[] = {CATCH_REC_LIST(INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS, INTERNAL_CATCH_REMOVE_PARENS(TypesList))};\
269275
constexpr auto num_types = sizeof(types_list) / sizeof(types_list[0]);\
270-
(void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestName<Types>::test ), CATCH_INTERNAL_LINEINFO, #ClassName, Catch::NameAndTags{ Name " - " + std::string(tmpl_types[index / num_types]) + '<' + std::string(types_list[index % num_types]) + '>', Tags } ), index++)... };/* NOLINT */ \
276+
(void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestName<Types>::test ), CATCH_INTERNAL_LINEINFO, #ClassName, Catch::NameAndTags{ Name INTERNAL_CATCH_CONFIG_NAME_SEPARATOR + std::string(tmpl_types[index / num_types]) + '<' + std::string(types_list[index % num_types]) + '>', Tags } ), index++)... };/* NOLINT */ \
271277
}\
272278
};\
273279
static int INTERNAL_CATCH_UNIQUE_NAME( globalRegistrar ) = [](){\
@@ -316,7 +322,7 @@
316322
void reg_tests(){\
317323
size_t index = 0;\
318324
using expander = size_t[];\
319-
(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 */ \
325+
(void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestName<Types>::test ), CATCH_INTERNAL_LINEINFO, #ClassName##_catch_sr, Catch::NameAndTags{ Name INTERNAL_CATCH_CONFIG_NAME_SEPARATOR INTERNAL_CATCH_STRINGIZE(TmplList) INTERNAL_CATCH_CONFIG_NAME_SEPARATOR + std::to_string(index), Tags } ), index++)... };/* NOLINT */ \
320326
}\
321327
};\
322328
static int INTERNAL_CATCH_UNIQUE_NAME( globalRegistrar ) = [](){\

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ set(TEST_SOURCES
113113
${SELF_TEST_DIR}/UsageTests/Class.tests.cpp
114114
${SELF_TEST_DIR}/UsageTests/Compilation.tests.cpp
115115
${SELF_TEST_DIR}/UsageTests/Condition.tests.cpp
116+
${SELF_TEST_DIR}/UsageTests/CustomNaming.tests.cpp
116117
${SELF_TEST_DIR}/UsageTests/Decomposition.tests.cpp
117118
${SELF_TEST_DIR}/UsageTests/EnumToString.tests.cpp
118119
${SELF_TEST_DIR}/UsageTests/Exception.tests.cpp

tests/SelfTest/Baselines/automake.sw.approved.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ Nor would this
4646
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test run that succeeds - Template_Foo<int>
4747
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test run that succeeds - std::vector<float>
4848
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test run that succeeds - std::vector<int>
49+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test with a custom separator--Template_Foo<float>
50+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test with a custom separator--Template_Foo<int>
51+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test with a custom separator--std::vector<float>
52+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test with a custom separator--std::vector<int>
4953
:test-result: FAIL A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that fails - Template_Foo_2<float, 6>
5054
:test-result: FAIL A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that fails - Template_Foo_2<int, 2>
5155
:test-result: FAIL A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that fails - std::array<float, 6>
@@ -54,18 +58,28 @@ Nor would this
5458
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that succeeds - Template_Foo_2<int,2>
5559
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that succeeds - std::array<float,6>
5660
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that succeeds - std::array<int,2>
61+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test with a custom separator--Template_Foo_2<float,6>
62+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test with a custom separator--Template_Foo_2<int,2>
63+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test with a custom separator--std::array<float,6>
64+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test with a custom separator--std::array<int,2>
5765
:test-result: FAIL A TEMPLATE_TEST_CASE_METHOD based test run that fails - double
5866
:test-result: FAIL A TEMPLATE_TEST_CASE_METHOD based test run that fails - float
5967
:test-result: FAIL A TEMPLATE_TEST_CASE_METHOD based test run that fails - int
6068
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD based test run that succeeds - double
6169
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD based test run that succeeds - float
6270
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD based test run that succeeds - int
71+
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD based test with a custom separator--double
72+
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD based test with a custom separator--float
73+
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD based test with a custom separator--int
6374
:test-result: FAIL A TEMPLATE_TEST_CASE_METHOD_SIG based test run that fails - 1
6475
:test-result: FAIL A TEMPLATE_TEST_CASE_METHOD_SIG based test run that fails - 3
6576
:test-result: FAIL A TEMPLATE_TEST_CASE_METHOD_SIG based test run that fails - 6
6677
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds - 1
6778
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds - 3
6879
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds - 6
80+
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test with a custom separator--1
81+
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test with a custom separator--3
82+
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test with a custom separator--6
6983
:test-result: FAIL A TEST_CASE_METHOD based test run that fails
7084
:test-result: PASS A TEST_CASE_METHOD based test run that succeeds
7185
:test-result: PASS A Template product test case - Foo<float>
@@ -247,6 +261,9 @@ Message from section two
247261
:test-result: PASS Template test case method with test types specified inside std::tuple - MyTypes - 0
248262
:test-result: PASS Template test case method with test types specified inside std::tuple - MyTypes - 1
249263
:test-result: PASS Template test case method with test types specified inside std::tuple - MyTypes - 2
264+
:test-result: PASS Template test case method with test types specified inside std::tuple with a custom separator--MyTypes--0
265+
:test-result: PASS Template test case method with test types specified inside std::tuple with a custom separator--MyTypes--1
266+
:test-result: PASS Template test case method with test types specified inside std::tuple with a custom separator--MyTypes--2
250267
:test-result: PASS Template test case with test types specified inside non-copyable and non-movable std::tuple - NonCopyableAndNonMovableTypes - 0
251268
:test-result: PASS Template test case with test types specified inside non-copyable and non-movable std::tuple - NonCopyableAndNonMovableTypes - 1
252269
:test-result: PASS Template test case with test types specified inside non-default-constructible std::tuple - MyNonDefaultConstructibleTypes - 0

tests/SelfTest/Baselines/automake.sw.multi.approved.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test run that succeeds - Template_Foo<int>
4545
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test run that succeeds - std::vector<float>
4646
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test run that succeeds - std::vector<int>
47+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test with a custom separator--Template_Foo<float>
48+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test with a custom separator--Template_Foo<int>
49+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test with a custom separator--std::vector<float>
50+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test with a custom separator--std::vector<int>
4751
:test-result: FAIL A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that fails - Template_Foo_2<float, 6>
4852
:test-result: FAIL A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that fails - Template_Foo_2<int, 2>
4953
:test-result: FAIL A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that fails - std::array<float, 6>
@@ -52,18 +56,28 @@
5256
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that succeeds - Template_Foo_2<int,2>
5357
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that succeeds - std::array<float,6>
5458
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that succeeds - std::array<int,2>
59+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test with a custom separator--Template_Foo_2<float,6>
60+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test with a custom separator--Template_Foo_2<int,2>
61+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test with a custom separator--std::array<float,6>
62+
:test-result: PASS A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test with a custom separator--std::array<int,2>
5563
:test-result: FAIL A TEMPLATE_TEST_CASE_METHOD based test run that fails - double
5664
:test-result: FAIL A TEMPLATE_TEST_CASE_METHOD based test run that fails - float
5765
:test-result: FAIL A TEMPLATE_TEST_CASE_METHOD based test run that fails - int
5866
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD based test run that succeeds - double
5967
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD based test run that succeeds - float
6068
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD based test run that succeeds - int
69+
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD based test with a custom separator--double
70+
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD based test with a custom separator--float
71+
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD based test with a custom separator--int
6172
:test-result: FAIL A TEMPLATE_TEST_CASE_METHOD_SIG based test run that fails - 1
6273
:test-result: FAIL A TEMPLATE_TEST_CASE_METHOD_SIG based test run that fails - 3
6374
:test-result: FAIL A TEMPLATE_TEST_CASE_METHOD_SIG based test run that fails - 6
6475
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds - 1
6576
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds - 3
6677
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds - 6
78+
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test with a custom separator--1
79+
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test with a custom separator--3
80+
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test with a custom separator--6
6781
:test-result: FAIL A TEST_CASE_METHOD based test run that fails
6882
:test-result: PASS A TEST_CASE_METHOD based test run that succeeds
6983
:test-result: PASS A Template product test case - Foo<float>
@@ -240,6 +254,9 @@
240254
:test-result: PASS Template test case method with test types specified inside std::tuple - MyTypes - 0
241255
:test-result: PASS Template test case method with test types specified inside std::tuple - MyTypes - 1
242256
:test-result: PASS Template test case method with test types specified inside std::tuple - MyTypes - 2
257+
:test-result: PASS Template test case method with test types specified inside std::tuple with a custom separator--MyTypes--0
258+
:test-result: PASS Template test case method with test types specified inside std::tuple with a custom separator--MyTypes--1
259+
:test-result: PASS Template test case method with test types specified inside std::tuple with a custom separator--MyTypes--2
243260
:test-result: PASS Template test case with test types specified inside non-copyable and non-movable std::tuple - NonCopyableAndNonMovableTypes - 0
244261
:test-result: PASS Template test case with test types specified inside non-copyable and non-movable std::tuple - NonCopyableAndNonMovableTypes - 1
245262
:test-result: PASS Template test case with test types specified inside non-default-constructible std::tuple - MyNonDefaultConstructibleTypes - 0

0 commit comments

Comments
 (0)