Replaced dodgy self-test code with slightly less dodgy self test meta test cases

This commit is contained in:
Phil Nash 2011-01-14 08:38:46 +00:00
parent f063c2ae2a
commit f36d777343
2 changed files with 173 additions and 57 deletions

View File

@ -10,67 +10,60 @@
*
*/
#include "../catch.hpp"
#include "../catch_runner.hpp"
#include "../internal/catch_self_test.hpp"
#include "../catch_default_main.hpp"
// This code runs the meta tests and verifies that the failing ones failed and the successful ones succeeded
int main (int argc, char * const argv[])
TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" )
{
using namespace Catch;
bool showAllResults = false;
if( argc > 1 && ( std::string( argv[1] ) == "-s" || std::string( argv[1] ) == "--success" ) )
showAllResults = true;
std::ostringstream ossSucceeding;
std::ostringstream ossFailing;
Config config;
config.setReporter( "Basic" );
config.setIncludeWhat( Config::Include::SuccessfulResults );
Runner runner( config );
config.setStreamBuf( ossSucceeding.rdbuf() );
runner.runMatching( "succeeding/*" );
std::string succeedingResults = ossSucceeding.str();
config.setStreamBuf( ossFailing.rdbuf() );
runner.runMatching( "failing/*" );
std::string failingResults = ossFailing.str();
int result = 0;
if( succeedingResults.find( "failed" ) != std::string::npos )
{
std::cerr << "Some tests that should have succeeded failed:\n\n" << succeedingResults;
result = 1;
EmbeddedRunner runner;
runner.runMatching( "succeeding/*" );
CHECK( runner.getReporter().getSucceeded() == 53 );
CHECK( runner.getReporter().getFailed() == 0 );
}
else if( showAllResults )
{
std::cout << succeedingResults << "\n\n";
}
if( failingResults.find( "succeeded" ) != std::string::npos )
{
std::cerr << "Some tests that should have failed succeeded:\n\n" << failingResults;
result = 1;
}
else if( showAllResults )
{
std::cout << failingResults << "\n\n";
}
EmbeddedRunner runner;
runner.runMatching( "failing/*" );
if( result == 0 )
CHECK( runner.getReporter().getSucceeded() == 0 );
CHECK( runner.getReporter().getFailed() == 53 );
}
}
TEST_CASE( "selftest/succeeding", "Runs all Catch self tests that should succeed and checks their results" )
{
using namespace Catch;
// Run a nested Runner - we scope it here so it restores our runner
// at the end of scope
{
const size_t expectedTestCaseCount = 106; // !TBD factor this out
size_t testCaseCount = runner.getSuccessCount() + runner.getFailureCount();
std::cout << "All " << testCaseCount << " test(s) completed successfully" << std::endl;
if( testCaseCount != expectedTestCaseCount )
SelfTestConfig config;
{
std::cerr << "- but we were expecting " << expectedTestCaseCount
<< " test to run. Were some added or removed, or were they not compiled in?"
<< std::endl;
return 1;
Runner runner( config );
runner.runMatching( "succeeding/*" );
}
CHECK( config.getReporter().getSucceeded() == 53 );
CHECK( config.getReporter().getFailed() == 0 );
}
}
TEST_CASE( "selftest/failing", "Runs all Catch self tests that should fail and checks their results" )
{
using namespace Catch;
// Run a nested Runner - we scope it here so it restores our runner
// at the end of scope
{
SelfTestConfig config;
{
Runner runner( config );
runner.runMatching( "failing/*" );
}
CHECK( config.getReporter().getSucceeded() == 0 );
CHECK( config.getReporter().getFailed() == 53 );
}
return result;
}

View File

@ -0,0 +1,123 @@
/*
* 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
#include "../catch.hpp"
#include "catch_runner_impl.hpp"
namespace Catch
{
class SelfTestReporter : public IReporter
{
public:
///////////////////////////////////////////////////////////////////////////
SelfTestReporter()
: m_succeeded( 0 ),
m_failed( 0 )
{
}
///////////////////////////////////////////////////////////////////////////
static std::string getDescription()
{
return "Captures results for self test purposes";
}
///////////////////////////////////////////////////////////////////////////
size_t getSucceeded() const
{
return m_succeeded;
}
///////////////////////////////////////////////////////////////////////////
size_t getFailed() const
{
return m_failed;
}
private: // IReporter
///////////////////////////////////////////////////////////////////////////
virtual void StartTesting(){}
///////////////////////////////////////////////////////////////////////////
virtual void EndTesting( std::size_t succeeded, std::size_t failed )
{
m_succeeded = succeeded;
m_failed = failed;
}
///////////////////////////////////////////////////////////////////////////
virtual void StartGroup( const std::string& ){}
virtual void EndGroup( const std::string&, std::size_t, std::size_t ){}
virtual void StartTestCase( const TestCaseInfo& ){}
virtual void StartSection( const std::string&, const std::string ){}
virtual void EndSection( const std::string&, std::size_t, std::size_t ){}
virtual void Result( const ResultInfo& ){}
virtual void EndTestCase( const TestCaseInfo&, std::size_t, std::size_t, const std::string&, const std::string& ){}
private:
size_t m_succeeded;
size_t m_failed;
};
class SelfTestConfig : public Config
{
public:
SelfTestConfig()
: m_reporter( new SelfTestReporter() )
{
// reporter will be deleted by the base config class
setReporter( m_reporter );
setStreamBuf( m_oss.rdbuf() );
}
SelfTestReporter& getReporter()
{
return *m_reporter;
}
private:
std::ostringstream m_oss;
SelfTestReporter* m_reporter;
};
class EmbeddedRunner
{
public:
EmbeddedRunner()
{
}
std::size_t runMatching
(
const std::string& rawTestSpec
)
{
std::size_t result;
Runner runner( m_config );
result = runner.runMatching( rawTestSpec );
return result;
}
SelfTestReporter& getReporter()
{
return m_config.getReporter();
}
private:
SelfTestConfig m_config;
};
}
#endif // TWOBLUECUBES_CATCH_SELF_TEST_HPP_INCLUDED