]>
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 !BOOST_LEAF_CFG_CAPTURE | |
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 | ||
20 | #ifdef BOOST_LEAF_TEST_SINGLE_HEADER | |
21 | # include "leaf.hpp" | |
22 | #else | |
23 | # include <boost/leaf/capture.hpp> | |
24 | # include <boost/leaf/result.hpp> | |
25 | # include <boost/leaf/handle_errors.hpp> | |
26 | #endif | |
27 | ||
20effc67 TL |
28 | #include "_test_ec.hpp" |
29 | #include "lightweight_test.hpp" | |
30 | ||
31 | namespace leaf = boost::leaf; | |
32 | ||
33 | template <int> struct info { int value; }; | |
34 | ||
35 | template <class F> | |
36 | void test( F f ) | |
37 | { | |
38 | { | |
39 | int c=0; | |
40 | auto r = f(); | |
41 | leaf::try_handle_all( | |
42 | [&r]() -> leaf::result<void> | |
43 | { | |
44 | BOOST_LEAF_CHECK(std::move(r)); | |
45 | return { }; | |
46 | }, | |
47 | [&c]( info<1> const & x ) | |
48 | { | |
49 | BOOST_TEST_EQ(x.value, 1); | |
50 | BOOST_TEST_EQ(c, 0); | |
51 | c = 1; | |
52 | }, | |
53 | [&c] | |
54 | { | |
55 | BOOST_TEST_EQ(c, 0); | |
56 | c = 2; | |
57 | } ); | |
58 | BOOST_TEST_EQ(c, 1); | |
59 | } | |
60 | ||
61 | { | |
62 | int c=0; | |
63 | auto r = f(); | |
64 | leaf::try_handle_all( | |
65 | [&r]() -> leaf::result<void> | |
66 | { | |
67 | BOOST_LEAF_CHECK(std::move(r)); | |
68 | return { }; | |
69 | }, | |
70 | [&c]( info<2> const & x ) | |
71 | { | |
72 | BOOST_TEST_EQ(x.value, 2); | |
73 | BOOST_TEST_EQ(c, 0); | |
74 | c = 1; | |
75 | }, | |
76 | [&c] | |
77 | { | |
78 | BOOST_TEST_EQ(c, 0); | |
79 | c = 2; | |
80 | } ); | |
81 | BOOST_TEST_EQ(c, 2); | |
82 | } | |
83 | ||
84 | { | |
85 | auto r = f(); | |
86 | int what = leaf::try_handle_all( | |
87 | [&r]() -> leaf::result<int> | |
88 | { | |
89 | BOOST_LEAF_CHECK(std::move(r)); | |
90 | return 0; | |
91 | }, | |
92 | []( info<1> const & x ) | |
93 | { | |
94 | BOOST_TEST_EQ(x.value, 1); | |
95 | return 1; | |
96 | }, | |
97 | [] | |
98 | { | |
99 | return 2; | |
100 | } ); | |
101 | BOOST_TEST_EQ(what, 1); | |
102 | } | |
103 | ||
104 | { | |
105 | auto r = f(); | |
106 | int what = leaf::try_handle_all( | |
107 | [&r]() -> leaf::result<int> | |
108 | { | |
109 | BOOST_LEAF_CHECK(std::move(r)); | |
110 | return 0; | |
111 | }, | |
112 | []( info<2> const & x ) | |
113 | { | |
114 | BOOST_TEST_EQ(x.value, 2); | |
115 | return 1; | |
116 | }, | |
117 | [] | |
118 | { | |
119 | return 2; | |
120 | } ); | |
121 | BOOST_TEST_EQ(what, 2); | |
122 | } | |
123 | } | |
124 | ||
125 | int main() | |
126 | { | |
127 | test( [] | |
128 | { | |
129 | return leaf::capture( | |
1e59de90 | 130 | std::make_shared<leaf::leaf_detail::polymorphic_context_impl<leaf::context<info<1>, info<2>, info<3>>>>(), |
20effc67 TL |
131 | []() -> leaf::result<int> |
132 | { | |
133 | return leaf::new_error(errc_a::a0, info<1>{1}, info<3>{3}); | |
134 | } ); | |
135 | } ); | |
136 | ||
137 | test( [] | |
138 | { | |
139 | return leaf::capture( | |
1e59de90 | 140 | std::make_shared<leaf::leaf_detail::polymorphic_context_impl<leaf::context<info<1>, info<2>, info<3>>>>(), |
20effc67 TL |
141 | []() -> leaf::result<void> |
142 | { | |
143 | return leaf::new_error(errc_a::a0, info<1>{1}, info<3>{3}); | |
144 | } ); | |
145 | } ); | |
146 | ||
147 | return boost::report_errors(); | |
148 | } | |
1e59de90 TL |
149 | |
150 | #endif |