TestPlugIn.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #ifndef CPPUNIT_PLUGIN_TESTPLUGIN
  2. #define CPPUNIT_PLUGIN_TESTPLUGIN
  3. #include <cppunit/Portability.h>
  4. #if !defined(CPPUNIT_NO_TESTPLUGIN)
  5. #include <cppunit/plugin/PlugInParameters.h>
  6. CPPUNIT_NS_BEGIN
  7. class Test;
  8. class TestFactoryRegistry;
  9. class TestResult;
  10. class XmlOutputter;
  11. CPPUNIT_NS_END
  12. /*! \file
  13. */
  14. /*! \brief Test plug-in interface.
  15. * \ingroup WritingTestPlugIn
  16. *
  17. * This class define the interface implemented by test plug-in. A pointer to that
  18. * interface is returned by the function exported by the test plug-in.
  19. *
  20. * Plug-in are loaded/unloaded by PlugInManager. When a plug-in is loaded,
  21. * initialize() is called. Before unloading the plug-in, the PlugInManager
  22. * call uninitialize().
  23. *
  24. * addListener() and removeListener() are called respectively before and after
  25. * the test run.
  26. *
  27. * addXmlOutputterHooks() and removeXmlOutputterHooks() are called respectively
  28. * before and after writing the XML output using a XmlOutputter.
  29. *
  30. * \see CPPUNIT_PLUGIN_IMPLEMENT, CPPUNIT_PLUGIN_EXPORTED_FUNCTION_IMPL
  31. * \see CppUnit::TestPlugInDefaultImpl, CppUnit::XmlOutputter.
  32. */
  33. struct CppUnitTestPlugIn
  34. {
  35. /*! \brief Called just after loading the dynamic library.
  36. *
  37. * Override this method to add additional suite to the registry, though this
  38. * is preferably done using the macros (CPPUNIT_TEST_SUITE_REGISTRATION...).
  39. * If you are creating a custom listener to extends the plug-in runner,
  40. * you can use this to configure the listener using the \a parameters.
  41. *
  42. * You could also use the parameters to specify some global parameter, such
  43. * as test datas location, database name...
  44. *
  45. * N.B.: Parameters interface is not define yet, and the plug-in runner does
  46. * not yet support plug-in parameter.
  47. */
  48. virtual void initialize( CPPUNIT_NS::TestFactoryRegistry *registry,
  49. const CPPUNIT_NS::PlugInParameters &parameters ) =0;
  50. /*! \brief Gives a chance to the plug-in to register TestListener.
  51. *
  52. * Override this method to add a TestListener for the test run. This is useful
  53. * if you are writing a custom TestListener, but also if you need to
  54. * setUp some global resource: listen to TestListener::startTestRun(),
  55. * and TestListener::endTestRun().
  56. */
  57. virtual void addListener( CPPUNIT_NS::TestResult *eventManager ) =0;
  58. /*! \brief Gives a chance to the plug-in to remove its registered TestListener.
  59. *
  60. * Override this method to remove a TestListener that has been added.
  61. */
  62. virtual void removeListener( CPPUNIT_NS::TestResult *eventManager ) =0;
  63. /*! \brief Provides a way for the plug-in to register some XmlOutputterHook.
  64. */
  65. virtual void addXmlOutputterHooks( CPPUNIT_NS::XmlOutputter *outputter ) =0;
  66. /*! \brief Called when the XmlOutputter is destroyed.
  67. *
  68. * Can be used to free some resources allocated by addXmlOutputterHooks().
  69. */
  70. virtual void removeXmlOutputterHooks() = 0;
  71. /*! \brief Called just before unloading the dynamic library.
  72. *
  73. * Override this method to unregister test factory added in initialize().
  74. * This is necessary to keep the TestFactoryRegistry 'clean'. When
  75. * the plug-in is unloaded from memory, the TestFactoryRegistry will hold
  76. * reference on test that are no longer available if they are not
  77. * unregistered.
  78. */
  79. virtual void uninitialize( CPPUNIT_NS::TestFactoryRegistry *registry ) =0;
  80. virtual ~CppUnitTestPlugIn() {}
  81. };
  82. /*! \brief Name of the function exported by a test plug-in.
  83. * \ingroup WritingTestPlugIn
  84. *
  85. * The signature of the exported function is:
  86. * \code
  87. * CppUnitTestPlugIn *CPPUNIT_PLUGIN_EXPORTED_NAME(void);
  88. * \endcode
  89. */
  90. #define CPPUNIT_PLUGIN_EXPORTED_NAME cppunitTestPlugIn
  91. /*! \brief Type of the function exported by a plug-in.
  92. * \ingroup WritingTestPlugIn
  93. */
  94. typedef CppUnitTestPlugIn *(*TestPlugInSignature)();
  95. /*! \brief Implements the function exported by the test plug-in
  96. * \ingroup WritingTestPlugIn
  97. */
  98. #define CPPUNIT_PLUGIN_EXPORTED_FUNCTION_IMPL( TestPlugInInterfaceType ) \
  99. CPPUNIT_PLUGIN_EXPORT CppUnitTestPlugIn *CPPUNIT_PLUGIN_EXPORTED_NAME(void) \
  100. { \
  101. static TestPlugInInterfaceType plugIn; \
  102. return &plugIn; \
  103. } \
  104. typedef char __CppUnitPlugInExportFunctionDummyTypeDef // dummy typedef so it can end with ';'
  105. // Note: This include should remain after definition of CppUnitTestPlugIn
  106. #include <cppunit/plugin/TestPlugInDefaultImpl.h>
  107. /*! \def CPPUNIT_PLUGIN_IMPLEMENT_MAIN()
  108. * \brief Implements the 'main' function for the plug-in.
  109. *
  110. * This macros implements the main() function for dynamic library.
  111. * For example, WIN32 requires a DllMain function, while some Unix
  112. * requires a main() function. This macros takes care of the implementation.
  113. */
  114. // Win32
  115. #if defined(CPPUNIT_HAVE_WIN32_DLL_LOADER)
  116. #if !defined(APIENTRY)
  117. #define WIN32_LEAN_AND_MEAN
  118. #define NOGDI
  119. #define NOUSER
  120. #define NOKERNEL
  121. #define NOSOUND
  122. #define NOMINMAX
  123. #define BLENDFUNCTION void // for mingw & gcc
  124. #include <windows.h>
  125. #endif
  126. #define CPPUNIT_PLUGIN_IMPLEMENT_MAIN() \
  127. BOOL APIENTRY DllMain( HANDLE hModule, \
  128. DWORD ul_reason_for_call, \
  129. LPVOID lpReserved ) \
  130. { \
  131. return TRUE; \
  132. } \
  133. typedef char __CppUnitPlugInImplementMainDummyTypeDef
  134. // Unix
  135. #elif defined(CPPUNIT_HAVE_UNIX_DLL_LOADER) || defined(CPPUNIT_HAVE_UNIX_SHL_LOADER)
  136. #define CPPUNIT_PLUGIN_IMPLEMENT_MAIN() \
  137. int main( int argc, char *argv[] ) \
  138. { \
  139. return 0; \
  140. } \
  141. typedef char __CppUnitPlugInImplementMainDummyTypeDef
  142. // Other
  143. #else // other platforms don't require anything specifics
  144. #endif
  145. /*! \brief Implements and exports the test plug-in interface.
  146. * \ingroup WritingTestPlugIn
  147. *
  148. * This macro exports the test plug-in function using the subclass,
  149. * and implements the 'main' function for the plug-in using
  150. * CPPUNIT_PLUGIN_IMPLEMENT_MAIN().
  151. *
  152. * When using this macro, CppUnit must be linked as a DLL (shared library).
  153. * Otherwise, tests registered to the TestFactoryRegistry in the DLL will
  154. * not be visible to the DllPlugInTester.
  155. *
  156. * \see CppUnitTestPlugIn
  157. * \see CPPUNIT_PLUGIN_EXPORTED_FUNCTION_IMPL(), CPPUNIT_PLUGIN_IMPLEMENT_MAIN().
  158. */
  159. #define CPPUNIT_PLUGIN_IMPLEMENT() \
  160. CPPUNIT_PLUGIN_EXPORTED_FUNCTION_IMPL( CPPUNIT_NS::TestPlugInDefaultImpl ); \
  161. CPPUNIT_PLUGIN_IMPLEMENT_MAIN()
  162. #endif // !defined(CPPUNIT_NO_TESTPLUGIN)
  163. #endif // CPPUNIT_PLUGIN_TESTPLUGIN