Refactor Values() generator function to resolve more complicated type transformations

This commit is contained in:
stoorx 2025-02-24 13:44:21 +03:00
parent 0bdccf4aa2
commit 96e776c3c0
2 changed files with 15 additions and 30 deletions

View file

@ -332,9 +332,10 @@ internal::ParamGenerator<typename Container::value_type> ValuesIn(
// INSTANTIATE_TEST_SUITE_P(FloatingNumbers, BazTest, Values(1, 2, 3.5));
//
//
template <typename... T>
internal::ValueArray<T...> Values(T... v) {
return internal::ValueArray<T...>(std::move(v)...);
template <typename... Ts>
internal::ParamGenerator<std::common_type_t<Ts...>> Values(Ts... vs) {
return ValuesIn(
std::array<std::common_type_t<Ts...>, sizeof...(Ts)>{std::move(vs)...});
}
// Bool() allows generating tests with parameters in a set of (false, true).

View file

@ -180,6 +180,11 @@ class ParamGeneratorInterface {
virtual ParamIteratorInterface<T>* End() const = 0;
};
template <class GeneratedT,
typename StdFunction =
std::function<const GeneratedT&(const GeneratedT&)>>
class ParamConverterGenerator;
// Wraps ParamGeneratorInterface<T> and provides general generator syntax
// compatible with the STL Container concept.
// This class implements copy initialization semantics and the contained
@ -201,6 +206,11 @@ class ParamGenerator {
iterator begin() const { return iterator(impl_->Begin()); }
iterator end() const { return iterator(impl_->End()); }
template <typename R>
operator ParamGenerator<R>() {
return ParamConverterGenerator<T>(*this);
}
private:
std::shared_ptr<const ParamGeneratorInterface<T>> impl_;
};
@ -796,30 +806,6 @@ internal::ParamGenerator<typename Container::value_type> ValuesIn(
const Container& container);
namespace internal {
// Used in the Values() function to provide polymorphic capabilities.
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
template <typename... Ts>
class ValueArray {
public:
explicit ValueArray(Ts... v) : v_(FlatTupleConstructTag{}, std::move(v)...) {}
template <typename T>
operator ParamGenerator<T>() const { // NOLINT
return ValuesIn(MakeVector<T>(std::make_index_sequence<sizeof...(Ts)>()));
}
private:
template <typename T, size_t... I>
std::vector<T> MakeVector(std::index_sequence<I...>) const {
return std::vector<T>{static_cast<T>(v_.template Get<I>())...};
}
FlatTuple<Ts...> v_;
};
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
template <typename... T>
class CartesianProductGenerator
@ -1020,9 +1006,7 @@ class ParamGeneratorConverter : public ParamGeneratorInterface<To> {
Func converter_;
}; // class ParamGeneratorConverter
template <class GeneratedT,
typename StdFunction =
std::function<const GeneratedT&(const GeneratedT&)>>
template <class GeneratedT, typename StdFunction>
class ParamConverterGenerator {
public:
ParamConverterGenerator(ParamGenerator<GeneratedT> g) // NOLINT