Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bowie7070 opened this issue Oct 5, 2022 · 2 comments

Comments

@bowie7070
Copy link

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.

@cschreib
Copy link

cschreib commented Dec 9, 2022

I opened a PR recently to do this; see #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.

@tophyr
Copy link

tophyr commented Dec 4, 2023

#2771 does this as well now. Thanks to @cschreib for the ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants