1 /* Unit testing for outcomes
2 (C) 2013-2020 Niall Douglas <http://www.nedproductions.biz/> (7 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.
31 #pragma warning(disable : 4702) // unreachable code
34 #include <boost/outcome/outcome.hpp>
35 #include <boost/test/unit_test.hpp>
36 #include <boost/test/unit_test_monitor.hpp>
38 BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_udts
, "Tests that the outcome works as intended with user-defined types")
40 using namespace BOOST_OUTCOME_V2_NAMESPACE
;
41 // No default constructor, no copy/move, no assignment
51 udt(const udt
&) = delete;
53 udt
&operator=(const udt
&) = delete;
54 udt
&operator=(udt
&&) = delete;
57 outcome
<udt
> foo(in_place_type
<udt
>, 5);
58 BOOST_CHECK(5 == foo
.value().a
);
60 #ifndef BOOST_NO_EXCEPTIONS
61 // Emplace construct, throws during move and copy
66 explicit udt(std::string _a
)
71 udt(const udt
& /*unused*/) { throw std::logic_error("copy"); }
72 udt(udt
&& /*unused*/) noexcept(false) { throw std::logic_error("move"); } // NOLINT
73 udt
&operator=(const udt
& /*unused*/) { throw std::logic_error("copy"); }
74 udt
&operator=(udt
&& /*unused*/) noexcept(false) { throw std::logic_error("move"); } // NOLINT
77 static_assert(!std::is_default_constructible
<udt
>::value
, "udt is default constructible");
78 static_assert(std::is_copy_constructible
<udt
>::value
, "udt is not copy constructible");
79 static_assert(std::is_move_constructible
<udt
>::value
, "udt is not move constructible");
80 static_assert(!std::is_default_constructible
<outcome
<udt
>>::value
, "outcome<udt> is default constructible");
81 static_assert(std::is_copy_constructible
<outcome
<udt
>>::value
, "outcome<udt> is not copy constructible");
82 static_assert(std::is_move_constructible
<outcome
<udt
>>::value
, "outcome<udt> is not move constructible");
84 outcome
<udt
> foo(in_place_type
<udt
>, "niall");
85 BOOST_CHECK("niall" == foo
.value().a
);
88 auto foo2(foo
); // NOLINT
91 catch(const std::logic_error
&e
)
93 BOOST_CHECK(!strcmp(e
.what(), "copy"));
99 BOOST_CHECK("niall" == foo
.value().a
);
102 auto foo2(std::move(foo
));
105 catch(const std::logic_error
&e
)
107 BOOST_CHECK(!strcmp(e
.what(), "move"));
113 BOOST_CHECK("niall" == foo
.value().a
); // NOLINT
114 // Does throwing during copy assignment work?
116 outcome
<udt
> foo2(in_place_type
<udt
>, "douglas");
122 catch(const std::logic_error
&e
)
124 BOOST_CHECK(!strcmp(e
.what(), "copy"));
125 BOOST_CHECK(foo2
.value().a
== "douglas");
131 BOOST_CHECK("niall" == foo
.value().a
);
133 // Does throwing during move assignment work?
135 outcome
<udt
> foo2(in_place_type
<udt
>, "douglas");
138 foo2
= std::move(foo
);
141 catch(const std::logic_error
&e
)
143 BOOST_CHECK(!strcmp(e
.what(), "move"));
144 BOOST_CHECK(foo2
.value().a
== "douglas");
150 BOOST_CHECK("niall" == foo
.value().a
); // NOLINT