TestMacros.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef UNITTEST_TESTMACROS_H
  2. #define UNITTEST_TESTMACROS_H
  3. #include "Config.h"
  4. #include "ExecuteTest.h"
  5. #include "AssertException.h"
  6. #include "TestDetails.h"
  7. #include "MemoryOutStream.h"
  8. #ifndef UNITTEST_POSIX
  9. #define UNITTEST_THROW_SIGNALS
  10. #else
  11. #include "Posix/SignalTranslator.h"
  12. #endif
  13. #ifdef TEST
  14. #error UnitTest++ redefines TEST
  15. #endif
  16. #ifdef TEST_EX
  17. #error UnitTest++ redefines TEST_EX
  18. #endif
  19. #ifdef TEST_FIXTURE_EX
  20. #error UnitTest++ redefines TEST_FIXTURE_EX
  21. #endif
  22. #define SUITE(Name) \
  23. namespace Suite##Name { \
  24. namespace UnitTestSuite { \
  25. inline char const* GetSuiteName () { \
  26. return #Name ; \
  27. } \
  28. } \
  29. } \
  30. namespace Suite##Name
  31. #define TEST_EX(Name, List) \
  32. class Test##Name : public UnitTest::Test \
  33. { \
  34. public: \
  35. Test##Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {} \
  36. private: \
  37. virtual void RunImpl() const; \
  38. } test##Name##Instance; \
  39. \
  40. UnitTest::ListAdder adder##Name (List, &test##Name##Instance); \
  41. \
  42. void Test##Name::RunImpl() const
  43. #define TEST(Name) TEST_EX(Name, UnitTest::Test::GetTestList())
  44. #define TEST_FIXTURE_EX(Fixture, Name, List) \
  45. class Fixture##Name##Helper : public Fixture \
  46. { \
  47. public: \
  48. explicit Fixture##Name##Helper(UnitTest::TestDetails const& details) : m_details(details) {} \
  49. void RunImpl(); \
  50. UnitTest::TestDetails const& m_details; \
  51. private: \
  52. Fixture##Name##Helper(Fixture##Name##Helper const&); \
  53. Fixture##Name##Helper& operator =(Fixture##Name##Helper const&); \
  54. }; \
  55. \
  56. class Test##Fixture##Name : public UnitTest::Test \
  57. { \
  58. public: \
  59. Test##Fixture##Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {} \
  60. private: \
  61. virtual void RunImpl() const; \
  62. } test##Fixture##Name##Instance; \
  63. \
  64. UnitTest::ListAdder adder##Fixture##Name (List, &test##Fixture##Name##Instance); \
  65. \
  66. void Test##Fixture##Name::RunImpl() const \
  67. { \
  68. bool ctorOk = false; \
  69. try { \
  70. Fixture##Name##Helper fixtureHelper(m_details); \
  71. ctorOk = true; \
  72. UnitTest::ExecuteTest(fixtureHelper, m_details); \
  73. } \
  74. catch (UnitTest::AssertException const& e) \
  75. { \
  76. UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details.testName, m_details.suiteName, e.Filename(), e.LineNumber()), e.what()); \
  77. } \
  78. catch (std::exception const& e) \
  79. { \
  80. UnitTest::MemoryOutStream stream; \
  81. stream << "Unhandled exception: " << e.what(); \
  82. UnitTest::CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); \
  83. } \
  84. catch (...) { \
  85. if (ctorOk) \
  86. { \
  87. UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \
  88. "Unhandled exception while destroying fixture " #Fixture); \
  89. } \
  90. else \
  91. { \
  92. UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \
  93. "Unhandled exception while constructing fixture " #Fixture); \
  94. } \
  95. } \
  96. } \
  97. void Fixture##Name##Helper::RunImpl()
  98. #define TEST_FIXTURE(Fixture,Name) TEST_FIXTURE_EX(Fixture, Name, UnitTest::Test::GetTestList())
  99. #endif