1 /* Unit testing for outcomes
2 (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (6 commits)
5 Boost Software License - Version 1.0 - August 17th, 2003
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:
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.
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.
30 #if defined(__cpp_coroutines)
32 #include <boost/outcome/coroutine_support.hpp>
33 #include <boost/outcome/outcome.hpp>
34 #include <boost/outcome/try.hpp>
35 #include <boost/test/unit_test.hpp>
36 #include <boost/test/unit_test_monitor.hpp>
40 template <class T> using eager = BOOST_OUTCOME_V2_NAMESPACE::awaitables::eager<T>;
41 template <class T> using lazy = BOOST_OUTCOME_V2_NAMESPACE::awaitables::lazy<T>;
42 template <class T, class E = boost::system::error_code> using result = BOOST_OUTCOME_V2_NAMESPACE::result<T, E>;
44 inline eager<result<int>> eager_int(int x) { co_return x + 1; }
45 inline lazy<result<int>> lazy_int(int x) { co_return x + 1; }
46 inline eager<result<int>> eager_error() { co_return boost::system::errc::not_enough_memory; }
47 inline lazy<result<int>> lazy_error() { co_return boost::system::errc::not_enough_memory; }
48 inline eager<result<void>> eager_void() { co_return boost::system::errc::not_enough_memory; }
49 inline lazy<result<void>> lazy_void() { co_return boost::system::errc::not_enough_memory; }
51 template <class U, class... Args> inline eager<result<std::string>> eager_coawait(U &&f, Args... args)
53 BOOST_OUTCOME_CO_TRY(co_await f(args...));
56 template <class U, class... Args> inline lazy<result<std::string>> lazy_coawait(U &&f, Args... args)
58 BOOST_OUTCOME_CO_TRY(co_await f(args...));
62 #ifndef BOOST_NO_EXCEPTIONS
63 struct custom_exception_type
66 inline lazy<result<int, boost::exception_ptr>> result_exception(boost::exception_ptr e)
68 boost::rethrow_exception(e);
72 inline lazy<BOOST_OUTCOME_V2_NAMESPACE::outcome<int>> outcome_exception(boost::exception_ptr e)
74 boost::rethrow_exception(e);
79 inline eager<int> eager_int2(int x) { co_return x + 1; }
80 inline lazy<int> lazy_int2(int x) { co_return x + 1; }
81 inline eager<void> eager_void2() { co_return; }
82 inline lazy<void> lazy_void2() { co_return; }
83 } // namespace coroutines
85 BOOST_OUTCOME_AUTO_TEST_CASE(works_result_coroutine, "Tests that results are eager and lazy awaitable")
87 using namespace coroutines;
88 auto eager_await = [](auto t) { return t.await_resume(); };
89 auto lazy_await = [](auto t) {
91 return t.await_resume();
94 BOOST_CHECK(eager_await(eager_int(5)).value() == 6);
95 BOOST_CHECK(lazy_await(lazy_int(5)).value() == 6);
96 BOOST_CHECK(eager_await(eager_error()).error() == boost::system::errc::not_enough_memory);
97 BOOST_CHECK(lazy_await(lazy_error()).error() == boost::system::errc::not_enough_memory);
98 BOOST_CHECK(eager_await(eager_void()).error() == boost::system::errc::not_enough_memory);
99 BOOST_CHECK(lazy_await(lazy_void()).error() == boost::system::errc::not_enough_memory);
101 BOOST_CHECK(eager_await(eager_coawait(eager_int, 5)).value() == "hi");
102 BOOST_CHECK(lazy_await(lazy_coawait(lazy_int, 5)).value() == "hi");
104 #ifndef BOOST_NO_EXCEPTIONS
105 auto e = boost::copy_exception(custom_exception_type());
106 BOOST_CHECK_THROW(lazy_await(result_exception(e)).value(), custom_exception_type);
107 BOOST_CHECK_THROW(lazy_await(outcome_exception(e)).value(), custom_exception_type);
110 BOOST_CHECK(eager_await(eager_int2(5)) == 6);
111 BOOST_CHECK(lazy_await(lazy_int2(5)) == 6);
112 eager_await(eager_void2());
113 lazy_await(lazy_void2());