mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 12:55:40 +02:00
Ability to register line# for testing
Factored file/ line storage and formatting into common class. Used in a static registry so failure messages can be asserted to have the file/ line in.
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "catch_self_test.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
ATTRIBUTE_NORETURN
|
||||
@@ -114,9 +116,32 @@ TEST_CASE( "./failing/exceptions/in-section", "Exceptions thrown from sections r
|
||||
{
|
||||
SECTION( "the section", "" )
|
||||
{
|
||||
SECTION( "the section2", "" )
|
||||
CATCH_REGISTER_LINE_INFO( "the section2" ) SECTION( "the section2", "" )
|
||||
{
|
||||
throw std::domain_error( "Exception from section" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE( "./succeeding/exceptions/error messages", "The error messages produced by exceptions caught by Catch matched the expected form" )
|
||||
{
|
||||
Catch::EmbeddedRunner runner;
|
||||
|
||||
SECTION( "custom, unexpected", "" )
|
||||
{
|
||||
runner.runMatching( "./failing/exceptions/custom" );
|
||||
INFO( runner.getOutput() );
|
||||
CHECK( runner.getOutput().find( "Unexpected exception" ) != std::string::npos );
|
||||
CHECK( runner.getOutput().find( "custom exception" ) != std::string::npos );
|
||||
}
|
||||
|
||||
SECTION( "in section", "" )
|
||||
{
|
||||
runner.runMatching( "./failing/exceptions/in-section" );
|
||||
INFO( runner.getOutput() );
|
||||
CHECK( runner.getOutput().find( "Unexpected exception" ) != std::string::npos );
|
||||
CHECK( runner.getOutput().find( "Exception from section" ) != std::string::npos );
|
||||
CHECK( runner.getOutput().find( CATCH_GET_LINE_INFO( "the section2" ) ) != std::string::npos );
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -10,10 +10,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include "catch.hpp"
|
||||
|
||||
|
||||
#include "catch_self_test.hpp"
|
||||
|
||||
TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" )
|
||||
@@ -47,7 +43,7 @@ TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results"
|
||||
"Number of 'succeeding' tests is fixed" )
|
||||
{
|
||||
runner.runMatching( "./succeeding/*" );
|
||||
CHECK( runner.getSuccessCount() == 262 );
|
||||
CHECK( runner.getSuccessCount() == 267 );
|
||||
CHECK( runner.getFailureCount() == 0 );
|
||||
}
|
||||
|
||||
|
@@ -16,7 +16,6 @@
|
||||
|
||||
namespace Catch
|
||||
{
|
||||
|
||||
class EmbeddedRunner
|
||||
{
|
||||
public:
|
||||
@@ -26,29 +25,8 @@ namespace Catch
|
||||
{
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
std::size_t runMatching
|
||||
(
|
||||
const std::string& rawTestSpec
|
||||
)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
Config config;
|
||||
config.setStreamBuf( oss.rdbuf() );
|
||||
config.setReporter( "basic" );
|
||||
|
||||
std::size_t result;
|
||||
|
||||
// Scoped because Runner doesn't report EndTesting until its destructor
|
||||
{
|
||||
Runner runner( config );
|
||||
result = runner.runMatching( rawTestSpec );
|
||||
m_successes = runner.getSuccessCount();
|
||||
m_failures = runner.getFailureCount();
|
||||
}
|
||||
m_output = oss.str();
|
||||
return result;
|
||||
}
|
||||
( const std::string& rawTestSpec );
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
std::string getOutput
|
||||
@@ -148,6 +126,54 @@ namespace Catch
|
||||
Expected::Result m_expectedResult;
|
||||
};
|
||||
|
||||
|
||||
struct LineInfoRegistry
|
||||
{
|
||||
static LineInfoRegistry& get
|
||||
()
|
||||
{
|
||||
static LineInfoRegistry s_instance;
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
void registerLineInfo
|
||||
(
|
||||
const std::string& name,
|
||||
const SourceLineInfo& info
|
||||
)
|
||||
{
|
||||
m_registry.insert( std::make_pair( name, info ) );
|
||||
}
|
||||
|
||||
const SourceLineInfo* find( const std::string& name ) const
|
||||
{
|
||||
std::map<std::string, SourceLineInfo>::const_iterator it = m_registry.find( name );
|
||||
return it == m_registry.end() ? NULL : &(it->second);
|
||||
}
|
||||
|
||||
const std::string infoForName( const std::string& name ) const
|
||||
{
|
||||
std::map<std::string, SourceLineInfo>::const_iterator it = m_registry.find( name );
|
||||
if( it == m_registry.end() )
|
||||
return "";
|
||||
std::ostringstream oss;
|
||||
oss << it->second;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
std::map<std::string, SourceLineInfo> m_registry;
|
||||
};
|
||||
|
||||
struct LineInfoRegistrar
|
||||
{
|
||||
LineInfoRegistrar( const char* name, const SourceLineInfo& lineInfo )
|
||||
{
|
||||
LineInfoRegistry::get().registerLineInfo( name, lineInfo );
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#define CATCH_REGISTER_LINE_INFO( name ) ::Catch::LineInfoRegistrar INTERNAL_CATCH_UNIQUE_NAME( lineRegistrar )( name, ::Catch::SourceLineInfo( __FILE__, __LINE__ ) );
|
||||
#define CATCH_GET_LINE_INFO( name ) ::Catch::LineInfoRegistry::get().infoForName( name )
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_SELF_TEST_HPP_INCLUDED
|
||||
|
Reference in New Issue
Block a user