- Main Page
- Related Pages
- Classes
- Files

- File List
- File Members

# TestMacros.h

    00001 #ifndef UNITTEST_TESTMACROS_H
    00002 #define UNITTEST_TESTMACROS_H
    00003
    00004 #include "Config.h"
    00005 #include "ExecuteTest.h"
    00006 #include "AssertException.h"
    00007 #include "TestDetails.h"
    00008 #include "MemoryOutStream.h"
    00009
    00010 #ifndef UNITTEST_POSIX
    00011         #define UNITTEST_THROW_SIGNALS
    00012 #else
    00013         #include "Posix/SignalTranslator.h"
    00014 #endif
    00015
    00016 #ifdef TEST
    00017     #error UnitTest++ redefines TEST
    00018 #endif
    00019
    00020 #ifdef TEST_EX
    00021         #error UnitTest++ redefines TEST_EX
    00022 #endif
    00023
    00024 #ifdef TEST_FIXTURE_EX
    00025         #error UnitTest++ redefines TEST_FIXTURE_EX
    00026 #endif
    00027
    00028 #define SUITE(Name)                                                         \
    00029         namespace Suite##Name {                                                        \
    00030         namespace UnitTestSuite {                                           \
    00031             inline char const* GetSuiteName () {                            \
    00032                 return #Name ;                                              \
    00033             }                                                               \
    00034         }                                                                   \
    00035     }                                                                       \
    00036         namespace Suite##Name
    00037
    00038 #define TEST_EX(Name, List)                                                \
    00039     class Test##Name : public UnitTest::Test                               \
    00040     {                                                                      \
    00041     public:                                                                \
    00042                 Test##Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {}  \
    00043     private:                                                               \
    00044         virtual void RunImpl() const;   \
    00045     } test##Name##Instance;                                                \
    00046                                                                                                                                                    \
    00047     UnitTest::ListAdder adder##Name (List, &test##Name##Instance);         \
    00048                                                                                                                                                    \
    00049     void Test##Name::RunImpl() const
    00050
    00051
    00052 #define TEST(Name) TEST_EX(Name, UnitTest::Test::GetTestList())
    00053
    00054
    00055 #define TEST_FIXTURE_EX(Fixture, Name, List)                                         \
    00056     class Fixture##Name##Helper : public Fixture                                                                         \
    00057         {                                                                                                                                                                \
    00058         public:                                                                                                                                                  \
    00059         explicit Fixture##Name##Helper(UnitTest::TestDetails const& details) : m_details(details) {} \
    00060         void RunImpl();                           \
    00061         UnitTest::TestDetails const& m_details;                                      \
    00062     private:                                                                         \
    00063         Fixture##Name##Helper(Fixture##Name##Helper const&);                         \
    00064         Fixture##Name##Helper& operator =(Fixture##Name##Helper const&);             \
    00065     };                                                                               \
    00066                                                                                                                                                                          \
    00067     class Test##Fixture##Name : public UnitTest::Test                                \
    00068     {                                                                                \
    00069     public:                                                                          \
    00070             Test##Fixture##Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {} \
    00071     private:                                                                         \
    00072         virtual void RunImpl() const;             \
    00073     } test##Fixture##Name##Instance;                                                 \
    00074                                                                                                                                                                          \
    00075     UnitTest::ListAdder adder##Fixture##Name (List, &test##Fixture##Name##Instance); \
    00076                                                                                                                                                                          \
    00077     void Test##Fixture##Name::RunImpl() const    \
    00078         {                                                                                                                                                                \
    00079                 bool ctorOk = false;                                                                                                             \
    00080                 try {                                                                                                                                            \
    00081                         Fixture##Name##Helper fixtureHelper(m_details);                                                  \
    00082                         ctorOk = true;                                                                                                                   \
    00083                         UnitTest::ExecuteTest(fixtureHelper, m_details);                                         \
    00084                 }                                                                                                                                                        \
    00085                 catch (UnitTest::AssertException const& e)                                                                                       \
    00086                 {                                                                                                                                                        \
    00087                         UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details.testName, m_details.suiteName, e.Filename(), e.LineNumber()), e.what()); \
    00088                 }                                                                                                                                                        \
    00089                 catch (std::exception const& e)                                                                                          \
    00090                 {                                                                                                                                                        \
    00091                         UnitTest::MemoryOutStream stream;                                                                                                        \
    00092                         stream << "Unhandled exception: " << e.what();                                                   \
    00093                         UnitTest::CurrentTest::Results()->OnTestFailure(m_details, stream.GetText());                            \
    00094                 }                                                                                                                                                        \
    00095                 catch (...) {                                                                                                                            \
    00096                         if (ctorOk)                                                                                                                              \
    00097                         {                                                                                                                                                \
    00098                     UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__),  \
    00099                                         "Unhandled exception while destroying fixture " #Fixture);               \
    00100                         }                                                                                                                                                \
    00101                         else                                                                                                                                     \
    00102                         {                                                                                                                                                \
    00103                                 UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__),   \
    00104                                         "Unhandled exception while constructing fixture " #Fixture);         \
    00105                         }                                                                                                                                                \
    00106                 }                                                                                                                                                        \
    00107     }                                                                                \
    00108     void Fixture##Name##Helper::RunImpl()
    00109
    00110 #define TEST_FIXTURE(Fixture,Name) TEST_FIXTURE_EX(Fixture, Name, UnitTest::Test::GetTestList())
    00111
    00112
    00113 #endif