Skip to content

Commit

Permalink
Improve naming for TEMPLATE_LIST_TEST_CASE_METHOD
Browse files Browse the repository at this point in the history
It's not often very useful to name tests simply by incrementing numbers.
This change adds a mechanism by which a name can be generated using both
the index of the type in the type list, and also some from the type
itself somehow.
  • Loading branch information
tophyr committed Dec 11, 2023
1 parent c18b88f commit df90ea3
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 20 deletions.
14 changes: 12 additions & 2 deletions src/catch2/internal/catch_template_test_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@
template<typename... Types> \
struct TestName { \
void reg_tests() { \
using Catch::IndexedTestTypeName; \
size_t index = 0; \
using expander = size_t[]; \
(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 */\
(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 + IndexedTestTypeName<Types>{}(index), Tags } ), index++)... };/* NOLINT */\
} \
};\
static int INTERNAL_CATCH_UNIQUE_NAME( globalRegistrar ) = [](){ \
Expand Down Expand Up @@ -320,9 +321,10 @@
template<typename...Types>\
struct TestNameClass{\
void reg_tests(){\
using Catch::IndexedTestTypeName;\
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_CONFIG_NAME_SEPARATOR INTERNAL_CATCH_STRINGIZE(TmplList) INTERNAL_CATCH_CONFIG_NAME_SEPARATOR + std::to_string(index), Tags } ), index++)... };/* NOLINT */ \
(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 + IndexedTestTypeName<Types>{}(index), Tags } ), index++)... };/* NOLINT */ \
}\
};\
static int INTERNAL_CATCH_UNIQUE_NAME( globalRegistrar ) = [](){\
Expand All @@ -339,5 +341,13 @@
#define INTERNAL_CATCH_TEMPLATE_LIST_TEST_CASE_METHOD(ClassName, Name, Tags, TmplList) \
INTERNAL_CATCH_TEMPLATE_LIST_TEST_CASE_METHOD_2( INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEMPLATE_TEST_ ), INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEMPLATE_TEST_ ), ClassName, Name, Tags, TmplList )

namespace Catch {
template <typename T>
struct IndexedTestTypeName {
std::string operator()(size_t index) const {
return std::to_string(index);
}
};
}

