mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-26 07:16:10 +01:00
Reformatting
This commit is contained in:
parent
0afa09f7c1
commit
ee18b8c507
@ -1,13 +1,9 @@
|
||||
/*
|
||||
* catch.hpp
|
||||
* Catch
|
||||
*
|
||||
* Created by Phil on 22/10/2010.
|
||||
* Copyright 2010 Two Blue Cubes Ltd
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef TWOBLUECUBES_CATCH_HPP_INCLUDED
|
||||
|
@ -1,15 +1,10 @@
|
||||
/*
|
||||
* catch_runner.hpp
|
||||
* Catch
|
||||
*
|
||||
* Created by Phil on 31/10/2010.
|
||||
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED
|
||||
|
||||
@ -25,25 +20,19 @@
|
||||
#include <stdlib.h>
|
||||
#include <limits>
|
||||
|
||||
namespace Catch
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
inline int Main
|
||||
(
|
||||
Config& config
|
||||
)
|
||||
{
|
||||
namespace Catch {
|
||||
|
||||
inline int Main( Config& config ) {
|
||||
|
||||
// Handle list request
|
||||
if( config.listWhat() != Config::List::None )
|
||||
return List( config );
|
||||
|
||||
// Open output file, if specified
|
||||
std::ofstream ofs;
|
||||
if( !config.getFilename().empty() )
|
||||
{
|
||||
if( !config.getFilename().empty() ) {
|
||||
ofs.open( config.getFilename().c_str() );
|
||||
if( ofs.fail() )
|
||||
{
|
||||
if( ofs.fail() ) {
|
||||
std::cerr << "Unable to open file: '" << config.getFilename() << "'" << std::endl;
|
||||
return (std::numeric_limits<int>::max)();
|
||||
}
|
||||
@ -53,43 +42,32 @@ namespace Catch
|
||||
Runner runner( config );
|
||||
|
||||
// Run test specs specified on the command line - or default to all
|
||||
if( !config.testsSpecified() )
|
||||
{
|
||||
if( !config.testsSpecified() ) {
|
||||
config.getReporter()->StartGroup( "" );
|
||||
runner.runAll();
|
||||
config.getReporter()->EndGroup( "", runner.getTotals() );
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
// !TBD We should get all the testcases upfront, report any missing,
|
||||
// then just run them
|
||||
std::vector<std::string>::const_iterator it = config.getTestSpecs().begin();
|
||||
std::vector<std::string>::const_iterator itEnd = config.getTestSpecs().end();
|
||||
for(; it != itEnd; ++it )
|
||||
{
|
||||
for(; it != itEnd; ++it ) {
|
||||
Totals prevTotals = runner.getTotals();
|
||||
config.getReporter()->StartGroup( *it );
|
||||
if( runner.runMatching( *it ) == 0 )
|
||||
{
|
||||
if( runner.runMatching( *it ) == 0 ) {
|
||||
// Use reporter?
|
||||
// std::cerr << "\n[Unable to match any test cases with: " << *it << "]" << std::endl;
|
||||
}
|
||||
config.getReporter()->EndGroup( *it, runner.getTotals() - prevTotals );
|
||||
}
|
||||
}
|
||||
|
||||
return static_cast<int>( runner.getTotals().assertions.failed );
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
inline void showHelp
|
||||
(
|
||||
std::string exeName
|
||||
)
|
||||
{
|
||||
inline void showHelp( std::string exeName ) {
|
||||
std::string::size_type pos = exeName.find_last_of( "/\\" );
|
||||
if( pos != std::string::npos )
|
||||
{
|
||||
if( pos != std::string::npos ) {
|
||||
exeName = exeName.substr( pos+1 );
|
||||
}
|
||||
|
||||
@ -104,25 +82,16 @@ namespace Catch
|
||||
<< "For more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line" << std::endl;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
inline int Main
|
||||
(
|
||||
int argc,
|
||||
char* const argv[],
|
||||
Config& config
|
||||
)
|
||||
{
|
||||
inline int Main( int argc, char* const argv[], Config& config ) {
|
||||
ArgParser( argc, argv, config );
|
||||
|
||||
if( !config.getMessage().empty() )
|
||||
{
|
||||
if( !config.getMessage().empty() ) {
|
||||
std::cerr << config.getMessage() << std::endl;
|
||||
return (std::numeric_limits<int>::max)();
|
||||
}
|
||||
|
||||
// Handle help
|
||||
if( config.showHelp() )
|
||||
{
|
||||
if( config.showHelp() ) {
|
||||
showHelp( argv[0] );
|
||||
return 0;
|
||||
}
|
||||
@ -130,13 +99,7 @@ namespace Catch
|
||||
return Main( config );
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
inline int Main
|
||||
(
|
||||
int argc,
|
||||
char* const argv[]
|
||||
)
|
||||
{
|
||||
inline int Main( int argc, char* const argv[] ) {
|
||||
Config config;
|
||||
// if( isDebuggerActive() )
|
||||
// config.useStream( "debug" );
|
||||
|
@ -1,13 +1,9 @@
|
||||
/*
|
||||
* catch_with_main.hpp
|
||||
* Catch
|
||||
*
|
||||
* Created by Phil on 01/11/2010.
|
||||
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*
|
||||
*/
|
||||
#ifndef TWOBLUECUBES_CATCH_WITH_MAIN_HPP_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_WITH_MAIN_HPP_INCLUDED
|
||||
|
@ -1,55 +1,44 @@
|
||||
/*
|
||||
* main.cpp
|
||||
* Catch - Test
|
||||
*
|
||||
* Created by Phil on 22/10/2010.
|
||||
* Copyright 2010 Two Blue Cubes Ltd
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*
|
||||
*/
|
||||
|
||||
#include "catch_self_test.hpp"
|
||||
|
||||
TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" )
|
||||
{
|
||||
TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" ) {
|
||||
using namespace Catch;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
SECTION( "selftest/expected result",
|
||||
"Tests do what they claim" )
|
||||
{
|
||||
"Tests do what they claim" ) {
|
||||
|
||||
SECTION( "selftest/expected result/failing tests",
|
||||
"Tests in the 'failing' branch fail" )
|
||||
{
|
||||
"Tests in the 'failing' branch fail" ) {
|
||||
MetaTestRunner::runMatching( "./failing/*", MetaTestRunner::Expected::ToFail );
|
||||
}
|
||||
|
||||
SECTION( "selftest/expected result/succeeding tests",
|
||||
"Tests in the 'succeeding' branch succeed" )
|
||||
{
|
||||
"Tests in the 'succeeding' branch succeed" ) {
|
||||
MetaTestRunner::runMatching( "./succeeding/*", MetaTestRunner::Expected::ToSucceed );
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
SECTION( "selftest/test counts",
|
||||
"Number of test cases that run is fixed" )
|
||||
{
|
||||
"Number of test cases that run is fixed" ) {
|
||||
EmbeddedRunner runner;
|
||||
|
||||
SECTION( "selftest/test counts/succeeding tests",
|
||||
"Number of 'succeeding' tests is fixed" )
|
||||
{
|
||||
"Number of 'succeeding' tests is fixed" ) {
|
||||
runner.runMatching( "./succeeding/*" );
|
||||
CHECK( runner.getTotals().assertions.passed == 276 );
|
||||
CHECK( runner.getTotals().assertions.failed == 0 );
|
||||
}
|
||||
|
||||
SECTION( "selftest/test counts/failing tests",
|
||||
"Number of 'failing' tests is fixed" )
|
||||
{
|
||||
"Number of 'failing' tests is fixed" ) {
|
||||
runner.runMatching( "./failing/*" );
|
||||
CHECK( runner.getTotals().assertions.passed == 0 );
|
||||
CHECK( runner.getTotals().assertions.failed == 71 );
|
||||
@ -57,12 +46,10 @@ TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results"
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE( "meta/Misc/Sections", "looped tests" )
|
||||
{
|
||||
TEST_CASE( "meta/Misc/Sections", "looped tests" ) {
|
||||
Catch::EmbeddedRunner runner;
|
||||
|
||||
runner.runMatching( "./mixed/Misc/Sections/nested2" );
|
||||
CHECK( runner.getTotals().assertions.passed == 2 );
|
||||
CHECK( runner.getTotals().assertions.failed == 1 );
|
||||
|
||||
}
|
||||
|
@ -15,10 +15,7 @@
|
||||
|
||||
namespace Catch{
|
||||
|
||||
std::size_t EmbeddedRunner::runMatching(
|
||||
const std::string& rawTestSpec,
|
||||
const std::string& reporter )
|
||||
{
|
||||
std::size_t EmbeddedRunner::runMatching( const std::string& rawTestSpec, const std::string& reporter ) {
|
||||
std::ostringstream oss;
|
||||
Config config;
|
||||
config.setStreamBuf( oss.rdbuf() );
|
||||
@ -38,17 +35,11 @@ namespace Catch{
|
||||
return result;
|
||||
}
|
||||
|
||||
void MockReporter::Result
|
||||
(
|
||||
const ResultInfo& resultInfo
|
||||
)
|
||||
{
|
||||
void MockReporter::Result( const ResultInfo& resultInfo ) {
|
||||
if( resultInfo.getResultType() == ResultWas::Ok )
|
||||
return;
|
||||
|
||||
|
||||
switch( resultInfo.getResultType() )
|
||||
{
|
||||
switch( resultInfo.getResultType() ) {
|
||||
case ResultWas::Info:
|
||||
m_log << "Info";
|
||||
break;
|
||||
@ -96,10 +87,8 @@ namespace Catch{
|
||||
m_log << resultInfo.getExpandedExpression();
|
||||
}
|
||||
|
||||
void MockReporter::openLabel( const std::string& label, const std::string& arg )
|
||||
{
|
||||
if( shouldRecord( label ) )
|
||||
{
|
||||
void MockReporter::openLabel( const std::string& label, const std::string& arg ) {
|
||||
if( shouldRecord( label ) ) {
|
||||
m_log << m_indent << "\\" << label;
|
||||
if( !arg.empty() )
|
||||
m_log << " " << arg;
|
||||
@ -108,10 +97,8 @@ namespace Catch{
|
||||
}
|
||||
}
|
||||
|
||||
void MockReporter::closeLabel( const std::string& label, const std::string& arg )
|
||||
{
|
||||
if( shouldRecord( label ) )
|
||||
{
|
||||
void MockReporter::closeLabel( const std::string& label, const std::string& arg ) {
|
||||
if( shouldRecord( label ) ) {
|
||||
m_indent = m_indent.substr( 0, m_indent.size()-1 );
|
||||
m_log << m_indent << "/" << label;
|
||||
if( !arg.empty() )
|
||||
|
@ -1,13 +1,9 @@
|
||||
/*
|
||||
* catch_self_test.hpp
|
||||
* Catch
|
||||
*
|
||||
* Created by Phil on 14/01/2011.
|
||||
* Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*
|
||||
*/
|
||||
#ifndef TWOBLUECUBES_CATCH_SELF_TEST_HPP_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_SELF_TEST_HPP_INCLUDED
|
||||
@ -16,8 +12,8 @@
|
||||
|
||||
#include "set"
|
||||
|
||||
namespace Catch
|
||||
{
|
||||
namespace Catch {
|
||||
|
||||
class MockReporter : public SharedImpl<IReporter> {
|
||||
public:
|
||||
|
||||
@ -105,44 +101,27 @@ namespace Catch
|
||||
std::set<std::string> m_recorders;
|
||||
};
|
||||
|
||||
class EmbeddedRunner
|
||||
{
|
||||
class EmbeddedRunner {
|
||||
|
||||
public:
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
EmbeddedRunner
|
||||
()
|
||||
: m_reporter( new MockReporter() )
|
||||
{
|
||||
}
|
||||
EmbeddedRunner() : m_reporter( new MockReporter() ) {}
|
||||
|
||||
std::size_t runMatching
|
||||
( const std::string& rawTestSpec,
|
||||
const std::string& reporter = "basic" );
|
||||
std::size_t runMatching( const std::string& rawTestSpec,
|
||||
const std::string& reporter = "basic" );
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
std::string getOutput
|
||||
()
|
||||
{
|
||||
std::string getOutput() {
|
||||
return m_output;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
const Totals& getTotals
|
||||
()
|
||||
const
|
||||
{
|
||||
const Totals& getTotals() const {
|
||||
return m_totals;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
void addRecorder( const std::string& recorder )
|
||||
{
|
||||
void addRecorder( const std::string& recorder ) {
|
||||
m_reporter->addRecorder( recorder );
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
std::string getLog() const
|
||||
{
|
||||
std::string getLog() const {
|
||||
return m_reporter->getLog();
|
||||
}
|
||||
|
||||
@ -152,52 +131,29 @@ namespace Catch
|
||||
Ptr<MockReporter> m_reporter;
|
||||
};
|
||||
|
||||
class MetaTestRunner
|
||||
{
|
||||
class MetaTestRunner {
|
||||
|
||||
public:
|
||||
struct Expected
|
||||
{
|
||||
enum Result
|
||||
{
|
||||
ToSucceed,
|
||||
ToFail
|
||||
};
|
||||
};
|
||||
struct Expected { enum Result {
|
||||
ToSucceed,
|
||||
ToFail
|
||||
}; };
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
MetaTestRunner
|
||||
(
|
||||
Expected::Result expectedResult
|
||||
)
|
||||
: m_expectedResult( expectedResult )
|
||||
{
|
||||
}
|
||||
MetaTestRunner( Expected::Result expectedResult ) : m_expectedResult( expectedResult ) {}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
static void runMatching
|
||||
(
|
||||
const std::string& testSpec,
|
||||
Expected::Result expectedResult
|
||||
)
|
||||
{
|
||||
static void runMatching( const std::string& testSpec,
|
||||
Expected::Result expectedResult ) {
|
||||
forEach( Context::getTestCaseRegistry().getMatchingTestCases( testSpec ),
|
||||
MetaTestRunner( expectedResult ) );
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
void operator()
|
||||
(
|
||||
const TestCaseInfo& testCase
|
||||
)
|
||||
{
|
||||
void operator()( const TestCaseInfo& testCase ) {
|
||||
EmbeddedRunner runner;
|
||||
runner.runMatching( testCase.getName() );
|
||||
Totals totals = runner.getTotals();
|
||||
switch( m_expectedResult )
|
||||
{
|
||||
switch( m_expectedResult ) {
|
||||
case Expected::ToSucceed:
|
||||
if( totals.assertions.failed > 0 )
|
||||
{
|
||||
if( totals.assertions.failed > 0 ) {
|
||||
INFO( runner.getOutput() );
|
||||
FAIL( "Expected test case '"
|
||||
<< testCase.getName()
|
||||
@ -206,8 +162,7 @@ namespace Catch
|
||||
}
|
||||
break;
|
||||
case Expected::ToFail:
|
||||
if( totals.assertions.passed > 0 )
|
||||
{
|
||||
if( totals.assertions.passed > 0 ) {
|
||||
INFO( runner.getOutput() );
|
||||
FAIL( "Expected test case '"
|
||||
<< testCase.getName()
|
||||
@ -223,32 +178,24 @@ namespace Catch
|
||||
};
|
||||
|
||||
|
||||
struct LineInfoRegistry
|
||||
{
|
||||
static LineInfoRegistry& get
|
||||
()
|
||||
{
|
||||
struct LineInfoRegistry {
|
||||
|
||||
static LineInfoRegistry& get() {
|
||||
static LineInfoRegistry s_instance;
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
void registerLineInfo
|
||||
(
|
||||
const std::string& name,
|
||||
const SourceLineInfo& info
|
||||
)
|
||||
{
|
||||
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
|
||||
{
|
||||
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
|
||||
{
|
||||
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 "";
|
||||
@ -260,10 +207,8 @@ namespace Catch
|
||||
std::map<std::string, SourceLineInfo> m_registry;
|
||||
};
|
||||
|
||||
struct LineInfoRegistrar
|
||||
{
|
||||
LineInfoRegistrar( const char* name, const SourceLineInfo& lineInfo )
|
||||
{
|
||||
struct LineInfoRegistrar {
|
||||
LineInfoRegistrar( const char* name, const SourceLineInfo& lineInfo ) {
|
||||
LineInfoRegistry::get().registerLineInfo( name, lineInfo );
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user