diff options
Diffstat (limited to 'include/cppunit')
| -rw-r--r-- | include/cppunit/CompilerOutputter.h | 52 | ||||
| -rw-r--r-- | include/cppunit/Portability.h | 7 | ||||
| -rw-r--r-- | include/cppunit/Test.h | 6 | ||||
| -rw-r--r-- | include/cppunit/TestComposite.h | 4 | ||||
| -rw-r--r-- | include/cppunit/TestListener.h | 67 | ||||
| -rw-r--r-- | include/cppunit/TestResult.h | 3 | ||||
| -rw-r--r-- | include/cppunit/config-msvc6.h | 5 |
7 files changed, 111 insertions, 33 deletions
diff --git a/include/cppunit/CompilerOutputter.h b/include/cppunit/CompilerOutputter.h index 0075972..b2b183d 100644 --- a/include/cppunit/CompilerOutputter.h +++ b/include/cppunit/CompilerOutputter.h @@ -21,13 +21,12 @@ class TestResultCollector; * * Printing the test results in a compiler compatible format (assertion * location has the same format as compiler error), allow you to use your - * IDE to jump to the assertion failure. + * IDE to jump to the assertion failure. Location format can be customized (see + * setLocationFormat() ). * * For example, when running the test in a post-build with VC++, if an assertion * fails, you can jump to the assertion by pressing F4 (jump to next error). * - * You should use defaultOutputter() to create an instance. - * * Heres is an example of usage (from examples/cppunittest/CppUnitTestMain.cpp): * \code * int main( int argc, char* argv[] ) { @@ -42,9 +41,8 @@ class TestResultCollector; * if ( selfTest ) * { // Change the default outputter to a compiler error format outputter * // The test runner owns the new outputter. - * runner.setOutputter( CppUnit::CompilerOutputter::defaultOutputter( - * &runner.result(), - * std::cerr ) ); + * runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), + * std::cerr ) ); * } * * // Run the test and don't wait a key if post build check. @@ -59,14 +57,48 @@ class CPPUNIT_API CompilerOutputter : public Outputter { public: /*! Constructs a CompilerOutputter object. + * \param result Result of the test run. + * \param stream Stream used to output test result. + * \param locationFormat Error location format used by your compiler. Default + * to \c CPPUNIT_COMPILER_LOCATION_FORMAT which is defined + * in the configuration file. See setLocationFormat() for detail. + * \see setLocationFormat(). */ CompilerOutputter( TestResultCollector *result, - std::ostream &stream ); + std::ostream &stream, + const std::string &locationFormat = CPPUNIT_COMPILER_LOCATION_FORMAT ); /// Destructor. virtual ~CompilerOutputter(); + /*! Sets the error location format. + * + * Indicates the format used to report location of failed assertion. This format should + * match the one used by your compiler. + * + * The location format is a string in which the occurence of the following character + * sequence are replaced: + * + * - "%l" => replaced by the line number + * - "%p" => replaced by the full path name of the file ("G:\prg\vc\cppunit\MyTest.cpp") + * - "%f" => replaced by the base name of the file ("MyTest.cpp") + * + * Some examples: + * + * - VC++ error location format: "%p(%l):" => produce "G:\prg\MyTest.cpp(43):" + * - GCC error location format: "%f:%l:" => produce "MyTest.cpp(43):" + * + * Thoses are the two compilers currently <em>supported</em> (gcc format is used if + * VC++ is not detected). If you want your compiler to be automatically supported by + * CppUnit, send a mail to the mailing list (preferred), or submit a feature request + * that indicates how to detect your compiler with the preprocessor (#ifdef...) and + * your compiler location format. + */ + void setLocationFormat( const std::string &locationFormat ); + /*! Creates an instance of an outputter that matches your current compiler. + * \deprecated This class is specialized through parameterization instead of subclassing... + * Use CompilerOutputter::CompilerOutputter instead. */ static CompilerOutputter *defaultOutputter( TestResultCollector *result, std::ostream &stream ); @@ -93,12 +125,18 @@ private: /// Prevents the use of the copy operator. void operator =( const CompilerOutputter © ); + virtual bool processLocationFormatCommand( char command, + const SourceLine &sourceLine ); + + virtual std::string extractBaseName( const std::string &fileName ) const; + typedef std::vector<std::string> Lines; static Lines splitMessageIntoLines( std::string message ); private: TestResultCollector *m_result; std::ostream &m_stream; + std::string m_locationFormat; }; diff --git a/include/cppunit/Portability.h b/include/cppunit/Portability.h index 902de56..1661da0 100644 --- a/include/cppunit/Portability.h +++ b/include/cppunit/Portability.h @@ -42,6 +42,13 @@ #endif +// Compiler error location format for CompilerOutputter +// If not define, assumes that it's gcc +// See class CompilerOutputter for format. +#ifndef CPPUNIT_COMPILER_LOCATION_FORMAT +#define CPPUNIT_COMPILER_LOCATION_FORMAT "%f:%l:" +#endif + /* perform portability hacks */ diff --git a/include/cppunit/Test.h b/include/cppunit/Test.h index dde4958..0ed98f9 100644 --- a/include/cppunit/Test.h +++ b/include/cppunit/Test.h @@ -64,6 +64,8 @@ public: /*! \brief Finds the test with the specified name and its parents test. * \param testName Name of the test to find. + * \param testPath If the test is found, then all the tests traversed to access + * \a test are added to \a testPath, including \c this and \a test. * \return \c true if a test with the specified name is found, \c false otherwise. */ virtual bool findTestPath( const std::string &testName, @@ -71,6 +73,8 @@ public: /*! \brief Finds the specified test and its parents test. * \param test Test to find. + * \param testPath If the test is found, then all the tests traversed to access + * \a test are added to \a testPath, including \c this and \a test. * \return \c true if the specified test is found, \c false otherwise. */ virtual bool findTestPath( const Test *test, @@ -93,7 +97,7 @@ public: protected: /*! Throws an exception if the specified index is invalid. - * \param Zero base index of a child test. + * \param index Zero base index of a child test. * \exception std::out_of_range is \a index is < 0 or >= getChildTestCount(). */ virtual void checkIsValidIndex( int index ) const; diff --git a/include/cppunit/TestComposite.h b/include/cppunit/TestComposite.h index 7faeea8..2d86d5e 100644 --- a/include/cppunit/TestComposite.h +++ b/include/cppunit/TestComposite.h @@ -31,6 +31,10 @@ private: TestComposite( const TestComposite &other ); TestComposite &operator =( const TestComposite &other ); + virtual void doStartSuite( TestResult *controller ); + virtual void doRunChildTests( TestResult *controller ); + virtual void doEndSuite( TestResult *controller ); + private: const std::string m_name; }; diff --git a/include/cppunit/TestListener.h b/include/cppunit/TestListener.h index 2140fe4..2a309c6 100644 --- a/include/cppunit/TestListener.h +++ b/include/cppunit/TestListener.h @@ -25,8 +25,10 @@ class TestFailure; * unanticipated problems signified by exceptions that are not generated * by the framework. * - * \code + * Here is an example to track test time: + * * + * \code * #include <cppunit/TestListener.h> * #include <cppunit/Test.h> * #include <time.h> // for clock() @@ -48,35 +50,42 @@ class TestFailure; * // ... (interface to add/read test timing result) * * private: - * - * class Clock + * Clock _chronometer; + * }; + * \endcode + * + * And another example that track failure/sucess at test suite level and captures + * the TestPath of each suite: + * \code + * class SuiteTracker : public CppUnit::TestListener + * { + * public: + * void startSuite( CppUnit::Test *suite ) * { - * public: - * Clock() : _startTime( 0 ), _endTime(0) {} - * - * void start() - * { - * _startTime = clock(); - * } + * m_currentPath.add( suite ); + * } + * + * void addFailure( const TestFailure &failure ) + * { + * m_suiteFailure.top() = false; + * } * - * void end() - * { - * _endTime = clock(); - * } + * void endSuite( CppUnit::Test *suite ) + * { + * m_suiteStatus.insert( std::make_pair( suite, m_suiteFailure.top() ) ); + * m_suitePaths.insert( std::make_pair( suite, m_currentPath ) ); * - * double elapsedTime() const - * { - * return double(_endTime - _startTime) / CLOCKS_PER_SEC; - * } - * - * private: - * clock_t _startTime, _endTime; - * }; + * m_currentPath.up(); + * m_suiteFailure.pop(); + * } * - * Clock _chronometer; + * private: + * std::stack<bool> m_suiteFailure; + * CppUnit::TestPath m_currentPath; + * std::map<CppUnit::Test *, bool> m_suiteStatus; + * std::map<CppUnit::Test *, CppUnit::TestPath> m_suitePaths; * }; - * - * + * \endcode * * \see TestResult */ @@ -97,6 +106,14 @@ public: /// Called just after a TestCase was run (even if a failure occured). virtual void endTest( Test *test ) {} + + /*! Called by a TestComposite just before running its child tests. + */ + virtual void startSuite( Test *suite ) {} + + /*! Called by a TestComposite after running its child tests. + */ + virtual void endSuite( Test *suite ) {} }; diff --git a/include/cppunit/TestResult.h b/include/cppunit/TestResult.h index b573cf3..200c7fb 100644 --- a/include/cppunit/TestResult.h +++ b/include/cppunit/TestResult.h @@ -61,6 +61,9 @@ public: virtual void addFailure( Test *test, Exception *e ); virtual void endTest( Test *test ); + virtual void startSuite( Test *test ); + virtual void endSuite( Test *test ); + protected: void addFailure( const TestFailure &failure ); diff --git a/include/cppunit/config-msvc6.h b/include/cppunit/config-msvc6.h index c5ad455..7047851 100644 --- a/include/cppunit/config-msvc6.h +++ b/include/cppunit/config-msvc6.h @@ -55,6 +55,11 @@ #define CPPUNIT_NEED_DLL_DECL 1 #endif +// Compiler error location format for CompilerOutputter +// See class CompilerOutputter for format. +#undef CPPUNIT_COMPILER_LOCATION_FORMAT +#define CPPUNIT_COMPILER_LOCATION_FORMAT "%p(%l):" + #if _MSC_VER > 1000 // VC++ #pragma warning( disable : 4786 ) // disable warning debug symbol > 255... #endif // _MSC_VER > 1000 |