#endif // CATCH_TEMPLATE_TEST_REGISTRY_HPP_INCLUDED
3 changes: 3 additions & 0 deletions tests/SelfTest/Baselines/automake.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ Message from section two
:test-result: FAIL Tabs and newlines show in output
:test-result: PASS Tag alias can be registered against tag patterns
:test-result: PASS Tags with spaces and non-alphanumerical characters are accepted
:test-result: PASS Template list test case with specialized IndexedTestTypeName--NamedTypes--A
:test-result: PASS Template list test case with specialized IndexedTestTypeName--NamedTypes--B
:test-result: PASS Template list test case with specialized IndexedTestTypeName--NamedTypes--C
:test-result: PASS Template test case method with test types specified inside std::tuple - MyTypes - 0
:test-result: PASS Template test case method with test types specified inside std::tuple - MyTypes - 1
:test-result: PASS Template test case method with test types specified inside std::tuple - MyTypes - 2
Expand Down
3 changes: 3 additions & 0 deletions tests/SelfTest/Baselines/automake.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@
:test-result: FAIL Tabs and newlines show in output
:test-result: PASS Tag alias can be registered against tag patterns
:test-result: PASS Tags with spaces and non-alphanumerical characters are accepted
:test-result: PASS Template list test case with specialized IndexedTestTypeName--NamedTypes--A
:test-result: PASS Template list test case with specialized IndexedTestTypeName--NamedTypes--B
:test-result: PASS Template list test case with specialized IndexedTestTypeName--NamedTypes--C
:test-result: PASS Template test case method with test types specified inside std::tuple - MyTypes - 0
:test-result: PASS Template test case method with test types specified inside std::tuple - MyTypes - 1
:test-result: PASS Template test case method with test types specified inside std::tuple - MyTypes - 2
Expand Down
7 changes: 5 additions & 2 deletions tests/SelfTest/Baselines/compact.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,9 @@ Tag.tests.cpp:<line number>: passed: registry.add( "@no square bracket at start]
Tag.tests.cpp:<line number>: passed: registry.add( "[@no square bracket at end", "", Catch::SourceLineInfo( "file", 3 ) )
Tag.tests.cpp:<line number>: passed: testCase.tags.size() == 2 for: 2 == 2
Tag.tests.cpp:<line number>: passed: testCase.tags, VectorContains( Tag( "tag with spaces" ) ) && VectorContains( Tag( "I said \"good day\" sir!"_catch_sr ) ) for: { {?}, {?} } ( Contains: {?} and Contains: {?} )
CustomNaming.tests.cpp:<line number>: passed: Template_Foo<TestType>{}.size() == 0 for: 0 == 0
CustomNaming.tests.cpp:<line number>: passed: Template_Foo<TestType>{}.size() == 0 for: 0 == 0
CustomNaming.tests.cpp:<line number>: passed: Template_Foo<TestType>{}.size() == 0 for: 0 == 0
Class.tests.cpp:<line number>: passed: Template_Fixture<TestType>::m_a == 1 for: 1 == 1
Class.tests.cpp:<line number>: passed: Template_Fixture<TestType>::m_a == 1 for: 1 == 1
Class.tests.cpp:<line number>: passed: Template_Fixture<TestType>::m_a == 1 for: 1.0 == 1
Expand Down Expand Up @@ -2706,7 +2709,7 @@ InternalBenchmark.tests.cpp:<line number>: passed: med == 18. for: 18.0 == 18.0
InternalBenchmark.tests.cpp:<line number>: passed: q3 == 23. for: 23.0 == 23.0
Misc.tests.cpp:<line number>: passed:
Misc.tests.cpp:<line number>: passed:
test cases: 434 | 329 passed | 85 failed | 6 skipped | 14 failed as expected
assertions: 2277 | 2096 passed | 146 failed | 35 failed as expected
test cases: 437 | 332 passed | 85 failed | 6 skipped | 14 failed as expected
assertions: 2280 | 2099 passed | 146 failed | 35 failed as expected


7 changes: 5 additions & 2 deletions tests/SelfTest/Baselines/compact.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,9 @@ Tag.tests.cpp:<line number>: passed: registry.add( "@no square bracket at start]
Tag.tests.cpp:<line number>: passed: registry.add( "[@no square bracket at end", "", Catch::SourceLineInfo( "file", 3 ) )
Tag.tests.cpp:<line number>: passed: testCase.tags.size() == 2 for: 2 == 2
Tag.tests.cpp:<line number>: passed: testCase.tags, VectorContains( Tag( "tag with spaces" ) ) && VectorContains( Tag( "I said \"good day\" sir!"_catch_sr ) ) for: { {?}, {?} } ( Contains: {?} and Contains: {?} )
CustomNaming.tests.cpp:<line number>: passed: Template_Foo<TestType>{}.size() == 0 for: 0 == 0
CustomNaming.tests.cpp:<line number>: passed: Template_Foo<TestType>{}.size() == 0 for: 0 == 0
CustomNaming.tests.cpp:<line number>: passed: Template_Foo<TestType>{}.size() == 0 for: 0 == 0
Class.tests.cpp:<line number>: passed: Template_Fixture<TestType>::m_a == 1 for: 1 == 1
Class.tests.cpp:<line number>: passed: Template_Fixture<TestType>::m_a == 1 for: 1 == 1
Class.tests.cpp:<line number>: passed: Template_Fixture<TestType>::m_a == 1 for: 1.0 == 1
Expand Down Expand Up @@ -2695,7 +2698,7 @@ InternalBenchmark.tests.cpp:<line number>: passed: med == 18. for: 18.0 == 18.0
InternalBenchmark.tests.cpp:<line number>: passed: q3 == 23. for: 23.0 == 23.0
Misc.tests.cpp:<line number>: passed:
Misc.tests.cpp:<line number>: passed:
test cases: 434 | 329 passed | 85 failed | 6 skipped | 14 failed as expected
assertions: 2277 | 2096 passed | 146 failed | 35 failed as expected
test cases: 437 | 332 passed | 85 failed | 6 skipped | 14 failed as expected
assertions: 2280 | 2099 passed | 146 failed | 35 failed as expected


4 changes: 2 additions & 2 deletions tests/SelfTest/Baselines/console.std.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,6 @@ due to unexpected exception with message:
Why would you throw a std::string?

===============================================================================
test cases: 434 | 343 passed | 70 failed | 7 skipped | 14 failed as expected
assertions: 2260 | 2096 passed | 129 failed | 35 failed as expected
test cases: 437 | 346 passed | 70 failed | 7 skipped | 14 failed as expected
assertions: 2263 | 2099 passed | 129 failed | 35 failed as expected

37 changes: 35 additions & 2 deletions tests/SelfTest/Baselines/console.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11793,6 +11793,39 @@ Tag.tests.cpp:<line number>: PASSED:
with expansion:
{ {?}, {?} } ( Contains: {?} and Contains: {?} )

-------------------------------------------------------------------------------
Template list test case with specialized IndexedTestTypeName--NamedTypes--A
-------------------------------------------------------------------------------
CustomNaming.tests.cpp:<line number>
...............................................................................

CustomNaming.tests.cpp:<line number>: PASSED:
REQUIRE( Template_Foo<TestType>{}.size() == 0 )
with expansion:
0 == 0

-------------------------------------------------------------------------------
Template list test case with specialized IndexedTestTypeName--NamedTypes--B
-------------------------------------------------------------------------------
CustomNaming.tests.cpp:<line number>
...............................................................................

CustomNaming.tests.cpp:<line number>: PASSED:
REQUIRE( Template_Foo<TestType>{}.size() == 0 )
with expansion:
0 == 0

-------------------------------------------------------------------------------
Template list test case with specialized IndexedTestTypeName--NamedTypes--C
-------------------------------------------------------------------------------
CustomNaming.tests.cpp:<line number>
...............................................................................

CustomNaming.tests.cpp:<line number>: PASSED:
REQUIRE( Template_Foo<TestType>{}.size() == 0 )
with expansion:
0 == 0

-------------------------------------------------------------------------------
Template test case method with test types specified inside std::tuple - MyTypes
- 0
Expand Down Expand Up @@ -18949,6 +18982,6 @@ Misc.tests.cpp:<line number>
Misc.tests.cpp:<line number>: PASSED:

===============================================================================
test cases: 434 | 329 passed | 85 failed | 6 skipped | 14 failed as expected
assertions: 2277 | 2096 passed | 146 failed | 35 failed as expected
test cases: 437 | 332 passed | 85 failed | 6 skipped | 14 failed as expected
assertions: 2280 | 2099 passed | 146 failed | 35 failed as expected

37 changes: 35 additions & 2 deletions tests/SelfTest/Baselines/console.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11786,6 +11786,39 @@ Tag.tests.cpp:<line number>: PASSED:
with expansion:
{ {?}, {?} } ( Contains: {?} and Contains: {?} )

-------------------------------------------------------------------------------
Template list test case with specialized IndexedTestTypeName--NamedTypes--A
-------------------------------------------------------------------------------
CustomNaming.tests.cpp:<line number>
...............................................................................

CustomNaming.tests.cpp:<line number>: PASSED:
REQUIRE( Template_Foo<TestType>{}.size() == 0 )
with expansion:
0 == 0

-------------------------------------------------------------------------------
Template list test case with specialized IndexedTestTypeName--NamedTypes--B
-------------------------------------------------------------------------------
CustomNaming.tests.cpp:<line number>
...............................................................................

CustomNaming.tests.cpp:<line number>: PASSED:
REQUIRE( Template_Foo<TestType>{}.size() == 0 )
with expansion:
0 == 0

-------------------------------------------------------------------------------
Template list test case with specialized IndexedTestTypeName--NamedTypes--C
-------------------------------------------------------------------------------
CustomNaming.tests.cpp:<line number>
...............................................................................

CustomNaming.tests.cpp:<line number>: PASSED:
REQUIRE( Template_Foo<TestType>{}.size() == 0 )
with expansion:
0 == 0

-------------------------------------------------------------------------------
Template test case method with test types specified inside std::tuple - MyTypes
- 0
Expand Down Expand Up @@ -18938,6 +18971,6 @@ Misc.tests.cpp:<line number>
Misc.tests.cpp:<line number>: PASSED:

===============================================================================
test cases: 434 | 329 passed | 85 failed | 6 skipped | 14 failed as expected
assertions: 2277 | 2096 passed | 146 failed | 35 failed as expected
test cases: 437 | 332 passed | 85 failed | 6 skipped | 14 failed as expected
assertions: 2280 | 2099 passed | 146 failed | 35 failed as expected

5 changes: 4 additions & 1 deletion tests/SelfTest/Baselines/junit.sw.approved.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact
>
<testsuite name="<exe-name>" errors="17" failures="129" skipped="12" tests="2289" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testsuite name="<exe-name>" errors="17" failures="129" skipped="12" tests="2292" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties>
<property name="random-seed" value="1"/>
<property name="filters" value="&quot;*&quot; ~[!nonportable] ~[!benchmark] ~[approvals]"/>
Expand Down Expand Up @@ -1337,6 +1337,9 @@ at Misc.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="Tag alias can be registered against tag patterns/The same tag alias can only be registered once" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Tag alias can be registered against tag patterns/Tag aliases must be of the form [@name]" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Tags with spaces and non-alphanumerical characters are accepted" time="{duration}" status="run"/>
<testcase classname="<exe-name>.Template_Foo" name="Template list test case with specialized IndexedTestTypeName--NamedTypes--A" time="{duration}" status="run"/>
<testcase classname="<exe-name>.Template_Foo" name="Template list test case with specialized IndexedTestTypeName--NamedTypes--B" time="{duration}" status="run"/>
<testcase classname="<exe-name>.Template_Foo" name="Template list test case with specialized IndexedTestTypeName--NamedTypes--C" time="{duration}" status="run"/>
<testcase classname="<exe-name>.Template_Fixture" name="Template test case method with test types specified inside std::tuple - MyTypes - 0" time="{duration}" status="run"/>
<testcase classname="<exe-name>.Template_Fixture" name="Template test case method with test types specified inside std::tuple - MyTypes - 1" time="{duration}" status="run"/>
<testcase classname="<exe-name>.Template_Fixture" name="Template test case method with test types specified inside std::tuple - MyTypes - 2" time="{duration}" status="run"/>
Expand Down
5 changes: 4 additions & 1 deletion tests/SelfTest/Baselines/junit.sw.multi.approved.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="<exe-name>" errors="17" failures="129" skipped="12" tests="2289" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testsuite name="<exe-name>" errors="17" failures="129" skipped="12" tests="2292" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties>
<property name="random-seed" value="1"/>
<property name="filters" value="&quot;*&quot; ~[!nonportable] ~[!benchmark] ~[approvals]"/>
Expand Down Expand Up @@ -1336,6 +1336,9 @@ at Misc.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="Tag alias can be registered against tag patterns/The same tag alias can only be registered once" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Tag alias can be registered against tag patterns/Tag aliases must be of the form [@name]" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Tags with spaces and non-alphanumerical characters are accepted" time="{duration}" status="run"/>
<testcase classname="<exe-name>.Template_Foo" name="Template list test case with specialized IndexedTestTypeName--NamedTypes--A" time="{duration}" status="run"/>
<testcase classname="<exe-name>.Template_Foo" name="Template list test case with specialized IndexedTestTypeName--NamedTypes--B" time="{duration}" status="run"/>
<testcase classname="<exe-name>.Template_Foo" name="Template list test case with specialized IndexedTestTypeName--NamedTypes--C" time="{duration}" status="run"/>
<testcase classname="<exe-name>.Template_Fixture" name="Template test case method with test types specified inside std::tuple - MyTypes - 0" time="{duration}" status="run"/>
<testcase classname="<exe-name>.Template_Fixture" name="Template test case method with test types specified inside std::tuple - MyTypes - 1" time="{duration}" status="run"/>
<testcase classname="<exe-name>.Template_Fixture" name="Template test case method with test types specified inside std::tuple - MyTypes - 2" time="{duration}" status="run"/>
Expand Down
3 changes: 3 additions & 0 deletions tests/SelfTest/Baselines/sonarqube.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,9 @@ at Condition.tests.cpp:<line number>
<testCase name="A TEMPLATE_TEST_CASE_METHOD_SIG based test with a custom separator--1" duration="{duration}"/>
<testCase name="A TEMPLATE_TEST_CASE_METHOD_SIG based test with a custom separator--3" duration="{duration}"/>
<testCase name="A TEMPLATE_TEST_CASE_METHOD_SIG based test with a custom separator--6" duration="{duration}"/>
<testCase name="Template list test case with specialized IndexedTestTypeName--NamedTypes--A" duration="{duration}"/>
<testCase name="Template list test case with specialized IndexedTestTypeName--NamedTypes--B" duration="{duration}"/>
<testCase name="Template list test case with specialized IndexedTestTypeName--NamedTypes--C" duration="{duration}"/>
<testCase name="Template test case method with test types specified inside std::tuple with a custom separator--MyTypes--0" duration="{duration}"/>
<testCase name="Template test case method with test types specified inside std::tuple with a custom separator--MyTypes--1" duration="{duration}"/>
<testCase name="Template test case method with test types specified inside std::tuple with a custom separator--MyTypes--2" duration="{duration}"/>
Expand Down
3 changes: 3 additions & 0 deletions tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,9 @@ at Condition.tests.cpp:<line number>
<testCase name="A TEMPLATE_TEST_CASE_METHOD_SIG based test with a custom separator--1" duration="{duration}"/>
<testCase name="A TEMPLATE_TEST_CASE_METHOD_SIG based test with a custom separator--3" duration="{duration}"/>
<testCase name="A TEMPLATE_TEST_CASE_METHOD_SIG based test with a custom separator--6" duration="{duration}"/>
<testCase name="Template list test case with specialized IndexedTestTypeName--NamedTypes--A" duration="{duration}"/>
<testCase name="Template list test case with specialized IndexedTestTypeName--NamedTypes--B" duration="{duration}"/>
<testCase name="Template list test case with specialized IndexedTestTypeName--NamedTypes--C" duration="{duration}"/>
<testCase name="Template test case method with test types specified inside std::tuple with a custom separator--MyTypes--0" duration="{duration}"/>
<testCase name="Template test case method with test types specified inside std::tuple with a custom separator--MyTypes--1" duration="{duration}"/>
<testCase name="Template test case method with test types specified inside std::tuple with a custom separator--MyTypes--2" duration="{duration}"/>
Expand Down
Loading

0 comments on commit df90ea3

Please sign in to comment.