| 1 | /* Unit testing for outcomes |
| 2 | (C) 2013-2024 Niall Douglas <http://www.nedproductions.biz/> (1 commit) |
| 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 | #include <boost/outcome/std_result.hpp> |
| 31 | #include <boost/outcome/try.hpp> |
| 32 | #include <boost/test/unit_test.hpp> |
| 33 | #include <boost/test/unit_test_monitor.hpp> |
| 34 | |
| 35 | namespace outcome = BOOST_OUTCOME_V2_NAMESPACE; |
| 36 | |
| 37 | namespace stdalias = std; |
| 38 | |
| 39 | enum class error |
| 40 | { |
| 41 | test, |
| 42 | abcde |
| 43 | }; |
| 44 | |
| 45 | class error_category_impl : public std::error_category |
| 46 | { |
| 47 | public: |
| 48 | const char *name() const noexcept override { return "test" ; } |
| 49 | |
| 50 | std::string message(int code) const noexcept override |
| 51 | { |
| 52 | switch(static_cast<error>(code)) |
| 53 | { |
| 54 | case error::test: |
| 55 | return "test" ; |
| 56 | case error::abcde: |
| 57 | return "abcde" ; |
| 58 | } |
| 59 | return "unknown" ; |
| 60 | } |
| 61 | }; |
| 62 | const std::error_category &error_category() noexcept |
| 63 | { |
| 64 | static error_category_impl instance; |
| 65 | return instance; |
| 66 | } |
| 67 | |
| 68 | stdalias::error_code make_error_code(error error) noexcept |
| 69 | { |
| 70 | return {static_cast<int>(error), error_category()}; |
| 71 | } |
| 72 | |
| 73 | namespace std |
| 74 | { |
| 75 | template <> struct is_error_code_enum<error> : true_type |
| 76 | { |
| 77 | }; |
| 78 | } // namespace std |
| 79 | |
| 80 | template <typename T> using enum_result = outcome::basic_result<T, error, outcome::policy::default_policy<T, error, void>>; |
| 81 | |
| 82 | enum_result<int> test() |
| 83 | { |
| 84 | return 5; |
| 85 | } |
| 86 | |
| 87 | outcome::std_result<int> abc() |
| 88 | { |
| 89 | static_assert(std::is_error_code_enum<error>::value, "custom enum is not marked convertible to error code" ); |
| 90 | static_assert(std::is_constructible<stdalias::error_code, error>::value, "error code is not explicitly constructible from custom enum" ); |
| 91 | static_assert(std::is_convertible<error, stdalias::error_code>::value, "error code is not implicitly constructible from custom enum" ); |
| 92 | stdalias::error_code ec = error::test; // custom enum is definitely convertible to error code |
| 93 | BOOST_OUTCOME_TRY(test()); // hence this should compile, as implicit conversions work here |
| 94 | (void) ec; |
| 95 | |
| 96 | // But explicit conversions are required between dissimilar basic_result, implicit conversions are disabled |
| 97 | static_assert(std::is_constructible<outcome::std_result<int>, enum_result<int>>::value, "basic_result with error code is not explicitly constructible from basic_result with custom enum" ); |
| 98 | static_assert(!std::is_convertible<enum_result<int>, outcome::std_result<int>>::value, "basic_result with error code is implicitly constructible from basic_result with custom enum" ); |
| 99 | return 5; |
| 100 | } |
| 101 | |
| 102 | BOOST_OUTCOME_AUTO_TEST_CASE(issues_0203_test, "enum convertible to error code works as designed" ) |
| 103 | { |
| 104 | BOOST_CHECK(abc().value() == 5); |
| 105 | } |
| 106 | |