1010# include < boost/leaf/handle_errors.hpp>
1111# include < boost/leaf/result.hpp>
1212# include < boost/leaf/diagnostics.hpp>
13+ # include < boost/leaf/exception.hpp>
1314#endif
1415
1516#if !BOOST_LEAF_CFG_CAPTURE || defined(BOOST_LEAF_NO_EXCEPTIONS)
@@ -22,6 +23,7 @@ int main()
2223#else
2324
2425#include < sstream>
26+ #include < stdexcept>
2527#include < string>
2628
2729namespace leaf = boost::leaf;
@@ -119,6 +121,118 @@ int main()
119121 }
120122#endif
121123
124+ // //////////////////////////////////////
125+
126+ {
127+ auto captured = leaf::try_capture_all (
128+ []() -> leaf::result<void >
129+ {
130+ auto load = leaf::on_error ( other_info<1 >{1 }, alloc_fail_info{42 }, other_info<2 >{2 } );
131+ throw std::runtime_error (" test" );
132+ } );
133+
134+ int r = leaf::try_handle_all (
135+ [&]() -> leaf::result<int >
136+ {
137+ (void ) captured.value ();
138+ return 0 ;
139+ },
140+ []( other_info<1 > const & oi1, other_info<2 > const * oi2, alloc_fail_info const * afi )
141+ {
142+ BOOST_TEST_EQ (oi1.value , 1 );
143+ BOOST_TEST_EQ (oi2, nullptr );
144+ BOOST_TEST_EQ (afi, nullptr );
145+ return 1 ;
146+ },
147+ []
148+ {
149+ return 2 ;
150+ } );
151+ BOOST_TEST_EQ (r, 1 );
152+ }
153+
154+ #if BOOST_LEAF_CFG_DIAGNOSTICS && BOOST_LEAF_CFG_STD_STRING
155+ {
156+ int r = leaf::try_catch (
157+ []() -> int
158+ {
159+ auto load = leaf::on_error ( other_info<1 >{1 }, alloc_fail_info{42 }, other_info<2 >{2 } );
160+ throw std::runtime_error (" test" );
161+ },
162+ []( leaf::diagnostic_details const & dd )
163+ {
164+ std::ostringstream s;
165+ s << dd;
166+ std::string str = s.str ();
167+ BOOST_TEST (str.find (" other_info<1>" ) != std::string::npos);
168+ BOOST_TEST (str.find (" other_info<2>" ) == std::string::npos);
169+ BOOST_TEST (str.find (" alloc_fail_info" ) == std::string::npos);
170+ return 1 ;
171+ },
172+ []
173+ {
174+ return 2 ;
175+ } );
176+ BOOST_TEST_EQ (r, 1 );
177+ }
178+ #endif
179+
180+ // //////////////////////////////////////
181+
182+ {
183+ auto captured = leaf::try_capture_all (
184+ []() -> leaf::result<void >
185+ {
186+ auto load = leaf::on_error ( other_info<1 >{1 }, alloc_fail_info{42 }, other_info<2 >{2 } );
187+ leaf::throw_exception (std::runtime_error (" test" ));
188+ } );
189+
190+ int r = leaf::try_handle_all (
191+ [&]() -> leaf::result<int >
192+ {
193+ (void ) captured.value ();
194+ return 0 ;
195+ },
196+ []( std::bad_alloc const &, other_info<1 > const & oi1, other_info<2 > const * oi2, alloc_fail_info const * afi )
197+ {
198+ BOOST_TEST_EQ (oi1.value , 1 );
199+ BOOST_TEST_EQ (oi2, nullptr );
200+ BOOST_TEST_EQ (afi, nullptr );
201+ return 1 ;
202+ },
203+ []
204+ {
205+ return 2 ;
206+ } );
207+ BOOST_TEST_EQ (r, 1 );
208+ }
209+
210+ #if BOOST_LEAF_CFG_DIAGNOSTICS && BOOST_LEAF_CFG_STD_STRING
211+ {
212+ int r = leaf::try_catch (
213+ []() -> int
214+ {
215+ auto load = leaf::on_error ( other_info<1 >{1 }, alloc_fail_info{42 }, other_info<2 >{2 } );
216+ leaf::throw_exception (std::runtime_error (" test" ));
217+ },
218+ []( std::bad_alloc const &, leaf::diagnostic_details const & dd )
219+ {
220+ std::ostringstream s;
221+ s << dd;
222+ std::string str = s.str ();
223+ BOOST_TEST (str.find (" other_info<1>" ) != std::string::npos);
224+ BOOST_TEST (str.find (" other_info<2>" ) == std::string::npos);
225+ BOOST_TEST (str.find (" alloc_fail_info" ) == std::string::npos);
226+ return 1 ;
227+ },
228+ []
229+ {
230+ return 2 ;
231+ } );
232+ BOOST_TEST_EQ (r, 1 );
233+ }
234+ #endif
235+
122236 return boost::report_errors ();
123237}
124238
0 commit comments