catch_interfaces_reporter.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Created by Phil on 31/12/2010.
  3. * Copyright 2010 Two Blue Cubes Ltd. 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_INTERFACES_REPORTER_H_INCLUDED
  9. #define TWOBLUECUBES_CATCH_INTERFACES_REPORTER_H_INCLUDED
  10. #include "catch_section_info.h"
  11. #include "catch_common.h"
  12. #include "catch_config.hpp"
  13. #include "catch_totals.h"
  14. #include "catch_test_case_info.h"
  15. #include "catch_assertionresult.h"
  16. #include "catch_message.h"
  17. #include "catch_option.hpp"
  18. #include "catch_stringref.h"
  19. #if defined(CATCH_CONFIG_ENABLE_BENCHMARKING)
  20. #include "benchmark/catch_estimate.hpp"
  21. #include "benchmark/catch_outlier_classification.hpp"
  22. #endif // CATCH_CONFIG_ENABLE_BENCHMARKING
  23. #include <string>
  24. #include <iosfwd>
  25. #include <map>
  26. #include <set>
  27. #include <memory>
  28. #include <algorithm>
  29. namespace Catch {
  30. struct ReporterConfig {
  31. explicit ReporterConfig( IConfigPtr const& _fullConfig );
  32. ReporterConfig( IConfigPtr const& _fullConfig, std::ostream& _stream );
  33. std::ostream& stream() const;
  34. IConfigPtr fullConfig() const;
  35. private:
  36. std::ostream* m_stream;
  37. IConfigPtr m_fullConfig;
  38. };
  39. struct ReporterPreferences {
  40. bool shouldRedirectStdOut = false;
  41. bool shouldReportAllAssertions = false;
  42. };
  43. template<typename T>
  44. struct LazyStat : Option<T> {
  45. LazyStat& operator=( T const& _value ) {
  46. Option<T>::operator=( _value );
  47. used = false;
  48. return *this;
  49. }
  50. void reset() {
  51. Option<T>::reset();
  52. used = false;
  53. }
  54. bool used = false;
  55. };
  56. struct TestRunInfo {
  57. TestRunInfo( std::string const& _name );
  58. std::string name;
  59. };
  60. struct GroupInfo {
  61. GroupInfo( std::string const& _name,
  62. std::size_t _groupIndex,
  63. std::size_t _groupsCount );
  64. std::string name;
  65. std::size_t groupIndex;
  66. std::size_t groupsCounts;
  67. };
  68. struct AssertionStats {
  69. AssertionStats( AssertionResult const& _assertionResult,
  70. std::vector<MessageInfo> const& _infoMessages,
  71. Totals const& _totals );
  72. AssertionStats( AssertionStats const& ) = default;
  73. AssertionStats( AssertionStats && ) = default;
  74. AssertionStats& operator = ( AssertionStats const& ) = delete;
  75. AssertionStats& operator = ( AssertionStats && ) = delete;
  76. virtual ~AssertionStats();
  77. AssertionResult assertionResult;
  78. std::vector<MessageInfo> infoMessages;
  79. Totals totals;
  80. };
  81. struct SectionStats {
  82. SectionStats( SectionInfo const& _sectionInfo,
  83. Counts const& _assertions,
  84. double _durationInSeconds,
  85. bool _missingAssertions );
  86. SectionStats( SectionStats const& ) = default;
  87. SectionStats( SectionStats && ) = default;
  88. SectionStats& operator = ( SectionStats const& ) = default;
  89. SectionStats& operator = ( SectionStats && ) = default;
  90. virtual ~SectionStats();
  91. SectionInfo sectionInfo;
  92. Counts assertions;
  93. double durationInSeconds;
  94. bool missingAssertions;
  95. };
  96. struct TestCaseStats {
  97. TestCaseStats( TestCaseInfo const& _testInfo,
  98. Totals const& _totals,
  99. std::string const& _stdOut,
  100. std::string const& _stdErr,
  101. bool _aborting );
  102. TestCaseStats( TestCaseStats const& ) = default;
  103. TestCaseStats( TestCaseStats && ) = default;
  104. TestCaseStats& operator = ( TestCaseStats const& ) = default;
  105. TestCaseStats& operator = ( TestCaseStats && ) = default;
  106. virtual ~TestCaseStats();
  107. TestCaseInfo testInfo;
  108. Totals totals;
  109. std::string stdOut;
  110. std::string stdErr;
  111. bool aborting;
  112. };
  113. struct TestGroupStats {
  114. TestGroupStats( GroupInfo const& _groupInfo,
  115. Totals const& _totals,
  116. bool _aborting );
  117. TestGroupStats( GroupInfo const& _groupInfo );
  118. TestGroupStats( TestGroupStats const& ) = default;
  119. TestGroupStats( TestGroupStats && ) = default;
  120. TestGroupStats& operator = ( TestGroupStats const& ) = default;
  121. TestGroupStats& operator = ( TestGroupStats && ) = default;
  122. virtual ~TestGroupStats();
  123. GroupInfo groupInfo;
  124. Totals totals;
  125. bool aborting;
  126. };
  127. struct TestRunStats {
  128. TestRunStats( TestRunInfo const& _runInfo,
  129. Totals const& _totals,
  130. bool _aborting );
  131. TestRunStats( TestRunStats const& ) = default;
  132. TestRunStats( TestRunStats && ) = default;
  133. TestRunStats& operator = ( TestRunStats const& ) = default;
  134. TestRunStats& operator = ( TestRunStats && ) = default;
  135. virtual ~TestRunStats();
  136. TestRunInfo runInfo;
  137. Totals totals;
  138. bool aborting;
  139. };
  140. #if defined(CATCH_CONFIG_ENABLE_BENCHMARKING)
  141. struct BenchmarkInfo {
  142. std::string name;
  143. double estimatedDuration;
  144. int iterations;
  145. int samples;
  146. unsigned int resamples;
  147. double clockResolution;
  148. double clockCost;
  149. };
  150. template <class Duration>
  151. struct BenchmarkStats {
  152. BenchmarkInfo info;
  153. std::vector<Duration> samples;
  154. Benchmark::Estimate<Duration> mean;
  155. Benchmark::Estimate<Duration> standardDeviation;
  156. Benchmark::OutlierClassification outliers;
  157. double outlierVariance;
  158. template <typename Duration2>
  159. operator BenchmarkStats<Duration2>() const {
  160. std::vector<Duration2> samples2;
  161. samples2.reserve(samples.size());
  162. std::transform(samples.begin(), samples.end(), std::back_inserter(samples2), [](Duration d) { return Duration2(d); });
  163. return {
  164. info,
  165. std::move(samples2),
  166. mean,
  167. standardDeviation,
  168. outliers,
  169. outlierVariance,
  170. };
  171. }
  172. };
  173. #endif // CATCH_CONFIG_ENABLE_BENCHMARKING
  174. struct IStreamingReporter {
  175. virtual ~IStreamingReporter() = default;
  176. // Implementing class must also provide the following static methods:
  177. // static std::string getDescription();
  178. // static std::set<Verbosity> getSupportedVerbosities()
  179. virtual ReporterPreferences getPreferences() const = 0;
  180. virtual void noMatchingTestCases( std::string const& spec ) = 0;
  181. virtual void reportInvalidArguments(std::string const&) {}
  182. virtual void testRunStarting( TestRunInfo const& testRunInfo ) = 0;
  183. virtual void testGroupStarting( GroupInfo const& groupInfo ) = 0;
  184. virtual void testCaseStarting( TestCaseInfo const& testInfo ) = 0;
  185. virtual void sectionStarting( SectionInfo const& sectionInfo ) = 0;
  186. #if defined(CATCH_CONFIG_ENABLE_BENCHMARKING)
  187. virtual void benchmarkPreparing( std::string const& ) {}
  188. virtual void benchmarkStarting( BenchmarkInfo const& ) {}
  189. virtual void benchmarkEnded( BenchmarkStats<> const& ) {}
  190. virtual void benchmarkFailed( std::string const& ) {}
  191. #endif // CATCH_CONFIG_ENABLE_BENCHMARKING
  192. virtual void assertionStarting( AssertionInfo const& assertionInfo ) = 0;
  193. // The return value indicates if the messages buffer should be cleared:
  194. virtual bool assertionEnded( AssertionStats const& assertionStats ) = 0;
  195. virtual void sectionEnded( SectionStats const& sectionStats ) = 0;
  196. virtual void testCaseEnded( TestCaseStats const& testCaseStats ) = 0;
  197. virtual void testGroupEnded( TestGroupStats const& testGroupStats ) = 0;
  198. virtual void testRunEnded( TestRunStats const& testRunStats ) = 0;
  199. virtual void skipTest( TestCaseInfo const& testInfo ) = 0;
  200. // Default empty implementation provided
  201. virtual void fatalErrorEncountered( StringRef name );
  202. virtual bool isMulti() const;
  203. };
  204. using IStreamingReporterPtr = std::unique_ptr<IStreamingReporter>;
  205. struct IReporterFactory {
  206. virtual ~IReporterFactory();
  207. virtual IStreamingReporterPtr create( ReporterConfig const& config ) const = 0;
  208. virtual std::string getDescription() const = 0;
  209. };
  210. using IReporterFactoryPtr = std::shared_ptr<IReporterFactory>;
  211. struct IReporterRegistry {
  212. using FactoryMap = std::map<std::string, IReporterFactoryPtr>;
  213. using Listeners = std::vector<IReporterFactoryPtr>;
  214. virtual ~IReporterRegistry();
  215. virtual IStreamingReporterPtr create( std::string const& name, IConfigPtr const& config ) const = 0;
  216. virtual FactoryMap const& getFactories() const = 0;
  217. virtual Listeners const& getListeners() const = 0;
  218. };
  219. } // end namespace Catch
  220. #endif // TWOBLUECUBES_CATCH_INTERFACES_REPORTER_H_INCLUDED