TestSuiteBuilderContext.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef CPPUNIT_HELPER_TESTSUITEBUILDERCONTEXT_H
  2. #define CPPUNIT_HELPER_TESTSUITEBUILDERCONTEXT_H
  3. #include <cppunit/Portability.h>
  4. #include <cppunit/portability/CppUnitMap.h>
  5. #include <string>
  6. #if CPPUNIT_NEED_DLL_DECL
  7. #pragma warning( push )
  8. #pragma warning( disable: 4251 ) // X needs to have dll-interface to be used by clients of class Z
  9. #endif
  10. CPPUNIT_NS_BEGIN
  11. class TestSuite;
  12. class TestFixture;
  13. class TestFixtureFactory;
  14. class TestNamer;
  15. /*! \brief Context used when creating test suite in HelperMacros.
  16. *
  17. * Base class for all context used when creating test suite. The
  18. * actual context type during test suite creation is TestSuiteBuilderContext.
  19. *
  20. * \sa CPPUNIT_TEST_SUITE, CPPUNIT_TEST_SUITE_ADD_TEST,
  21. * CPPUNIT_TEST_SUITE_ADD_CUSTOM_TESTS.
  22. */
  23. class CPPUNIT_API TestSuiteBuilderContextBase
  24. {
  25. public:
  26. /*! \brief Constructs a new context.
  27. *
  28. * You should not use this. The context is created in
  29. * CPPUNIT_TEST_SUITE().
  30. */
  31. TestSuiteBuilderContextBase( TestSuite &suite,
  32. const TestNamer &namer,
  33. TestFixtureFactory &factory );
  34. virtual ~TestSuiteBuilderContextBase();
  35. /*! \brief Adds a test to the fixture suite.
  36. *
  37. * \param test Test to add to the fixture suite. Must not be \c NULL.
  38. */
  39. void addTest( Test *test );
  40. /*! \brief Returns the fixture name.
  41. * \return Fixture name. It is the name used to name the fixture
  42. * suite.
  43. */
  44. std::string getFixtureName() const;
  45. /*! \brief Returns the name of the test for the specified method.
  46. *
  47. * \param testMethodName Name of the method that implements a test.
  48. * \return A string that is the concatenation of the test fixture name
  49. * (returned by getFixtureName()) and\a testMethodName,
  50. * separated using '::'. This provides a fairly unique name for a given
  51. * test.
  52. */
  53. std::string getTestNameFor( const std::string &testMethodName ) const;
  54. /*! \brief Adds property pair.
  55. * \param key PropertyKey string to add.
  56. * \param value PropertyValue string to add.
  57. */
  58. void addProperty( const std::string &key,
  59. const std::string &value );
  60. /*! \brief Returns property value assigned to param key.
  61. * \param key PropertyKey string.
  62. */
  63. const std::string getStringProperty( const std::string &key ) const;
  64. protected:
  65. TestFixture *makeTestFixture() const;
  66. // Notes: we use a vector here instead of a map to work-around the
  67. // shared std::map in dll bug in VC6.
  68. // See http://www.dinkumware.com/vc_fixes.html for detail.
  69. typedef std::pair<std::string,std::string> Property;
  70. typedef CppUnitVector<Property> Properties;
  71. TestSuite &m_suite;
  72. const TestNamer &m_namer;
  73. TestFixtureFactory &m_factory;
  74. private:
  75. Properties m_properties;
  76. };
  77. /*! \brief Type-sage context used when creating test suite in HelperMacros.
  78. *
  79. * \sa TestSuiteBuilderContextBase.
  80. */
  81. template<class Fixture>
  82. class TestSuiteBuilderContext : public TestSuiteBuilderContextBase
  83. {
  84. public:
  85. typedef Fixture FixtureType;
  86. TestSuiteBuilderContext( TestSuiteBuilderContextBase &contextBase )
  87. : TestSuiteBuilderContextBase( contextBase )
  88. {
  89. }
  90. /*! \brief Returns a new TestFixture instance.
  91. * \return A new fixture instance. The fixture instance is returned by
  92. * the TestFixtureFactory passed on construction. The actual type
  93. * is that of the fixture on which the static method suite()
  94. * was called.
  95. */
  96. FixtureType *makeFixture() const
  97. {
  98. return CPPUNIT_STATIC_CAST( FixtureType *,
  99. TestSuiteBuilderContextBase::makeTestFixture() );
  100. }
  101. };
  102. CPPUNIT_NS_END
  103. #if CPPUNIT_NEED_DLL_DECL
  104. #pragma warning( pop )
  105. #endif
  106. #endif // CPPUNIT_HELPER_TESTSUITEBUILDERCONTEXT_H