root/library/tests/unittest-cpp/TestMacros.h @ 706

Revision 706, 5.1 kB (checked in by smidl, 15 years ago)

eol-native

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