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 public static member function throwing. 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([] { out << "a::f::old" << std::endl; }) 27 .postcondition([] { out << "a::f::post" << std::endl; }) 28 .except([] { out << "a::f::except" << std::endl; }) 29 ; 30 out << "a::f::body" << std::endl; 31 throw a_err(); // Test this throws. 32 } 33 }; 34 main()35int main() { 36 std::ostringstream ok; 37 38 try { 39 out.str(""); 40 a::f(); 41 BOOST_TEST(false); 42 } catch(a_err const&) { 43 ok.str(""); ok 44 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS 45 << "a::static_inv" << std::endl 46 #endif 47 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS 48 << "a::f::pre" << std::endl 49 #endif 50 #ifndef BOOST_CONTRACT_NO_OLDS 51 << "a::f::old" << std::endl 52 #endif 53 << "a::f::body" << std::endl // Test this threw. 54 // Test no post (but still static inv) because body threw. 55 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS 56 << "a::static_inv" << std::endl 57 #endif 58 #ifndef BOOST_CONTRACT_NO_EXCEPTS 59 << "a::f::except" << std::endl 60 #endif 61 ; 62 BOOST_TEST(out.eq(ok.str())); 63 } catch(...) { BOOST_TEST(false); } 64 65 return boost::report_errors(); 66 } 67 68