]>
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_STD_SYSTEM_ERROR | |
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/handle_errors.hpp> | |
24 | #endif | |
25 | ||
20effc67 TL |
26 | #include "_test_res.hpp" |
27 | #include "lightweight_test.hpp" | |
28 | ||
29 | namespace leaf = boost::leaf; | |
30 | ||
31 | template <int> struct info { int value; }; | |
32 | ||
33 | template <class ResType> | |
34 | ResType f( bool succeed ) | |
35 | { | |
36 | if( succeed ) | |
37 | return 42; | |
38 | else | |
39 | return make_error_code(std::errc::no_such_file_or_directory); | |
40 | } | |
41 | ||
42 | template <class ResType> | |
43 | ResType g( bool succeed ) | |
44 | { | |
45 | if( auto r = f<ResType>(succeed) ) | |
46 | return r; | |
47 | else | |
48 | return leaf::error_id(r.error()).load(info<42>{42}).to_error_code(); | |
49 | } | |
50 | ||
51 | template <class ResType> | |
52 | void test() | |
53 | { | |
54 | { | |
55 | int r = leaf::try_handle_all( | |
56 | [] | |
57 | { | |
58 | return g<ResType>(true); | |
59 | }, | |
60 | [] | |
61 | { | |
62 | return -1; | |
63 | } ); | |
64 | BOOST_TEST_EQ(r, 42); | |
65 | } | |
66 | { | |
67 | int r = leaf::try_handle_all( | |
68 | [&] | |
69 | { | |
70 | auto r = g<ResType>(false); | |
71 | BOOST_TEST(!r); | |
72 | auto ec = r.error(); | |
73 | BOOST_TEST_EQ(ec.message(), "LEAF error"); | |
74 | BOOST_TEST(!std::strcmp(ec.category().name(),"LEAF error")); | |
75 | return r; | |
76 | }, | |
77 | []( info<42> const & x, std::error_code const & ec ) | |
78 | { | |
79 | BOOST_TEST_EQ(x.value, 42); | |
80 | BOOST_TEST_EQ(ec, make_error_code(std::errc::no_such_file_or_directory)); | |
81 | return 1; | |
82 | }, | |
83 | [] | |
84 | { | |
85 | return 2; | |
86 | } ); | |
87 | BOOST_TEST_EQ(r, 1); | |
88 | } | |
89 | } | |
90 | ||
91 | int main() | |
92 | { | |
93 | test<test_res<int, std::error_code>>(); | |
94 | test<test_res<int const, std::error_code>>(); | |
95 | test<test_res<int, std::error_code> const>(); | |
96 | test<test_res<int const, std::error_code> const>(); | |
97 | return boost::report_errors(); | |
98 | } | |
1e59de90 TL |
99 | |
100 | #endif |