crashrpt
Tests.h
1 /*************************************************************************************
2 This file is a part of CrashRpt library.
3 Copyright (c) 2003-2013 The CrashRpt project authors. All Rights Reserved.
4 
5 Use of this source code is governed by a BSD-style license
6 that can be found in the License.txt file in the root of the source
7 tree. All contributing project authors may
8 be found in the Authors.txt file in the root of the source tree.
9 ***************************************************************************************/
10 
11 #pragma once
12 #include "stdafx.h"
13 
14 extern BOOL g_bRunningFromUNICODEFolder; // Are we running from a UNICODE-named folder?
15 
16 // What action to perform
17 enum eAction
18 {
19  GET_NAMES, // Return test names
20  RUN_TESTS // Run tests
21 };
22 
23 // Test suite class
25 {
26 public:
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 
72 protected:
73 
74  bool BeforeTest(const char* szFunction);
75  void AfterTest(const char* szFunction);
76 
77 private:
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 );\
98 else\
99 {\
100  if(BeforeTest( #Test ))\
101  Test();\
102  AfterTest( #Test);\
103 }
104 
105 #define END_TEST_MAP() }
106 
108 {
109 public:
110 
111  BEGIN_TEST_MAP( TopLevelTestSuite, "All tests")
112  UNREFERENCED_PARAMETER(test_list);
113  UNREFERENCED_PARAMETER(action);
114  END_TEST_MAP()
115 
117  :CTestSuite(NULL)
118  {
119  }
120 
121  virtual void SetUp()
122  {
123  }
124 
125  virtual void TearDown()
126  {
127  }
128 
129 };
130 
132 {
133 public:
134 
135  static CTestRegistry* GetRegistry();
136 
137  CTestRegistry();
138 
139  CTestSuite* GetTopSuite();
140 
141 private:
142 
143  CTestSuite* m_pTopSuite; // The top-level test suite.
144 };
145 
146 extern 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 
158 template <class T>
160 {
161 public:
162 
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 
Definition: Tests.h:131
Definition: Tests.h:159
Definition: Tests.h:107
Definition: Tests.h:24