2
0
mirror of https://github.com/boostorg/leaf.git synced 2026-01-19 04:22:08 +00:00

Fixing bug in e_LastError, adding unit tests

This commit is contained in:
Emil Dotchevski
2021-02-10 18:35:46 -08:00
parent afb916a137
commit 294728e5b5
5 changed files with 114 additions and 1 deletions

View File

@@ -40,6 +40,8 @@ struct e_errno
{
int value;
explicit e_errno(int value=errno): value(value) { }
template <class CharT, class Traits>
friend std::basic_ostream<CharT, Traits> & operator<<( std::basic_ostream<CharT, Traits> & os, e_errno const & err )
{
@@ -57,9 +59,13 @@ namespace windows
{
unsigned value;
explicit e_LastError(unsigned value): value(value) { }
#ifdef _WIN32
e_LastError(): value(GetLastError()) { }
template <class CharT, class Traits>
friend std::basic_ostream<CharT, Traits> & operator<<( std::basic_ostream<CharT, Traits> os, e_LastError const & err )
friend std::basic_ostream<CharT, Traits> & operator<<( std::basic_ostream<CharT, Traits> & os, e_LastError const & err )
{
struct msg_buf
{

View File

@@ -132,6 +132,8 @@ if option_enable_unit_tests
'defer_nested_success_exception_test',
'defer_nested_success_result_test',
'diagnostic_info_test',
'e_errno_test',
'e_LastError_test',
'error_code_test',
'error_id_test',
'exception_test',

View File

@@ -64,6 +64,8 @@ run defer_nested_new_error_result_test.cpp ;
run defer_nested_success_exception_test.cpp ;
run defer_nested_success_result_test.cpp ;
run diagnostic_info_test.cpp ;
run e_errno_test.cpp ;
run e_LastError_test.cpp ;
run error_code_test.cpp ;
run error_id_test.cpp ;
run exception_test.cpp ;

59
test/e_LastError_test.cpp Normal file
View File

@@ -0,0 +1,59 @@
// Copyright (c) 2018-2021 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)
#ifndef _WIN32
#include <iostream>
int main()
{
std::cout << "This test requires Windows";
return 0;
}
#else
#ifdef BOOST_LEAF_TEST_SINGLE_HEADER
# include "leaf.hpp"
#else
# include <boost/leaf/common.hpp>
# include <boost/leaf/handle_errors.hpp>
# include <boost/leaf/result.hpp>
#endif
#include "lightweight_test.hpp"
#include <sstream>
namespace leaf = boost::leaf;
int main()
{
SetLastError(ERROR_FILE_NOT_FOUND);
std::stringstream ss;
ss << leaf::windows::e_LastError{};
BOOST_TEST(ss.str().find("The system cannot find the file specified") != std::string::npos);
int r = leaf::try_handle_all(
[]() -> leaf::result<int>
{
SetLastError(ERROR_FILE_NOT_FOUND);
struct reset_LastError { ~reset_LastError() {SetLastError(0); } } reset;
return leaf::new_error( leaf::windows::e_LastError{} );
},
[]( leaf::windows::e_LastError e )
{
BOOST_TEST_EQ(GetLastError(), 0);
BOOST_TEST_EQ(e.value, ERROR_FILE_NOT_FOUND);
return 1;
},
[]
{
return 2;
} );
BOOST_TEST_EQ(r, 1);
return boost::report_errors();
}
#endif

44
test/e_errno_test.cpp Normal file
View File

@@ -0,0 +1,44 @@
// Copyright (c) 2018-2021 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/common.hpp>
# include <boost/leaf/handle_errors.hpp>
# include <boost/leaf/result.hpp>
#endif
#include "lightweight_test.hpp"
#include <sstream>
namespace leaf = boost::leaf;
int main()
{
errno = ENOENT;
std::stringstream ss;
ss << leaf::e_errno{};
BOOST_TEST(ss.str().find(std::strerror(ENOENT)) != std::string::npos);
int r = leaf::try_handle_all(
[]() -> leaf::result<int>
{
struct reset_errno { ~reset_errno() { errno=0; } } reset;
return leaf::new_error( leaf::e_errno{} );
},
[]( leaf::e_errno e )
{
BOOST_TEST_EQ(errno, 0);
BOOST_TEST_EQ(e.value, ENOENT);
return 1;
},
[]
{
return 2;
} );
BOOST_TEST_EQ(r, 1);
return boost::report_errors();
}