1/* Unit testing for outcomes
2(C) 2013-2024 Niall Douglas <http://www.nedproductions.biz/> (14 commits)
3
4
5Boost Software License - Version 1.0 - August 17th, 2003
6
7Permission is hereby granted, free of charge, to any person or organization
8obtaining a copy of the software and accompanying documentation covered by
9this license (the "Software") to use, reproduce, display, distribute,
10execute, and transmit the Software, and to prepare derivative works of the
11Software, and to permit third-parties to whom the Software is furnished to
12do so, all subject to the following:
13
14The copyright notices in the Software and this entire statement, including
15the above license grant, this restriction and the following disclaimer,
16must be included in all copies of the Software, in whole or in part, and
17all derivative works of the Software, unless such copies or derivative
18works are solely in the form of machine-executable object code generated by
19a source language processor.
20
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27DEALINGS IN THE SOFTWARE.
28*/
29
30#define _CRT_SECURE_NO_WARNINGS
31#define BOOST_OUTCOME_ENABLE_LEGACY_SUPPORT_FOR 210 // legacy ADL hooks support
32
33#include <boost/outcome.hpp>
34#include <boost/test/unit_test.hpp>
35#include <boost/test/unit_test_monitor.hpp>
36
37#include <iostream>
38
39namespace hook_test
40{
41 using BOOST_OUTCOME_V2_NAMESPACE::in_place_type;
42 // Use static storage to convey extended error info from result construction to outcome conversion
43 static char extended_error_info[256];
44
45 // Use the error_code type as the ADL bridge for the hooks by creating a type here
46 struct error_code : public boost::system::error_code
47 {
48 error_code() = default;
49 error_code(boost::system::error_code ec) // NOLINT
50 : boost::system::error_code(ec)
51 {
52 }
53 };
54 // Localise result to using the local error_code so this namespace gets looked up for the hooks
55 template <class R> using result = BOOST_OUTCOME_V2_NAMESPACE::result<R, error_code>;
56 // Specialise the result construction hook for our localised result
57 template <class U> constexpr inline void hook_result_construction(result<int> *res, U && /*unused*/) noexcept
58 {
59 // Write the value in the result into the static storage
60 snprintf(s: extended_error_info, maxlen: sizeof(extended_error_info), format: "%d", res->assume_value()); // NOLINT
61 }
62 template <class U> constexpr inline void hook_result_construction(result<std::string> *res, U && /*unused*/) noexcept
63 {
64 // Write the value in the result into the static storage
65 snprintf(s: extended_error_info, maxlen: sizeof(extended_error_info), format: "%s", res->assume_value().c_str()); // NOLINT
66 }
67} // namespace hook_test
68
69BOOST_OUTCOME_AUTO_TEST_CASE(works_result_hooks, "Tests that you can hook result's construction")
70{
71 using namespace hook_test;
72 result<int> a(5);
73 BOOST_CHECK(!strcmp(extended_error_info, "5")); // NOLINT
74 result<std::string> b("niall");
75 BOOST_CHECK(!strcmp(extended_error_info, "niall")); // NOLINT
76}
77
78//! [extended_error_coding2]
79namespace hook_test
80{
81 // Localise outcome to using the local error_code so this namespace gets looked up for the hooks
82 template <class R> using outcome = BOOST_OUTCOME_V2_NAMESPACE::outcome<R, error_code, std::string>;
83
84 // Specialise the outcome copy and move conversion hook for our localised result
85 template <class T, class U> constexpr inline void hook_outcome_copy_construction(outcome<T> *res, const result<U> & /*unused*/) noexcept
86 {
87 // when copy constructing from a result<T>, place extended_error_coding::extended_error_info into the payload
88 std::cout << "hook_outcome_copy_construction fires" << std::endl;
89 BOOST_OUTCOME_V2_NAMESPACE::hooks::override_outcome_exception(res, extended_error_info);
90 }
91 template <class T, class U> constexpr inline void hook_outcome_move_construction(outcome<T> *res, result<U> && /*unused*/) noexcept
92 {
93 // when move constructing from a result<T>, place extended_error_coding::extended_error_info into the payload
94 std::cout << "hook_outcome_move_construction fires" << std::endl;
95 BOOST_OUTCOME_V2_NAMESPACE::hooks::override_outcome_exception(res, extended_error_info);
96 }
97} // namespace hook_test
98
99BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_hooks, "Tests that you can hook outcome's conversion from a result")
100{
101 using namespace hook_test;
102 outcome<int> a(result<int>(5));
103 BOOST_REQUIRE(a.has_exception()); // NOLINT
104 BOOST_CHECK(a.exception() == "5");
105 outcome<std::string> b(result<std::string>("niall"));
106 BOOST_CHECK(b.exception() == "niall");
107
108 // Make sure hook does not fire for any other kind of outcome copy or move, only when converting from our custom result only
109 outcome<int> c(5);
110 outcome<long> d(c); // can't be the same type as source, else copy elision takes place and no ADL hook calling
111 BOOST_CHECK(!d.has_exception());
112 outcome<int> e(BOOST_OUTCOME_V2_NAMESPACE::result<int>(5));
113 BOOST_CHECK(!e.has_exception());
114}
115

source code of boost/libs/outcome/test/tests/hooks.cpp