• 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 free function .post().
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()19 void 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([] {
24             out << "f::post" << std::endl;
25             throw err(); // Test this throws.
26         })
27         .except([] { out << "f::except" << std::endl; })
28     ;
29     out << "f::body" << std::endl;
30 }
31 
main()32 int main() {
33     std::ostringstream ok;
34 
35     boost::contract::set_postcondition_failure(
36             [] (boost::contract::from) { throw; });
37 
38     try {
39         out.str("");
40         f();
41         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
42                 BOOST_TEST(false);
43             } catch(err const&) {
44         #endif
45         ok.str(""); ok << "" // Suppress a warning.
46             #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
47                 << "f::pre" << std::endl
48             #endif
49             #ifndef BOOST_CONTRACT_NO_OLDS
50                 << "f::old" << std::endl
51             #endif
52             << "f::body" << std::endl
53             #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
54                 << "f::post" << std::endl // Test this threw.
55             #endif
56         ;
57         BOOST_TEST(out.eq(ok.str()));
58     } catch(...) { BOOST_TEST(false); }
59 
60     return boost::report_errors();
61 }
62 
63