PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Tests.h

http://crashrpt.googlecode.com/
C++ Header | 174 lines | 103 code | 46 blank | 25 comment | 4 complexity | aea2e034fe17fa87426e5af9b426e7cf MD5 | raw file
  1/************************************************************************************* 
  2This file is a part of CrashRpt library.
  3Copyright (c) 2003-2013 The CrashRpt project authors. All Rights Reserved.
  4
  5Use of this source code is governed by a BSD-style license
  6that can be found in the License.txt file in the root of the source
  7tree. All contributing project authors may
  8be found in the Authors.txt file in the root of the source tree.
  9***************************************************************************************/
 10
 11#pragma once
 12#include "stdafx.h"
 13
 14extern BOOL g_bRunningFromUNICODEFolder; // Are we running from a UNICODE-named folder?
 15
 16// What action to perform
 17enum eAction
 18{
 19    GET_NAMES, // Return test names
 20    RUN_TESTS  // Run tests
 21};
 22
 23// Test suite class
 24class CTestSuite
 25{
 26public:
 27
 28    // Constructor
 29    CTestSuite(CTestSuite* pParentSuite=NULL);
 30
 31    // Allocates resources used by tests in this suite
 32    virtual void SetUp() = 0;  
 33
 34    // Frees resources used by tests in this suite
 35    virtual void TearDown() = 0;
 36
 37    // Returns suite name and description
 38    virtual void GetSuiteInfo(std::string& sName, std::string& sDescription) = 0;
 39
 40    // Returns the list of tests in this suite or runs tests
 41    virtual void DoWithMyTests(eAction action, std::vector<std::string>& test_list) = 0;
 42
 43    // Runs all or some tests from this test suite
 44    bool Run(std::set<std::string>& SuitesToRun);
 45
 46    // Returns test list in this test suite 
 47    virtual std::vector<std::string> GetTestList(std::set<std::string>& SuitesToRun, bool bIncludeChildren = false);
 48
 49    // Returns parent test suite
 50    CTestSuite* GetParentSuite();  
 51
 52    // Sets parent suite
 53    void SetParentSuite(CTestSuite* pParent);
 54
 55    // Returns count of child test suites
 56    UINT GetChildSuiteCount();  
 57
 58    // Returns i-th child test suite
 59    CTestSuite* GetChildSuite(UINT i);  
 60
 61    // Adds a child test suite
 62    void AddChildSuite(CTestSuite* pChildSuite);
 63
 64    // Returns the list of errors
 65    std::vector<std::string> GetErrorList(bool bIncludeChildren = false);
 66
 67    void ClearErrors();
 68
 69    // Adds an error message to the list.
 70    void AddErrorMsg(const char* szFunction, const char* szAssertion, const char* szMsg, ...);
 71
 72protected: 
 73
 74    bool BeforeTest(const char* szFunction);
 75    void AfterTest(const char* szFunction);
 76
 77private:
 78
 79    CTestSuite* m_pParentSuite;   // Parent test suite
 80    std::vector<CTestSuite*> m_apChildSuites;     // The list of child test suites
 81    std::vector<std::string> m_asErrorMsg; // The list of error messages
 82    bool m_bSuiteSetUpFailed;
 83    bool m_bTestFailed;
 84};
 85
 86#define BEGIN_TEST_MAP( TestSuite , Description)\
 87    virtual void GetSuiteInfo(std::string& sName, std::string& sDescription)\
 88{\
 89    sName = std::string( #TestSuite );\
 90    sDescription = std::string( Description );\
 91}\
 92    virtual void DoWithMyTests(eAction action, std::vector<std::string>& test_list)\
 93{
 94
 95#define REGISTER_TEST( Test )\
 96    if(action==GET_NAMES)\
 97    test_list.push_back( #Test );\
 98else\
 99{\
100    if(BeforeTest( #Test ))\
101    Test();\
102    AfterTest( #Test);\
103}
104
105#define END_TEST_MAP() }
106
107class TopLevelTestSuite : public CTestSuite
108{
109public:
110
111    BEGIN_TEST_MAP( TopLevelTestSuite, "All tests")
112        UNREFERENCED_PARAMETER(test_list);
113        UNREFERENCED_PARAMETER(action);
114    END_TEST_MAP()
115
116        TopLevelTestSuite()
117        :CTestSuite(NULL)
118    {
119    }
120
121    virtual void SetUp()
122    {
123    }
124
125    virtual void TearDown()
126    {
127    }
128
129};
130
131class CTestRegistry
132{
133public: 
134
135    static CTestRegistry* GetRegistry();
136
137    CTestRegistry();
138
139    CTestSuite* GetTopSuite();
140
141private:
142
143    CTestSuite* m_pTopSuite; // The top-level test suite.  
144};
145
146extern CTestSuite* g_pCurTestSuite;
147
148#define TEST_ASSERT(expr)\
149    if(!(expr)) { g_pCurTestSuite->AddErrorMsg(__FUNCTION__, #expr, NULL); \
150    goto test_cleanup; }
151
152#define TEST_ASSERT_MSG(expr, ...)\
153	if(!(expr)) { g_pCurTestSuite->AddErrorMsg((__FUNCTION__), (#expr), __VA_ARGS__); \
154    goto test_cleanup; }
155
156#define __TEST_CLEANUP__ test_cleanup:
157
158template <class T>
159class CTestSuiteRegistrator
160{
161public:
162
163    CTestSuiteRegistrator()
164    {		
165        CTestRegistry* pRegistry = CTestRegistry::GetRegistry();  
166        CTestSuite* pTopSuite = pRegistry->GetTopSuite();
167        CTestSuite* pSuite = new T();  
168        pSuite->SetParentSuite(pTopSuite);
169        pTopSuite->AddChildSuite(pSuite);
170    }
171};
172
173#define REGISTER_TEST_SUITE( Suite ) CTestSuiteRegistrator<Suite> __reg_##Suite;
174