Search code examples
c++googletesttemplate-meta-programming

Is it possible to provide gtest type parameterized test macro a type list?


I find gtest type parameterized test very efficient in building unit tests. However, I wonder is it possible to avoid hard coded the types in ::testing::Types<>?

Here is example from gtest website:

using MyTypes = ::testing::Types<char, int, unsigned int>;
INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);

I have been using boost mp11 for template meta programming (TMP). Using mp11, I can easily construct a list containing different types. Therefore, instead of hard coded char, int, unsigned int inside the ::testing::Types<>, I hope I can do something as following:

using L1 = mp_list_c<char, int, uint32_t>;
using MyTypes = ::testing::Types<L1>;
INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);

The sample I present here seems not gain much since L1 is hard coded as well. However, if the ::testing::Types<> could accept a type list of some kind, I cold use TMP tricks to "dynamically" construct the type list. Is there a way to achieve what I want?


Solution

  • You can write a utility which will perform conversion:

    template<typename T>
    struct ToTestTypes;
    
    template<template<typename ...> typename U, typename...Ts>
    struct ToTestTypes<U<Ts...>>
    {
        using type = ::testing::Types<Ts...>;
    };
    
    template<typename T>
    using ToTestTypes_t = typename ToTestTypes<T>::type;
    

    This converts instance of any type parametrized type template into respective gtest type: ::testing::Types< ... >, so it will work with mp_list_c.

    I was unable to find gtest equivalent of this (maybe there is one).

    Demo