Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Cumulative reporter base can be customized to not expand assertions

Commit
d02ea5adeecb094966ca09f80e027c9c3c11f502
Author
Martin Hořeňovský ·
Parents
9b4e69333f86

Files · Download .tar.gz

Changes

 src/catch2/reporters/catch_reporter_cumulative_base.cpp | 12 ++++++++++--
 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;