added tests for throwing from .old()

This commit is contained in:
Lorenzo Caminiti
2015-12-15 11:51:44 -08:00
parent 07248b63ea
commit ec79700b26
11 changed files with 538 additions and 141 deletions

View File

@@ -1,5 +1,5 @@
// Test constructor body throwing (in middle branch of inheritance tree).
// Test throw form constructor body (in middle branch of inheritance tree).
#include "../aux_/oteststream.hpp"
#include <boost/contract/constructor.hpp>
@@ -44,7 +44,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 {};
b() :
boost::contract::constructor_precondition<b>([] {
@@ -56,7 +56,7 @@ struct b
.postcondition([&] { out << "b::ctor::post" << std::endl; })
;
out << "b::ctor::body" << std::endl;
throw b::e(); // Test body throw (from inheritance mid branch).
throw b::err(); // Test body throws (from inheritance mid branch).
}
};
@@ -87,31 +87,30 @@ struct a
int main() {
std::ostringstream ok;
bool threw = false;
out.str("");
try { a aa; }
catch(b::e const&) { threw = true; }
BOOST_TEST(threw);
ok.str(""); ok
<< "a::ctor::pre" << std::endl
<< "b::ctor::pre" << std::endl
<< "c::ctor::pre" << std::endl
<< "c::static_inv" << std::endl
<< "c::ctor::old" << std::endl
<< "c::ctor::body" << std::endl
<< "c::static_inv" << std::endl
<< "c::inv" << std::endl
<< "c::ctor::post" << std::endl
<< "b::static_inv" << std::endl
<< "b::ctor::old" << std::endl
<< "b::ctor::body" << std::endl
// Test b body threw so only static inv exit checked and then C++
// construction mechanism quits.
<< "b::static_inv" << std::endl
;
BOOST_TEST(out.eq(ok.str()));
try {
out.str("");
a aa;
BOOST_TEST(false);
} catch(b::err const&) {
ok.str(""); ok
<< "a::ctor::pre" << std::endl
<< "b::ctor::pre" << std::endl
<< "c::ctor::pre" << std::endl
<< "c::static_inv" << std::endl
<< "c::ctor::old" << std::endl
<< "c::ctor::body" << std::endl
<< "c::static_inv" << std::endl
<< "c::inv" << std::endl
<< "c::ctor::post" << std::endl
<< "b::static_inv" << std::endl
<< "b::ctor::old" << std::endl
<< "b::ctor::body" << std::endl // Test this threw...
<< "b::static_inv" << std::endl // ... so check only this after.
;
BOOST_TEST(out.eq(ok.str()));
} catch(...) { BOOST_TEST(false); }
return boost::report_errors();
}