]>
Commit | Line | Data |
---|---|---|
92f5a8d4 | 1 | /* Unit testing for outcomes |
1e59de90 | 2 | (C) 2013-2022 Niall Douglas <http://www.nedproductions.biz/> (7 commits) |
92f5a8d4 TL |
3 | |
4 | ||
5 | Boost Software License - Version 1.0 - August 17th, 2003 | |
6 | ||
7 | Permission is hereby granted, free of charge, to any person or organization | |
8 | obtaining a copy of the software and accompanying documentation covered by | |
9 | this license (the "Software") to use, reproduce, display, distribute, | |
10 | execute, and transmit the Software, and to prepare derivative works of the | |
11 | Software, and to permit third-parties to whom the Software is furnished to | |
12 | do so, all subject to the following: | |
13 | ||
14 | The copyright notices in the Software and this entire statement, including | |
15 | the above license grant, this restriction and the following disclaimer, | |
16 | must be included in all copies of the Software, in whole or in part, and | |
17 | all derivative works of the Software, unless such copies or derivative | |
18 | works are solely in the form of machine-executable object code generated by | |
19 | a source language processor. | |
20 | ||
21 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
22 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
23 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | |
24 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | |
25 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | |
26 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
27 | DEALINGS IN THE SOFTWARE. | |
28 | */ | |
29 | ||
30 | #define _CRT_SECURE_NO_WARNINGS | |
31 | ||
32 | #include <boost/outcome.hpp> | |
33 | #include <boost/test/unit_test.hpp> | |
34 | #include <boost/test/unit_test_monitor.hpp> | |
35 | ||
36 | #ifndef BOOST_NO_EXCEPTIONS | |
37 | ||
38 | #ifdef _MSC_VER | |
39 | #define BOOST_OUTCOME_POSIX_OPEN ::_open | |
40 | #include <io.h> | |
41 | #else | |
42 | #define BOOST_OUTCOME_POSIX_OPEN ::open | |
43 | #include <fcntl.h> | |
44 | #endif | |
45 | ||
46 | BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_fileopen, "Tests that the outcome semantically represents opening a file") | |
47 | { | |
48 | using namespace BOOST_OUTCOME_V2_NAMESPACE; | |
49 | ||
50 | //! [file_open_example] | |
51 | auto openfile = [](std::string path) noexcept->outcome<int> | |
52 | { | |
53 | int fd; | |
54 | do | |
55 | { | |
56 | fd = BOOST_OUTCOME_POSIX_OPEN(path.c_str(), 0); // NOLINT | |
57 | } while(-1 == fd && EINTR == errno); | |
58 | try | |
59 | { | |
60 | if(-1 == fd) | |
61 | { | |
62 | int code = errno; | |
63 | // If a temporary failure, this is an expected unexpected outcome | |
64 | if(EBUSY == code || EISDIR == code || ELOOP == code || ENOENT == code || ENOTDIR == code || EPERM == code || EACCES == code) | |
65 | { | |
66 | return boost::system::error_code(code, boost::system::generic_category()); | |
67 | } | |
68 | // If a non-temporary failure, this is an unexpected outcome | |
69 | return boost::copy_exception(boost::system::system_error(code, boost::system::generic_category(), strerror(code))); | |
70 | } | |
71 | return fd; | |
72 | } | |
73 | catch(...) | |
74 | { | |
75 | // Any exception thrown is truly unexpected | |
76 | return boost::current_exception(); | |
77 | } | |
78 | }; | |
79 | auto a = openfile("shouldneverexistnotever"); | |
80 | BOOST_CHECK(!a); | |
81 | BOOST_CHECK(!a.has_value()); | |
82 | BOOST_CHECK(!a.has_exception()); | |
83 | BOOST_CHECK(a.has_error()); | |
84 | BOOST_CHECK(a.error() == boost::system::error_code(ENOENT, boost::system::generic_category())); | |
85 | //! [file_open_example] | |
86 | } | |
87 | #endif |