• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 .post().
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([] {
28                 out << "a::f::post" << std::endl;
29                 throw a_err(); // Test this throws.
30             })
31             .except([] { out << "a::f::except" << std::endl; })
32         ;
33         out << "a::f::body" << std::endl;
34     }
35 };
36 
main()37 int main() {
38     std::ostringstream ok;
39 
40     boost::contract::set_postcondition_failure(
41             [] (boost::contract::from) { throw; });
42 
43     try {
44         out.str("");
45         a::f();
46         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
47                 BOOST_TEST(false);
48             } catch(a_err const&) {
49         #endif
50         ok.str(""); ok
51             #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
52                 << "a::static_inv" << std::endl
53             #endif
54             #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
55                 << "a::f::pre" << std::endl
56             #endif
57             #ifndef BOOST_CONTRACT_NO_OLDS
58                 << "a::f::old" << std::endl
59             #endif
60             << "a::f::body" << std::endl
61             #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
62                 << "a::static_inv" << std::endl
63             #endif
64             #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
65                 << "a::f::post" << std::endl // Test this threw.
66             #endif
67         ;
68         BOOST_TEST(out.eq(ok.str()));
69     } catch(...) { BOOST_TEST(false); }
70 
71     return boost::report_errors();
72 }
73 
74