From 294728e5b52bd41b38fa85e830d85fffb1e59a34 Mon Sep 17 00:00:00 2001 From: Emil Dotchevski Date: Wed, 10 Feb 2021 18:35:46 -0800 Subject: [PATCH] Fixing bug in e_LastError, adding unit tests --- include/boost/leaf/common.hpp | 8 ++++- meson.build | 2 ++ test/Jamfile.v2 | 2 ++ test/e_LastError_test.cpp | 59 +++++++++++++++++++++++++++++++++++ test/e_errno_test.cpp | 44 ++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 test/e_LastError_test.cpp create mode 100644 test/e_errno_test.cpp diff --git a/include/boost/leaf/common.hpp b/include/boost/leaf/common.hpp index 6973e7c..4fad6b7 100644 --- a/include/boost/leaf/common.hpp +++ b/include/boost/leaf/common.hpp @@ -40,6 +40,8 @@ struct e_errno { int value; + explicit e_errno(int value=errno): value(value) { } + template friend std::basic_ostream & operator<<( std::basic_ostream & 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 - friend std::basic_ostream & operator<<( std::basic_ostream os, e_LastError const & err ) + friend std::basic_ostream & operator<<( std::basic_ostream & os, e_LastError const & err ) { struct msg_buf { diff --git a/meson.build b/meson.build index bbe0926..29f5f4a 100644 --- a/meson.build +++ b/meson.build @@ -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', diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 5ecbd1a..55eac91 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -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 ; diff --git a/test/e_LastError_test.cpp b/test/e_LastError_test.cpp new file mode 100644 index 0000000..9edfcaa --- /dev/null +++ b/test/e_LastError_test.cpp @@ -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 + +int main() +{ + std::cout << "This test requires Windows"; + return 0; +} + +#else + +#ifdef BOOST_LEAF_TEST_SINGLE_HEADER +# include "leaf.hpp" +#else +# include +# include +# include +#endif + +#include "lightweight_test.hpp" +#include + +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 + { + 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 diff --git a/test/e_errno_test.cpp b/test/e_errno_test.cpp new file mode 100644 index 0000000..b868840 --- /dev/null +++ b/test/e_errno_test.cpp @@ -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 +# include +# include +#endif + +#include "lightweight_test.hpp" +#include + +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 + { + 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(); +}