Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cUnitTest.h
Go to the documentation of this file.
1 //
6 //
7 
8 #ifndef _INC_cUnitTest_H
9 #define _INC_cUnitTest_H
10 #ifndef NO_PRAGMA_ONCE
11 #pragma once
12 #endif
13 
14 #include "cUnitTestDecl.h"
15 #include "StrConst.h"
16 #include "StrArg.h"
17 #include "FileName.h"
18 #include "cLogLevel.h"
19 #include "cLogMgr.h"
20 #include "cDebugAssert.h"
21 #include "cSingleton.h"
22 #include "cArray.h"
23 #include "cFilePath.h"
24 #include "cAppState.h"
25 #include "cOSModImpl.h"
26 
27 namespace Gray
28 {
29  class cLogProcessor;
30 
32  {
43  };
44 
45  class cUnitTestRegister;
46 
47  struct GRAYCORE_LINK cUnitTestCur // static
48  {
53 
54  static int sm_nCreatedUnitTests;
55  static const FILECHAR_t* k_TestFiles; // a sub directory under m_sTestOutDir containing all the test files.
56 
57  // Sample Test const data.
58  static const StrLen_t k_TEXTBLOB_LEN = 566;
59  static const cStrConst k_sTextBlob;
60  static const StrLen_t k_TEXTLINES_QTY = 18;
61  static const cStrConst k_asTextLines[k_TEXTLINES_QTY + 1];
62 
63  static bool GRAYCALL TestTypes();
64  };
65 
67  {
73 
74  public:
75  cUnitTest();
76  virtual ~cUnitTest() noexcept(false); // M$ test force use of noexcept(false);
77 
78  const FILECHAR_t* get_TestInpDir() const;
79 
81  virtual void RunUnitTest() = 0;
82  };
83 
85  {
90  public:
93  protected:
94  cUnitTestRegister(const LOGCHAR_t* pszTestName, UNITTEST_LEVEL_TYPE nTestLevel = UNITTEST_LEVEL_Core);
95  virtual ~cUnitTestRegister();
96  virtual cUnitTest* CreateUnitTest() = 0;
97  public:
98  void RunUnitTest();
99  };
100 
101  template <class T = cUnitTest>
102  class cUnitTestRegisterT : public cUnitTestRegister, public cSingletonStatic< cUnitTestRegisterT<T> >
103  {
108  public:
110  : cUnitTestRegister(pszTestName, nTestLevel)
112  {
113  }
114  virtual cUnitTest* CreateUnitTest() override
115  {
118  return new T();
119  }
120  };
121 
123  {
128  private:
129  cAppState& m_AppState;
130  APPSTATE_TYPE_ m_eAppStatePrev;
131  THREADID_t m_nMainThreadPrev;
132 
133  public:
135  : m_AppState(cAppState::I())
136  {
137  // called for M$ tests.
138  m_eAppStatePrev = m_AppState.get_AppState();
139  m_nMainThreadPrev = m_AppState.get_MainThreadId();
140  m_AppState.InitAppState(); // set to APPSTATE_Run
141  }
142 
144  {
145  m_AppState.put_AppState(m_eAppStatePrev); // destructors should be called next.
146  m_AppState.m_nMainThreadId = m_nMainThreadPrev;
147  }
148  };
149 
150  class GRAYCORE_LINK cUnitTests : public cSingleton<cUnitTests>, public cUnitTestCur
151  {
155 
156  friend class cUnitTestRegister;
157 
158  public:
160  static AssertCallback_t UnitTest_AssertCallback;
161  AssertCallback_t* m_pAssertOrig;
162 
164  cArrayString<LOGCHAR_t> m_aTestNames; // just run these tests.
165 
168 
170 
171  bool m_bRunning;
173 
174  public:
175  cUnitTests();
176 
177  HRESULT InitTestOutDir();
178  bool RegisterUnitTest(cUnitTestRegister* pTest);
179 
180  void SetTestLevel(UNITTEST_LEVEL_TYPE nTestLevel);
181  bool TestActive(const cUnitTestRegister* pUnitTest, bool remove);
182 
183  cUnitTestRegister* FindUnitTest(const char* pszName) const;
184 
185  bool IsTestInteractive() const noexcept;
186  bool TestInteractivePrompt(const char* pszMsg) noexcept;
187 
188  const FILECHAR_t* get_TestInpDir() const;
189  const FILECHAR_t* get_TestOutDir() const;
190 
191  void RunInitialize();
192  void RunCleanup();
193 
195  HRESULT RunUnitTests(UNITTEST_LEVEL_TYPE nTestLevel = UNITTEST_LEVEL_Common, const LOGCHAR_t* pszTestNameMatch = nullptr);
196 
197  CHEAPOBJECT_IMPL; // dynamic singleton
198  };
199 
200 #define UNITTEST_TRUE(x) ASSERT(x) // UNITTEST_TRUE is different from a normal ASSERT ?
201 #define UNITTEST_TRUE2(x,d) ASSERT(x) // UNITTEST_TRUE with a description
202 
203  // Deprecate all below in favor of UNITTEST2_*
204 
205 #define UNITTEST_REGISTER_NAME(n) g_UnitTest_##n
206 #define UNITTEST_REGISTER(n,l) ::Gray::cUnitTestRegisterT< UNITTEST_N(n) > UNITTEST_REGISTER_NAME(n)( #n, l ); // instantiate to register cUnitTest .
207 
208  // Allow an external hard link. optional.
209 #define UNITTEST_EXT_NAME(n) g_pUnitTest_##n
210 #define UNITTEST_EXT_DEF(n) cUnitTestRegister* UNITTEST_EXT_NAME(n) = &UNITTEST_REGISTER_NAME(n);
211 
212 } // namespace
213 
214 using namespace Gray; // Since this header is typically only included right before the unit test.
215 
216 #endif // _INC_cUnitTest_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define CHEAPOBJECT_IMPL
Definition: cHeapObject.h:32
Definition: cAppState.h:82
THREADID_t m_nMainThreadId
The thread we started with. main().
Definition: cAppState.h:104
APPSTATE_TYPE_ get_AppState() const noexcept
Definition: cAppState.h:142
void put_AppState(APPSTATE_TYPE_ eAppState) noexcept
Definition: cAppState.h:148
void InitAppState() noexcept
Definition: cAppState.cpp:273
THREADID_t get_MainThreadId() const noexcept
Definition: cAppState.h:156
Definition: cArray.h:864
Definition: cLogAppender.h:168
Definition: cSingleton.h:30
Definition: cSingleton.h:127
Definition: StrConst.h:39
Definition: cUnitTest.h:123
cUnitTestAppState()
Definition: cUnitTest.h:134
~cUnitTestAppState() noexcept
Definition: cUnitTest.h:143
Definition: cUnitTest.h:103
cUnitTestRegisterT(const LOGCHAR_t *pszTestName, UNITTEST_LEVEL_TYPE nTestLevel=UNITTEST_LEVEL_Core)
Definition: cUnitTest.h:109
virtual cUnitTest * CreateUnitTest() override
must implement this.
Definition: cUnitTest.h:114
Definition: cUnitTest.h:85
const LOGCHAR_t * m_pszTestName
Display Name for the unit test.
Definition: cUnitTest.h:91
virtual cUnitTest * CreateUnitTest()=0
must implement this.
const UNITTEST_LEVEL_TYPE m_nTestLevel
at what level does this test run?
Definition: cUnitTest.h:92
Definition: cUnitTest.h:67
virtual void RunUnitTest()=0
Run the test.
Definition: cUnitTest.h:151
cStringF m_sTestInpDir
root for source of test input files. might change based on cOSModImpl?
Definition: cUnitTest.h:166
AssertCallback_t * m_pAssertOrig
Definition: cUnitTest.h:161
int m_iFailures
Count total unit test failures.
Definition: cUnitTest.h:172
static AssertCallback_t UnitTest_AssertCallback
redirect assert here for test failure. requires _DEBUG or _DEBUG_FAST.
Definition: cUnitTest.h:160
cArrayPtr< cUnitTestRegister > m_aUnitTests
list of all registered unit tests. Register as they get instantiate by C runtime static loader.
Definition: cUnitTest.h:159
cArrayString< LOGCHAR_t > m_aTestNames
Definition: cUnitTest.h:164
UNITTEST_LEVEL_TYPE m_nTestLevel
restore the original assert.
Definition: cUnitTest.h:163
cLogProcessor * m_pLog
cLogMgr::I() for output of tests. Why not just use DEBUG_MSG ??
Definition: cUnitTest.h:169
cStringF m_sTestOutDir
global config for input files.
Definition: cUnitTest.h:167
bool m_bRunning
We are actively running in the Gray test framework. Not in M$ framework.
Definition: cUnitTest.h:171
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
virtual void RunUnitTest() override
Definition: cFileTextReader.Tests.cpp:14
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
APPSTATE_TYPE_
Definition: cAppState.h:28
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
public cUnitTest
Definition: cAppConsole.Tests.cpp:11
char LOGCHAR_t
always just use UTF8 for logs, don't bother with UNICODE.
Definition: cLogLevel.h:17
UNITTEST_LEVEL_TYPE
Definition: cUnitTest.h:32
@ UNITTEST_LEVEL_Slow
5=slow tests
Definition: cUnitTest.h:40
@ UNITTEST_LEVEL_All
6=interactive tests, need special external rigs, db, etc
Definition: cUnitTest.h:41
@ UNITTEST_LEVEL_None
Definition: cUnitTest.h:35
@ UNITTEST_LEVEL_Common
4 = Common or application level tests.
Definition: cUnitTest.h:39
@ UNITTEST_LEVEL_Crit
1=critical tests. usually stuff i want to debug now.
Definition: cUnitTest.h:36
@ UNITTEST_LEVEL_Core
2=only the most basic tests.
Definition: cUnitTest.h:37
@ UNITTEST_LEVEL_Off
These test don't work yet. or is broken.
Definition: cUnitTest.h:42
@ UNITTEST_LEVEL_Lib
3
Definition: cUnitTest.h:38
Definition: cUnitTest.h:48
static const FILECHAR_t * k_TestFiles
Definition: cUnitTest.h:55
static const cStrConst k_sTextBlob
a single allocated k_TEXTBLOB_LEN+1
Definition: cUnitTest.h:59
static int sm_nCreatedUnitTests
Count the cUnitTest objects I have created. NOT just m_aUnitTests.
Definition: cUnitTest.h:54