| 1 | // Copyright 2018-2023 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 | |
| 7 | #include <boost/leaf/config.hpp> |
| 8 | |
| 9 | #ifdef BOOST_LEAF_NO_EXCEPTIONS |
| 10 | |
| 11 | #include <iostream> |
| 12 | |
| 13 | int main() |
| 14 | { |
| 15 | std::cout << "Unit test not applicable." << std::endl; |
| 16 | return 0; |
| 17 | } |
| 18 | |
| 19 | #else |
| 20 | |
| 21 | #ifdef BOOST_LEAF_TEST_SINGLE_HEADER |
| 22 | # include "leaf.hpp" |
| 23 | #else |
| 24 | # include <boost/leaf/handle_errors.hpp> |
| 25 | # include <boost/leaf/exception.hpp> |
| 26 | # include <boost/leaf/pred.hpp> |
| 27 | #endif |
| 28 | |
| 29 | #include "lightweight_test.hpp" |
| 30 | |
| 31 | namespace leaf = boost::leaf; |
| 32 | |
| 33 | enum class ErrorCode |
| 34 | { |
| 35 | E_GENERIC_UNEXPECTED, |
| 36 | E_GENERIC_PARSE |
| 37 | }; |
| 38 | |
| 39 | int test1( int a ) |
| 40 | { |
| 41 | if( a == 1 ) |
| 42 | BOOST_LEAF_THROW_EXCEPTION(car: ErrorCode::E_GENERIC_UNEXPECTED); |
| 43 | return 32; |
| 44 | } |
| 45 | |
| 46 | float test2(int a) |
| 47 | { |
| 48 | return leaf::try_catch( |
| 49 | try_block: [&] |
| 50 | { |
| 51 | auto val = test1(a); |
| 52 | (void) val; |
| 53 | return 4.5; |
| 54 | }, |
| 55 | h: [](leaf::match<ErrorCode,ErrorCode::E_GENERIC_UNEXPECTED>) |
| 56 | { |
| 57 | BOOST_LEAF_THROW_EXCEPTION(car: ErrorCode::E_GENERIC_PARSE); |
| 58 | return 0; |
| 59 | } |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | void test3(int a) |
| 64 | { |
| 65 | int x = 0; |
| 66 | leaf::try_catch( |
| 67 | try_block: [&] |
| 68 | { |
| 69 | auto val = test2(a); |
| 70 | (void) val; |
| 71 | }, |
| 72 | h: [&](leaf::match<ErrorCode, ErrorCode::E_GENERIC_PARSE>) |
| 73 | { |
| 74 | x = 1; |
| 75 | }, |
| 76 | h: [&](ErrorCode e) |
| 77 | { |
| 78 | x = 2; |
| 79 | }, |
| 80 | h: [&]() |
| 81 | { |
| 82 | x = 3; |
| 83 | } |
| 84 | ); |
| 85 | BOOST_TEST_EQ(x, 1); |
| 86 | } |
| 87 | |
| 88 | int main() |
| 89 | { |
| 90 | test3(a: 1); |
| 91 | return boost::report_errors(); |
| 92 | } |
| 93 | |
| 94 | #endif |
| 95 | |