Catch2 icon indicating copy to clipboard operation
Catch2 copied to clipboard

Allow control over generated part of test case name for TEMPLATE_LIST_TEST_CASE

Open bowie7070 opened this issue 3 years ago • 2 comments

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<int, long, float, double>;

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<typename TestType> static void TestFunc();       \
        namespace {\
        namespace INTERNAL_CATCH_MAKE_NAMESPACE(TestName){\
        INTERNAL_CATCH_TYPE_GEN\
        template<typename... Types>                               \
        struct TestName {                                         \
            void reg_tests() {                                          \
                size_t index = 0;                                    \
                using expander = size_t[];                           \
                (void)expander{(Catch::AutoReg( Catch::makeTestInvoker( &TestFunc<Types> ), 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<TestName, TmplList>::type; \
                TestInit t;                                           \
                t.reg_tests();                                        \
                return 0;                                             \
            }();                                                      \
        }}\
        CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION                       \
        template<typename TestType>                                   \
        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.

bowie7070 avatar Oct 05 '22 22:10 bowie7070

I opened a PR recently to do this; see https://github.com/catchorg/Catch2/pull/2537. The Catch2 test suites fails because the type names are not portable across all compilers in the CI matrix, but have I used this fork for my own projects without issues.

cschreib avatar Dec 09 '22 09:12 cschreib

https://github.com/catchorg/Catch2/pull/2771 does this as well now. Thanks to @cschreib for the ideas.

tophyr avatar Dec 04 '23 05:12 tophyr