diff options
| author | Phil Nash | 2013-03-27 19:08:16 +0000 |
|---|---|---|
| committer | Phil Nash | 2013-03-27 19:08:16 +0000 |
| commit | b052bd729af9f46b378c7e9ffc3f74f81b5cbb93 (patch) | |
| tree | 05f6151b059e77e9989d77b0ca26e489fe373d0d /include | |
| parent | dd26e889b528a8275e4a2395f896dec211103ea6 (diff) | |
| download | catch3-b052bd729af9f46b378c7e9ffc3f74f81b5cbb93.tar.gz | |
Refactored string wrapper
- to be much more flexible (writes to vector)
- fixed a couple of bugs
Diffstat (limited to 'include')
| -rw-r--r-- | include/catch_runner.hpp | 2 | ||||
| -rw-r--r-- | include/internal/catch_line_wrap.h | 32 | ||||
| -rw-r--r-- | include/internal/catch_line_wrap.hpp | 121 | ||||
| -rw-r--r-- | include/reporters/catch_reporter_console.hpp | 11 |
4 files changed, 111 insertions, 55 deletions
diff --git a/include/catch_runner.hpp b/include/catch_runner.hpp index edceeccb..6d63b878 100644 --- a/include/catch_runner.hpp +++ b/include/catch_runner.hpp @@ -161,7 +161,7 @@ namespace Catch { displayedSpecificOption = true; std::cout << "\n" << opt.optionNames() << " " << opt.argsSynopsis() << "\n\n" << opt.optionSummary() << "\n\n" - << wrapLongStrings( opt.optionDescription(), 80, 2 ) << "\n" << std::endl; + << LineWrapper().setIndent( 2 ).wrap( opt.optionDescription() ).toString() << "\n" << std::endl; } } diff --git a/include/internal/catch_line_wrap.h b/include/internal/catch_line_wrap.h index 1197fb0b..e08dada1 100644 --- a/include/internal/catch_line_wrap.h +++ b/include/internal/catch_line_wrap.h @@ -9,11 +9,39 @@ #define TWOBLUECUBES_CATCH_LINE_WRAP_H_INCLUDED #include <string> +#include <vector> namespace Catch { - void wrapLongStrings( std::ostream& stream, const std::string& str, std::size_t columns, std::size_t indent = 0 ); - std::string wrapLongStrings( const std::string& str, std::size_t columns, std::size_t indent = 0 ); + class LineWrapper { + public: + LineWrapper( std::size_t _indent, std::size_t _right ); + LineWrapper( std::size_t _right ); + LineWrapper(); + + LineWrapper& setIndent( std::size_t _indent ); + LineWrapper& setRight( std::size_t _right ); + + LineWrapper& wrap( std::string const& _str ); + + std::ostream& intoStream( std::ostream& stream ) const; + std::string toString() const; + + typedef std::vector<std::string>::const_iterator const_iterator; + + const_iterator begin() const { return lines.begin(); } + const_iterator end() const { return lines.end(); } + + private: + void wrapInternal( std::string const& _str ); + void addLine( const std::string& _line ); + + std::string indent; + std::size_t right; + std::size_t nextTab; + std::size_t tab; + std::vector<std::string> lines; + }; } // end namespace Catch diff --git a/include/internal/catch_line_wrap.hpp b/include/internal/catch_line_wrap.hpp index 8a734fd8..bb8247d9 100644 --- a/include/internal/catch_line_wrap.hpp +++ b/include/internal/catch_line_wrap.hpp @@ -12,60 +12,91 @@ namespace Catch { - namespace { - inline void addIndent( std::ostream& os, std::size_t indent ) { - while( indent-- > 0 ) - os << ' '; - } - - inline void recursivelyWrapLine( std::ostream& os, std::string paragraph, std::size_t columns, std::size_t indent ) { - std::size_t width = columns-indent; - std::size_t tab = 0; - std::size_t wrapPoint = width; - for( std::size_t pos = 0; pos < paragraph.size(); ++pos ) { - if( pos == width ) { - addIndent( os, indent ); - if( paragraph[wrapPoint] == ' ' ) { - os << paragraph.substr( 0, wrapPoint ) << "\n"; - while( paragraph[++wrapPoint] == ' ' ); - } - else { - os << paragraph.substr( 0, --wrapPoint ) << "-\n"; - } - return recursivelyWrapLine( os, paragraph.substr( wrapPoint ), columns, indent+tab ); - } - if( paragraph[pos] == '\t' ) { - tab = pos; - paragraph = paragraph.substr( 0, tab ) + paragraph.substr( tab+1 ); - pos--; + + LineWrapper::LineWrapper( std::size_t _indent, std::size_t _right ) + : indent( _indent, ' ' ), + right( _right ), + nextTab( 0 ), + tab( 0 ) + {} + LineWrapper::LineWrapper( std::size_t _right ) + : right( _right ), + nextTab( 0 ), + tab( 0 ) + {} + LineWrapper::LineWrapper() + : right( CATCH_CONFIG_CONSOLE_WIDTH-1 ), + nextTab( 0 ), + tab( 0 ) + {} + + LineWrapper& LineWrapper::setIndent( std::size_t _indent ) { + indent = std::string( _indent, ' ' ); + return *this; + } + LineWrapper& LineWrapper::setRight( std::size_t _right ) { + right = _right; + return *this; + } + LineWrapper& LineWrapper::wrap( std::string const& _str ) { + nextTab = tab = 0; + wrapInternal( _str ); + return *this; + } + void LineWrapper::wrapInternal( std::string const& _str ) { + std::size_t width = right - indent.size(); + std::size_t wrapPoint = width-tab; + for( std::size_t pos = 0; pos < _str.size(); ++pos ) { + if( _str[pos] == '\n' ) + { + addLine( _str.substr( 0, pos ) ); + nextTab = tab = 0; + return wrapInternal( _str.substr( pos+1 ) ); + } + if( pos == width-tab ) { + if( _str[wrapPoint] == ' ' ) { + addLine( _str.substr( 0, wrapPoint ) ); + while( _str[++wrapPoint] == ' ' ); } - else if( paragraph[pos] == ' ' ) { - wrapPoint = pos; + else { + addLine( _str.substr( 0, --wrapPoint ) + '-' ); } + return wrapInternal( _str.substr( wrapPoint ) ); + } + if( _str[pos] == '\t' ) { + nextTab = pos; + std::string withoutTab = _str.substr( 0, nextTab ) + _str.substr( nextTab+1 ); + return wrapInternal( withoutTab ); + } + else if( _str[pos] == ' ' ) { + wrapPoint = pos; } - addIndent( os, indent ); - os << paragraph; } + addLine( _str ); } - void wrapLongStrings( std::ostream& stream, const std::string& str, std::size_t columns, std::size_t indent ) { - std::string::size_type pos = 0; - std::string::size_type newline = str.find_first_of( '\n' ); - while( newline != std::string::npos ) { - std::string paragraph = str.substr( pos, newline-pos ); - recursivelyWrapLine( stream, paragraph, columns, indent ); - stream << "\n"; - pos = newline+1; - newline = str.find_first_of( '\n', pos ); + std::ostream& LineWrapper::intoStream( std::ostream& stream ) const { + for( const_iterator it = begin(), itEnd = end(); + it != itEnd; ++it ) { + if( it != begin() ) + stream << "\n"; + stream << *it; } - if( pos != str.size() ) - recursivelyWrapLine( stream, str.substr( pos, str.size()-pos ), columns, indent ); + return stream; } - - std::string wrapLongStrings( const std::string& str, std::size_t columns, std::size_t indent ) { + std::string LineWrapper::toString() const { std::ostringstream oss; - wrapLongStrings( oss, str, columns, indent ); - return oss.str(); + intoStream( oss ); + return oss.str(); + } + + void LineWrapper::addLine( const std::string& _line ) { + if( tab > 0 ) + lines.push_back( indent + std::string( tab, ' ' ) + _line ); + else + lines.push_back( indent + _line ); + if( nextTab > 0 ) + tab = nextTab; } } // end namespace Catch diff --git a/include/reporters/catch_reporter_console.hpp b/include/reporters/catch_reporter_console.hpp index 042dbacc..b621aa6e 100644 --- a/include/reporters/catch_reporter_console.hpp +++ b/include/reporters/catch_reporter_console.hpp @@ -211,7 +211,7 @@ namespace Catch { if( result.hasExpandedExpression() ) { stream << "with expansion:\n"; TextColour colourGuard( TextColour::ReconstructedExpression ); - stream << wrapLongStrings( result.getExpandedExpression() ) << "\n"; + stream << LineWrapper().setIndent(2).wrap( result.getExpandedExpression() ).toString() << "\n"; } } void printMessage() const { @@ -219,8 +219,9 @@ namespace Catch { stream << messageLabel << ":" << "\n"; for( std::vector<MessageInfo>::const_iterator it = messages.begin(), itEnd = messages.end(); it != itEnd; - ++it ) { - stream << wrapLongStrings( it->message ) << "\n"; + ++it ) { + LineWrapper().setIndent(2).wrap( it->message ).intoStream( stream ); + stream << "\n"; } } void printSourceInfo() const { @@ -228,10 +229,6 @@ namespace Catch { stream << result.getSourceInfo() << ": "; } - static std::string wrapLongStrings( std::string const& _string ){ - return Catch::wrapLongStrings( _string, CATCH_CONFIG_CONSOLE_WIDTH-1, 2 ); - } - std::ostream& stream; AssertionStats const& stats; AssertionResult const& result; |