>From d1e433cde919b4eedbc18c38cef1d5ad568242a4 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 19 Dec 2014 15:27:08 +0100 Subject: [PATCH 2/2] Don't complain about not closing test documents in case of test failure. If the new document test object is being destroyed because of stack unwinding, don't complain about forgetting to call its close() or close_discard_changes() method as they could well be present in the code and not called just because of the test failure and instead just close the document implicitly. This is one of the rare situations where using std::uncaught_exception() is actually the right thing to do. --- wx_test_new.hpp | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/wx_test_new.hpp b/wx_test_new.hpp index 1d66c98..18f550a 100644 --- a/wx_test_new.hpp +++ b/wx_test_new.hpp @@ -33,6 +33,8 @@ #include #include +#include + /// Helper base class for classes testing creation of specific new documents. /// /// This class provides methods for closing the current document, optionally @@ -55,15 +57,32 @@ class wx_test_new_document_base ~wx_test_new_document_base() { - // As we don't want to throw an exception from the dtor, all we can do - // is to complain to the user directly. + // Normally either close() or close_discard_changes() should be called, + // so complain about forgetting to do this if neither was. Except that + // we shouldn't do this if we're unwinding due to an exception from a + // test failure, as this is not a bug in the test code then. if(opened_) { - wxSafeShowMessage - ("Programming error" - ,"A document created during unattended test hasn't been closed, " - "please report this." - ); + if(std::uncaught_exception()) + { + // Moreover, in case of exception, try to close the window to + // avoid showing message boxes asking the user if it should be + // saved: this is undesirable in an unattended test. + do_close(); + + wxTEST_DIALOG + (wxYield() + ,wxExpectModal(wxNO).Optional() + ); + } + else + { + wxSafeShowMessage + ("Programming error" + ,"A document created during unattended test hasn't been closed, " + "please report this." + ); + } } } -- 2.1.0