Cumulative reporter base can be customized to not expand assertions

This commit is contained in:
Martin Hořeňovský 2021-11-13 23:53:09 +01:00
parent 9b4e69333f
commit d02ea5adee
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
2 changed files with 26 additions and 2 deletions

View File

@ -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 );
}

View File

@ -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;