2
0
mirror of https://github.com/boostorg/leaf.git synced 2026-02-08 10:52:16 +00:00
Files
leaf/test/handle_all_error_code_test.cpp
2019-01-20 19:49:20 -08:00

88 lines
1.8 KiB
C++

// Copyright (c) 2018 Emil Dotchevski
// Copyright (c) 2018 Second Spectrum, 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/error_code.hpp>
#include <boost/leaf/handle.hpp>
#include "boost/core/lightweight_test.hpp"
#include <cstring>
namespace leaf = boost::leaf;
template <int> struct info { int value; };
int main()
{
{
int r = leaf::handle_all(
[ ]
{
return std::error_code();
},
[ ]
{
return ENOENT;
} );
BOOST_TEST(!r);
}
{
int r = leaf::handle_all(
[&]
{
std::error_code ec = get_error_code(leaf::new_error(info<1>{1}));
BOOST_TEST(ec);
BOOST_TEST(ec.message()=="LEAF error, use with leaf::handle_some or leaf::handle_all.");
BOOST_TEST(!std::strcmp(ec.category().name(),"LEAF error, use with leaf::handle_some or leaf::handle_all."));
return ec;
},
[ ]( info<1> const & x )
{
BOOST_TEST(x.value==1);
return 1;
},
[ ]
{
return 2;
} );
BOOST_TEST(r==1);
}
{
int r = leaf::handle_all(
[&]
{
return get_error_code(leaf::new_error(info<2>{2},std::error_code(ENOENT,std::system_category())));
},
[ ]( info<2> const & x, std::error_code const & ec )
{
BOOST_TEST(x.value==2);
BOOST_TEST(ec==std::error_code(ENOENT,std::system_category()));
return 1;
},
[ ]
{
return 2;
} );
BOOST_TEST(r==1);
}
{
int r = leaf::handle_all(
[&]
{
return std::error_code(ENOENT,std::system_category());
},
[ ]( std::error_code const & )
{
return 1;
},
[ ]( leaf::failed<std::error_code> && ec )
{
BOOST_TEST(ec.value==std::error_code(ENOENT,std::system_category()));
return 2;
} );
BOOST_TEST(r==2);
}
return boost::report_errors();
}