From 02e597c2ccff546d0e4739263fa3aceee4f0125a Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Thu, 21 Apr 2011 19:43:55 +0100 Subject: [PATCH] report file/ line of section of unexpected exception is thrown --- Test/ExceptionTests.cpp | 11 +++++++++++ internal/catch_interfaces_capture.h | 2 ++ internal/catch_runner_impl.hpp | 14 ++++++++++---- internal/catch_section.hpp | 10 +++++++--- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/Test/ExceptionTests.cpp b/Test/ExceptionTests.cpp index 1d45200e..ea783979 100644 --- a/Test/ExceptionTests.cpp +++ b/Test/ExceptionTests.cpp @@ -109,3 +109,14 @@ TEST_CASE_NORETURN( "./failing/exceptions/custom/double", "Unexpected custom exc { throw double( 3.14 ); } + +TEST_CASE( "./failing/exceptions/in-section", "Exceptions thrown from sections report file/ line or section" ) +{ + SECTION( "the section", "" ) + { + SECTION( "the section2", "" ) + { + throw std::domain_error( "Exception from section" ); + } + } +} diff --git a/internal/catch_interfaces_capture.h b/internal/catch_interfaces_capture.h index 82d8d2d6..f3a530d6 100644 --- a/internal/catch_interfaces_capture.h +++ b/internal/catch_interfaces_capture.h @@ -33,6 +33,8 @@ namespace Catch virtual bool sectionStarted ( const std::string& name, const std::string& description, + const std::string& filename, + std::size_t line, std::size_t& successes, std::size_t& failures ) = 0; diff --git a/internal/catch_runner_impl.hpp b/internal/catch_runner_impl.hpp index 4eb4d71d..85df0d6f 100644 --- a/internal/catch_runner_impl.hpp +++ b/internal/catch_runner_impl.hpp @@ -360,6 +360,8 @@ namespace Catch { do { + m_currentResult.setFileAndLine( m_runningTest->getTestCaseInfo().getFilename(), + m_runningTest->getTestCaseInfo().getLine() ); runCurrentTest( redirectedCout, redirectedCerr ); } while( m_runningTest->hasUntestedSections() ); @@ -459,14 +461,20 @@ namespace Catch virtual bool sectionStarted ( const std::string& name, - const std::string& description, + const std::string& description, + const std::string& filename, + std::size_t line, std::size_t& successes, std::size_t& failures ) { - if( !m_runningTest->addSection( name ) ) + std::ostringstream oss; + oss << filename << ":" << line; + + if( !m_runningTest->addSection( oss.str() ) ) return false; + m_currentResult.setFileAndLine( filename, line ); m_reporter->StartSection( name, description ); successes = m_successes; failures = m_failures; @@ -553,8 +561,6 @@ namespace Catch m_runningTest->reset(); StreamRedirect coutRedir( std::cout, redirectedCout ); StreamRedirect cerrRedir( std::cerr, redirectedCerr ); - m_currentResult.setFileAndLine( m_runningTest->getTestCaseInfo().getFilename(), - m_runningTest->getTestCaseInfo().getLine() ); m_runningTest->getTestCaseInfo().invoke(); m_runningTest->ranToCompletion(); } diff --git a/internal/catch_section.hpp b/internal/catch_section.hpp index 6a0b8032..084aa21d 100644 --- a/internal/catch_section.hpp +++ b/internal/catch_section.hpp @@ -26,10 +26,12 @@ namespace Catch Section ( const std::string& name, - const std::string& description + const std::string& description, + const std::string& filename, + std::size_t line ) : m_name( name ), - m_sectionIncluded( Hub::getResultCapture().sectionStarted( name, description, m_successes, m_failures ) ) + m_sectionIncluded( Hub::getResultCapture().sectionStarted( name, description, filename, line, m_successes, m_failures ) ) { } @@ -50,6 +52,7 @@ namespace Catch } private: + std::string m_name; std::size_t m_successes; std::size_t m_failures; @@ -58,6 +61,7 @@ namespace Catch } // end namespace Catch -#define INTERNAL_CATCH_SECTION( name, desc ) if( Catch::Section INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::Section( name, desc ) ) +#define INTERNAL_CATCH_SECTION( name, desc ) \ + if( Catch::Section INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::Section( name, desc, __FILE__, __LINE__ ) ) #endif // TWOBLUECUBES_CATCH_SECTION_HPP_INCLUDED