static_throwing_body.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 public static member function throwing.
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/public_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 a_err {}; // Global decl so visible in MSVC10 lambdas.
  13. struct a {
  14. static void static_invariant() { out << "a::static_inv" << std::endl; }
  15. void invariant() const { out << "a::inv" << std::endl; }
  16. static void f() {
  17. boost::contract::check c = boost::contract::public_function<a>()
  18. .precondition([] { out << "a::f::pre" << std::endl; })
  19. .old([] { out << "a::f::old" << std::endl; })
  20. .postcondition([] { out << "a::f::post" << std::endl; })
  21. .except([] { out << "a::f::except" << std::endl; })
  22. ;
  23. out << "a::f::body" << std::endl;
  24. throw a_err(); // Test this throws.
  25. }
  26. };
  27. int main() {
  28. std::ostringstream ok;
  29. try {
  30. out.str("");
  31. a::f();
  32. BOOST_TEST(false);
  33. } catch(a_err const&) {
  34. ok.str(""); ok
  35. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  36. << "a::static_inv" << std::endl
  37. #endif
  38. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  39. << "a::f::pre" << std::endl
  40. #endif
  41. #ifndef BOOST_CONTRACT_NO_OLDS
  42. << "a::f::old" << std::endl
  43. #endif
  44. << "a::f::body" << std::endl // Test this threw.
  45. // Test no post (but still static inv) because body threw.
  46. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  47. << "a::static_inv" << std::endl
  48. #endif
  49. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  50. << "a::f::except" << std::endl
  51. #endif
  52. ;
  53. BOOST_TEST(out.eq(ok.str()));
  54. } catch(...) { BOOST_TEST(false); }
  55. return boost::report_errors();
  56. }