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