Allow providing descriptions for dialog tests expectations.

This makes test failure errors much more intelligible, e.g. in the following
case

	... some code ...
	wxTEST_DIALOG(wxYield, wxExpectModal<wxMessageDialog>(wxID_OK));
	... some more code ...
	wxTEST_DIALOG(wxYield, wxExpectModal<wxMessageDialog>(wxID_OK));

it was previously impossible to distinguish the first test failure from the
second one from just the failure description (it could be done by looking at
the line numbers since the recent change however), but with

	...
	wxTEST_DIALOG(wxYield, wxExpectModal<wxMessageDialog>(wxID_OK).
		Describe("first warning message box"));
	...
	wxTEST_DIALOG(wxYield, wxExpectModal<wxMessageDialog>(wxID_OK).
		Describe("successful completion message box"));

the failure becomes immediately clear.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@78412 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2015-01-24 22:08:51 +00:00
parent 95177f3876
commit 9099ae2ded

View File

@ -44,13 +44,23 @@ public:
wxModalExpectation() : m_isOptional(false) {} wxModalExpectation() : m_isOptional(false) {}
virtual ~wxModalExpectation() {} virtual ~wxModalExpectation() {}
wxString GetDescription() const
{
return m_description.empty() ? GetDefaultDescription() : m_description;
}
bool IsOptional() const { return m_isOptional; } bool IsOptional() const { return m_isOptional; }
virtual int Invoke(wxDialog *dlg) const = 0; virtual int Invoke(wxDialog *dlg) const = 0;
virtual wxString GetDescription() const = 0;
protected: protected:
// Override to return the default description of the expected dialog used
// if no specific description for this particular expectation is given.
virtual wxString GetDefaultDescription() const = 0;
// User-provided description of the dialog, may be empty.
wxString m_description;
// Is this dialog optional, i.e. not required to be shown? // Is this dialog optional, i.e. not required to be shown?
bool m_isOptional; bool m_isOptional;
}; };
@ -67,14 +77,14 @@ template<class T> class wxExpectModal {};
Every such specialization must be derived from wxExpectModalBase; there's Every such specialization must be derived from wxExpectModalBase; there's
no other use for this class than to serve as wxExpectModal<T>'s base class. no other use for this class than to serve as wxExpectModal<T>'s base class.
T must be a class derived from wxDialog. T must be a class derived from wxDialog and E is the derived class type.
*/ */
template<class T> template<class T, class E = wxExpectModal<T> >
class wxExpectModalBase : public wxModalExpectation class wxExpectModalBase : public wxModalExpectation
{ {
public: public:
typedef T DialogType; typedef T DialogType;
typedef wxExpectModal<DialogType> ExpectationType; typedef E ExpectationType;
/** /**
Returns a copy of the expectation where the expected dialog is marked Returns a copy of the expectation where the expected dialog is marked
@ -90,6 +100,20 @@ public:
return e; return e;
} }
/**
Sets a description shown in the error message if the expectation fails.
Using this method with unique descriptions for the different dialogs is
recommended to make it easier to find out which one of the expected
dialogs exactly was not shown.
*/
ExpectationType Describe(const wxString& description) const
{
ExpectationType e(*static_cast<const ExpectationType*>(this));
e.m_description = description;
return e;
}
protected: protected:
virtual int Invoke(wxDialog *dlg) const virtual int Invoke(wxDialog *dlg) const
{ {
@ -101,7 +125,7 @@ protected:
} }
/// Returns description of the expected dialog (by default, its class). /// Returns description of the expected dialog (by default, its class).
virtual wxString GetDescription() const virtual wxString GetDefaultDescription() const
{ {
return wxCLASSINFO(T)->GetClassName(); return wxCLASSINFO(T)->GetClassName();
} }
@ -118,7 +142,8 @@ protected:
// wxExpectModal<T> specializations for common dialogs: // wxExpectModal<T> specializations for common dialogs:
template<class T> template<class T>
class wxExpectDismissableModal : public wxExpectModalBase<T> class wxExpectDismissableModal
: public wxExpectModalBase<T, wxExpectDismissableModal<T> >
{ {
public: public:
explicit wxExpectDismissableModal(int id) explicit wxExpectDismissableModal(int id)