?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  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. #define UNITTEST_SUITE(Name)                  \
  19.    namespace Suite ## Name {                  \
  20.       namespace UnitTestSuite {               \
  21.          inline char const* GetSuiteName () { \
  22.             return #Name;                     \
  23.          }                                    \
  24.       }                                       \
  25.    }                                          \
  26.    namespace Suite ## Name
  27.  
  28. #define UNITTEST_IMPL_TEST(Name, List)                                                   \
  29.    class Test ## Name : public UnitTest::Test                                            \
  30.    {                                                                                     \
  31.    public:                                                                               \
  32.       Test ## Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {} \
  33.    private:                                                                              \
  34.       virtual void RunImpl() const;                                                      \
  35.    } static test ## Name ## Instance;                                                    \
  36.                                                                                          \
  37.    static UnitTest::ListAdder adder ## Name (List, &test ## Name ## Instance);           \
  38.                                                                                          \
  39.    void Test ## Name::RunImpl() const
  40.  
  41.  
  42. #define UNITTEST_TEST(Name) UNITTEST_IMPL_TEST(Name, UnitTest::Test::GetTestList())
  43.  
  44.  
  45. #define UNITTEST_IMPL_TEST_FIXTURE(Fixture, Name, List)                                                                             \
  46.    class Fixture ## Name ## Helper : public Fixture                                                                      \
  47.    {                                                                                                                     \
  48.    public:                                                                                                               \
  49.       explicit Fixture ## Name ## Helper(UnitTest::TestDetails const& details) : m_details(details) {}                   \
  50.       void RunImpl();                                                                                                    \
  51.       UnitTest::TestDetails const& m_details;                                                                            \
  52.       virtual ~Fixture ## Name ## Helper();                                                                                                                              \
  53.    private:                                                                                                              \
  54.       Fixture ## Name ## Helper(Fixture ## Name ## Helper const&);                                                       \
  55.       Fixture ## Name ## Helper& operator =(Fixture ## Name ## Helper const&);                                           \
  56.    };                                                                                                                    \
  57.    Fixture ## Name ## Helper::~Fixture ## Name ## Helper(){}                                                             \
  58.                                                                                                                          \
  59.    class Test ## Fixture ## Name : public UnitTest::Test                                                                 \
  60.    {                                                                                                                     \
  61.    public:                                                                                                               \
  62.       Test ## Fixture ## Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {}                      \
  63.    private:                                                                                                              \
  64.       virtual void RunImpl() const;                                                                                      \
  65.    } static test ## Fixture ## Name ## Instance;                                                                         \
  66.                                                                                                                          \
  67.    static UnitTest::ListAdder adder ## Fixture ## Name (List, &test ## Fixture ## Name ## Instance);                     \
  68.                                                                                                                          \
  69.    void Test ## Fixture ## Name::RunImpl() const                                                                         \
  70.    {                                                                                                                     \
  71.       volatile bool ctorOk = false;                                                                                      \
  72.       UNITTEST_IMPL_TRY                                                                                                             \
  73.          ({                                                                                                              \
  74.          Fixture ## Name ## Helper fixtureHelper(m_details);                                                             \
  75.          ctorOk = true;                                                                                                  \
  76.          UnitTest::ExecuteTest(fixtureHelper, m_details, false);                                                         \
  77.       })                                                                                                                 \
  78.       UNITTEST_IMPL_CATCH (UnitTest::AssertException, e,                                                                            \
  79.       {                                                                                                                  \
  80.          (void)e;                                                                                                        \
  81.       })                                                                                                                 \
  82.       UNITTEST_IMPL_CATCH (std::exception, e,                                                                                       \
  83.       {                                                                                                                  \
  84.          UnitTest::MemoryOutStream stream;                                                                               \
  85.          stream << "Unhandled exception: " << e.what();                                                                  \
  86.          UnitTest::CurrentTest::Results()->OnTestFailure(m_details, stream.GetText());                                   \
  87.       })                                                                                                                 \
  88.       UNITTEST_IMPL_CATCH_ALL                                                                                                       \
  89.          ({                                                                                                              \
  90.          if (ctorOk)                                                                                                     \
  91.          {                                                                                                               \
  92.             UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__),                  \
  93.                                                             "Unhandled exception while destroying fixture " #Fixture);   \
  94.          }                                                                                                               \
  95.          else                                                                                                            \
  96.          {                                                                                                               \
  97.             UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__),                  \
  98.                                                             "Unhandled exception while constructing fixture " #Fixture); \
  99.          }                                                                                                               \
  100.       })                                                                                                                 \
  101.    }                                                                                                                     \
  102.    void Fixture ## Name ## Helper::RunImpl()
  103.  
  104. #define UNITTEST_TEST_FIXTURE(Fixture,Name) UNITTEST_IMPL_TEST_FIXTURE(Fixture, Name, UnitTest::Test::GetTestList())
  105.  
  106. #ifndef UNITTEST_DISABLE_SHORT_MACROS
  107.    #ifdef SUITE
  108.       #error SUITE already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_SUITE instead
  109.    #else
  110.       #define SUITE UNITTEST_SUITE
  111.    #endif
  112.  
  113.    #ifdef TEST
  114.       #error TEST already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_TEST instead
  115.    #else
  116.       #define TEST UNITTEST_TEST
  117.    #endif
  118.  
  119.    #ifdef TEST_FIXTURE
  120.       #error TEST_FIXTURE already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_TEST_FIXTURE instead
  121.    #else
  122.       #define TEST_FIXTURE UNITTEST_TEST_FIXTURE
  123.    #endif
  124. #endif
  125.  
  126. #endif
  127.