throwing_body.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (C) 2008-2018 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  3. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  4. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  5. // Test throw from free function body.
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/function.hpp>
  8. #include <boost/contract/check.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <sstream>
  11. boost::contract::test::detail::oteststream out;
  12. struct err {}; // Global decl so visible in MSVC10 lambdas.
  13. void f() {
  14. boost::contract::check c = boost::contract::function()
  15. .precondition([] { out << "f::pre" << std::endl; })
  16. .old([] { out << "f::old" << std::endl; })
  17. .postcondition([] { out << "f::post" << std::endl; })
  18. .except([] { out << "f::except" << std::endl; })
  19. ;
  20. out << "f::body" << std::endl;
  21. throw err(); // Test body throws.
  22. }
  23. int main() {
  24. std::ostringstream ok;
  25. try {
  26. out.str("");
  27. f();
  28. BOOST_TEST(false);
  29. } catch(err const&) {
  30. ok.str(""); ok
  31. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  32. << "f::pre" << std::endl
  33. #endif
  34. #ifndef BOOST_CONTRACT_NO_OLDS
  35. << "f::old" << std::endl
  36. #endif
  37. << "f::body" << std::endl // Test this threw.
  38. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  39. << "f::except" << std::endl;
  40. #endif
  41. ;
  42. BOOST_TEST(out.eq(ok.str()));
  43. } catch(...) { BOOST_TEST(false); }
  44. return boost::report_errors();
  45. }