2013-12-03 19:52:41 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 27/11/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_COMMON_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_COMMON_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "catch_common.h"
|
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
bool startsWith( std::string const& s, std::string const& prefix ) {
|
|
|
|
return s.size() >= prefix.size() && s.substr( 0, prefix.size() ) == prefix;
|
|
|
|
}
|
|
|
|
bool endsWith( std::string const& s, std::string const& suffix ) {
|
|
|
|
return s.size() >= suffix.size() && s.substr( s.size()-suffix.size(), suffix.size() ) == suffix;
|
|
|
|
}
|
|
|
|
bool contains( std::string const& s, std::string const& infix ) {
|
|
|
|
return s.find( infix ) != std::string::npos;
|
|
|
|
}
|
|
|
|
void toLowerInPlace( std::string& s ) {
|
|
|
|
std::transform( s.begin(), s.end(), s.begin(), ::tolower );
|
|
|
|
}
|
|
|
|
std::string toLower( std::string const& s ) {
|
|
|
|
std::string lc = s;
|
|
|
|
toLowerInPlace( lc );
|
|
|
|
return lc;
|
|
|
|
}
|
|
|
|
std::string trim( std::string const& str ) {
|
|
|
|
static char const* whitespaceChars = "\n\r\t ";
|
|
|
|
std::string::size_type start = str.find_first_not_of( whitespaceChars );
|
|
|
|
std::string::size_type end = str.find_last_not_of( whitespaceChars );
|
|
|
|
|
|
|
|
return start != std::string::npos ? str.substr( start, 1+end-start ) : "";
|
|
|
|
}
|
2014-12-19 19:16:19 +01:00
|
|
|
|
|
|
|
bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) {
|
|
|
|
bool replaced = false;
|
|
|
|
std::size_t i = str.find( replaceThis );
|
|
|
|
while( i != std::string::npos ) {
|
|
|
|
replaced = true;
|
|
|
|
str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() );
|
|
|
|
if( i < str.size()-withThis.size() )
|
|
|
|
i = str.find( replaceThis, i+withThis.size() );
|
|
|
|
else
|
|
|
|
i = std::string::npos;
|
|
|
|
}
|
|
|
|
return replaced;
|
|
|
|
}
|
|
|
|
|
2013-12-03 19:52:41 +01:00
|
|
|
pluralise::pluralise( std::size_t count, std::string const& label )
|
|
|
|
: m_count( count ),
|
|
|
|
m_label( label )
|
|
|
|
{}
|
|
|
|
|
|
|
|
std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) {
|
|
|
|
os << pluraliser.m_count << " " << pluraliser.m_label;
|
|
|
|
if( pluraliser.m_count != 1 )
|
|
|
|
os << "s";
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLineInfo::SourceLineInfo() : line( 0 ){}
|
2013-12-04 09:12:30 +01:00
|
|
|
SourceLineInfo::SourceLineInfo( char const* _file, std::size_t _line )
|
2013-12-03 19:52:41 +01:00
|
|
|
: file( _file ),
|
|
|
|
line( _line )
|
|
|
|
{}
|
|
|
|
SourceLineInfo::SourceLineInfo( SourceLineInfo const& other )
|
|
|
|
: file( other.file ),
|
|
|
|
line( other.line )
|
|
|
|
{}
|
|
|
|
bool SourceLineInfo::empty() const {
|
|
|
|
return file.empty();
|
|
|
|
}
|
|
|
|
bool SourceLineInfo::operator == ( SourceLineInfo const& other ) const {
|
|
|
|
return line == other.line && file == other.file;
|
|
|
|
}
|
2015-03-04 08:08:53 +01:00
|
|
|
bool SourceLineInfo::operator < ( SourceLineInfo const& other ) const {
|
|
|
|
return line < other.line || ( line == other.line && file < other.file );
|
|
|
|
}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
|
|
|
std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ) {
|
|
|
|
#ifndef __GNUG__
|
|
|
|
os << info.file << "(" << info.line << ")";
|
|
|
|
#else
|
|
|
|
os << info.file << ":" << info.line;
|
|
|
|
#endif
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
void throwLogicError( std::string const& message, SourceLineInfo const& locationInfo ) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << locationInfo << ": Internal Catch error: '" << message << "'";
|
2014-05-28 19:53:01 +02:00
|
|
|
if( alwaysTrue() )
|
2013-12-03 19:52:41 +01:00
|
|
|
throw std::logic_error( oss.str() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_COMMON_HPP_INCLUDED
|
|
|
|
|