mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-26 07:16:10 +01:00
Extracted stand-alone micro-library, tbc_text_format.h, from catch_text.h.
- use this version in clara.h so clara can stand alone.
This commit is contained in:
parent
b756243935
commit
0dc49d1b57
@ -9,6 +9,9 @@
|
||||
#ifndef TWOBLUECUBES_CATCH_CLARA_H_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_CLARA_H_INCLUDED
|
||||
|
||||
#define CLARA_CONFIG_CONSOLE_WIDTH CATCH_CONFIG_CONSOLE_WIDTH
|
||||
|
||||
|
||||
// Declare Clara inside the Catch namespace
|
||||
#define CLICHE_CLARA_OUTER_NAMESPACE Catch
|
||||
#include "clara.h"
|
||||
|
@ -28,7 +28,6 @@
|
||||
#include "catch_tags.hpp"
|
||||
#include "catch_test_spec.hpp"
|
||||
#include "catch_version.hpp"
|
||||
#include "catch_text.hpp"
|
||||
#include "catch_message.hpp"
|
||||
#include "catch_legacy_reporter_adapter.hpp"
|
||||
#include "catch_timer.hpp"
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Created by Phil on 18/4/2013.
|
||||
* Copyright 2013 Two Blue Cubes Ltd. All rights reserved.
|
||||
* Created by Phil on 10/2/2014.
|
||||
* Copyright 2014 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)
|
||||
@ -10,52 +10,15 @@
|
||||
|
||||
#include "catch_config.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#define TBC_TEXT_FORMAT_CONSOLE_WIDTH CATCH_CONFIG_CONSOLE_WIDTH
|
||||
|
||||
#define CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE Catch
|
||||
#include "tbc_text_format.h"
|
||||
#undef CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE
|
||||
|
||||
namespace Catch {
|
||||
|
||||
struct TextAttributes {
|
||||
TextAttributes()
|
||||
: initialIndent( std::string::npos ),
|
||||
indent( 0 ),
|
||||
width( CATCH_CONFIG_CONSOLE_WIDTH-1 ),
|
||||
tabChar( '\t' )
|
||||
{}
|
||||
|
||||
TextAttributes& setInitialIndent( std::size_t _value ) { initialIndent = _value; return *this; }
|
||||
TextAttributes& setIndent( std::size_t _value ) { indent = _value; return *this; }
|
||||
TextAttributes& setWidth( std::size_t _value ) { width = _value; return *this; }
|
||||
TextAttributes& setTabChar( char _value ) { tabChar = _value; return *this; }
|
||||
|
||||
std::size_t initialIndent; // indent of first line, or npos
|
||||
std::size_t indent; // indent of subsequent lines, or all if initialIndent is npos
|
||||
std::size_t width; // maximum width of text, including indent. Longer text will wrap
|
||||
char tabChar; // If this char is seen the indent is changed to current pos
|
||||
};
|
||||
|
||||
class Text {
|
||||
public:
|
||||
Text( std::string const& _str, TextAttributes const& _attr = TextAttributes() );
|
||||
void spliceLine( std::size_t _indent, std::string& _remainder, std::size_t _pos );
|
||||
|
||||
typedef std::vector<std::string>::const_iterator const_iterator;
|
||||
|
||||
const_iterator begin() const { return lines.begin(); }
|
||||
const_iterator end() const { return lines.end(); }
|
||||
std::string const& last() const { return lines.back(); }
|
||||
std::size_t size() const { return lines.size(); }
|
||||
std::string const& operator[]( std::size_t _index ) const { return lines[_index]; }
|
||||
std::string toString() const;
|
||||
|
||||
friend std::ostream& operator << ( std::ostream& _stream, Text const& _text );
|
||||
|
||||
private:
|
||||
std::string str;
|
||||
TextAttributes attr;
|
||||
std::vector<std::string> lines;
|
||||
};
|
||||
|
||||
} // end namespace Catch
|
||||
using Tbc::Text;
|
||||
using Tbc::TextAttributes;
|
||||
}
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_TEXT_H_INCLUDED
|
||||
|
@ -1,95 +0,0 @@
|
||||
/*
|
||||
* Created by Phil on 20/4/2013.
|
||||
* Copyright 2013 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_TEXT_HPP_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_TEXT_HPP_INCLUDED
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Catch {
|
||||
|
||||
Text::Text( std::string const& _str, TextAttributes const& _attr )
|
||||
: attr( _attr )
|
||||
{
|
||||
std::string wrappableChars = " [({.,/|\\-";
|
||||
std::size_t indent = _attr.initialIndent != std::string::npos
|
||||
? _attr.initialIndent
|
||||
: _attr.indent;
|
||||
std::string remainder = _str;
|
||||
|
||||
while( !remainder.empty() ) {
|
||||
if( lines.size() >= 1000 ) {
|
||||
lines.push_back( "... message truncated due to excessive size" );
|
||||
return;
|
||||
}
|
||||
std::size_t tabPos = std::string::npos;
|
||||
std::size_t width = (std::min)( remainder.size(), _attr.width - indent );
|
||||
std::size_t pos = remainder.find_first_of( '\n' );
|
||||
if( pos <= width ) {
|
||||
width = pos;
|
||||
}
|
||||
pos = remainder.find_last_of( _attr.tabChar, width );
|
||||
if( pos != std::string::npos ) {
|
||||
tabPos = pos;
|
||||
if( remainder[width] == '\n' )
|
||||
width--;
|
||||
remainder = remainder.substr( 0, tabPos ) + remainder.substr( tabPos+1 );
|
||||
}
|
||||
|
||||
if( width == remainder.size() ) {
|
||||
spliceLine( indent, remainder, width );
|
||||
}
|
||||
else if( remainder[width] == '\n' ) {
|
||||
spliceLine( indent, remainder, width );
|
||||
if( width <= 1 || remainder.size() != 1 )
|
||||
remainder = remainder.substr( 1 );
|
||||
indent = _attr.indent;
|
||||
}
|
||||
else {
|
||||
pos = remainder.find_last_of( wrappableChars, width );
|
||||
if( pos != std::string::npos && pos > 0 ) {
|
||||
spliceLine( indent, remainder, pos );
|
||||
if( remainder[0] == ' ' )
|
||||
remainder = remainder.substr( 1 );
|
||||
}
|
||||
else {
|
||||
spliceLine( indent, remainder, width-1 );
|
||||
lines.back() += "-";
|
||||
}
|
||||
if( lines.size() == 1 )
|
||||
indent = _attr.indent;
|
||||
if( tabPos != std::string::npos )
|
||||
indent += tabPos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Text::spliceLine( std::size_t _indent, std::string& _remainder, std::size_t _pos ) {
|
||||
lines.push_back( std::string( _indent, ' ' ) + _remainder.substr( 0, _pos ) );
|
||||
_remainder = _remainder.substr( _pos );
|
||||
}
|
||||
|
||||
std::string Text::toString() const {
|
||||
std::ostringstream oss;
|
||||
oss << *this;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
std::ostream& operator << ( std::ostream& _stream, Text const& _text ) {
|
||||
for( Text::const_iterator it = _text.begin(), itEnd = _text.end();
|
||||
it != itEnd; ++it ) {
|
||||
if( it != _text.begin() )
|
||||
_stream << "\n";
|
||||
_stream << *it;
|
||||
}
|
||||
return _stream;
|
||||
}
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_TEXT_HPP_INCLUDED
|
@ -18,7 +18,13 @@
|
||||
#endif
|
||||
#ifndef TWOBLUECUBES_CLARA_H_ALREADY_INCLUDED
|
||||
|
||||
#include "catch_text.h" // This will get moved out too
|
||||
#define CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE Clara
|
||||
#include "tbc_text_format.h"
|
||||
#undef CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
// Use optional outer namespace
|
||||
#ifdef CLICHE_CLARA_OUTER_NAMESPACE
|
||||
@ -27,6 +33,15 @@ namespace CLICHE_CLARA_OUTER_NAMESPACE {
|
||||
|
||||
namespace Clara {
|
||||
namespace Detail {
|
||||
|
||||
#ifdef CLARA_CONSOLE_WIDTH
|
||||
const unsigned int consoleWidth = CLARA_CONFIG_CONSOLE_WIDTH;
|
||||
#else
|
||||
const unsigned int consoleWidth = 80;
|
||||
#endif
|
||||
|
||||
using namespace ::Clara::Tbc;
|
||||
|
||||
template<typename T> struct RemoveConstRef{ typedef T type; };
|
||||
template<typename T> struct RemoveConstRef<T&>{ typedef T type; };
|
||||
template<typename T> struct RemoveConstRef<T const&>{ typedef T type; };
|
||||
@ -416,18 +431,18 @@ namespace Clara {
|
||||
m_boundProcessName = Detail::makeBoundField( f );
|
||||
}
|
||||
|
||||
void optUsage( std::ostream& os, std::size_t indent = 0, std::size_t width = CATCH_CONFIG_CONSOLE_WIDTH ) const {
|
||||
void optUsage( std::ostream& os, std::size_t indent = 0, std::size_t width = Detail::consoleWidth ) const {
|
||||
typename std::vector<Arg>::const_iterator itBegin = m_options.begin(), itEnd = m_options.end(), it;
|
||||
std::size_t maxWidth = 0;
|
||||
for( it = itBegin; it != itEnd; ++it )
|
||||
maxWidth = (std::max)( maxWidth, it->commands().size() );
|
||||
|
||||
for( it = itBegin; it != itEnd; ++it ) {
|
||||
Catch::Text usage( it->commands(), Catch::TextAttributes()
|
||||
Detail::Text usage( it->commands(), Detail::TextAttributes()
|
||||
.setWidth( maxWidth+indent )
|
||||
.setIndent( indent ) );
|
||||
// !TBD handle longer usage strings
|
||||
Catch::Text desc( it->description, Catch::TextAttributes()
|
||||
Detail::Text desc( it->description, Detail::TextAttributes()
|
||||
.setWidth( width - maxWidth -3 ) );
|
||||
|
||||
for( std::size_t i = 0; i < (std::max)( usage.size(), desc.size() ); ++i ) {
|
||||
|
153
include/internal/tbc_text_format.h
Normal file
153
include/internal/tbc_text_format.h
Normal file
@ -0,0 +1,153 @@
|
||||
/*
|
||||
* Created by Phil on 18/4/2013.
|
||||
* Copyright 2013 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)
|
||||
*/
|
||||
// Only use header guard if we are not using an outer namespace
|
||||
#ifndef CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE
|
||||
# ifdef TWOBLUECUBES_TEXT_FORMAT_H_INCLUDED
|
||||
# ifndef TWOBLUECUBES_TEXT_FORMAT_H_ALREADY_INCLUDED
|
||||
# define TWOBLUECUBES_TEXT_FORMAT_H_ALREADY_INCLUDED
|
||||
# endif
|
||||
# else
|
||||
# define TWOBLUECUBES_TEXT_FORMAT_H_INCLUDED
|
||||
# endif
|
||||
#endif
|
||||
#ifndef TWOBLUECUBES_TEXT_FORMAT_H_ALREADY_INCLUDED
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
// Use optional outer namespace
|
||||
#ifdef CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE
|
||||
namespace CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE {
|
||||
#endif
|
||||
|
||||
namespace Tbc {
|
||||
|
||||
#ifdef TBC_TEXT_FORMAT_CONSOLE_WIDTH
|
||||
const unsigned int consoleWidth = TBC_TEXT_FORMAT_CONSOLE_WIDTH;
|
||||
#else
|
||||
const unsigned int consoleWidth = 80;
|
||||
#endif
|
||||
|
||||
struct TextAttributes {
|
||||
TextAttributes()
|
||||
: initialIndent( std::string::npos ),
|
||||
indent( 0 ),
|
||||
width( consoleWidth-1 ),
|
||||
tabChar( '\t' )
|
||||
{}
|
||||
|
||||
TextAttributes& setInitialIndent( std::size_t _value ) { initialIndent = _value; return *this; }
|
||||
TextAttributes& setIndent( std::size_t _value ) { indent = _value; return *this; }
|
||||
TextAttributes& setWidth( std::size_t _value ) { width = _value; return *this; }
|
||||
TextAttributes& setTabChar( char _value ) { tabChar = _value; return *this; }
|
||||
|
||||
std::size_t initialIndent; // indent of first line, or npos
|
||||
std::size_t indent; // indent of subsequent lines, or all if initialIndent is npos
|
||||
std::size_t width; // maximum width of text, including indent. Longer text will wrap
|
||||
char tabChar; // If this char is seen the indent is changed to current pos
|
||||
};
|
||||
|
||||
class Text {
|
||||
public:
|
||||
Text( std::string const& _str, TextAttributes const& _attr = TextAttributes() )
|
||||
: attr( _attr )
|
||||
{
|
||||
std::string wrappableChars = " [({.,/|\\-";
|
||||
std::size_t indent = _attr.initialIndent != std::string::npos
|
||||
? _attr.initialIndent
|
||||
: _attr.indent;
|
||||
std::string remainder = _str;
|
||||
|
||||
while( !remainder.empty() ) {
|
||||
if( lines.size() >= 1000 ) {
|
||||
lines.push_back( "... message truncated due to excessive size" );
|
||||
return;
|
||||
}
|
||||
std::size_t tabPos = std::string::npos;
|
||||
std::size_t width = (std::min)( remainder.size(), _attr.width - indent );
|
||||
std::size_t pos = remainder.find_first_of( '\n' );
|
||||
if( pos <= width ) {
|
||||
width = pos;
|
||||
}
|
||||
pos = remainder.find_last_of( _attr.tabChar, width );
|
||||
if( pos != std::string::npos ) {
|
||||
tabPos = pos;
|
||||
if( remainder[width] == '\n' )
|
||||
width--;
|
||||
remainder = remainder.substr( 0, tabPos ) + remainder.substr( tabPos+1 );
|
||||
}
|
||||
|
||||
if( width == remainder.size() ) {
|
||||
spliceLine( indent, remainder, width );
|
||||
}
|
||||
else if( remainder[width] == '\n' ) {
|
||||
spliceLine( indent, remainder, width );
|
||||
if( width <= 1 || remainder.size() != 1 )
|
||||
remainder = remainder.substr( 1 );
|
||||
indent = _attr.indent;
|
||||
}
|
||||
else {
|
||||
pos = remainder.find_last_of( wrappableChars, width );
|
||||
if( pos != std::string::npos && pos > 0 ) {
|
||||
spliceLine( indent, remainder, pos );
|
||||
if( remainder[0] == ' ' )
|
||||
remainder = remainder.substr( 1 );
|
||||
}
|
||||
else {
|
||||
spliceLine( indent, remainder, width-1 );
|
||||
lines.back() += "-";
|
||||
}
|
||||
if( lines.size() == 1 )
|
||||
indent = _attr.indent;
|
||||
if( tabPos != std::string::npos )
|
||||
indent += tabPos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void spliceLine( std::size_t _indent, std::string& _remainder, std::size_t _pos ) {
|
||||
lines.push_back( std::string( _indent, ' ' ) + _remainder.substr( 0, _pos ) );
|
||||
_remainder = _remainder.substr( _pos );
|
||||
}
|
||||
|
||||
typedef std::vector<std::string>::const_iterator const_iterator;
|
||||
|
||||
const_iterator begin() const { return lines.begin(); }
|
||||
const_iterator end() const { return lines.end(); }
|
||||
std::string const& last() const { return lines.back(); }
|
||||
std::size_t size() const { return lines.size(); }
|
||||
std::string const& operator[]( std::size_t _index ) const { return lines[_index]; }
|
||||
std::string toString() const {
|
||||
std::ostringstream oss;
|
||||
oss << *this;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
inline friend std::ostream& operator << ( std::ostream& _stream, Text const& _text ) {
|
||||
for( Text::const_iterator it = _text.begin(), itEnd = _text.end();
|
||||
it != itEnd; ++it ) {
|
||||
if( it != _text.begin() )
|
||||
_stream << "\n";
|
||||
_stream << *it;
|
||||
}
|
||||
return _stream;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string str;
|
||||
TextAttributes attr;
|
||||
std::vector<std::string> lines;
|
||||
};
|
||||
|
||||
} // end namespace Tbc
|
||||
|
||||
#ifdef CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE
|
||||
} // end outer namespace
|
||||
#endif
|
||||
|
||||
#endif // TWOBLUECUBES_TEXT_FORMAT_H_ALREADY_INCLUDED
|
@ -78,6 +78,7 @@
|
||||
26847E5C16BBACB60043B9C1 /* catch_message.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_message.hpp; sourceTree = "<group>"; };
|
||||
26847E5D16BBADB40043B9C1 /* catch_message.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = catch_message.cpp; path = ../../../SelfTest/SurrogateCpps/catch_message.cpp; sourceTree = "<group>"; };
|
||||
268F47B018A93F7800D8C14F /* catch_clara.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = catch_clara.h; sourceTree = "<group>"; };
|
||||
268F47B118A944DB00D8C14F /* tbc_text_format.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = tbc_text_format.h; path = ../../../../include/internal/tbc_text_format.h; sourceTree = "<group>"; };
|
||||
26948284179A9AB900ED166E /* SectionTrackerTests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SectionTrackerTests.cpp; path = ../../../SelfTest/SectionTrackerTests.cpp; sourceTree = "<group>"; };
|
||||
26948287179EF7F900ED166E /* catch_test_case_tracker.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_test_case_tracker.hpp; sourceTree = "<group>"; };
|
||||
2694A1FB16A0000E004816E3 /* catch_text.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = catch_text.cpp; sourceTree = "<group>"; };
|
||||
@ -202,6 +203,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
26C5F3EC17514B970056FB3C /* clara.h */,
|
||||
268F47B118A944DB00D8C14F /* tbc_text_format.h */,
|
||||
);
|
||||
name = External;
|
||||
sourceTree = "<group>";
|
||||
|
Loading…
Reference in New Issue
Block a user