Replace shared_ptrs with unique_ptrs in CumulativeReporterBase nodes

Closes #2089
This commit is contained in:
Martin Hořeňovský 2020-11-26 14:45:27 +01:00
parent bfe5553416
commit 677adf8ade
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
4 changed files with 30 additions and 26 deletions

View File

@ -17,7 +17,7 @@ namespace Catch {
BySectionInfo( BySectionInfo const& other ): BySectionInfo( BySectionInfo const& other ):
m_other( other.m_other ) {} m_other( other.m_other ) {}
bool operator()( bool operator()(
std::shared_ptr<CumulativeReporterBase::SectionNode> const& Detail::unique_ptr<CumulativeReporterBase::SectionNode> const&
node ) const { node ) const {
return ( return (
( node->stats.sectionInfo.name == m_other.name ) && ( node->stats.sectionInfo.name == m_other.name ) &&
@ -37,27 +37,30 @@ namespace Catch {
void void
CumulativeReporterBase::sectionStarting( SectionInfo const& sectionInfo ) { CumulativeReporterBase::sectionStarting( SectionInfo const& sectionInfo ) {
SectionStats incompleteStats( sectionInfo, Counts(), 0, false ); SectionStats incompleteStats( sectionInfo, Counts(), 0, false );
std::shared_ptr<SectionNode> node; SectionNode* node;
if ( m_sectionStack.empty() ) { if ( m_sectionStack.empty() ) {
if ( !m_rootSection ) if ( !m_rootSection ) {
m_rootSection = m_rootSection =
std::make_shared<SectionNode>( incompleteStats ); Detail::make_unique<SectionNode>( incompleteStats );
node = m_rootSection; }
node = m_rootSection.get();
} else { } else {
SectionNode& parentNode = *m_sectionStack.back(); SectionNode& parentNode = *m_sectionStack.back();
auto it = std::find_if( parentNode.childSections.begin(), auto it = std::find_if( parentNode.childSections.begin(),
parentNode.childSections.end(), parentNode.childSections.end(),
BySectionInfo( sectionInfo ) ); BySectionInfo( sectionInfo ) );
if ( it == parentNode.childSections.end() ) { if ( it == parentNode.childSections.end() ) {
node = std::make_shared<SectionNode>( incompleteStats ); auto newNode =
parentNode.childSections.push_back( node ); Detail::make_unique<SectionNode>( incompleteStats );
node = newNode.get();
parentNode.childSections.push_back( std::move( newNode ) );
} else { } else {
node = *it; node = it->get();
} }
} }
m_deepestSection = node.get(); m_deepestSection = node;
m_sectionStack.push_back( node.get() ); m_sectionStack.push_back( node );
} }
bool CumulativeReporterBase::assertionEnded( bool CumulativeReporterBase::assertionEnded(
@ -84,11 +87,10 @@ namespace Catch {
void CumulativeReporterBase::testCaseEnded( void CumulativeReporterBase::testCaseEnded(
TestCaseStats const& testCaseStats ) { TestCaseStats const& testCaseStats ) {
auto node = std::make_shared<TestCaseNode>( testCaseStats ); auto node = Detail::make_unique<TestCaseNode>( testCaseStats );
assert( m_sectionStack.size() == 0 ); assert( m_sectionStack.size() == 0 );
node->children.push_back( m_rootSection ); node->children.push_back( std::move(m_rootSection) );
m_testCases.push_back( node ); m_testCases.push_back( std::move(node) );
m_rootSection.reset();
assert( m_deepestSection ); assert( m_deepestSection );
m_deepestSection->stdOut = testCaseStats.stdOut; m_deepestSection->stdOut = testCaseStats.stdOut;
@ -97,9 +99,9 @@ namespace Catch {
void CumulativeReporterBase::testGroupEnded( void CumulativeReporterBase::testGroupEnded(
TestGroupStats const& testGroupStats ) { TestGroupStats const& testGroupStats ) {
auto node = std::make_shared<TestGroupNode>( testGroupStats ); auto node = Detail::make_unique<TestGroupNode>( testGroupStats );
node->children.swap( m_testCases ); node->children.swap( m_testCases );
m_testGroups.push_back( node ); m_testGroups.push_back( std::move(node) );
} }
void CumulativeReporterBase::testRunEnded( TestRunStats const& testRunStats ) { void CumulativeReporterBase::testRunEnded( TestRunStats const& testRunStats ) {

View File

@ -23,7 +23,7 @@ namespace Catch {
struct Node { struct Node {
explicit Node( T const& _value ) : value( _value ) {} explicit Node( T const& _value ) : value( _value ) {}
using ChildNodes = std::vector<std::shared_ptr<ChildNodeT>>; using ChildNodes = std::vector<Detail::unique_ptr<ChildNodeT>>;
T value; T value;
ChildNodes children; ChildNodes children;
}; };
@ -35,7 +35,7 @@ namespace Catch {
} }
SectionStats stats; SectionStats stats;
std::vector<std::shared_ptr<SectionNode>> childSections; std::vector<Detail::unique_ptr<SectionNode>> childSections;
std::vector<AssertionStats> assertions; std::vector<AssertionStats> assertions;
std::string stdOut; std::string stdOut;
std::string stdErr; std::string stdErr;
@ -73,12 +73,12 @@ namespace Catch {
std::ostream& stream; std::ostream& stream;
// Note: We rely on pointer identity being stable, which is why // Note: We rely on pointer identity being stable, which is why
// which is why we store around pointers rather than values. // which is why we store around pointers rather than values.
std::vector<std::shared_ptr<TestCaseNode>> m_testCases; std::vector<Detail::unique_ptr<TestCaseNode>> m_testCases;
std::vector<std::shared_ptr<TestGroupNode>> m_testGroups; std::vector<Detail::unique_ptr<TestGroupNode>> m_testGroups;
std::vector<TestRunNode> m_testRuns; std::vector<TestRunNode> m_testRuns;
std::shared_ptr<SectionNode> m_rootSection; Detail::unique_ptr<SectionNode> m_rootSection;
SectionNode* m_deepestSection = nullptr; SectionNode* m_deepestSection = nullptr;
std::vector<SectionNode*> m_sectionStack; std::vector<SectionNode*> m_sectionStack;
}; };

View File

@ -28,15 +28,17 @@ namespace Catch {
} }
void SonarQubeReporter::writeGroup(TestGroupNode const& groupNode) { void SonarQubeReporter::writeGroup(TestGroupNode const& groupNode) {
std::map<std::string, TestGroupNode::ChildNodes> testsPerFile; std::map<std::string, std::vector<TestCaseNode const*>> testsPerFile;
for (auto const& child : groupNode.children) for ( auto const& child : groupNode.children ) {
testsPerFile[child->value.testInfo->lineInfo.file].push_back(child); testsPerFile[child->value.testInfo->lineInfo.file].push_back(
child.get() );
}
for (auto const& kv : testsPerFile) for (auto const& kv : testsPerFile)
writeTestFile(kv.first, kv.second); writeTestFile(kv.first, kv.second);
} }
void SonarQubeReporter::writeTestFile(std::string const& filename, TestGroupNode::ChildNodes const& testCaseNodes) { void SonarQubeReporter::writeTestFile(std::string const& filename, std::vector<TestCaseNode const*> const& testCaseNodes) {
XmlWriter::ScopedElement e = xml.scopedElement("file"); XmlWriter::ScopedElement e = xml.scopedElement("file");
xml.writeAttribute("path", filename); xml.writeAttribute("path", filename);

View File

@ -42,7 +42,7 @@ namespace Catch {
void writeGroup(TestGroupNode const& groupNode); void writeGroup(TestGroupNode const& groupNode);
void writeTestFile(std::string const& filename, TestGroupNode::ChildNodes const& testCaseNodes); void writeTestFile(std::string const& filename, std::vector<TestCaseNode const*> const& testCaseNodes);
void writeTestCase(TestCaseNode const& testCaseNode); void writeTestCase(TestCaseNode const& testCaseNode);