2011-01-14 09:38:46 +01:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2011-05-24 09:23:02 +02:00
|
|
|
#include "catch.hpp"
|
2011-01-14 09:38:46 +01:00
|
|
|
|
|
|
|
namespace Catch
|
|
|
|
{
|
2011-02-09 10:08:10 +01:00
|
|
|
class EmbeddedRunner
|
2011-01-14 09:38:46 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2011-02-09 10:08:10 +01:00
|
|
|
EmbeddedRunner
|
2011-02-03 21:00:46 +01:00
|
|
|
()
|
2011-01-14 09:38:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-02-09 10:08:10 +01:00
|
|
|
std::size_t runMatching
|
2012-02-15 09:20:06 +01:00
|
|
|
( const std::string& rawTestSpec );
|
2011-03-11 20:44:59 +01:00
|
|
|
|
2011-01-14 17:21:11 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2011-02-09 10:08:10 +01:00
|
|
|
std::string getOutput
|
2011-02-03 21:00:46 +01:00
|
|
|
()
|
2011-01-14 09:38:46 +01:00
|
|
|
{
|
2011-02-09 10:08:10 +01:00
|
|
|
return m_output;
|
2011-01-14 09:38:46 +01:00
|
|
|
}
|
|
|
|
|
2011-01-14 17:21:11 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2011-02-09 10:08:10 +01:00
|
|
|
std::size_t getSuccessCount
|
2011-02-03 21:00:46 +01:00
|
|
|
()
|
2011-02-09 10:08:10 +01:00
|
|
|
const
|
2011-01-14 09:38:46 +01:00
|
|
|
{
|
2011-02-09 10:08:10 +01:00
|
|
|
return m_successes;
|
2011-01-14 09:38:46 +01:00
|
|
|
}
|
|
|
|
|
2011-01-14 17:21:11 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2011-02-09 10:08:10 +01:00
|
|
|
std:: size_t getFailureCount
|
2011-02-03 21:00:46 +01:00
|
|
|
()
|
2011-02-09 10:08:10 +01:00
|
|
|
const
|
2011-01-14 09:38:46 +01:00
|
|
|
{
|
2011-02-09 10:08:10 +01:00
|
|
|
return m_failures;
|
2011-01-14 09:38:46 +01:00
|
|
|
}
|
2011-02-09 10:08:10 +01:00
|
|
|
|
2011-01-14 09:38:46 +01:00
|
|
|
private:
|
2011-02-09 10:08:10 +01:00
|
|
|
std::size_t m_successes;
|
|
|
|
std::size_t m_failures;
|
|
|
|
std::string m_output;
|
2011-01-14 09:38:46 +01:00
|
|
|
};
|
2011-03-11 20:44:59 +01:00
|
|
|
|
|
|
|
class MetaTestRunner
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct Expected
|
|
|
|
{
|
|
|
|
enum Result
|
|
|
|
{
|
|
|
|
ToSucceed,
|
|
|
|
ToFail
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
static void runMatching
|
|
|
|
(
|
|
|
|
const std::string& testSpec,
|
|
|
|
Expected::Result expectedResult
|
|
|
|
)
|
|
|
|
{
|
|
|
|
forEach( Hub::getTestCaseRegistry().getMatchingTestCases( testSpec ),
|
|
|
|
MetaTestRunner( expectedResult ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
MetaTestRunner
|
|
|
|
(
|
|
|
|
Expected::Result expectedResult
|
|
|
|
)
|
|
|
|
: m_expectedResult( expectedResult )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
void operator()
|
|
|
|
(
|
|
|
|
const TestCaseInfo& testCase
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EmbeddedRunner runner;
|
|
|
|
runner.runMatching( testCase.getName() );
|
|
|
|
switch( m_expectedResult )
|
|
|
|
{
|
|
|
|
case Expected::ToSucceed:
|
|
|
|
if( runner.getFailureCount() > 0 )
|
|
|
|
{
|
|
|
|
INFO( runner.getOutput() );
|
|
|
|
FAIL( "Expected test case '"
|
|
|
|
<< testCase.getName()
|
|
|
|
<< "' to succeed but there was/ were "
|
|
|
|
<< runner.getFailureCount() << " failure(s)" );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Expected::ToFail:
|
|
|
|
if( runner.getSuccessCount() > 0 )
|
|
|
|
{
|
|
|
|
INFO( runner.getOutput() );
|
|
|
|
FAIL( "Expected test case '"
|
|
|
|
<< testCase.getName()
|
|
|
|
<< "' to fail but there was/ were "
|
|
|
|
<< runner.getSuccessCount() << " success(es)" );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
Expected::Result m_expectedResult;
|
|
|
|
};
|
|
|
|
|
2012-02-15 09:20:06 +01:00
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
};
|
2011-01-14 09:38:46 +01:00
|
|
|
}
|
|
|
|
|
2012-02-15 09:20:06 +01:00
|
|
|
#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 )
|
|
|
|
|
2011-01-14 09:38:46 +01:00
|
|
|
#endif // TWOBLUECUBES_CATCH_SELF_TEST_HPP_INCLUDED
|