Skip to content
New issue

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

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

Already on GitHub? # to your account

Support parameterized test on types without instantiating the types #667

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mrizaln
Copy link

@mrizaln mrizaln commented Mar 17, 2025

Problem:

Parameterized test on types requires instantiating the types itself inside a std::tuple. This has been a major pain point for me, and looking at the issues, someone also think the same: #539.

Solution:

Instead of using std::tuple, we can use a custom type that wraps a list of types that has no state (type_list). We also create a single wrapper type that wraps the type to be passed to ut::events::test as arg (wrapped_type). The purpose of the type itself is just to carry the type information it wraps. By doing this, any kind of types can be copied around without instantiating it.

namespace detail {
template <typename T>
struct wrapped_type {
  using type = T;
};
}

template <typename... Ts>
struct type_list {
  using types = std::tuple<Ts...>;
  static constexpr auto size = sizeof...(Ts);

  template <std::size_t I>
    requires(I < size)
  using get = detail::wrapped_type<std::tuple_element_t<I, types>>;
};

type_list then participate with other types on the operator| overloads so that it can be used.

template <typename F, typename... Ts>
[[nodiscard]] constexpr auto operator|(const F& f, type_list<Ts...>);

The usage should be like this:

"parameterized test"_test = []<typename T>() {
   // do something...
} | type_list<std::unique_ptr<int>, std::packaged_task<void()>>;

Issue: #539

Reviewers:
@

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

Successfully merging this pull request may close these issues.

1 participant