]>
Commit | Line | Data |
---|---|---|
1e59de90 | 1 | // Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc. |
20effc67 TL |
2 | |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
1e59de90 TL |
6 | #include <boost/leaf/config.hpp> |
7 | ||
8 | #if defined(BOOST_LEAF_NO_EXCEPTIONS) || !BOOST_LEAF_CFG_CAPTURE | |
20effc67 TL |
9 | |
10 | #include <iostream> | |
11 | ||
12 | int main() | |
13 | { | |
14 | std::cout << "Unit test not applicable." << std::endl; | |
15 | return 0; | |
16 | } | |
17 | ||
18 | #else | |
19 | ||
1e59de90 TL |
20 | #ifdef BOOST_LEAF_TEST_SINGLE_HEADER |
21 | # include "leaf.hpp" | |
22 | #else | |
23 | # include <boost/leaf/capture.hpp> | |
24 | # include <boost/leaf/handle_errors.hpp> | |
25 | # include <boost/leaf/exception.hpp> | |
26 | # include <boost/leaf/on_error.hpp> | |
27 | #endif | |
28 | ||
20effc67 TL |
29 | #include "lightweight_test.hpp" |
30 | ||
31 | namespace leaf = boost::leaf; | |
32 | ||
33 | template <int> struct info { int value; }; | |
34 | ||
35 | template <class... E, class F> | |
36 | void test( F f_ ) | |
37 | { | |
38 | auto f = | |
39 | [=] | |
40 | { | |
41 | try | |
42 | { | |
43 | leaf::capture( std::make_shared<leaf::leaf_detail::polymorphic_context_impl<leaf::context<info<1>, info<2>, info<3>>>>(), f_); | |
44 | BOOST_TEST(false); | |
45 | return std::exception_ptr(); | |
46 | } | |
47 | catch(...) | |
48 | { | |
49 | return std::current_exception(); | |
50 | } | |
51 | }; | |
52 | ||
53 | { | |
54 | int c=0; | |
55 | auto ep = f(); | |
56 | leaf::try_catch( | |
57 | [&ep] | |
58 | { | |
59 | return std::rethrow_exception(ep); | |
60 | }, | |
61 | [&c]( info<1> const & x ) | |
62 | { | |
63 | BOOST_TEST_EQ(x.value, 1); | |
64 | BOOST_TEST_EQ(c, 0); | |
65 | c = 1; | |
66 | }, | |
67 | [&c] | |
68 | { | |
69 | BOOST_TEST_EQ(c, 0); | |
70 | c = 2; | |
71 | } ); | |
72 | BOOST_TEST_EQ(c, 1); | |
73 | } | |
74 | ||
75 | { | |
76 | int c=0; | |
77 | auto ep = f(); | |
78 | leaf::try_catch( | |
79 | [&ep] | |
80 | { | |
81 | return std::rethrow_exception(ep); | |
82 | }, | |
83 | [&c]( info<2> const & x ) | |
84 | { | |
85 | BOOST_TEST_EQ(x.value, 2); | |
86 | BOOST_TEST_EQ(c, 0); | |
87 | c = 1; | |
88 | }, | |
89 | [&c] | |
90 | { | |
91 | BOOST_TEST_EQ(c, 0); | |
92 | c = 2; | |
93 | } ); | |
94 | BOOST_TEST_EQ(c, 2); | |
95 | } | |
96 | ||
97 | { | |
98 | auto ep = f(); | |
99 | int what = leaf::try_catch( | |
100 | [&ep] | |
101 | { | |
102 | std::rethrow_exception(ep); return 0; | |
103 | }, | |
104 | []( info<1> const & x ) | |
105 | { | |
106 | BOOST_TEST_EQ(x.value, 1); | |
107 | return 1; | |
108 | }, | |
109 | [] | |
110 | { | |
111 | return 2; | |
112 | } ); | |
113 | BOOST_TEST_EQ(what, 1); | |
114 | } | |
115 | ||
116 | { | |
117 | auto ep = f(); | |
118 | int what = leaf::try_catch( | |
119 | [&ep] | |
120 | { | |
121 | std::rethrow_exception(ep); return 0; | |
122 | }, | |
123 | []( info<2> const & x ) | |
124 | { | |
125 | BOOST_TEST_EQ(x.value, 2); | |
126 | return 1; | |
127 | }, | |
128 | [] | |
129 | { | |
130 | return 2; | |
131 | } ); | |
132 | BOOST_TEST_EQ(what, 2); | |
133 | } | |
134 | ||
135 | { | |
136 | auto ep = f(); | |
137 | bool what = leaf::try_catch( | |
138 | [&ep] | |
139 | { | |
140 | std::rethrow_exception(ep); return true; | |
141 | }, | |
142 | []( info<1> const & x, info<2> const & ) | |
143 | { | |
144 | return true; | |
145 | }, | |
146 | []( info<1> const & x, info<3> const & y ) | |
147 | { | |
148 | BOOST_TEST_EQ(x.value, 1); | |
149 | BOOST_TEST_EQ(y.value, 3); | |
150 | return false; | |
151 | }, | |
152 | []( info<1> const & x ) | |
153 | { | |
154 | return true; | |
155 | }, | |
156 | [] | |
157 | { | |
158 | return true; | |
159 | } ); | |
160 | BOOST_TEST(!what); | |
161 | } | |
162 | ||
163 | { | |
164 | auto ep = f(); | |
165 | bool what = leaf::try_catch( | |
166 | [&ep] | |
167 | { | |
168 | std::rethrow_exception(ep); return false; | |
169 | }, | |
170 | []( info<1> const & x, info<2> const & ) | |
171 | { | |
172 | return false; | |
173 | }, | |
174 | []( info<1> const & x, info<3> const & y ) | |
175 | { | |
176 | BOOST_TEST_EQ(x.value, 1); | |
177 | BOOST_TEST_EQ(y.value, 3); | |
178 | return true; | |
179 | }, | |
180 | []( info<1> const & x ) | |
181 | { | |
182 | return false; | |
183 | }, | |
184 | [] | |
185 | { | |
186 | return false; | |
187 | } ); | |
188 | BOOST_TEST(what); | |
189 | } | |
190 | } | |
191 | ||
192 | int main() | |
193 | { | |
194 | test<info<1>, info<2>, info<3>>( | |
195 | [] | |
196 | { | |
197 | throw leaf::exception(info<1>{1}, info<3>{3}); // Derives from leaf::leaf::error_id | |
198 | } ); | |
199 | test<info<1>, info<2>, info<3>>( | |
200 | [] | |
201 | { | |
202 | auto load = leaf::on_error( info<1>{1}, info<3>{3} ); | |
203 | throw leaf::exception(); // Derives from leaf::leaf::error_id | |
204 | } ); | |
205 | test<info<1>, info<2>, info<3>>( | |
206 | [] | |
207 | { | |
208 | auto load = leaf::on_error( info<1>{1}, info<3>{3} ); | |
209 | throw std::exception(); // Does not derive from leaf::leaf::error_id | |
210 | } ); | |
211 | return boost::report_errors(); | |
212 | } | |
213 | ||
214 | #endif |