LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
visitortest.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "visitortest.h"
10 #include <QtTest>
11 #include <visitor.h>
12 
13 QTEST_MAIN (LC::Util::VisitorTest)
14 
15 namespace LC
16 {
17 namespace Util
18 {
19  using Variant_t = std::variant<int, char, std::string, QString, double, float>;
20 
21  struct S1
22  {
23  int field1;
24  double field2;
25  };
26 
27  struct S2
28  {
29  int field1;
30  double field2;
31  };
32 
33  using SVariant_t = std::variant<S1, S2>;
34 
35  void VisitorTest::testBasicVisitor ()
36  {
37  Variant_t v { 'a' };
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; });
45  QCOMPARE (res, true);
46  }
47 
48  void VisitorTest::testBasicVisitorGenericFallback ()
49  {
50  Variant_t v { 'a' };
51  const auto& res = Visit (v,
52  [] (char) { return true; },
53  [] (int) { return false; },
54  [] (auto) { return false; });
55  QCOMPARE (res, true);
56  }
57 
58  void VisitorTest::testBasicVisitorCoercion ()
59  {
60  Variant_t v { 'a' };
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; });
67  QCOMPARE (res, true);
68  }
69 
70  void VisitorTest::testBasicVisitorCoercionGenericFallback ()
71  {
72  Variant_t v { 'a' };
73  const auto& res = Visit (v,
74  [] (int) { return false; },
75  [] (QString) { return false; },
76  [] (auto) { return true; });
77  QCOMPARE (res, true);
78  }
79 
80 #define NC nc = std::unique_ptr<int> {}
81 
82  void VisitorTest::testNonCopyableFunctors ()
83  {
84  Variant_t v { 'a' };
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; });
92  QCOMPARE (res, true);
93  }
94 #undef NC
95 
96  void VisitorTest::testAcceptsRValueRef ()
97  {
98  const auto& res = Visit (Variant_t { 'a' },
99  [] (char) { return true; },
100  [] (auto) { return false; });
101  QCOMPARE (res, true);
102  }
103 
104  void VisitorTest::testLValueRef ()
105  {
106  Variant_t v { 'a' };
107  int ref = 0;
108  auto& res = Visit (v, [&ref] (auto) -> int& { return ref; });
109  res = 10;
110  QCOMPARE (ref, 10);
111  }
112 
113  void VisitorTest::testLValueRef2 ()
114  {
115  SVariant_t v { S1 { 0, 0 } };
116  Visit (v, [] (auto& s) -> int& { return s.field1; }) = 10;
117  const auto& res = Visit (v, [] (const auto& s) -> const int& { return s.field1; });
118  QCOMPARE (res, 10);
119  }
120 
121  void VisitorTest::testPrepareVisitor ()
122  {
123  Variant_t v { 'a' };
124  Visitor visitor
125  {
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; }
132  };
133 
134  const auto& res = visitor (v);
135  QCOMPARE (res, true);
136  }
137 
138  void VisitorTest::testPrepareVisitorConst ()
139  {
140  const Variant_t v { 'a' };
141  Visitor visitor
142  {
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; }
149  };
150 
151  const auto& res = visitor (v);
152  QCOMPARE (res, true);
153  }
154 
155  void VisitorTest::testPrepareVisitorRValue ()
156  {
157  Visitor visitor
158  {
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; }
165  };
166 
167  const auto& res = visitor (Variant_t { 'a' });
168  QCOMPARE (res, true);
169  }
170 
171  void VisitorTest::testPrepareVisitorFinally ()
172  {
173  Variant_t v { 'a' };
174 
175  bool fin = false;
176 
177  auto visitor = Visitor
178  {
179  [] (char) { return true; },
180  [] (auto) { return false; }
181  }.Finally ([&fin] { fin = true; });
182 
183  const auto& res = visitor (v);
184  QCOMPARE (res, true);
185  QCOMPARE (fin, true);
186  }
187 
188  void VisitorTest::testPrepareJustAutoVisitor ()
189  {
190  using Variant_t = std::variant<int, double, float>;
191 
192  Visitor visitor
193  {
194  [] (auto e) { return std::to_string (e); }
195  };
196 
197  const auto& res = visitor (Variant_t { 10 });
198  QCOMPARE (res, std::string { "10" });
199  }
200 
201  void VisitorTest::testPrepareRecursiveVisitor ()
202  {
203  using SubVariant_t = std::variant<int, double, float>;
204  using Variant_t = std::variant<SubVariant_t, QString>;
205 
206  Visitor visitor
207  {
208  [] (const QString& str) { return str; },
209  Visitor { [] (auto e) { return QString::fromStdString (std::to_string (e)); } }
210  };
211 
212  const auto& res = visitor (Variant_t { SubVariant_t { 10 } });
213  QCOMPARE (res, QString { "10" });
214  }
215 
216  void VisitorTest::testPrepareVisitorMutable ()
217  {
218  Variant_t v { 'a' };
219  Visitor visitor
220  {
221  [] (int) mutable { return true; },
222  [] (auto) mutable { return false; }
223  };
224 
225  const auto& res = visitor (v);
226  QCOMPARE (res, false);
227  }
228 }
229 }
visitor.h
LC::Util::S2::field1
int field1
Definition: visitortest.cpp:41
LC::Util::Variant_t
std::variant< int, char, std::string, QString, double, float > Variant_t
Definition: visitortest.cpp:31
LC::Util::Visit
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:212
LC::Util::VisitorTest
Definition: visitortest.h:29
LC::Util::S1::field2
double field2
Definition: visitortest.cpp:36
LC::Util::SVariant_t
std::variant< S1, S2 > SVariant_t
Definition: visitortest.cpp:45
NC
#define NC
Definition: visitortest.cpp:92
LC
Definition: constants.h:14
LC::Util::S2::field2
double field2
Definition: visitortest.cpp:42
LC::Util::S1::field1
int field1
Definition: visitortest.cpp:35
visitortest.h
LC::Util::Visitor
Visitor(Args &&...) -> Visitor< Void, Args... >