Hegel 0.18.2
Property-based testing for C++
Loading...
Searching...
No Matches
explicit.h
1#pragma once
2
3#include <any>
4#include <optional>
5#include <string>
6#include <type_traits>
7#include <utility>
8#include <vector>
9
10#include "repr.h"
11
12namespace hegel {
13
21 template <typename T> struct NamedValue {
23 std::string name;
26 };
27
38 template <typename T>
39 NamedValue<std::decay_t<T>> named(std::string name, T&& value) {
40 return {std::move(name), std::forward<T>(value)};
41 }
42
88 public:
90 // One value: its name if a named draw takes it, the value, and how
91 // to print it.
92 struct Value {
93 std::optional<std::string> name;
94 std::any value;
95 std::string (*render)(const std::any&);
96 };
98
107 template <
108 typename... Ts,
109 typename = std::enable_if_t<
110 !(std::is_same_v<std::decay_t<Ts>, ExplicitTestCase> || ...)>>
111 explicit ExplicitTestCase(Ts&&... values) {
112 (add(std::forward<Ts>(values)), ...);
113 }
114
116 const std::vector<Value>& values() const { return values_; }
118
119 private:
120 template <typename T> static std::string render(const std::any& value) {
121 return internal::repr(std::any_cast<const T&>(value));
122 }
123
124 template <typename T> static Value make_value(T value) {
125 return Value{std::nullopt, std::any(std::move(value)),
126 &ExplicitTestCase::render<T>};
127 }
128
129 template <typename T> struct is_named : std::false_type {};
130 template <typename T>
131 struct is_named<NamedValue<T>> : std::true_type {};
132
133 template <typename T> static Value positional(T&& value) {
134 using V = std::decay_t<T>;
135 if constexpr (std::is_same_v<V, const char*> ||
136 std::is_same_v<V, char*>) {
137 return make_value(std::string(value));
138 } else {
139 return make_value(V(std::forward<T>(value)));
140 }
141 }
142
143 template <typename T> void add(T&& value) {
144 if constexpr (is_named<std::decay_t<T>>::value) {
145 Value named_value = positional(std::forward<T>(value).value);
146 named_value.name = value.name;
147 values_.push_back(std::move(named_value));
148 } else {
149 values_.push_back(positional(std::forward<T>(value)));
150 }
151 }
152
153 std::vector<Value> values_;
154 };
155
156} // namespace hegel
ExplicitTestCase(Ts &&... values)
An explicit test case with the given values.
Definition explicit.h:111
Main namespace.
Definition config.h:29
NamedValue< std::decay_t< T > > named(std::string name, T &&value)
Give a value of an ExplicitTestCase to the draw named name.
Definition explicit.h:39
A value of an ExplicitTestCase that a named draw takes.
Definition explicit.h:21
std::string name
The name of the draw that takes the value.
Definition explicit.h:23
T value
The value.
Definition explicit.h:25