userver: userver/utest/parameter_names.hpp Source File
Loading...
Searching...
No Matches
parameter_names.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <string>
8#include <tuple>
9#include <type_traits>
10
11#include <gtest/gtest.h>
12
14
15USERVER_NAMESPACE_BEGIN
16
17namespace utest {
18
19namespace impl {
20
21template <typename ParamType>
22std::string TestParamToString(const ParamType& param) {
23 if constexpr (requires { ParamType::test_name; }) {
24 static_assert(std::is_same_v<std::string, decltype(param.test_name)>, "Test name should be a string");
25 return param.test_name;
26 } else {
27 return ::testing::PrintToString(param);
28 }
29}
30
31template <typename... Args>
32std::string TestTupleParamToString(const std::tuple<Args...>& params_tuple) {
33 static_assert((sizeof...(Args) != 0), "Test parameters should have at least one dimension");
34 // Underscore is the only valid non-alphanumeric character that can be used
35 // as a separator, see IsValidParamName in gtest-param-util.h.
36 return std::apply(
37 [](const auto& first, const auto&... rest) {
38 return (TestParamToString(first) + ... + ("_" + TestParamToString(rest)));
39 },
40 params_tuple
41 );
42}
43
44} // namespace impl
45
46// clang-format off
47
93
94// clang-format on
95struct PrintTestName final {
96 template <typename ParamType>
97 std::string operator()(const testing::TestParamInfo<ParamType>& info) const {
98 if constexpr (requires { std::tuple_size<ParamType>::value; }) {
99 return impl::TestTupleParamToString(info.param);
100 } else {
101 return impl::TestParamToString(info.param);
102 }
103 }
104};
105
106} // namespace utest
107
108USERVER_NAMESPACE_END