catch_reporter_tap.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Created by Colton Wolkins on 2015-08-15.
  3. * Copyright 2015 Martin Moene. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED
  9. #define TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED
  10. // Don't #include any Catch headers here - we can assume they are already
  11. // included before this header.
  12. // This is not good practice in general but is necessary in this case so this
  13. // file can be distributed as a single header that works with the main
  14. // Catch single header.
  15. #include <algorithm>
  16. namespace Catch {
  17. struct TAPReporter : StreamingReporterBase<TAPReporter> {
  18. using StreamingReporterBase::StreamingReporterBase;
  19. ~TAPReporter() override;
  20. static std::string getDescription() {
  21. return "Reports test results in TAP format, suitable for test harnesses";
  22. }
  23. ReporterPreferences getPreferences() const override {
  24. return m_reporterPrefs;
  25. }
  26. void noMatchingTestCases( std::string const& spec ) override {
  27. stream << "# No test cases matched '" << spec << "'" << std::endl;
  28. }
  29. void assertionStarting( AssertionInfo const& ) override {}
  30. bool assertionEnded( AssertionStats const& _assertionStats ) override {
  31. ++counter;
  32. stream << "# " << currentTestCaseInfo->name << std::endl;
  33. AssertionPrinter printer( stream, _assertionStats, counter );
  34. printer.print();
  35. stream << std::endl;
  36. return true;
  37. }
  38. void testRunEnded( TestRunStats const& _testRunStats ) override {
  39. printTotals( _testRunStats.totals );
  40. stream << "\n" << std::endl;
  41. StreamingReporterBase::testRunEnded( _testRunStats );
  42. }
  43. private:
  44. std::size_t counter = 0;
  45. class AssertionPrinter {
  46. public:
  47. AssertionPrinter& operator= ( AssertionPrinter const& ) = delete;
  48. AssertionPrinter( AssertionPrinter const& ) = delete;
  49. AssertionPrinter( std::ostream& _stream, AssertionStats const& _stats, std::size_t _counter )
  50. : stream( _stream )
  51. , result( _stats.assertionResult )
  52. , messages( _stats.infoMessages )
  53. , itMessage( _stats.infoMessages.begin() )
  54. , printInfoMessages( true )
  55. , counter(_counter)
  56. {}
  57. void print() {
  58. itMessage = messages.begin();
  59. switch( result.getResultType() ) {
  60. case ResultWas::Ok:
  61. printResultType( passedString() );
  62. printOriginalExpression();
  63. printReconstructedExpression();
  64. if ( ! result.hasExpression() )
  65. printRemainingMessages( Colour::None );
  66. else
  67. printRemainingMessages();
  68. break;
  69. case ResultWas::ExpressionFailed:
  70. if (result.isOk()) {
  71. printResultType(passedString());
  72. } else {
  73. printResultType(failedString());
  74. }
  75. printOriginalExpression();
  76. printReconstructedExpression();
  77. if (result.isOk()) {
  78. printIssue(" # TODO");
  79. }
  80. printRemainingMessages();
  81. break;
  82. case ResultWas::ThrewException:
  83. printResultType( failedString() );
  84. printIssue( "unexpected exception with message:" );
  85. printMessage();
  86. printExpressionWas();
  87. printRemainingMessages();
  88. break;
  89. case ResultWas::FatalErrorCondition:
  90. printResultType( failedString() );
  91. printIssue( "fatal error condition with message:" );
  92. printMessage();
  93. printExpressionWas();
  94. printRemainingMessages();
  95. break;
  96. case ResultWas::DidntThrowException:
  97. printResultType( failedString() );
  98. printIssue( "expected exception, got none" );
  99. printExpressionWas();
  100. printRemainingMessages();
  101. break;
  102. case ResultWas::Info:
  103. printResultType( "info" );
  104. printMessage();
  105. printRemainingMessages();
  106. break;
  107. case ResultWas::Warning:
  108. printResultType( "warning" );
  109. printMessage();
  110. printRemainingMessages();
  111. break;
  112. case ResultWas::ExplicitFailure:
  113. printResultType( failedString() );
  114. printIssue( "explicitly" );
  115. printRemainingMessages( Colour::None );
  116. break;
  117. // These cases are here to prevent compiler warnings
  118. case ResultWas::Unknown:
  119. case ResultWas::FailureBit:
  120. case ResultWas::Exception:
  121. printResultType( "** internal error **" );
  122. break;
  123. }
  124. }
  125. private:
  126. static Colour::Code dimColour() { return Colour::FileName; }
  127. static const char* failedString() { return "not ok"; }
  128. static const char* passedString() { return "ok"; }
  129. void printSourceInfo() const {
  130. Colour colourGuard( dimColour() );
  131. stream << result.getSourceInfo() << ":";
  132. }
  133. void printResultType( std::string const& passOrFail ) const {
  134. if( !passOrFail.empty() ) {
  135. stream << passOrFail << ' ' << counter << " -";
  136. }
  137. }
  138. void printIssue( std::string const& issue ) const {
  139. stream << " " << issue;
  140. }
  141. void printExpressionWas() {
  142. if( result.hasExpression() ) {
  143. stream << ";";
  144. {
  145. Colour colour( dimColour() );
  146. stream << " expression was:";
  147. }
  148. printOriginalExpression();
  149. }
  150. }
  151. void printOriginalExpression() const {
  152. if( result.hasExpression() ) {
  153. stream << " " << result.getExpression();
  154. }
  155. }
  156. void printReconstructedExpression() const {
  157. if( result.hasExpandedExpression() ) {
  158. {
  159. Colour colour( dimColour() );
  160. stream << " for: ";
  161. }
  162. std::string expr = result.getExpandedExpression();
  163. std::replace( expr.begin(), expr.end(), '\n', ' ');
  164. stream << expr;
  165. }
  166. }
  167. void printMessage() {
  168. if ( itMessage != messages.end() ) {
  169. stream << " '" << itMessage->message << "'";
  170. ++itMessage;
  171. }
  172. }
  173. void printRemainingMessages( Colour::Code colour = dimColour() ) {
  174. if (itMessage == messages.end()) {
  175. return;
  176. }
  177. // using messages.end() directly (or auto) yields compilation error:
  178. std::vector<MessageInfo>::const_iterator itEnd = messages.end();
  179. const std::size_t N = static_cast<std::size_t>( std::distance( itMessage, itEnd ) );
  180. {
  181. Colour colourGuard( colour );
  182. stream << " with " << pluralise( N, "message" ) << ":";
  183. }
  184. for(; itMessage != itEnd; ) {
  185. // If this assertion is a warning ignore any INFO messages
  186. if( printInfoMessages || itMessage->type != ResultWas::Info ) {
  187. stream << " '" << itMessage->message << "'";
  188. if ( ++itMessage != itEnd ) {
  189. Colour colourGuard( dimColour() );
  190. stream << " and";
  191. }
  192. }
  193. }
  194. }
  195. private:
  196. std::ostream& stream;
  197. AssertionResult const& result;
  198. std::vector<MessageInfo> messages;
  199. std::vector<MessageInfo>::const_iterator itMessage;
  200. bool printInfoMessages;
  201. std::size_t counter;
  202. };
  203. void printTotals( const Totals& totals ) const {
  204. if( totals.testCases.total() == 0 ) {
  205. stream << "1..0 # Skipped: No tests ran.";
  206. } else {
  207. stream << "1.." << counter;
  208. }
  209. }
  210. };
  211. #ifdef CATCH_IMPL
  212. TAPReporter::~TAPReporter() {}
  213. #endif
  214. CATCH_REGISTER_REPORTER( "tap", TAPReporter )
  215. } // end namespace Catch
  216. #endif // TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED