00001
00002
00003
00004
00005
00006
00007
00009
00010 #ifndef __wx_test_h__
00011 #define __wx_test_h__
00012
00013 #include <wx/wxprec.h>
00014 #ifndef WX_PRECOMP
00015 #include "wx/wx.h"
00016 #endif
00017
00018 #include <vector>
00019 #include <wx/exception.h>
00020
00021 namespace wx
00022 {
00023
00025
00026
00027 class FailureException : public Exception
00028 {
00029 public:
00030 FailureException(const wxString& sError, ...)
00031 {
00032 va_list argptr;
00033 #ifdef _MSC_VER
00034 WorkaroundVa_start(argptr, sError);
00035 #else
00036 va_start(argptr, sError);
00037 #endif // def _MSC_VER
00038 PrintfV(sError, argptr);
00039 va_end(argptr);
00040 }
00041 };
00042
00043 class TestHarness;
00044
00045 class Test
00046 {
00047 friend class TestHarness;
00048
00049 public:
00050 Test(wxString const& sNewTestName, int nNewTestCount) :
00051 m_sTestName(sNewTestName), m_nTestCount(nNewTestCount),
00052 m_nCurrentIndex(0)
00053 {
00054 }
00055 virtual ~Test()
00056 {}
00057
00058 protected:
00059 virtual bool Run() = 0;
00060
00061 wxString GetName()
00062 {
00063 return m_sTestName;
00064 }
00065
00066
00067 bool Ok()
00068 {
00069 return ((m_nCurrentIndex == m_nTestCount))? true : false;
00070 }
00071
00072 int GetTestCount()
00073 {
00074 return m_nTestCount;
00075 }
00076
00077 int GetCurrentIndex()
00078 {
00079 return m_nCurrentIndex;
00080 }
00081
00082 bool Pass(int nSubTestIndex)
00083 {
00084 m_nCurrentIndex++;
00085 if(m_nCurrentIndex != nSubTestIndex)
00086 {
00087 wxString sError;
00088 sError.Printf(_T("Test error, wrong sub-test number - %s.Pass(%u), expecting %u"), m_sTestName.c_str(), nSubTestIndex, m_nCurrentIndex);
00089 throw FailureException(sError);
00090 }
00091 if(m_nCurrentIndex > m_nTestCount)
00092 {
00093 wxString sError;
00094 sError.Printf(_T("Test error: too many tests run - expecting max %u, got %u"), m_sTestName.c_str(), m_nTestCount, nSubTestIndex);
00095 throw FailureException(sError);
00096 }
00097
00098 wxLogDebug(wxT("Passed Test %u"), nSubTestIndex);
00099
00100 return true;
00101 }
00102
00103 private:
00104 wxString m_sTestName;
00105 int m_nTestCount;
00106 int m_nCurrentIndex;
00107 };
00108
00109 class TestHarness : private std::vector<Test *>
00110 {
00111 public:
00112 ~TestHarness()
00113 {
00114 for(int i = 0; i < size(); i++)
00115 {
00116 delete at(i);
00117 }
00118 clear();
00119 }
00120
00121 public:
00122 void Add(Test * ptst)
00123 {
00124 push_back(ptst);
00125 }
00126 void Run()
00127 {
00128 for(int i = 0; i < size(); i++)
00129 {
00130 bool bTestedOk = RunTest(at(i));
00131 }
00132 }
00133
00134 private:
00135 bool RunTest(Test * ptst)
00136 {
00137 wxString sName = ptst->GetName();
00138 try
00139 {
00140 ptst->Run();
00141 }
00142 catch(const FailureException& fle)
00143 {
00144 int nFailIndex = ptst->GetCurrentIndex() + 1;
00145 wxLogDebug(_T("'%s' Failed in test %u: %s"), sName.c_str(), nFailIndex, fle.GetString().c_str());
00146
00147 return false;
00148 }
00149 catch(...)
00150 {
00151 int nFailIndex = ptst->GetCurrentIndex() + 1;
00152 wxLogDebug(_T("'%s' Unhandled error thrown in sub-test %u"), sName.c_str(), nFailIndex);
00153
00154 return false;
00155 }
00156
00157 int nTestCount = ptst->GetTestCount();
00158 int nPassed = ptst->GetCurrentIndex();
00159 if(ptst->Ok())
00160 {
00161 wxLogDebug(_T("'%s' %u out of %u tests passed successfully."), sName.c_str(), nPassed, nTestCount);
00162 return true;
00163 }
00164 else
00165 {
00166 wxLogDebug(_T("'%s' Not all the expected tests were run. Expected %u, got %u"), sName.c_str(), nTestCount, nPassed);
00167 return false;
00168 }
00169
00170 return true;
00171 }
00172 };
00173
00174 }
00175
00176 #endif // ndef __wx_test_h__