Blame include/cppunit/TestPath.h

Packit 8c9aa0
#ifndef CPPUNIT_TESTPATH_H
Packit 8c9aa0
#define CPPUNIT_TESTPATH_H
Packit 8c9aa0
Packit 8c9aa0
#include <cppunit/Portability.h>
Packit 8c9aa0
Packit 8c9aa0
#if CPPUNIT_NEED_DLL_DECL
Packit 8c9aa0
#pragma warning( push )
Packit 8c9aa0
#pragma warning( disable: 4251 )  // X needs to have dll-interface to be used by clients of class Z
Packit 8c9aa0
#endif
Packit 8c9aa0
Packit 8c9aa0
#include <deque>
Packit 8c9aa0
#include <string>
Packit 8c9aa0
Packit 8c9aa0
CPPUNIT_NS_BEGIN
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
class Test;
Packit 8c9aa0
Packit 8c9aa0
/*! \brief A List of Test representing a path to access a Test.
Packit 8c9aa0
 * \ingroup ExecutingTest
Packit 8c9aa0
 *
Packit 8c9aa0
 * The path can be converted to a string and resolved from a string with toString()
Packit 8c9aa0
 * and TestPath( Test *root, const std::string &pathAsString ).
Packit 8c9aa0
 *
Packit 8c9aa0
 * Pointed tests are not owned by the class.
Packit 8c9aa0
 *
Packit 8c9aa0
 * \see Test::resolvedTestPath()
Packit 8c9aa0
 */
Packit 8c9aa0
class CPPUNIT_API TestPath
Packit 8c9aa0
{
Packit 8c9aa0
public:
Packit 8c9aa0
  /*! \brief Constructs an invalid path.
Packit 8c9aa0
   * 
Packit 8c9aa0
   * The path is invalid until a test is added with add().
Packit 8c9aa0
   */
Packit 8c9aa0
  TestPath();
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Constructs a valid path.
Packit 8c9aa0
   *
Packit 8c9aa0
   * \param root Test to add.
Packit 8c9aa0
   */
Packit 8c9aa0
  TestPath( Test *root );
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Constructs a path using a slice of another path.
Packit 8c9aa0
   * \param otherPath Path the test are copied from.
Packit 8c9aa0
   * \param indexFirst Zero based index of the first test to copy. Adjusted to be in valid
Packit 8c9aa0
   *                   range. \a count is adjusted with \a indexFirst.
Packit 8c9aa0
   * \param count Number of tests to copy. If < 0 then all test starting from index
Packit 8c9aa0
   *              \a indexFirst are copied.
Packit 8c9aa0
   */
Packit 8c9aa0
  TestPath( const TestPath &otherPath, 
Packit 8c9aa0
            int indexFirst, 
Packit 8c9aa0
            int count = -1 );
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Resolves a path from a string returned by toString().
Packit 8c9aa0
   *
Packit 8c9aa0
   * If \a pathAsString is an absolute path (begins with '/'), then the first test name
Packit 8c9aa0
   * of the path must be the name of \a searchRoot. Otherwise, \a pathAsString is a 
Packit 8c9aa0
   * relative path, and the first test found using Test::findTest() matching the first
Packit 8c9aa0
   * test name is used as root. An empty string resolve to a path containing 
Packit 8c9aa0
   * \a searchRoot.
Packit 8c9aa0
   *
Packit 8c9aa0
   * The resolved path is always valid.
Packit 8c9aa0
   *
Packit 8c9aa0
   * \param searchRoot Test used to resolve the path.
Packit 8c9aa0
   * \param pathAsString String that contains the path as a string created by toString().
Packit 8c9aa0
   * \exception std::invalid_argument if one of the test names can not be resolved.
Packit 8c9aa0
   * \see toString().
Packit 8c9aa0
   */
Packit 8c9aa0
  TestPath( Test *searchRoot, 
Packit 8c9aa0
            const std::string &pathAsString );
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Copy constructor.
Packit 8c9aa0
   * \param other Object to copy.
Packit 8c9aa0
   */
Packit 8c9aa0
  TestPath( const TestPath &other );
Packit 8c9aa0
Packit 8c9aa0
  virtual ~TestPath();
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Tests if the path contains at least one test.
Packit 8c9aa0
   * \return \c true if the path contains at least one test, otherwise returns \c false.
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual bool isValid() const;
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Adds a test to the path.
Packit 8c9aa0
   * \param test Pointer on the test to add. Must not be \c NULL.
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual void add( Test *test );
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Adds all the tests of the specified path.
Packit 8c9aa0
   * \param path Path that contains the test to add.
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual void add( const TestPath &path );
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Inserts a test at the specified index.
Packit 8c9aa0
   * \param test Pointer on the test to insert. Must not be \c NULL.
Packit 8c9aa0
   * \param index Zero based index indicating where the test is inserted.
Packit 8c9aa0
   * \exception std::out_of_range is \a index < 0 or \a index > getTestCount().
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual void insert( Test *test, int index );
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Inserts all the tests at the specified path at a given index.
Packit 8c9aa0
   * \param path Path that contains the test to insert.
Packit 8c9aa0
   * \param index Zero based index indicating where the tests are inserted.
Packit 8c9aa0
   * \exception std::out_of_range is \a index < 0 or \a index > getTestCount(), and
Packit 8c9aa0
   *            \a path is valid.
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual void insert( const TestPath &path, int index );
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Removes all the test from the path.
Packit 8c9aa0
   *
Packit 8c9aa0
   * The path becomes invalid after this call.
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual void removeTests();
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Removes the test at the specified index of the path.
Packit 8c9aa0
   * \param index Zero based index of the test to remove.
Packit 8c9aa0
   * \exception std::out_of_range is \a index < 0 or \a index >= getTestCount().
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual void removeTest( int index );
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Removes the last test.
Packit 8c9aa0
   * \exception std::out_of_range is the path is invalid.
Packit 8c9aa0
   * \see isValid().
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual void up();
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Returns the number of tests in the path.
Packit 8c9aa0
   * \return Number of tests in the path.
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual int getTestCount() const;
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Returns the test of the specified index.
Packit 8c9aa0
   * \param index Zero based index of the test to return.
Packit 8c9aa0
   * \return Pointer on the test at index \a index. Never \c NULL.
Packit 8c9aa0
   * \exception std::out_of_range is \a index < 0 or \a index >= getTestCount().
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual Test *getTestAt( int index ) const;
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Get the last test of the path.
Packit 8c9aa0
   * \return Pointer on the last test (test at the bottom of the hierarchy). Never \c NULL.
Packit 8c9aa0
   * \exception std::out_of_range if the path is not valid ( isValid() returns \c false ).
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual Test *getChildTest() const;
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Returns the path as a string.
Packit 8c9aa0
   *
Packit 8c9aa0
   * For example, if a path is composed of three tests named "All Tests", "Math" and
Packit 8c9aa0
   * "Math::testAdd", toString() will return:
Packit 8c9aa0
   *
Packit 8c9aa0
   * "All Tests/Math/Math::testAdd".
Packit 8c9aa0
   * 
Packit 8c9aa0
   * \return A string composed of the test names separated with a '/'. It is a relative
Packit 8c9aa0
   *         path.
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual std::string toString() const;
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Assignment operator.
Packit 8c9aa0
   * \param other Object to copy.
Packit 8c9aa0
   * \return This object.
Packit 8c9aa0
   */
Packit 8c9aa0
  TestPath &operator =( const TestPath &other );
Packit 8c9aa0
Packit 8c9aa0
protected:
Packit 8c9aa0
  /*! \brief Checks that the specified test index is within valid range.
Packit 8c9aa0
   * \param index Zero based index to check.
Packit 8c9aa0
   * \exception std::out_of_range is \a index < 0 or \a index >= getTestCount().
Packit 8c9aa0
   */
Packit 8c9aa0
  void checkIndexValid( int index ) const;
Packit 8c9aa0
Packit 8c9aa0
  /// A list of test names.
Packit 8c9aa0
  typedef std::deque<std::string> PathTestNames;
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Splits a path string into its test name components.
Packit 8c9aa0
   * \param pathAsString Path string created with toString().
Packit 8c9aa0
   * \param testNames Test name components are added to that container.
Packit 8c9aa0
   * \return \c true if the path is relative (does not begin with '/'), \c false
Packit 8c9aa0
   *         if it is absolute (begin with '/').
Packit 8c9aa0
   */
Packit 8c9aa0
  bool splitPathString( const std::string &pathAsString,
Packit 8c9aa0
                        PathTestNames &testNames );
Packit 8c9aa0
Packit 8c9aa0
  /*! \brief Finds the actual root of a path string and get the path string name components.
Packit 8c9aa0
   * \param searchRoot Test used as root if the path string is absolute, or to search
Packit 8c9aa0
   *                   the root test if the path string is relative.
Packit 8c9aa0
   * \param pathAsString Path string. May be absolute or relative.
Packit 8c9aa0
   * \param testNames Test name components are added to that container.
Packit 8c9aa0
   * \return Pointer on the resolved root test. Never \c NULL.
Packit 8c9aa0
   * \exception std::invalid_argument if either the root name can not be resolved or if
Packit 8c9aa0
   *            pathAsString contains no name components.
Packit 8c9aa0
   */
Packit 8c9aa0
  Test *findActualRoot( Test *searchRoot,
Packit 8c9aa0
                        const std::string &pathAsString,
Packit 8c9aa0
                        PathTestNames &testNames );
Packit 8c9aa0
Packit 8c9aa0
protected:
Packit 8c9aa0
  typedef std::deque<Test *> Tests;
Packit 8c9aa0
  Tests m_tests;
Packit 8c9aa0
Packit 8c9aa0
};
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
CPPUNIT_NS_END
Packit 8c9aa0
Packit 8c9aa0
#endif // CPPUNIT_TESTPATH_H
Packit 8c9aa0