mirror of
https://github.com/boostorg/contract.git
synced 2026-02-26 16:42:19 +00:00
added tests for throwing from .old()
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
|
||||
// Test constructor body throwing (in inheritance tree).
|
||||
// Test from public function (derived) body.
|
||||
|
||||
#include "../aux_/oteststream.hpp"
|
||||
#include <boost/contract/public_function.hpp>
|
||||
@@ -16,7 +16,7 @@ struct c {
|
||||
static void static_invariant() { out << "c::static_inv" << std::endl; }
|
||||
void invariant() const { out << "c::inv" << std::endl; }
|
||||
|
||||
struct e {};
|
||||
struct err {};
|
||||
|
||||
virtual void f(boost::contract::virtual_* v = 0) {
|
||||
boost::contract::guard c = boost::contract::public_function(v, this)
|
||||
@@ -28,7 +28,7 @@ struct c {
|
||||
.postcondition([&] { out << "c::f::post" << std::endl; })
|
||||
;
|
||||
out << "c::f::body" << std::endl;
|
||||
throw c::e();
|
||||
throw c::err(); // Test body throws.
|
||||
}
|
||||
};
|
||||
|
||||
@@ -42,7 +42,7 @@ struct b
|
||||
static void static_invariant() { out << "b::static_inv" << std::endl; }
|
||||
void invariant() const { out << "b::inv" << std::endl; }
|
||||
|
||||
struct e {};
|
||||
struct err {};
|
||||
|
||||
virtual void f(boost::contract::virtual_* v = 0) /* override */ {
|
||||
boost::contract::guard c = boost::contract::public_function<override_f>(
|
||||
@@ -55,7 +55,7 @@ struct b
|
||||
.postcondition([&] { out << "b::f::post" << std::endl; })
|
||||
;
|
||||
out << "b::f::body" << std::endl;
|
||||
throw b::e();
|
||||
throw b::err(); // Test body throws.
|
||||
}
|
||||
BOOST_CONTRACT_OVERRIDE(f)
|
||||
};
|
||||
@@ -70,7 +70,7 @@ struct a
|
||||
static void static_invariant() { out << "a::static_inv" << std::endl; }
|
||||
void invariant() const { out << "a::inv" << std::endl; }
|
||||
|
||||
struct e {};
|
||||
struct err {};
|
||||
|
||||
void f(boost::contract::virtual_* v = 0) /* override */ {
|
||||
boost::contract::guard c = boost::contract::public_function<override_f>(
|
||||
@@ -80,7 +80,7 @@ struct a
|
||||
.postcondition([&] { out << "a::f::post" << std::endl; })
|
||||
;
|
||||
out << "a::f::body" << std::endl;
|
||||
throw a::e();
|
||||
throw a::err(); // Test body throws.
|
||||
}
|
||||
BOOST_CONTRACT_OVERRIDE(f)
|
||||
};
|
||||
@@ -89,40 +89,40 @@ int main() {
|
||||
std::ostringstream ok;
|
||||
|
||||
a aa;
|
||||
c& ca = aa; // Test as virtual call via polymorphism.
|
||||
bool threw = false;
|
||||
out.str("");
|
||||
try { ca.f(); }
|
||||
catch(a::e const&) { threw = true; }
|
||||
BOOST_TEST(threw);
|
||||
ok.str(""); ok
|
||||
<< "c::static_inv" << std::endl
|
||||
<< "c::inv" << std::endl
|
||||
<< "b::static_inv" << std::endl
|
||||
<< "b::inv" << std::endl
|
||||
<< "a::static_inv" << std::endl
|
||||
<< "a::inv" << std::endl
|
||||
b& ba = aa; // Test as virtual call via polymorphism.
|
||||
try {
|
||||
out.str("");
|
||||
ba.f();
|
||||
BOOST_TEST(false);
|
||||
} catch(a::err const&) {
|
||||
ok.str(""); ok
|
||||
<< "c::static_inv" << std::endl
|
||||
<< "c::inv" << std::endl
|
||||
<< "b::static_inv" << std::endl
|
||||
<< "b::inv" << std::endl
|
||||
<< "a::static_inv" << std::endl
|
||||
<< "a::inv" << std::endl
|
||||
|
||||
<< "c::f::pre" << std::endl
|
||||
<< "b::f::pre" << std::endl
|
||||
<< "a::f::pre" << std::endl
|
||||
|
||||
<< "c::f::old" << std::endl
|
||||
<< "b::f::old" << std::endl
|
||||
<< "a::f::old" << std::endl
|
||||
<< "c::f::pre" << std::endl
|
||||
<< "b::f::pre" << std::endl
|
||||
<< "a::f::pre" << std::endl
|
||||
|
||||
<< "c::f::old" << std::endl
|
||||
<< "b::f::old" << std::endl
|
||||
<< "a::f::old" << std::endl
|
||||
|
||||
<< "a::f::body" << std::endl
|
||||
|
||||
<< "c::static_inv" << std::endl
|
||||
<< "c::inv" << std::endl
|
||||
<< "b::static_inv" << std::endl
|
||||
<< "b::inv" << std::endl
|
||||
<< "a::static_inv" << std::endl
|
||||
<< "a::inv" << std::endl
|
||||
|
||||
// Test no post (but still subcontracted inv) because body threw.
|
||||
;
|
||||
BOOST_TEST(out.eq(ok.str()));
|
||||
<< "a::f::body" << std::endl // Test this threw.
|
||||
|
||||
// Test no post (but still subcontracted inv) because body threw.
|
||||
<< "c::static_inv" << std::endl
|
||||
<< "c::inv" << std::endl
|
||||
<< "b::static_inv" << std::endl
|
||||
<< "b::inv" << std::endl
|
||||
<< "a::static_inv" << std::endl
|
||||
<< "a::inv" << std::endl
|
||||
;
|
||||
BOOST_TEST(out.eq(ok.str()));
|
||||
} catch(...) { BOOST_TEST(false); }
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user