19 using Variant_t = std::variant<int, char, std::string, QString, double, float>;
35 void VisitorTest::testBasicVisitor ()
38 const auto& res =
Visit (v,
39 [] (
char) {
return true; },
40 [] (int) {
return false; },
41 [] (std::string) {
return false; },
42 [] (QString) {
return false; },
43 [] (double) {
return false; },
44 [] (float) {
return false; });
48 void VisitorTest::testBasicVisitorGenericFallback ()
51 const auto& res =
Visit (v,
52 [] (
char) {
return true; },
53 [] (int) {
return false; },
54 [] (
auto) {
return false; });
58 void VisitorTest::testBasicVisitorCoercion ()
61 const auto& res =
Visit (v,
62 [] (
int) {
return true; },
63 [] (std::string) {
return false; },
64 [] (QString) {
return false; },
65 [] (double) {
return false; },
66 [] (float) {
return false; });
70 void VisitorTest::testBasicVisitorCoercionGenericFallback ()
73 const auto& res =
Visit (v,
74 [] (
int) {
return false; },
75 [] (QString) {
return false; },
76 [] (
auto) {
return true; });
80 #define NC nc = std::unique_ptr<int> {}
82 void VisitorTest::testNonCopyableFunctors ()
85 const auto& res =
Visit (v,
86 [
NC] (
char) {
return true; },
87 [
NC] (int) {
return false; },
88 [
NC] (std::string) {
return false; },
89 [
NC] (QString) {
return false; },
90 [
NC] (double) {
return false; },
91 [
NC] (float) {
return false; });
96 void VisitorTest::testAcceptsRValueRef ()
99 [] (char) {
return true; },
100 [] (
auto) {
return false; });
101 QCOMPARE (res,
true);
104 void VisitorTest::testLValueRef ()
108 auto& res =
Visit (v, [&ref] (
auto) ->
int& {
return ref; });
113 void VisitorTest::testLValueRef2 ()
116 Visit (v, [] (
auto& s) ->
int& {
return s.field1; }) = 10;
117 const auto& res =
Visit (v, [] (
const auto& s) ->
const int& {
return s.field1; });
121 void VisitorTest::testPrepareVisitor ()
126 [] (char) {
return true; },
127 [] (int) {
return false; },
128 [] (std::string) {
return false; },
129 [] (QString) {
return false; },
130 [] (double) {
return false; },
131 [] (float) {
return false; }
134 const auto& res = visitor (v);
135 QCOMPARE (res,
true);
138 void VisitorTest::testPrepareVisitorConst ()
143 [] (char) {
return true; },
144 [] (int) {
return false; },
145 [] (std::string) {
return false; },
146 [] (QString) {
return false; },
147 [] (double) {
return false; },
148 [] (float) {
return false; }
151 const auto& res = visitor (v);
152 QCOMPARE (res,
true);
155 void VisitorTest::testPrepareVisitorRValue ()
159 [] (char) {
return true; },
160 [] (int) {
return false; },
161 [] (std::string) {
return false; },
162 [] (QString) {
return false; },
163 [] (double) {
return false; },
164 [] (float) {
return false; }
167 const auto& res = visitor (
Variant_t {
'a' });
168 QCOMPARE (res,
true);
171 void VisitorTest::testPrepareVisitorFinally ()
179 [] (char) {
return true; },
180 [] (
auto) {
return false; }
181 }.Finally ([&fin] { fin =
true; });
183 const auto& res = visitor (v);
184 QCOMPARE (res,
true);
185 QCOMPARE (fin,
true);
188 void VisitorTest::testPrepareJustAutoVisitor ()
190 using Variant_t = std::variant<int, double, float>;
194 [] (
auto e) {
return std::to_string (e); }
197 const auto& res = visitor (
Variant_t { 10 });
198 QCOMPARE (res, std::string {
"10" });
201 void VisitorTest::testPrepareRecursiveVisitor ()
203 using SubVariant_t = std::variant<int, double, float>;
204 using Variant_t = std::variant<SubVariant_t, QString>;
208 [] (
const QString& str) {
return str; },
209 Visitor { [] (
auto e) {
return QString::fromStdString (std::to_string (e)); } }
212 const auto& res = visitor (
Variant_t { SubVariant_t { 10 } });
213 QCOMPARE (res, QString {
"10" });
216 void VisitorTest::testPrepareVisitorMutable ()
221 [] (int)
mutable {
return true; },
222 [] (
auto)
mutable {
return false; }
225 const auto& res = visitor (v);
226 QCOMPARE (res,
false);