Working with updating VS2010 to 2012 is here, and there are a couple of unittests that don't work due to poor code generation or programming errors, but I'm not sure what. The code that I publish is almost equal to the original, and also reproduces the problems.
Here is the case; all classes / implementations are in separate files.
class Base { public: virtual ~Base(){} void DoIt(){ DoItImpl(); } protected: virtual void DoItImpl() = 0; }; class Base2 : public Base { public: virtual void DoStuff() = 0; }; class Impl : public Base2 { public: Impl(); void DoStuff(); protected: void DoItImpl(); }; void Impl::DoStuff() { throw std::runtime_error( "oops" ); } void Impl::DoItImpl() { throw std::runtime_error( "oops" ); }
The above is in the dll and is checked with unittest ++ in exe (I drew the CHECK_THROW macro for clarity, but don't change anything):
SUITE( Base ) { TEST( Impl ) { Impl c; bool caught = false; try { c.DoIt(); } catch( const std::exception& ) { caught = true; } //this point is never reached, instead control goes to the //try/catch in unittest++ ExecuteTest function CHECK( caught ); } }
Is this a mistake or not, is there a workaround that I can use immediately, or some general rules on how to avoid this?
edit Added implementation of DoStuff() , if I call it instead of DoIt() , no problem!
edit . This should exclude the possibility of creating a unittest framework or any other code that is a problem, or catching a link with const, or the compiler, not knowing that runtime_error stems from the exception; I extended unittest macros to show what they actually do, and created a new project containing only this source file:
namespace SuiteImpl { class TestImpl { public: TestImpl() {} virtual void RunImpl() const; }; void TestImpl::RunImpl() const { xxx::Impl c; try { c.DoIt(); } catch( std::exception& ) { std::cout << "good" << std::endl; } } } int main() { SuiteImpl::TestImpl impl; try { impl.RunImpl(); } catch( const std::exception& ) { std::cout << "not good" << std::endl; } }
The output is not good .