LeechCraft  0.6.70-13729-g7046a9d2a7
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "visitortest.h"
31 #include <QtTest>
32 #include <visitor.h>
33 
34 QTEST_MAIN (LC::Util::VisitorTest)
35 
36 namespace LC
37 {
38 namespace Util
39 {
40  using Variant_t = std::variant<int, char, std::string, QString, double, float>;
41 
42  struct S1
43  {
44  int field1;
45  double field2;
46  };
47 
48  struct S2
49  {
50  int field1;
51  double field2;
52  };
53 
54  using SVariant_t = std::variant<S1, S2>;
55 
56  void VisitorTest::testBasicVisitor ()
57  {
58  Variant_t v { 'a' };
59  const auto& res = Visit (v,
60  [] (char) { return true; },
61  [] (int) { return false; },
62  [] (std::string) { return false; },
63  [] (QString) { return false; },
64  [] (double) { return false; },
65  [] (float) { return false; });
66  QCOMPARE (res, true);
67  }
68 
69  void VisitorTest::testBasicVisitorGenericFallback ()
70  {
71  Variant_t v { 'a' };
72  const auto& res = Visit (v,
73  [] (char) { return true; },
74  [] (int) { return false; },
75  [] (auto) { return false; });
76  QCOMPARE (res, true);
77  }
78 
79  void VisitorTest::testBasicVisitorCoercion ()
80  {
81  Variant_t v { 'a' };
82  const auto& res = Visit (v,
83  [] (int) { return true; },
84  [] (std::string) { return false; },
85  [] (QString) { return false; },
86  [] (double) { return false; },
87  [] (float) { return false; });
88  QCOMPARE (res, true);
89  }
90 
91  void VisitorTest::testBasicVisitorCoercionGenericFallback ()
92  {
93  Variant_t v { 'a' };
94  const auto& res = Visit (v,
95  [] (int) { return false; },
96  [] (QString) { return false; },
97  [] (auto) { return true; });
98  QCOMPARE (res, true);
99  }
100 
101 #define NC nc = std::unique_ptr<int> {}
102 
103  void VisitorTest::testNonCopyableFunctors ()
104  {
105  Variant_t v { 'a' };
106  const auto& res = Visit (v,
107  [NC] (char) { return true; },
108  [NC] (int) { return false; },
109  [NC] (std::string) { return false; },
110  [NC] (QString) { return false; },
111  [NC] (double) { return false; },
112  [NC] (float) { return false; });
113  QCOMPARE (res, true);
114  }
115 #undef NC
116 
117  void VisitorTest::testAcceptsRValueRef ()
118  {
119  const auto& res = Visit (Variant_t { 'a' },
120  [] (char) { return true; },
121  [] (auto) { return false; });
122  QCOMPARE (res, true);
123  }
124 
125  void VisitorTest::testLValueRef ()
126  {
127  Variant_t v { 'a' };
128  int ref = 0;
129  auto& res = Visit (v, [&ref] (auto) -> int& { return ref; });
130  res = 10;
131  QCOMPARE (ref, 10);
132  }
133 
134  void VisitorTest::testLValueRef2 ()
135  {
136  SVariant_t v { S1 { 0, 0 } };
137  Visit (v, [] (auto& s) -> int& { return s.field1; }) = 10;
138  const auto& res = Visit (v, [] (const auto& s) -> const int& { return s.field1; });
139  QCOMPARE (res, 10);
140  }
141 
142  void VisitorTest::testPrepareVisitor ()
143  {
144  Variant_t v { 'a' };
145  Visitor visitor
146  {
147  [] (char) { return true; },
148  [] (int) { return false; },
149  [] (std::string) { return false; },
150  [] (QString) { return false; },
151  [] (double) { return false; },
152  [] (float) { return false; }
153  };
154 
155  const auto& res = visitor (v);
156  QCOMPARE (res, true);
157  }
158 
159  void VisitorTest::testPrepareVisitorConst ()
160  {
161  const Variant_t v { 'a' };
162  Visitor visitor
163  {
164  [] (char) { return true; },
165  [] (int) { return false; },
166  [] (std::string) { return false; },
167  [] (QString) { return false; },
168  [] (double) { return false; },
169  [] (float) { return false; }
170  };
171 
172  const auto& res = visitor (v);
173  QCOMPARE (res, true);
174  }
175 
176  void VisitorTest::testPrepareVisitorRValue ()
177  {
178  Visitor visitor
179  {
180  [] (char) { return true; },
181  [] (int) { return false; },
182  [] (std::string) { return false; },
183  [] (QString) { return false; },
184  [] (double) { return false; },
185  [] (float) { return false; }
186  };
187 
188  const auto& res = visitor (Variant_t { 'a' });
189  QCOMPARE (res, true);
190  }
191 
192  void VisitorTest::testPrepareVisitorFinally ()
193  {
194  Variant_t v { 'a' };
195 
196  bool fin = false;
197 
198  auto visitor = Visitor
199  {
200  [] (char) { return true; },
201  [] (auto) { return false; }
202  }.Finally ([&fin] { fin = true; });
203 
204  const auto& res = visitor (v);
205  QCOMPARE (res, true);
206  QCOMPARE (fin, true);
207  }
208 
209  void VisitorTest::testPrepareJustAutoVisitor ()
210  {
211  using Variant_t = std::variant<int, double, float>;
212 
213  Visitor visitor
214  {
215  [] (auto e) { return std::to_string (e); }
216  };
217 
218  const auto& res = visitor (Variant_t { 10 });
219  QCOMPARE (res, std::string { "10" });
220  }
221 
222  void VisitorTest::testPrepareRecursiveVisitor ()
223  {
224  using SubVariant_t = std::variant<int, double, float>;
225  using Variant_t = std::variant<SubVariant_t, QString>;
226 
227  Visitor visitor
228  {
229  [] (const QString& str) { return str; },
230  Visitor { [] (auto e) { return QString::fromStdString (std::to_string (e)); } }
231  };
232 
233  const auto& res = visitor (Variant_t { SubVariant_t { 10 } });
234  QCOMPARE (res, QString { "10" });
235  }
236 
237  void VisitorTest::testPrepareVisitorMutable ()
238  {
239  Variant_t v { 'a' };
240  Visitor visitor
241  {
242  [] (int) mutable { return true; },
243  [] (auto) mutable { return false; }
244  };
245 
246  const auto& res = visitor (v);
247  QCOMPARE (res, false);
248  }
249 }
250 }
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:224
std::variant< int, char, std::string, QString, double, float > Variant_t
Definition: visitortest.cpp:40
std::variant< S1, S2 > SVariant_t
Definition: visitortest.cpp:54
#define NC
Visitor(Args &&...) -> Visitor< Void, Args... >
Definition: constants.h:35