Skip to content

Allow control over generated part of test case name for TEMPLATE_LIST_TEST_CASE #2542

@bowie7070

Description

@bowie7070

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions