CompilerOutputter.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #ifndef CPPUNIT_COMPILERTESTRESULTOUTPUTTER_H
  2. #define CPPUNIT_COMPILERTESTRESULTOUTPUTTER_H
  3. #include <cppunit/Portability.h>
  4. #include <cppunit/Outputter.h>
  5. #include <cppunit/portability/Stream.h>
  6. CPPUNIT_NS_BEGIN
  7. class Exception;
  8. class SourceLine;
  9. class Test;
  10. class TestFailure;
  11. class TestResultCollector;
  12. /*!
  13. * \brief Outputs a TestResultCollector in a compiler compatible format.
  14. * \ingroup WritingTestResult
  15. *
  16. * Printing the test results in a compiler compatible format (assertion
  17. * location has the same format as compiler error), allow you to use your
  18. * IDE to jump to the assertion failure. Location format can be customized (see
  19. * setLocationFormat() ).
  20. *
  21. * For example, when running the test in a post-build with VC++, if an assertion
  22. * fails, you can jump to the assertion by pressing F4 (jump to next error).
  23. *
  24. * Heres is an example of usage (from examples/cppunittest/CppUnitTestMain.cpp):
  25. * \code
  26. * int main( int argc, char* argv[] ) {
  27. * // if command line contains "-selftest" then this is the post build check
  28. * // => the output must be in the compiler error format.
  29. * bool selfTest = (argc > 1) &&
  30. * (std::string("-selftest") == argv[1]);
  31. *
  32. * CppUnit::TextUi::TestRunner runner;
  33. * runner.addTest( CppUnitTest::suite() ); // Add the top suite to the test runner
  34. *
  35. * if ( selfTest )
  36. * { // Change the default outputter to a compiler error format outputter
  37. * // The test runner owns the new outputter.
  38. * runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
  39. * std::cerr ) );
  40. * }
  41. *
  42. * // Run the test and don't wait a key if post build check.
  43. * bool wasSuccessful = runner.run( "", !selfTest );
  44. *
  45. * // Return error code 1 if the one of test failed.
  46. * return wasSuccessful ? 0 : 1;
  47. * }
  48. * \endcode
  49. */
  50. class CPPUNIT_API CompilerOutputter : public Outputter
  51. {
  52. public:
  53. /*! \brief Constructs a CompilerOutputter object.
  54. * \param result Result of the test run.
  55. * \param stream Stream used to output test result.
  56. * \param locationFormat Error location format used by your compiler. Default
  57. * to \c CPPUNIT_COMPILER_LOCATION_FORMAT which is defined
  58. * in the configuration file. See setLocationFormat() for detail.
  59. * \see setLocationFormat().
  60. */
  61. CompilerOutputter( TestResultCollector *result,
  62. OStream &stream,
  63. const std::string &locationFormat = CPPUNIT_COMPILER_LOCATION_FORMAT );
  64. /// Destructor.
  65. virtual ~CompilerOutputter();
  66. /*! \brief Sets the error location format.
  67. *
  68. * Indicates the format used to report location of failed assertion. This format should
  69. * match the one used by your compiler.
  70. *
  71. * The location format is a string in which the occurence of the following character
  72. * sequence are replaced:
  73. *
  74. * - "%l" => replaced by the line number
  75. * - "%p" => replaced by the full path name of the file ("G:\prg\vc\cppunit\MyTest.cpp")
  76. * - "%f" => replaced by the base name of the file ("MyTest.cpp")
  77. *
  78. * Some examples:
  79. *
  80. * - VC++ error location format: "%p(%l):" => produce "G:\prg\MyTest.cpp(43):"
  81. * - GCC error location format: "%f:%l:" => produce "MyTest.cpp:43:"
  82. *
  83. * Thoses are the two compilers currently <em>supported</em> (gcc format is used if
  84. * VC++ is not detected). If you want your compiler to be automatically supported by
  85. * CppUnit, send a mail to the mailing list (preferred), or submit a feature request
  86. * that indicates how to detect your compiler with the preprocessor (\#ifdef...) and
  87. * your compiler location format.
  88. */
  89. void setLocationFormat( const std::string &locationFormat );
  90. /*! \brief Creates an instance of an outputter that matches your current compiler.
  91. * \deprecated This class is specialized through parameterization instead of subclassing...
  92. * Use CompilerOutputter::CompilerOutputter instead.
  93. */
  94. static CompilerOutputter *defaultOutputter( TestResultCollector *result,
  95. OStream &stream );
  96. void write();
  97. void setNoWrap();
  98. void setWrapColumn( int wrapColumn );
  99. int wrapColumn() const;
  100. virtual void printSuccess();
  101. virtual void printFailureReport();
  102. virtual void printFailuresList();
  103. virtual void printStatistics();
  104. virtual void printFailureDetail( TestFailure *failure );
  105. virtual void printFailureLocation( SourceLine sourceLine );
  106. virtual void printFailureType( TestFailure *failure );
  107. virtual void printFailedTestName( TestFailure *failure );
  108. virtual void printFailureMessage( TestFailure *failure );
  109. private:
  110. /// Prevents the use of the copy constructor.
  111. CompilerOutputter( const CompilerOutputter &copy );
  112. /// Prevents the use of the copy operator.
  113. void operator =( const CompilerOutputter &copy );
  114. virtual bool processLocationFormatCommand( char command,
  115. const SourceLine &sourceLine );
  116. virtual std::string extractBaseName( const std::string &fileName ) const;
  117. private:
  118. TestResultCollector *m_result;
  119. OStream &m_stream;
  120. std::string m_locationFormat;
  121. int m_wrapColumn;
  122. };
  123. CPPUNIT_NS_END
  124. #endif // CPPUNIT_COMPILERTESTRESULTOUTPUTTER_H