Skip to content

Commit

Permalink
Fix for Issue 53
Browse files Browse the repository at this point in the history
  • Loading branch information
zajo committed Apr 5, 2023
1 parent e02fc6b commit 801e5e8
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/boost/leaf/handle_errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ try_handle_some( TryBlock && try_block, H && ... h ) noexcept
using R = typename std::decay<decltype(std::declval<TryBlock>()())>::type;
auto rr = ctx.template handle_error<R>(id, std::forward<H>(h)..., [&r]()->R { return std::move(r); });
if( !rr )
ctx.propagate(id);
ctx.propagate(rr.error());
return rr;
}
}
Expand Down Expand Up @@ -869,7 +869,7 @@ try_handle_some( TryBlock && try_block, H && ... h )
using R = typename std::decay<decltype(std::declval<TryBlock>()())>::type;
auto rr = ctx.template handle_error<R>(id, std::forward<H>(h)..., [&r]()->R { return std::move(r); });
if( !rr )
ctx.propagate(id);
ctx.propagate(rr.error());
return rr;
}
}
Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ if option_enable_unit_tests
'exception_test',
'exception_to_result_test',
'function_traits_test',
'github_issue53_test',
'github_issue53x_test',
'handle_all_other_result_test',
'handle_all_test',
'handle_basic_test',
Expand Down
2 changes: 2 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ run error_id_test.cpp ;
run exception_test.cpp ;
run exception_to_result_test.cpp ;
run function_traits_test.cpp ;
run github_issue53_test.cpp ;
run github_issue53x_test.cpp ;
run handle_all_other_result_test.cpp ;
run handle_all_test.cpp ;
run handle_basic_test.cpp ;
Expand Down
77 changes: 77 additions & 0 deletions test/github_issue53_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc.

// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifdef BOOST_LEAF_TEST_SINGLE_HEADER
# include "leaf.hpp"
#else
# include <boost/leaf/handle_errors.hpp>
# include <boost/leaf/result.hpp>
# include <boost/leaf/pred.hpp>
#endif

#include "lightweight_test.hpp"

namespace leaf = boost::leaf;

enum class ErrorCode
{
E_GENERIC_UNEXPECTED,
E_GENERIC_PARSE
};

leaf::result<int> test1( int a )
{
if( a == 1 )
return leaf::new_error(ErrorCode::E_GENERIC_UNEXPECTED);
return 32;
}

leaf::result<float> test2(int a)
{
return leaf::try_handle_some(
[&]() -> leaf::result<float>
{
BOOST_LEAF_AUTO(val, test1(a));
(void) val;
return 4.5;
},
[](leaf::match<ErrorCode,ErrorCode::E_GENERIC_UNEXPECTED>) -> leaf::result<float>
{
return leaf::new_error(ErrorCode::E_GENERIC_PARSE);
}
);
}

void test3(int a)
{
int x = 0;
leaf::try_handle_all(
[&]() -> leaf::result<void>
{
BOOST_LEAF_AUTO(val, test2(a));
(void) val;
return {};
},
[&](leaf::match<ErrorCode, ErrorCode::E_GENERIC_PARSE>)
{
x = 1;
},
[&](ErrorCode e)
{
x = 2;
},
[&]()
{
x = 3;
}
);
BOOST_TEST_EQ(x, 1);
}

int main()
{
test3(1);
return boost::report_errors();
}
93 changes: 93 additions & 0 deletions test/github_issue53x_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc.

// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)


#include <boost/leaf/config.hpp>

#ifdef BOOST_LEAF_NO_EXCEPTIONS

#include <iostream>

int main()
{
std::cout << "Unit test not applicable." << std::endl;
return 0;
}

#else

#ifdef BOOST_LEAF_TEST_SINGLE_HEADER
# include "leaf.hpp"
#else
# include <boost/leaf/handle_errors.hpp>
# include <boost/leaf/pred.hpp>
#endif

#include "lightweight_test.hpp"

namespace leaf = boost::leaf;

enum class ErrorCode
{
E_GENERIC_UNEXPECTED,
E_GENERIC_PARSE
};

int test1( int a )
{
if( a == 1 )
BOOST_LEAF_THROW_EXCEPTION(ErrorCode::E_GENERIC_UNEXPECTED);
return 32;
}

float test2(int a)
{
return leaf::try_catch(
[&]
{
auto val = test1(a);
(void) val;
return 4.5;
},
[](leaf::match<ErrorCode,ErrorCode::E_GENERIC_UNEXPECTED>)
{
BOOST_LEAF_THROW_EXCEPTION(ErrorCode::E_GENERIC_PARSE);
return 0;
}
);
}

void test3(int a)
{
int x = 0;
leaf::try_catch(
[&]
{
auto val = test2(a);
(void) val;
},
[&](leaf::match<ErrorCode, ErrorCode::E_GENERIC_PARSE>)
{
x = 1;
},
[&](ErrorCode e)
{
x = 2;
},
[&]()
{
x = 3;
}
);
BOOST_TEST_EQ(x, 1);
}

int main()
{
test3(1);
return boost::report_errors();
}

#endif

0 comments on commit 801e5e8

Please sign in to comment.