• 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 contracts in .cpp compiled to never check post/except (but not in .hpp).
8 
9 #include "lib_x.hpp"
10 #include "lib_y.hpp"
11 #include "../detail/oteststream.hpp"
12 #include <boost/contract/function.hpp>
13 #include <boost/contract/check.hpp>
14 #include <boost/detail/lightweight_test.hpp>
15 #include <sstream>
16 
f()17 void f() {
18     using boost::contract::test::detail::out;
19     boost::contract::check c = boost::contract::function()
20         // Capturing [&] so out() visible in MSVC10 lambdas.
21         .precondition([&] { out("f::pre\n"); })
22         .old([&] { out("f::old\n"); })
23         .postcondition([&] { out("f::post\n"); })
24     ;
25     out("f::body\n");
26 }
27 
main()28 int main() {
29     using boost::contract::test::detail::out;
30     std::ostringstream ok;
31 
32     out("");
33     f();
34     ok.str(""); ok // Test normal (no lib) case.
35         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
36             << "f::pre" << std::endl
37         #endif
38         #ifndef BOOST_CONTRACT_NO_OLDS
39             << "f::old" << std::endl
40         #endif
41         << "f::body" << std::endl
42         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
43             << "f::post" << std::endl
44         #endif
45     ;
46     BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(), ok.str()));
47 
48     out("");
49     x();
50     ok.str(""); ok // Test contracts in .cpp so no post (NO_POST in build file).
51         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
52             << "x::pre" << std::endl
53         #endif
54         << "x::body" << std::endl
55     ;
56     BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(), ok.str()));
57 
58     out("");
59     y();
60     ok.str(""); ok // Test contracts in .hpp so post (NO_POST in build file).
61         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
62             << "y::pre" << std::endl
63         #endif
64         #ifndef BOOST_CONTRACT_NO_OLDS
65             << "y::old" << std::endl
66         #endif
67         << "y::body" << std::endl
68         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
69             << "y::post" << std::endl
70         #endif
71     ;
72     BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(), ok.str()));
73 
74     return boost::report_errors();
75 }
76 
77