1 2 // Copyright (C) 2008-2018 Lorenzo Caminiti 3 // Distributed under the Boost Software License, Version 1.0 (see accompanying 4 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). 5 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html 6 7 // Test throw from free function body. 8 9 #include "../detail/oteststream.hpp" 10 #include <boost/contract/function.hpp> 11 #include <boost/contract/check.hpp> 12 #include <boost/detail/lightweight_test.hpp> 13 #include <sstream> 14 15 boost::contract::test::detail::oteststream out; 16 17 struct err {}; // Global decl so visible in MSVC10 lambdas. 18 f()19void f() { 20 boost::contract::check c = boost::contract::function() 21 .precondition([] { out << "f::pre" << std::endl; }) 22 .old([] { out << "f::old" << std::endl; }) 23 .postcondition([] { out << "f::post" << std::endl; }) 24 .except([] { out << "f::except" << std::endl; }) 25 ; 26 out << "f::body" << std::endl; 27 throw err(); // Test body throws. 28 } 29 main()30int main() { 31 std::ostringstream ok; 32 33 try { 34 out.str(""); 35 f(); 36 BOOST_TEST(false); 37 } catch(err const&) { 38 ok.str(""); ok 39 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS 40 << "f::pre" << std::endl 41 #endif 42 #ifndef BOOST_CONTRACT_NO_OLDS 43 << "f::old" << std::endl 44 #endif 45 << "f::body" << std::endl // Test this threw. 46 #ifndef BOOST_CONTRACT_NO_EXCEPTS 47 << "f::except" << std::endl; 48 #endif 49 ; 50 BOOST_TEST(out.eq(ok.str())); 51 } catch(...) { BOOST_TEST(false); } 52 53 return boost::report_errors(); 54 } 55 56