mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 12:55:40 +02:00
LineWrapper can indent first line differently to subsequent lines
- use this to wrap Given/ When/ Then with indent after the :
This commit is contained in:
@@ -32,10 +32,7 @@ namespace {
|
||||
GetConsoleScreenBufferInfo( stdoutHandle, &csbiInfo );
|
||||
originalAttributes = csbiInfo.wAttributes;
|
||||
}
|
||||
~Win32ColourImpl() {
|
||||
use( Colour::None );
|
||||
}
|
||||
|
||||
|
||||
virtual void use( Colour::Code _colourCode ) {
|
||||
switch( _colourCode ) {
|
||||
case Colour::None: return setTextAttribute( originalAttributes );
|
||||
@@ -86,10 +83,6 @@ namespace {
|
||||
// https://github.com/philsquared/Catch/pull/131
|
||||
class PosixColourImpl : public Detail::IColourImpl {
|
||||
public:
|
||||
PosixColourImpl() {
|
||||
use( Colour::None );
|
||||
}
|
||||
|
||||
virtual void use( Colour::Code _colourCode ) {
|
||||
switch( _colourCode ) {
|
||||
case Colour::None:
|
||||
|
@@ -18,7 +18,9 @@ namespace Catch {
|
||||
LineWrapper();
|
||||
|
||||
LineWrapper& setIndent( std::size_t _indent );
|
||||
LineWrapper& setInitialIndent( std::size_t _initalIndent );
|
||||
LineWrapper& setRight( std::size_t _right );
|
||||
LineWrapper& setTabChar( char _tabChar );
|
||||
|
||||
LineWrapper& wrap( std::string const& _str );
|
||||
|
||||
@@ -38,12 +40,15 @@ namespace Catch {
|
||||
void wrapInternal( std::string const& _str );
|
||||
void addLine( const std::string& _line );
|
||||
bool isWrapPoint( char c );
|
||||
std::size_t getCurrentIndent() const;
|
||||
|
||||
std::string indent;
|
||||
std::size_t right;
|
||||
std::size_t nextTab;
|
||||
std::size_t tab;
|
||||
std::size_t indent;
|
||||
std::size_t initialIndent;
|
||||
std::string wrappableChars;
|
||||
char tabChar;
|
||||
int recursionCount;
|
||||
std::vector<std::string> lines;
|
||||
};
|
||||
|
@@ -16,12 +16,19 @@ namespace Catch {
|
||||
: right( CATCH_CONFIG_CONSOLE_WIDTH-1 ),
|
||||
nextTab( 0 ),
|
||||
tab( 0 ),
|
||||
indent( 0 ),
|
||||
initialIndent( (std::size_t)-1 ), // use indent by default
|
||||
wrappableChars( " [({.,/|\\" ),
|
||||
tabChar( '\t' ),
|
||||
recursionCount( 0 )
|
||||
{}
|
||||
|
||||
LineWrapper& LineWrapper::setIndent( std::size_t _indent ) {
|
||||
indent = std::string( _indent, ' ' );
|
||||
indent = _indent;
|
||||
return *this;
|
||||
}
|
||||
LineWrapper& LineWrapper::setInitialIndent( std::size_t _initialIndent ) {
|
||||
initialIndent = _initialIndent;
|
||||
return *this;
|
||||
}
|
||||
LineWrapper& LineWrapper::setRight( std::size_t _right ) {
|
||||
@@ -33,13 +40,17 @@ namespace Catch {
|
||||
wrapInternal( _str );
|
||||
return *this;
|
||||
}
|
||||
LineWrapper& LineWrapper::setTabChar( char _tabChar ) {
|
||||
tabChar = _tabChar;
|
||||
return *this;
|
||||
}
|
||||
bool LineWrapper::isWrapPoint( char c ) {
|
||||
return wrappableChars.find( c ) != std::string::npos;
|
||||
}
|
||||
void LineWrapper::wrapInternal( std::string const& _str ) {
|
||||
assert( ++recursionCount < 100 );
|
||||
|
||||
std::size_t width = right - indent.size();
|
||||
std::size_t width = right - getCurrentIndent();
|
||||
std::size_t wrapPoint = width-tab;
|
||||
for( std::size_t pos = 0; pos < _str.size(); ++pos ) {
|
||||
if( _str[pos] == '\n' )
|
||||
@@ -61,7 +72,7 @@ namespace Catch {
|
||||
}
|
||||
return wrapInternal( _str.substr( wrapPoint ) );
|
||||
}
|
||||
if( _str[pos] == '\t' ) {
|
||||
if( _str[pos] == tabChar ) {
|
||||
nextTab = pos;
|
||||
std::string withoutTab = _str.substr( 0, nextTab ) + _str.substr( nextTab+1 );
|
||||
return wrapInternal( withoutTab );
|
||||
@@ -89,14 +100,18 @@ namespace Catch {
|
||||
}
|
||||
|
||||
void LineWrapper::addLine( const std::string& _line ) {
|
||||
if( tab > 0 )
|
||||
lines.push_back( indent + std::string( tab, ' ' ) + _line );
|
||||
else
|
||||
lines.push_back( indent + _line );
|
||||
lines.push_back( std::string( tab + getCurrentIndent(), ' ' ) + _line );
|
||||
if( nextTab > 0 )
|
||||
tab = nextTab;
|
||||
}
|
||||
|
||||
std::size_t LineWrapper::getCurrentIndent() const
|
||||
{
|
||||
return (initialIndent != (std::size_t)-1 && lines.empty() )
|
||||
? initialIndent
|
||||
: indent;
|
||||
}
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_LINE_WRAP_HPP_INCLUDED
|
||||
|
@@ -288,7 +288,8 @@ namespace Catch {
|
||||
if( !sections.empty() ) {
|
||||
typedef std::vector<ThreadedSectionInfo*>::const_reverse_iterator It;
|
||||
for( It it = sections.rbegin(), itEnd = sections.rend(); it != itEnd; ++it )
|
||||
stream << " " << (*it)->name << "\n";
|
||||
printUserString( (*it)->name, 2 );
|
||||
|
||||
}
|
||||
}
|
||||
stream << getDots() << "\n" << std::endl;
|
||||
@@ -306,10 +307,24 @@ namespace Catch {
|
||||
}
|
||||
{
|
||||
Colour colourGuard( Colour::Headers );
|
||||
stream << _name << "\n";
|
||||
printUserString( _name );
|
||||
}
|
||||
}
|
||||
|
||||
// if string has a : in first line will set indent to follow it on
|
||||
// subsequent lines
|
||||
void printUserString( std::string const& _string, std::size_t indent = 0 ) {
|
||||
std::size_t i = _string.find( ": " );
|
||||
if( i != std::string::npos )
|
||||
i+=2;
|
||||
else
|
||||
i = 0;
|
||||
stream << LineWrapper()
|
||||
.setIndent( indent+i)
|
||||
.setInitialIndent( indent )
|
||||
.wrap( _string ) << "\n";
|
||||
}
|
||||
|
||||
void printTotals( const Totals& totals ) {
|
||||
if( totals.assertions.total() == 0 ) {
|
||||
stream << "No tests ran";
|
||||
|
Reference in New Issue
Block a user