Reformatting

This commit is contained in:
Phil Nash 2012-05-16 15:02:51 +01:00
parent 0afa09f7c1
commit ee18b8c507
6 changed files with 65 additions and 191 deletions

View File

@ -1,13 +1,9 @@
/* /*
* catch.hpp
* Catch
*
* Created by Phil on 22/10/2010. * Created by Phil on 22/10/2010.
* Copyright 2010 Two Blue Cubes Ltd * Copyright 2010 Two Blue Cubes Ltd
* *
* Distributed under the Boost Software License, Version 1.0. (See accompanying * 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) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/ */
#ifndef TWOBLUECUBES_CATCH_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_HPP_INCLUDED

View File

@ -1,15 +1,10 @@
/* /*
* catch_runner.hpp
* Catch
*
* Created by Phil on 31/10/2010. * Created by Phil on 31/10/2010.
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
* *
* Distributed under the Boost Software License, Version 1.0. (See accompanying * 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) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/ */
#ifndef TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED #define TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED
@ -25,25 +20,19 @@
#include <stdlib.h> #include <stdlib.h>
#include <limits> #include <limits>
namespace Catch namespace Catch {
{
////////////////////////////////////////////////////////////////////////// inline int Main( Config& config ) {
inline int Main
(
Config& config
)
{
// Handle list request // Handle list request
if( config.listWhat() != Config::List::None ) if( config.listWhat() != Config::List::None )
return List( config ); return List( config );
// Open output file, if specified // Open output file, if specified
std::ofstream ofs; std::ofstream ofs;
if( !config.getFilename().empty() ) if( !config.getFilename().empty() ) {
{
ofs.open( config.getFilename().c_str() ); ofs.open( config.getFilename().c_str() );
if( ofs.fail() ) if( ofs.fail() ) {
{
std::cerr << "Unable to open file: '" << config.getFilename() << "'" << std::endl; std::cerr << "Unable to open file: '" << config.getFilename() << "'" << std::endl;
return (std::numeric_limits<int>::max)(); return (std::numeric_limits<int>::max)();
} }
@ -53,43 +42,32 @@ namespace Catch
Runner runner( config ); Runner runner( config );
// Run test specs specified on the command line - or default to all // Run test specs specified on the command line - or default to all
if( !config.testsSpecified() ) if( !config.testsSpecified() ) {
{
config.getReporter()->StartGroup( "" ); config.getReporter()->StartGroup( "" );
runner.runAll(); runner.runAll();
config.getReporter()->EndGroup( "", runner.getTotals() ); config.getReporter()->EndGroup( "", runner.getTotals() );
} }
else else {
{
// !TBD We should get all the testcases upfront, report any missing, // !TBD We should get all the testcases upfront, report any missing,
// then just run them // then just run them
std::vector<std::string>::const_iterator it = config.getTestSpecs().begin(); std::vector<std::string>::const_iterator it = config.getTestSpecs().begin();
std::vector<std::string>::const_iterator itEnd = config.getTestSpecs().end(); std::vector<std::string>::const_iterator itEnd = config.getTestSpecs().end();
for(; it != itEnd; ++it ) for(; it != itEnd; ++it ) {
{
Totals prevTotals = runner.getTotals(); Totals prevTotals = runner.getTotals();
config.getReporter()->StartGroup( *it ); config.getReporter()->StartGroup( *it );
if( runner.runMatching( *it ) == 0 ) if( runner.runMatching( *it ) == 0 ) {
{
// Use reporter? // Use reporter?
// std::cerr << "\n[Unable to match any test cases with: " << *it << "]" << std::endl; // std::cerr << "\n[Unable to match any test cases with: " << *it << "]" << std::endl;
} }
config.getReporter()->EndGroup( *it, runner.getTotals() - prevTotals ); config.getReporter()->EndGroup( *it, runner.getTotals() - prevTotals );
} }
} }
return static_cast<int>( runner.getTotals().assertions.failed ); 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( "/\\" ); std::string::size_type pos = exeName.find_last_of( "/\\" );
if( pos != std::string::npos ) if( pos != std::string::npos ) {
{
exeName = exeName.substr( pos+1 ); 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; << "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 ); ArgParser( argc, argv, config );
if( !config.getMessage().empty() ) if( !config.getMessage().empty() ) {
{
std::cerr << config.getMessage() << std::endl; std::cerr << config.getMessage() << std::endl;
return (std::numeric_limits<int>::max)(); return (std::numeric_limits<int>::max)();
} }
// Handle help // Handle help
if( config.showHelp() ) if( config.showHelp() ) {
{
showHelp( argv[0] ); showHelp( argv[0] );
return 0; return 0;
} }
@ -130,13 +99,7 @@ namespace Catch
return Main( config ); return Main( config );
} }
////////////////////////////////////////////////////////////////////////// inline int Main( int argc, char* const argv[] ) {
inline int Main
(
int argc,
char* const argv[]
)
{
Config config; Config config;
// if( isDebuggerActive() ) // if( isDebuggerActive() )
// config.useStream( "debug" ); // config.useStream( "debug" );

View File

@ -1,13 +1,9 @@
/* /*
* catch_with_main.hpp
* Catch
*
* Created by Phil on 01/11/2010. * Created by Phil on 01/11/2010.
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
* *
* Distributed under the Boost Software License, Version 1.0. (See accompanying * 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) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/ */
#ifndef TWOBLUECUBES_CATCH_WITH_MAIN_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_WITH_MAIN_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_WITH_MAIN_HPP_INCLUDED #define TWOBLUECUBES_CATCH_WITH_MAIN_HPP_INCLUDED

View File

@ -1,55 +1,44 @@
/* /*
* main.cpp
* Catch - Test
*
* Created by Phil on 22/10/2010. * Created by Phil on 22/10/2010.
* Copyright 2010 Two Blue Cubes Ltd * Copyright 2010 Two Blue Cubes Ltd
* *
* Distributed under the Boost Software License, Version 1.0. (See accompanying * 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) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/ */
#include "catch_self_test.hpp" #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; using namespace Catch;
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
SECTION( "selftest/expected result", SECTION( "selftest/expected result",
"Tests do what they claim" ) "Tests do what they claim" ) {
{
SECTION( "selftest/expected result/failing tests", SECTION( "selftest/expected result/failing tests",
"Tests in the 'failing' branch fail" ) "Tests in the 'failing' branch fail" ) {
{
MetaTestRunner::runMatching( "./failing/*", MetaTestRunner::Expected::ToFail ); MetaTestRunner::runMatching( "./failing/*", MetaTestRunner::Expected::ToFail );
} }
SECTION( "selftest/expected result/succeeding tests", SECTION( "selftest/expected result/succeeding tests",
"Tests in the 'succeeding' branch succeed" ) "Tests in the 'succeeding' branch succeed" ) {
{
MetaTestRunner::runMatching( "./succeeding/*", MetaTestRunner::Expected::ToSucceed ); MetaTestRunner::runMatching( "./succeeding/*", MetaTestRunner::Expected::ToSucceed );
} }
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
SECTION( "selftest/test counts", SECTION( "selftest/test counts",
"Number of test cases that run is fixed" ) "Number of test cases that run is fixed" ) {
{
EmbeddedRunner runner; EmbeddedRunner runner;
SECTION( "selftest/test counts/succeeding tests", SECTION( "selftest/test counts/succeeding tests",
"Number of 'succeeding' tests is fixed" ) "Number of 'succeeding' tests is fixed" ) {
{
runner.runMatching( "./succeeding/*" ); runner.runMatching( "./succeeding/*" );
CHECK( runner.getTotals().assertions.passed == 276 ); CHECK( runner.getTotals().assertions.passed == 276 );
CHECK( runner.getTotals().assertions.failed == 0 ); CHECK( runner.getTotals().assertions.failed == 0 );
} }
SECTION( "selftest/test counts/failing tests", SECTION( "selftest/test counts/failing tests",
"Number of 'failing' tests is fixed" ) "Number of 'failing' tests is fixed" ) {
{
runner.runMatching( "./failing/*" ); runner.runMatching( "./failing/*" );
CHECK( runner.getTotals().assertions.passed == 0 ); CHECK( runner.getTotals().assertions.passed == 0 );
CHECK( runner.getTotals().assertions.failed == 71 ); 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; Catch::EmbeddedRunner runner;
runner.runMatching( "./mixed/Misc/Sections/nested2" ); runner.runMatching( "./mixed/Misc/Sections/nested2" );
CHECK( runner.getTotals().assertions.passed == 2 ); CHECK( runner.getTotals().assertions.passed == 2 );
CHECK( runner.getTotals().assertions.failed == 1 ); CHECK( runner.getTotals().assertions.failed == 1 );
} }

View File

@ -15,10 +15,7 @@
namespace Catch{ namespace Catch{
std::size_t EmbeddedRunner::runMatching( std::size_t EmbeddedRunner::runMatching( const std::string& rawTestSpec, const std::string& reporter ) {
const std::string& rawTestSpec,
const std::string& reporter )
{
std::ostringstream oss; std::ostringstream oss;
Config config; Config config;
config.setStreamBuf( oss.rdbuf() ); config.setStreamBuf( oss.rdbuf() );
@ -38,17 +35,11 @@ namespace Catch{
return result; return result;
} }
void MockReporter::Result void MockReporter::Result( const ResultInfo& resultInfo ) {
(
const ResultInfo& resultInfo
)
{
if( resultInfo.getResultType() == ResultWas::Ok ) if( resultInfo.getResultType() == ResultWas::Ok )
return; return;
switch( resultInfo.getResultType() ) {
switch( resultInfo.getResultType() )
{
case ResultWas::Info: case ResultWas::Info:
m_log << "Info"; m_log << "Info";
break; break;
@ -96,10 +87,8 @@ namespace Catch{
m_log << resultInfo.getExpandedExpression(); m_log << resultInfo.getExpandedExpression();
} }
void MockReporter::openLabel( const std::string& label, const std::string& arg ) void MockReporter::openLabel( const std::string& label, const std::string& arg ) {
{ if( shouldRecord( label ) ) {
if( shouldRecord( label ) )
{
m_log << m_indent << "\\" << label; m_log << m_indent << "\\" << label;
if( !arg.empty() ) if( !arg.empty() )
m_log << " " << arg; m_log << " " << arg;
@ -108,10 +97,8 @@ namespace Catch{
} }
} }
void MockReporter::closeLabel( const std::string& label, const std::string& arg ) void MockReporter::closeLabel( const std::string& label, const std::string& arg ) {
{ if( shouldRecord( label ) ) {
if( shouldRecord( label ) )
{
m_indent = m_indent.substr( 0, m_indent.size()-1 ); m_indent = m_indent.substr( 0, m_indent.size()-1 );
m_log << m_indent << "/" << label; m_log << m_indent << "/" << label;
if( !arg.empty() ) if( !arg.empty() )

View File

@ -1,13 +1,9 @@
/* /*
* catch_self_test.hpp
* Catch
*
* Created by Phil on 14/01/2011. * Created by Phil on 14/01/2011.
* Copyright 2011 Two Blue Cubes Ltd. All rights reserved. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
* *
* Distributed under the Boost Software License, Version 1.0. (See accompanying * 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) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/ */
#ifndef TWOBLUECUBES_CATCH_SELF_TEST_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_SELF_TEST_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_SELF_TEST_HPP_INCLUDED #define TWOBLUECUBES_CATCH_SELF_TEST_HPP_INCLUDED
@ -16,8 +12,8 @@
#include "set" #include "set"
namespace Catch namespace Catch {
{
class MockReporter : public SharedImpl<IReporter> { class MockReporter : public SharedImpl<IReporter> {
public: public:
@ -105,44 +101,27 @@ namespace Catch
std::set<std::string> m_recorders; std::set<std::string> m_recorders;
}; };
class EmbeddedRunner class EmbeddedRunner {
{
public:
///////////////////////////////////////////////////////////////////////////
EmbeddedRunner
()
: m_reporter( new MockReporter() )
{
}
std::size_t runMatching public:
( const std::string& rawTestSpec, EmbeddedRunner() : m_reporter( new MockReporter() ) {}
std::size_t runMatching( const std::string& rawTestSpec,
const std::string& reporter = "basic" ); const std::string& reporter = "basic" );
/////////////////////////////////////////////////////////////////////////// std::string getOutput() {
std::string getOutput
()
{
return m_output; return m_output;
} }
/////////////////////////////////////////////////////////////////////////// const Totals& getTotals() const {
const Totals& getTotals
()
const
{
return m_totals; return m_totals;
} }
/////////////////////////////////////////////////////////////////////////// void addRecorder( const std::string& recorder ) {
void addRecorder( const std::string& recorder )
{
m_reporter->addRecorder( recorder ); m_reporter->addRecorder( recorder );
} }
/////////////////////////////////////////////////////////////////////////// std::string getLog() const {
std::string getLog() const
{
return m_reporter->getLog(); return m_reporter->getLog();
} }
@ -152,52 +131,29 @@ namespace Catch
Ptr<MockReporter> m_reporter; Ptr<MockReporter> m_reporter;
}; };
class MetaTestRunner class MetaTestRunner {
{
public: public:
struct Expected struct Expected { enum Result {
{
enum Result
{
ToSucceed, ToSucceed,
ToFail ToFail
}; }; };
};
/////////////////////////////////////////////////////////////////////////// MetaTestRunner( Expected::Result expectedResult ) : m_expectedResult( expectedResult ) {}
MetaTestRunner
(
Expected::Result expectedResult
)
: m_expectedResult( expectedResult )
{
}
/////////////////////////////////////////////////////////////////////////// static void runMatching( const std::string& testSpec,
static void runMatching Expected::Result expectedResult ) {
(
const std::string& testSpec,
Expected::Result expectedResult
)
{
forEach( Context::getTestCaseRegistry().getMatchingTestCases( testSpec ), forEach( Context::getTestCaseRegistry().getMatchingTestCases( testSpec ),
MetaTestRunner( expectedResult ) ); MetaTestRunner( expectedResult ) );
} }
/////////////////////////////////////////////////////////////////////////// void operator()( const TestCaseInfo& testCase ) {
void operator()
(
const TestCaseInfo& testCase
)
{
EmbeddedRunner runner; EmbeddedRunner runner;
runner.runMatching( testCase.getName() ); runner.runMatching( testCase.getName() );
Totals totals = runner.getTotals(); Totals totals = runner.getTotals();
switch( m_expectedResult ) switch( m_expectedResult ) {
{
case Expected::ToSucceed: case Expected::ToSucceed:
if( totals.assertions.failed > 0 ) if( totals.assertions.failed > 0 ) {
{
INFO( runner.getOutput() ); INFO( runner.getOutput() );
FAIL( "Expected test case '" FAIL( "Expected test case '"
<< testCase.getName() << testCase.getName()
@ -206,8 +162,7 @@ namespace Catch
} }
break; break;
case Expected::ToFail: case Expected::ToFail:
if( totals.assertions.passed > 0 ) if( totals.assertions.passed > 0 ) {
{
INFO( runner.getOutput() ); INFO( runner.getOutput() );
FAIL( "Expected test case '" FAIL( "Expected test case '"
<< testCase.getName() << testCase.getName()
@ -223,32 +178,24 @@ namespace Catch
}; };
struct LineInfoRegistry struct LineInfoRegistry {
{
static LineInfoRegistry& get static LineInfoRegistry& get() {
()
{
static LineInfoRegistry s_instance; static LineInfoRegistry s_instance;
return s_instance; return s_instance;
} }
void registerLineInfo void registerLineInfo( const std::string& name,
( const SourceLineInfo& info ) {
const std::string& name,
const SourceLineInfo& info
)
{
m_registry.insert( std::make_pair( name, 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 ); std::map<std::string, SourceLineInfo>::const_iterator it = m_registry.find( name );
return it == m_registry.end() ? NULL : &(it->second); 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 ); std::map<std::string, SourceLineInfo>::const_iterator it = m_registry.find( name );
if( it == m_registry.end() ) if( it == m_registry.end() )
return ""; return "";
@ -260,10 +207,8 @@ namespace Catch
std::map<std::string, SourceLineInfo> m_registry; std::map<std::string, SourceLineInfo> m_registry;
}; };
struct LineInfoRegistrar struct LineInfoRegistrar {
{ LineInfoRegistrar( const char* name, const SourceLineInfo& lineInfo ) {
LineInfoRegistrar( const char* name, const SourceLineInfo& lineInfo )
{
LineInfoRegistry::get().registerLineInfo( name, lineInfo ); LineInfoRegistry::get().registerLineInfo( name, lineInfo );
} }
}; };