2
0
mirror of https://github.com/boostorg/leaf.git synced 2026-02-03 09:12:18 +00:00
Files
leaf/test/handle_some_error_code_test.cpp
Emil Dotchevski 0c9081f6f3 failed<R>
2019-01-20 19:25:09 -08:00

120 lines
2.9 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 what=0;
std::error_code r = leaf::handle_some(
[&]
{
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 )
{
what = 1;
BOOST_TEST(x.value==1);
return std::error_code(ENOENT,std::system_category());
} );
BOOST_TEST(r==std::error_code(ENOENT,std::system_category()));
BOOST_TEST(what==1);
}
{
int what=0;
std::error_code ec = leaf::handle_some(
[&]
{
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 )
{
what = 1;
BOOST_TEST(x.value==2);
BOOST_TEST(ec==std::error_code(ENOENT,std::system_category()));
return ec;
} );
BOOST_TEST(ec==std::error_code(ENOENT,std::system_category()));
BOOST_TEST(what==1);
}
{
int what=0;
std::error_code ec = leaf::handle_some(
[&]
{
return std::error_code(ENOENT,std::system_category());
},
[&]( std::error_code const & ec )
{
what = 1;
return ec;
},
[&]( leaf::failed<std::error_code> const & ec )
{
what = 2;
BOOST_TEST(ec.value==std::error_code(ENOENT,std::system_category()));
return ec.value;
} );
BOOST_TEST(ec==std::error_code(ENOENT,std::system_category()));
BOOST_TEST(what==2);
}
{
int what=0;
std::error_code ec = leaf::handle_some(
[&]
{
return std::error_code(ENOENT,std::system_category());
},
[&]( std::error_code const & ec )
{
what = 1;
return ec;
},
[&]( leaf::failed<std::error_code> ec )
{
what = 2;
BOOST_TEST(ec.value==std::error_code(ENOENT,std::system_category()));
return ec.value;
} );
BOOST_TEST(ec==std::error_code(ENOENT,std::system_category()));
BOOST_TEST(what==2);
}
{
int what=0;
std::error_code ec = leaf::handle_some(
[&]
{
return std::error_code(ENOENT,std::system_category());
},
[&]( std::error_code const & ec )
{
what = 1;
return ec;
},
[&]( leaf::failed<std::error_code> && ec )
{
what = 2;
BOOST_TEST(ec.value==std::error_code(ENOENT,std::system_category()));
return ec.value;
} );
BOOST_TEST(ec==std::error_code(ENOENT,std::system_category()));
BOOST_TEST(what==2);
}
return boost::report_errors();
}