/tests/Tests.h

http://crashrpt.googlecode.com/ · C Header · 174 lines · 103 code · 46 blank · 25 comment · 4 complexity · aea2e034fe17fa87426e5af9b426e7cf MD5 · raw file

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