diff options
| author | Martin Hořeňovský | 2021-11-13 23:53:09 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2021-11-14 11:27:44 +0100 |
| commit | d02ea5adeecb094966ca09f80e027c9c3c11f502 (patch) | |
| tree | db3a3796771248b16509bc0252ac5f32cc287f8a | |
| parent | 9b4e69333f863dc31e124f658a3dedbe8d27632a (diff) | |
| download | catch3-d02ea5adeecb094966ca09f80e027c9c3c11f502.tar.gz | |
Cumulative reporter base can be customized to not expand assertions
| -rw-r--r-- | src/catch2/reporters/catch_reporter_cumulative_base.cpp | 12 | ||||
| -rw-r--r-- | src/catch2/reporters/catch_reporter_cumulative_base.hpp | 16 |
2 files changed, 26 insertions, 2 deletions
diff --git a/src/catch2/reporters/catch_reporter_cumulative_base.cpp b/src/catch2/reporters/catch_reporter_cumulative_base.cpp index b8ebcad0..85e9c156 100644 --- a/src/catch2/reporters/catch_reporter_cumulative_base.cpp +++ b/src/catch2/reporters/catch_reporter_cumulative_base.cpp @@ -104,8 +104,16 @@ namespace Catch { // Our section stack copy of the assertionResult will likely outlive the // temporary, so it must be expanded or discarded now to avoid calling // a destroyed object later. - static_cast<void>( - assertionStats.assertionResult.getExpandedExpression() ); + if ( m_shouldStoreFailedAssertions && + !assertionStats.assertionResult.isOk() ) { + static_cast<void>( + assertionStats.assertionResult.getExpandedExpression() ); + } + if ( m_shouldStoreSuccesfulAssertions && + assertionStats.assertionResult.isOk() ) { + static_cast<void>( + assertionStats.assertionResult.getExpandedExpression() ); + } SectionNode& sectionNode = *m_sectionStack.back(); sectionNode.assertionsAndBenchmarks.emplace_back( assertionStats ); } diff --git a/src/catch2/reporters/catch_reporter_cumulative_base.hpp b/src/catch2/reporters/catch_reporter_cumulative_base.hpp index 2db14b09..55abd1e1 100644 --- a/src/catch2/reporters/catch_reporter_cumulative_base.hpp +++ b/src/catch2/reporters/catch_reporter_cumulative_base.hpp @@ -49,6 +49,15 @@ namespace Catch { * If you are deriving from this class and override any testing related * member functions, you should first call into the base's implementation to * avoid breaking the tree construction. + * + * Due to the way this base functions, it has to expand assertions up-front, + * even if they are later unused (e.g. because the deriving reporter does + * not report successful assertions, or because the deriving reporter does + * not use assertion expansion at all). Derived classes can use two + * customization points, `m_shouldStoreSuccesfulAssertions` and + * `m_shouldStoreFailedAssertions`, to disable the expansion and gain extra + * performance. **Accessing the assertion expansions if it wasn't stored is + * UB.** */ struct CumulativeReporterBase : IStreamingReporter { template<typename T, typename ChildNodeT> @@ -116,7 +125,14 @@ namespace Catch { void listTags( std::vector<TagInfo> const& tags ) override; protected: + //! Should the cumulative base store the assertion expansion for succesful assertions? + bool m_shouldStoreSuccesfulAssertions = true; + //! Should the cumulative base store the assertion expansion for failed assertions? + bool m_shouldStoreFailedAssertions = true; + + //! Stream to write the output to std::ostream& stream; + // Note: We rely on pointer identity being stable, which is why // we store pointers to the nodes rather than the values. std::vector<Detail::unique_ptr<TestCaseNode>> m_testCases; |