mirror of
https://github.com/boostorg/leaf.git
synced 2026-01-19 04:22:08 +00:00
removed success/failure functions, plus more complete error_code_test
This commit is contained in:
9
.vscode/tasks.json
vendored
9
.vscode/tasks.json
vendored
@@ -563,15 +563,6 @@
|
||||
"command": "${workspaceRoot}/.vscode/msvc.bat && cd ${workspaceRoot}/bld/debug && meson test result_state_test"
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "success_failure_test",
|
||||
"type": "shell",
|
||||
"command": "cd ${workspaceRoot}/bld/debug && meson test success_failure_test",
|
||||
"problemMatcher": { "base": "$gcc", "fileLocation": ["relative","${workspaceRoot}/bld/debug"] },
|
||||
"windows": {
|
||||
"command": "${workspaceRoot}/.vscode/msvc.bat && cd ${workspaceRoot}/bld/debug && meson test success_failure_test"
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "try_catch_error_id_test",
|
||||
"type": "shell",
|
||||
|
||||
@@ -4906,47 +4906,6 @@ namespace boost { namespace leaf {
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
inline result<void> success() noexcept
|
||||
{
|
||||
return { };
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline result<T> success() noexcept
|
||||
{
|
||||
return { };
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline result<T> success( T && v ) noexcept
|
||||
{
|
||||
return { std::forward<T>(v) };
|
||||
}
|
||||
|
||||
inline result<void> failure( error_id err ) noexcept
|
||||
{
|
||||
return { err };
|
||||
}
|
||||
|
||||
inline result<void> failure() noexcept
|
||||
{
|
||||
return { new_error() };
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline result<T> failure( error_id err ) noexcept
|
||||
{
|
||||
return { err };
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline result<T> failure() noexcept
|
||||
{
|
||||
return { new_error() };
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
template <class R>
|
||||
struct is_result_type;
|
||||
|
||||
|
||||
@@ -420,47 +420,6 @@ namespace boost { namespace leaf {
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
inline result<void> success() noexcept
|
||||
{
|
||||
return { };
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline result<T> success() noexcept
|
||||
{
|
||||
return { };
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline result<T> success( T && v ) noexcept
|
||||
{
|
||||
return { std::forward<T>(v) };
|
||||
}
|
||||
|
||||
inline result<void> failure( error_id err ) noexcept
|
||||
{
|
||||
return { err };
|
||||
}
|
||||
|
||||
inline result<void> failure() noexcept
|
||||
{
|
||||
return { new_error() };
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline result<T> failure( error_id err ) noexcept
|
||||
{
|
||||
return { err };
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline result<T> failure() noexcept
|
||||
{
|
||||
return { new_error() };
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
template <class R>
|
||||
struct is_result_type;
|
||||
|
||||
|
||||
@@ -140,7 +140,6 @@ tests = [
|
||||
'result_load_test',
|
||||
'result_ref_test',
|
||||
'result_state_test',
|
||||
'success_failure_test',
|
||||
'try_catch_error_id_test',
|
||||
'try_catch_test',
|
||||
'try_exception_and_result_test',
|
||||
|
||||
@@ -88,7 +88,6 @@ run result_bad_result_test.cpp ;
|
||||
run result_load_test.cpp ;
|
||||
run result_ref_test.cpp ;
|
||||
run result_state_test.cpp ;
|
||||
run success_failure_test.cpp ;
|
||||
run context_deduction_test.cpp ;
|
||||
run try_catch_error_id_test.cpp ;
|
||||
run try_catch_test.cpp ;
|
||||
|
||||
@@ -55,6 +55,48 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template <class E>
|
||||
class test_res<void, E>
|
||||
{
|
||||
enum class variant
|
||||
{
|
||||
value,
|
||||
error
|
||||
};
|
||||
E error_;
|
||||
variant which_;
|
||||
public:
|
||||
test_res() noexcept:
|
||||
error_(),
|
||||
which_(variant::value)
|
||||
{
|
||||
}
|
||||
test_res( E const & error ) noexcept:
|
||||
error_(error),
|
||||
which_(variant::error)
|
||||
{
|
||||
}
|
||||
template <class Enum>
|
||||
test_res( Enum e, typename std::enable_if<std::is_error_code_enum<Enum>::value, Enum>::type * = 0 ):
|
||||
error_(make_error_code(e)),
|
||||
which_(variant::error)
|
||||
{
|
||||
}
|
||||
explicit operator bool() const noexcept
|
||||
{
|
||||
return which_==variant::value;
|
||||
}
|
||||
void value() const
|
||||
{
|
||||
BOOST_LEAF_ASSERT(which_==variant::value);
|
||||
}
|
||||
E const & error() const
|
||||
{
|
||||
BOOST_LEAF_ASSERT(which_==variant::error);
|
||||
return error_;
|
||||
}
|
||||
};
|
||||
|
||||
namespace boost { namespace leaf {
|
||||
|
||||
template <class T>
|
||||
|
||||
@@ -293,9 +293,305 @@ void test()
|
||||
#endif
|
||||
}
|
||||
|
||||
template <class R>
|
||||
void test_void()
|
||||
{
|
||||
#if __cplusplus >= 201703L
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return make_error_code(errc_a::a0);
|
||||
},
|
||||
[&]( leaf::match<std::error_code, leaf::category<errc_a>, leaf::category<errc_b>> code )
|
||||
{
|
||||
std::error_code const & ec = code.matched;
|
||||
BOOST_TEST_EQ(&ec.category(), &cat_errc_a());
|
||||
BOOST_TEST_EQ(ec, errc_a::a0);
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return make_error_code(errc_b::b0);
|
||||
},
|
||||
[&]( leaf::match<std::error_code, leaf::category<errc_a>, leaf::category<errc_b>> code )
|
||||
{
|
||||
std::error_code const & ec = code.matched;
|
||||
BOOST_TEST_EQ(&ec.category(), &cat_errc_b());
|
||||
BOOST_TEST_EQ(ec, errc_b::b0);
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return make_error_code(errc_b::b0);
|
||||
},
|
||||
[&]( leaf::match<std::error_code, leaf::category<errc_a>, errc_b::b0> code )
|
||||
{
|
||||
std::error_code const & ec = code.matched;
|
||||
BOOST_TEST_EQ(&ec.category(), &cat_errc_b());
|
||||
BOOST_TEST_EQ(ec, errc_b::b0);
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
#endif
|
||||
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[&]() -> R
|
||||
{
|
||||
return errc_a::a0; // testing without make_error_code
|
||||
},
|
||||
[&]( std::error_code const & ec )
|
||||
{
|
||||
BOOST_TEST(!leaf::is_error_id(ec));
|
||||
BOOST_TEST_EQ(ec, errc_a::a0);
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return make_error_code(errc_a::a0);
|
||||
},
|
||||
[&]( leaf::match<leaf::condition<errc_a>, errc_a::a0> code )
|
||||
{
|
||||
std::error_code const & ec = code.matched;
|
||||
BOOST_TEST_EQ(ec, errc_a::a0);
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
#if __cplusplus >= 201703L
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return make_error_code(errc_a::a0);
|
||||
},
|
||||
[&]( leaf::match<std::error_code, errc_a::a0> code )
|
||||
{
|
||||
std::error_code const & ec = code.matched;
|
||||
BOOST_TEST_EQ(ec, errc_a::a0);
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
#endif
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return make_error_code(errc_a::a0);
|
||||
},
|
||||
[&]( leaf::match<leaf::condition<errc_a>, errc_a::a0> code )
|
||||
{
|
||||
std::error_code const & ec = code.matched;
|
||||
BOOST_TEST_EQ(ec, errc_a::a0);
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return make_error_code(errc_a::a0);
|
||||
},
|
||||
[&]( leaf::match<leaf::condition<cond_x>, cond_x::x00> cond )
|
||||
{
|
||||
std::error_code const & ec = cond.matched;
|
||||
BOOST_TEST_EQ(ec, errc_a::a0);
|
||||
BOOST_TEST(ec==make_error_condition(cond_x::x00));
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
#if __cplusplus >= 201703L
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return make_error_code(errc_a::a0);
|
||||
},
|
||||
[&]( leaf::match<std::error_code, cond_x::x00> cond )
|
||||
{
|
||||
std::error_code const & ec = cond.matched;
|
||||
BOOST_TEST_EQ(ec, errc_a::a0);
|
||||
BOOST_TEST(ec==make_error_condition(cond_x::x00));
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
#endif
|
||||
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return leaf::new_error( e_wrapped_error_code { make_error_code(errc_a::a0) } ).to_error_code();
|
||||
},
|
||||
[&]( e_wrapped_error_code const & wec )
|
||||
{
|
||||
std::error_code const & ec = wec.value;
|
||||
BOOST_TEST_EQ(ec, errc_a::a0);
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return leaf::new_error( e_wrapped_error_code { make_error_code(errc_a::a0) } ).to_error_code();
|
||||
},
|
||||
[&]( leaf::match_value<leaf::condition<e_wrapped_error_code, errc_a>, errc_a::a0> code )
|
||||
{
|
||||
e_wrapped_error_code const & wec = code.matched;
|
||||
std::error_code const & ec = wec.value;
|
||||
BOOST_TEST_EQ(ec, errc_a::a0);
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
#if __cplusplus >= 201703L
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return leaf::new_error( e_wrapped_error_code { make_error_code(errc_a::a0) } ).to_error_code();
|
||||
},
|
||||
[&]( leaf::match_value<e_wrapped_error_code, errc_a::a0> code )
|
||||
{
|
||||
e_wrapped_error_code const & wec = code.matched;
|
||||
std::error_code const & ec = wec.value;
|
||||
BOOST_TEST_EQ(ec, errc_a::a0);
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
#endif
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return leaf::new_error( e_wrapped_error_code { make_error_code(errc_a::a0) } ).to_error_code();
|
||||
},
|
||||
[&]( leaf::match_value<leaf::condition<e_wrapped_error_code, cond_x>, cond_x::x00> cond )
|
||||
{
|
||||
e_wrapped_error_code const & wec = cond.matched;
|
||||
std::error_code const & ec = wec.value;
|
||||
BOOST_TEST_EQ(ec, errc_a::a0);
|
||||
BOOST_TEST(ec==make_error_condition(cond_x::x00));
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
#if __cplusplus >= 201703L
|
||||
{
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]() -> R
|
||||
{
|
||||
return leaf::new_error( e_wrapped_error_code { make_error_code(errc_a::a0) } ).to_error_code();
|
||||
},
|
||||
[&]( leaf::match_value<e_wrapped_error_code, cond_x::x00> cond )
|
||||
{
|
||||
e_wrapped_error_code const & wec = cond.matched;
|
||||
std::error_code const & ec = wec.value;
|
||||
BOOST_TEST_EQ(ec, errc_a::a0);
|
||||
BOOST_TEST(ec==make_error_condition(cond_x::x00));
|
||||
r = 42;
|
||||
},
|
||||
[&]
|
||||
{
|
||||
r = -42;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test<leaf::result<int>>();
|
||||
test<test_res<int, std::error_code>>();
|
||||
test_void<leaf::result<void>>();
|
||||
test_void<test_res<void, std::error_code>>();
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
// Copyright (c) 2018-2020 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/result.hpp>
|
||||
#include <boost/leaf/handle_error.hpp>
|
||||
#include "_test_ec.hpp"
|
||||
#include "lightweight_test.hpp"
|
||||
|
||||
namespace leaf = boost::leaf;
|
||||
|
||||
int main()
|
||||
{
|
||||
{ // success, void, default
|
||||
leaf::result<void> r = leaf::success();
|
||||
BOOST_TEST(r);
|
||||
}
|
||||
{ // success, int, default
|
||||
leaf::result<int> r = leaf::success<int>();
|
||||
BOOST_TEST(r);
|
||||
BOOST_TEST_EQ(r.value(), 0);
|
||||
}
|
||||
{ // success, int, value
|
||||
leaf::result<int> r = leaf::success(42);
|
||||
BOOST_TEST(r);
|
||||
BOOST_TEST_EQ(r.value(), 42);
|
||||
}
|
||||
|
||||
(void) leaf::new_error();
|
||||
{ // failure, void, default
|
||||
leaf::error_id const ce = leaf::current_error();
|
||||
leaf::result<void> r = leaf::failure();
|
||||
BOOST_TEST(!r);
|
||||
BOOST_TEST_NE(leaf::error_id(r.error()), ce);
|
||||
BOOST_TEST_EQ(leaf::error_id(r.error()), leaf::current_error());
|
||||
}
|
||||
{ // failure, int, default
|
||||
leaf::error_id const ce = leaf::current_error();
|
||||
leaf::result<int> r = leaf::failure<int>();
|
||||
BOOST_TEST(!r);
|
||||
BOOST_TEST_NE(leaf::error_id(r.error()), ce);
|
||||
BOOST_TEST_EQ(leaf::error_id(r.error()), leaf::current_error());
|
||||
}
|
||||
{ // failure, int, std::is_error_code_enum error code
|
||||
leaf::error_id const ce = leaf::current_error();
|
||||
leaf::result<int> r = errc_a::a0;
|
||||
BOOST_TEST(!r);
|
||||
BOOST_TEST_NE(leaf::error_id(r.error()), ce);
|
||||
BOOST_TEST_EQ(leaf::error_id(r.error()), leaf::current_error());
|
||||
}
|
||||
|
||||
{ // void, success
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]
|
||||
{
|
||||
return true ? leaf::success() : errc_a::a0;
|
||||
},
|
||||
|
||||
[&]
|
||||
{
|
||||
r = 1;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 0);
|
||||
}
|
||||
|
||||
{ // void, failure
|
||||
int r = 0;
|
||||
leaf::try_handle_all(
|
||||
[]
|
||||
{
|
||||
return false ? leaf::success() : errc_a::a0;
|
||||
},
|
||||
|
||||
[&]( std::error_code const & ec )
|
||||
{
|
||||
BOOST_TEST(ec == errc_a::a0);
|
||||
r = 1;
|
||||
},
|
||||
|
||||
[&]
|
||||
{
|
||||
r = 2;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 1);
|
||||
}
|
||||
|
||||
{ // int, success
|
||||
int r = leaf::try_handle_all(
|
||||
[]
|
||||
{
|
||||
return true ? leaf::success(42) : errc_a::a0;
|
||||
},
|
||||
|
||||
[&]
|
||||
{
|
||||
return 1;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 42);
|
||||
}
|
||||
|
||||
{ // int, failure
|
||||
int r = leaf::try_handle_all(
|
||||
[]
|
||||
{
|
||||
return false ? leaf::success(42) : errc_a::a0;
|
||||
},
|
||||
|
||||
[&]( std::error_code const & ec )
|
||||
{
|
||||
BOOST_TEST(ec == errc_a::a0);
|
||||
return 1;
|
||||
},
|
||||
|
||||
[&]
|
||||
{
|
||||
return 2;
|
||||
} );
|
||||
BOOST_TEST_EQ(r, 1);
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
Reference in New Issue
Block a user