📄 helpermacros.h
字号:
// //////////////////////////////////////////////////////////////////////////// Header file HelperMacros.h// (c)Copyright 2000, Baptiste Lepilleur.// Created: 2001/04/15// //////////////////////////////////////////////////////////////////////////#ifndef CPPUNIT_EXTENSIONS_HELPERMACROS_H#define CPPUNIT_EXTENSIONS_HELPERMACROS_H#include <cppunit/TestCaller.h>#include <cppunit/TestSuite.h>#include <cppunit/extensions/AutoRegisterSuite.h>#include <cppunit/extensions/ExceptionTestCaseDecorator.h>#include <cppunit/extensions/TestFixtureFactory.h>#include <cppunit/extensions/TestNamer.h>#include <cppunit/extensions/TestSuiteBuilderContext.h>#include <memory>/*! \addtogroup WritingTestFixture Writing test fixture *//** @{ *//** \file * Macros intended to ease the definition of test suites. * * The macros * CPPUNIT_TEST_SUITE(), CPPUNIT_TEST(), and CPPUNIT_TEST_SUITE_END() * are designed to facilitate easy creation of a test suite. * For example, * * \code * #include <cppunit/extensions/HelperMacros.h> * class MyTest : public CppUnit::TestFixture { * CPPUNIT_TEST_SUITE( MyTest ); * CPPUNIT_TEST( testEquality ); * CPPUNIT_TEST( testSetName ); * CPPUNIT_TEST_SUITE_END(); * public: * void testEquality(); * void testSetName(); * }; * \endcode * * The effect of these macros is to define two methods in the * class MyTest. The first method is an auxiliary function * named registerTests that you will not need to call directly. * The second function * \code static CppUnit::TestSuite *suite()\endcode * returns a pointer to the suite of tests defined by the CPPUNIT_TEST() * macros. * * Rather than invoking suite() directly, * the macro CPPUNIT_TEST_SUITE_REGISTRATION() is * used to create a static variable that automatically * registers its test suite in a global registry. * The registry yields a Test instance containing all the * registered suites. * \code * CPPUNIT_TEST_SUITE_REGISTRATION( MyTest ); * CppUnit::Test* tp = * CppUnit::TestFactoryRegistry::getRegistry().makeTest(); * \endcode * * The test suite macros can even be used with templated test classes. * For example: * * \code * template<typename CharType> * class StringTest : public CppUnit::TestFixture { * CPPUNIT_TEST_SUITE( StringTest ); * CPPUNIT_TEST( testAppend ); * CPPUNIT_TEST_SUITE_END(); * public: * ... * }; * \endcode * * You need to add in an implementation file: * * \code * CPPUNIT_TEST_SUITE_REGISTRATION( StringTest<char> ); * CPPUNIT_TEST_SUITE_REGISTRATION( StringTest<wchar_t> ); * \endcode *//*! \brief Begin test suite * * This macro starts the declaration of a new test suite. * Use CPPUNIT_TEST_SUB_SUITE() instead, if you wish to include the * test suite of the parent class. * * \param ATestFixtureType Type of the test case class. This type \b MUST * be derived from TestFixture. * \see CPPUNIT_TEST_SUB_SUITE, CPPUNIT_TEST, CPPUNIT_TEST_SUITE_END, * \see CPPUNIT_TEST_SUITE_REGISTRATION, CPPUNIT_TEST_EXCEPTION, CPPUNIT_TEST_FAIL. */#define CPPUNIT_TEST_SUITE( ATestFixtureType ) \ public: \ typedef ATestFixtureType TestFixtureType; \ \ private: \ static const CPPUNIT_NS::TestNamer &getTestNamer__() \ { \ static CPPUNIT_TESTNAMER_DECL( testNamer, ATestFixtureType ); \ return testNamer; \ } \ \ public: \ typedef CPPUNIT_NS::TestSuiteBuilderContext<TestFixtureType> \ TestSuiteBuilderContextType; \ \ static void \ addTestsToSuite( CPPUNIT_NS::TestSuiteBuilderContextBase &baseContext ) \ { \ TestSuiteBuilderContextType context( baseContext )/*! \brief Begin test suite (includes parent suite) * * This macro may only be used in a class whose parent class * defines a test suite using CPPUNIT_TEST_SUITE() or CPPUNIT_TEST_SUB_SUITE(). * * This macro begins the declaration of a test suite, in the same * manner as CPPUNIT_TEST_SUITE(). In addition, the test suite of the * parent is automatically inserted in the test suite being * defined. * * Here is an example: * * \code * #include <cppunit/extensions/HelperMacros.h> * class MySubTest : public MyTest { * CPPUNIT_TEST_SUB_SUITE( MySubTest, MyTest ); * CPPUNIT_TEST( testAdd ); * CPPUNIT_TEST( testSub ); * CPPUNIT_TEST_SUITE_END(); * public: * void testAdd(); * void testSub(); * }; * \endcode * * \param ATestFixtureType Type of the test case class. This type \b MUST * be derived from TestFixture. * \param ASuperClass Type of the parent class. * \see CPPUNIT_TEST_SUITE. */#define CPPUNIT_TEST_SUB_SUITE( ATestFixtureType, ASuperClass ) \ public: \ typedef ASuperClass ParentTestFixtureType; \ private: \ CPPUNIT_TEST_SUITE( ATestFixtureType ); \ ParentTestFixtureType::addTestsToSuite( baseContext )/*! \brief End declaration of the test suite. * * After this macro, member access is set to "private". * * \see CPPUNIT_TEST_SUITE. * \see CPPUNIT_TEST_SUITE_REGISTRATION. */#define CPPUNIT_TEST_SUITE_END() \ } \ \ static CPPUNIT_NS::TestSuite *suite() \ { \ const CPPUNIT_NS::TestNamer &namer = getTestNamer__(); \ std::auto_ptr<CPPUNIT_NS::TestSuite> suite( \ new CPPUNIT_NS::TestSuite( namer.getFixtureName() )); \ CPPUNIT_NS::ConcretTestFixtureFactory<TestFixtureType> factory; \ CPPUNIT_NS::TestSuiteBuilderContextBase context( *suite.get(), \ namer, \ factory ); \ TestFixtureType::addTestsToSuite( context ); \ return suite.release(); \ } \ private: /* dummy typedef so that the macro can still end with ';'*/ \ typedef int CppUnitDummyTypedefForSemiColonEnding__/*! \brief End declaration of an abstract test suite. * * Use this macro to indicate that the %TestFixture is abstract. No * static suite() method will be declared. * * After this macro, member access is set to "private". * * Here is an example of usage: * * The abstract test fixture: * \code * #include <cppunit/extensions/HelperMacros.h> * class AbstractDocument; * class AbstractDocumentTest : public CppUnit::TestFixture { * CPPUNIT_TEST_SUITE( AbstractDocumentTest ); * CPPUNIT_TEST( testInsertText ); * CPPUNIT_TEST_SUITE_END_ABSTRACT(); * public: * void testInsertText(); * * void setUp() * { * m_document = makeDocument(); * } * * void tearDown() * { * delete m_document; * } * protected: * virtual AbstractDocument *makeDocument() =0; * * AbstractDocument *m_document; * };\endcode * * The concret test fixture: * \code * class RichTextDocumentTest : public AbstractDocumentTest { * CPPUNIT_TEST_SUB_SUITE( RichTextDocumentTest, AbstractDocumentTest ); * CPPUNIT_TEST( testInsertFormatedText ); * CPPUNIT_TEST_SUITE_END(); * public: * void testInsertFormatedText(); * protected: * AbstractDocument *makeDocument() * { * return new RichTextDocument(); * } * };\endcode * * \see CPPUNIT_TEST_SUB_SUITE. * \see CPPUNIT_TEST_SUITE_REGISTRATION. */#define CPPUNIT_TEST_SUITE_END_ABSTRACT() \ } \ private: /* dummy typedef so that the macro can still end with ';'*/ \ typedef int CppUnitDummyTypedefForSemiColonEnding__/*! \brief Add a test to the suite (for custom test macro). * * The specified test will be added to the test suite being declared. This macro * is intended for \e advanced usage, to extend %CppUnit by creating new macro such * as CPPUNIT_TEST_EXCEPTION()... * * Between macro CPPUNIT_TEST_SUITE() and CPPUNIT_TEST_SUITE_END(), you can assume * that the following variables can be used: * \code * typedef TestSuiteBuilder<TestFixtureType> TestSuiteBuilderType; * TestSuiteBuilderType &context; * \endcode * * \c context can be used to name test case, create new test fixture instance, * or add test case to the test fixture suite. * * Below is an example that show how to use this macro to create new macro to add * test to the fixture suite. The macro below show how you would add a new type * of test case which fails if the execution last more than a given time limit. * It relies on an imaginary TimeOutTestCaller class which has an interface similar * to TestCaller. * * \code * #define CPPUNITEX_TEST_TIMELIMIT( testMethod, timeLimit ) \ * CPPUNIT_TEST_SUITE_ADD_TEST( (new TimeOutTestCaller<TestFixtureType>( \ * namer.getTestNameFor( #testMethod ), \ * &TestFixtureType::testMethod, \ * factory.makeFixture(), \ * timeLimit ) ) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -