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 public static member function .old(). 8 9 #include "../detail/oteststream.hpp" 10 #include <boost/contract/public_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 a_err {}; // Global decl so visible in MSVC10 lambdas. 18 19 struct a { static_invarianta20 static void static_invariant() { out << "a::static_inv" << std::endl; } invarianta21 void invariant() const { out << "a::inv" << std::endl; } 22 fa23 static void f() { 24 boost::contract::check c = boost::contract::public_function<a>() 25 .precondition([] { out << "a::f::pre" << std::endl; }) 26 .old([] { 27 out << "a::f::old" << std::endl; 28 throw a_err(); // Test this throws. 29 }) 30 .postcondition([] { out << "a::f::post" << std::endl; }) 31 .except([] { out << "a::f::except" << std::endl; }) 32 ; 33 out << "a::f::body" << std::endl; 34 } 35 }; 36 main()37int main() { 38 std::ostringstream ok; 39 40 boost::contract::set_old_failure([] (boost::contract::from) { throw; }); 41 42 try { 43 out.str(""); 44 a::f(); 45 #ifndef BOOST_CONTRACT_NO_OLDS 46 BOOST_TEST(false); 47 } catch(a_err const&) { 48 #endif 49 ok.str(""); ok 50 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS 51 << "a::static_inv" << std::endl 52 #endif 53 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS 54 << "a::f::pre" << std::endl 55 #endif 56 #ifndef BOOST_CONTRACT_NO_OLDS 57 << "a::f::old" << std::endl // Test this threw. 58 #else 59 << "a::f::body" << std::endl 60 // Test no post (but still static inv) because .old() threw. 61 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS 62 << "a::static_inv" << std::endl 63 #endif 64 #endif 65 ; 66 BOOST_TEST(out.eq(ok.str())); 67 } catch(...) { BOOST_TEST(false); } 68 69 return boost::report_errors(); 70 } 71 72