TestPath.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #ifndef CPPUNIT_TESTPATH_H
  2. #define CPPUNIT_TESTPATH_H
  3. #include <cppunit/Portability.h>
  4. #if CPPUNIT_NEED_DLL_DECL
  5. #pragma warning( push )
  6. #pragma warning( disable: 4251 ) // X needs to have dll-interface to be used by clients of class Z
  7. #endif
  8. #include <cppunit/portability/CppUnitDeque.h>
  9. #include <string>
  10. CPPUNIT_NS_BEGIN
  11. class Test;
  12. #if CPPUNIT_NEED_DLL_DECL
  13. // template class CPPUNIT_API std::deque<Test *>;
  14. #endif
  15. /*! \brief A List of Test representing a path to access a Test.
  16. * \ingroup ExecutingTest
  17. *
  18. * The path can be converted to a string and resolved from a string with toString()
  19. * and TestPath( Test *root, const std::string &pathAsString ).
  20. *
  21. * Pointed tests are not owned by the class.
  22. *
  23. * \see Test::resolvedTestPath()
  24. */
  25. class CPPUNIT_API TestPath
  26. {
  27. public:
  28. /*! \brief Constructs an invalid path.
  29. *
  30. * The path is invalid until a test is added with add().
  31. */
  32. TestPath();
  33. /*! \brief Constructs a valid path.
  34. *
  35. * \param root Test to add.
  36. */
  37. TestPath( Test *root );
  38. /*! \brief Constructs a path using a slice of another path.
  39. * \param otherPath Path the test are copied from.
  40. * \param indexFirst Zero based index of the first test to copy. Adjusted to be in valid
  41. * range. \a count is adjusted with \a indexFirst.
  42. * \param count Number of tests to copy. If < 0 then all test starting from index
  43. * \a indexFirst are copied.
  44. */
  45. TestPath( const TestPath &otherPath,
  46. int indexFirst,
  47. int count = -1 );
  48. /*! \brief Resolves a path from a string returned by toString().
  49. *
  50. * If \a pathAsString is an absolute path (begins with '/'), then the first test name
  51. * of the path must be the name of \a searchRoot. Otherwise, \a pathAsString is a
  52. * relative path, and the first test found using Test::findTest() matching the first
  53. * test name is used as root. An empty string resolve to a path containing
  54. * \a searchRoot.
  55. *
  56. * The resolved path is always valid.
  57. *
  58. * \param searchRoot Test used to resolve the path.
  59. * \param pathAsString String that contains the path as a string created by toString().
  60. * \exception std::invalid_argument if one of the test names can not be resolved.
  61. * \see toString().
  62. */
  63. TestPath( Test *searchRoot,
  64. const std::string &pathAsString );
  65. /*! \brief Copy constructor.
  66. * \param other Object to copy.
  67. */
  68. TestPath( const TestPath &other );
  69. virtual ~TestPath();
  70. /*! \brief Tests if the path contains at least one test.
  71. * \return \c true if the path contains at least one test, otherwise returns \c false.
  72. */
  73. virtual bool isValid() const;
  74. /*! \brief Adds a test to the path.
  75. * \param test Pointer on the test to add. Must not be \c NULL.
  76. */
  77. virtual void add( Test *test );
  78. /*! \brief Adds all the tests of the specified path.
  79. * \param path Path that contains the test to add.
  80. */
  81. virtual void add( const TestPath &path );
  82. /*! \brief Inserts a test at the specified index.
  83. * \param test Pointer on the test to insert. Must not be \c NULL.
  84. * \param index Zero based index indicating where the test is inserted.
  85. * \exception std::out_of_range is \a index < 0 or \a index > getTestCount().
  86. */
  87. virtual void insert( Test *test, int index );
  88. /*! \brief Inserts all the tests at the specified path at a given index.
  89. * \param path Path that contains the test to insert.
  90. * \param index Zero based index indicating where the tests are inserted.
  91. * \exception std::out_of_range is \a index < 0 or \a index > getTestCount(), and
  92. * \a path is valid.
  93. */
  94. virtual void insert( const TestPath &path, int index );
  95. /*! \brief Removes all the test from the path.
  96. *
  97. * The path becomes invalid after this call.
  98. */
  99. virtual void removeTests();
  100. /*! \brief Removes the test at the specified index of the path.
  101. * \param index Zero based index of the test to remove.
  102. * \exception std::out_of_range is \a index < 0 or \a index >= getTestCount().
  103. */
  104. virtual void removeTest( int index );
  105. /*! \brief Removes the last test.
  106. * \exception std::out_of_range is the path is invalid.
  107. * \see isValid().
  108. */
  109. virtual void up();
  110. /*! \brief Returns the number of tests in the path.
  111. * \return Number of tests in the path.
  112. */
  113. virtual int getTestCount() const;
  114. /*! \brief Returns the test of the specified index.
  115. * \param index Zero based index of the test to return.
  116. * \return Pointer on the test at index \a index. Never \c NULL.
  117. * \exception std::out_of_range is \a index < 0 or \a index >= getTestCount().
  118. */
  119. virtual Test *getTestAt( int index ) const;
  120. /*! \brief Get the last test of the path.
  121. * \return Pointer on the last test (test at the bottom of the hierarchy). Never \c NULL.
  122. * \exception std::out_of_range if the path is not valid ( isValid() returns \c false ).
  123. */
  124. virtual Test *getChildTest() const;
  125. /*! \brief Returns the path as a string.
  126. *
  127. * For example, if a path is composed of three tests named "All Tests", "Math" and
  128. * "Math::testAdd", toString() will return:
  129. *
  130. * "All Tests/Math/Math::testAdd".
  131. *
  132. * \return A string composed of the test names separated with a '/'. It is a relative
  133. * path.
  134. */
  135. virtual std::string toString() const;
  136. /*! \brief Assignment operator.
  137. * \param other Object to copy.
  138. * \return This object.
  139. */
  140. TestPath &operator =( const TestPath &other );
  141. protected:
  142. /*! \brief Checks that the specified test index is within valid range.
  143. * \param index Zero based index to check.
  144. * \exception std::out_of_range is \a index < 0 or \a index >= getTestCount().
  145. */
  146. void checkIndexValid( int index ) const;
  147. /// A list of test names.
  148. typedef CppUnitDeque<std::string> PathTestNames;
  149. /*! \brief Splits a path string into its test name components.
  150. * \param pathAsString Path string created with toString().
  151. * \param testNames Test name components are added to that container.
  152. * \return \c true if the path is relative (does not begin with '/'), \c false
  153. * if it is absolute (begin with '/').
  154. */
  155. bool splitPathString( const std::string &pathAsString,
  156. PathTestNames &testNames );
  157. /*! \brief Finds the actual root of a path string and get the path string name components.
  158. * \param searchRoot Test used as root if the path string is absolute, or to search
  159. * the root test if the path string is relative.
  160. * \param pathAsString Path string. May be absolute or relative.
  161. * \param testNames Test name components are added to that container.
  162. * \return Pointer on the resolved root test. Never \c NULL.
  163. * \exception std::invalid_argument if either the root name can not be resolved or if
  164. * pathAsString contains no name components.
  165. */
  166. Test *findActualRoot( Test *searchRoot,
  167. const std::string &pathAsString,
  168. PathTestNames &testNames );
  169. protected:
  170. typedef CppUnitDeque<Test *> Tests;
  171. Tests m_tests;
  172. };
  173. CPPUNIT_NS_END
  174. #endif // CPPUNIT_TESTPATH_H