]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc. |
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 | ||
6 | #include <boost/leaf/handle_errors.hpp> | |
7 | #include <boost/leaf/pred.hpp> | |
8 | #include "_test_res.hpp" | |
9 | #include "lightweight_test.hpp" | |
10 | ||
11 | namespace leaf = boost::leaf; | |
12 | ||
13 | template <int> struct info { int value; }; | |
14 | ||
15 | template <class ResType> | |
16 | ResType f( bool succeed ) | |
17 | { | |
18 | if( succeed ) | |
19 | return 42; | |
20 | else | |
21 | return make_error_code(errc_a::a0); | |
22 | } | |
23 | ||
24 | template <class ResType> | |
25 | ResType g( bool succeed ) | |
26 | { | |
27 | if( auto r = f<ResType>(succeed) ) | |
28 | return r; | |
29 | else | |
30 | return leaf::error_id(r.error()).load(info<42>{42}).to_error_code(); | |
31 | } | |
32 | ||
33 | template <class ResType> | |
34 | void test() | |
35 | { | |
36 | { | |
37 | ResType r = leaf::try_handle_some( | |
38 | [] | |
39 | { | |
40 | return g<ResType>(true); | |
41 | } ); | |
42 | BOOST_TEST(r); | |
43 | BOOST_TEST_EQ(r.value(), 42); | |
44 | } | |
45 | { | |
46 | int called = 0; | |
47 | ResType r = leaf::try_handle_some( | |
48 | [&] | |
49 | { | |
50 | auto r = g<ResType>(false); | |
51 | BOOST_TEST(!r); | |
52 | auto ec = r.error(); | |
53 | BOOST_TEST_EQ(ec.message(), "LEAF error"); | |
54 | BOOST_TEST(!std::strcmp(ec.category().name(),"LEAF error")); | |
55 | return r; | |
56 | }, | |
57 | [&]( info<42> const & x, leaf::match<leaf::condition<cond_x>, cond_x::x00> ec ) | |
58 | { | |
59 | called = 1; | |
60 | BOOST_TEST_EQ(x.value, 42); | |
61 | return ec.matched; | |
62 | } ); | |
63 | BOOST_TEST(!r); | |
64 | BOOST_TEST_EQ(r.error(), make_error_code(errc_a::a0)); | |
65 | BOOST_TEST(called); | |
66 | } | |
67 | } | |
68 | ||
69 | int main() | |
70 | { | |
71 | test<test_res<int, std::error_code>>(); | |
72 | test<test_res<int const, std::error_code>>(); | |
73 | test<test_res<int, std::error_code> const>(); | |
74 | test<test_res<int const, std::error_code> const>(); | |
75 | return boost::report_errors(); | |
76 | } |