mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-22 13:26:10 +01:00
First cut of Evaluate refactoring to remove int specialisations, among other things
This commit is contained in:
parent
e991c006b7
commit
39753558eb
@ -21,7 +21,25 @@ namespace Catch {
|
|||||||
virtual bool isBinaryExpression() const {
|
virtual bool isBinaryExpression() const {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
virtual void reconstructExpression( std::string& dest ) const = 0;
|
virtual std::string reconstructExpression() const = 0;
|
||||||
|
|
||||||
|
std::string reconstructExpressionImpl( std::string const& lhs, std::string const& rhs, std::string const& op ) const {
|
||||||
|
std::string dest;
|
||||||
|
char delim = lhs.size() + rhs.size() < 40 &&
|
||||||
|
lhs.find('\n') == std::string::npos &&
|
||||||
|
rhs.find('\n') == std::string::npos ? ' ' : '\n';
|
||||||
|
dest.reserve( 7 + lhs.size() + rhs.size() );
|
||||||
|
// 2 for spaces around operator
|
||||||
|
// 2 for operator
|
||||||
|
// 2 for parentheses (conditionally added later)
|
||||||
|
// 1 for negation (conditionally added later)
|
||||||
|
dest = lhs;
|
||||||
|
dest += delim;
|
||||||
|
dest += op;
|
||||||
|
dest += delim;
|
||||||
|
dest += rhs;
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
// Only simple binary comparisons can be decomposed.
|
// Only simple binary comparisons can be decomposed.
|
||||||
// If more complex check is required then wrap sub-expressions in parentheses.
|
// If more complex check is required then wrap sub-expressions in parentheses.
|
||||||
@ -66,7 +84,7 @@ namespace Catch {
|
|||||||
|
|
||||||
std::string const& reconstructExpression() const {
|
std::string const& reconstructExpression() const {
|
||||||
if( decomposedExpression != CATCH_NULL ) {
|
if( decomposedExpression != CATCH_NULL ) {
|
||||||
decomposedExpression->reconstructExpression( reconstructedExpression );
|
reconstructedExpression = decomposedExpression->reconstructExpression();
|
||||||
if( parenthesized ) {
|
if( parenthesized ) {
|
||||||
reconstructedExpression.insert( 0, 1, '(' );
|
reconstructedExpression.insert( 0, 1, '(' );
|
||||||
reconstructedExpression.append( 1, ')' );
|
reconstructedExpression.append( 1, ')' );
|
||||||
|
@ -46,166 +46,86 @@ namespace Internal {
|
|||||||
|
|
||||||
// So the compare overloads can be operator agnostic we convey the operator as a template
|
// So the compare overloads can be operator agnostic we convey the operator as a template
|
||||||
// enum, which is used to specialise an Evaluator for doing the comparison.
|
// enum, which is used to specialise an Evaluator for doing the comparison.
|
||||||
template<typename T1, typename T2, Operator Op>
|
template<Operator Op>
|
||||||
class Evaluator{};
|
class Evaluator{};
|
||||||
|
|
||||||
template<typename T1, typename T2>
|
template<>
|
||||||
struct Evaluator<T1, T2, IsEqualTo> {
|
struct Evaluator<IsEqualTo> {
|
||||||
static bool evaluate( T1 const& lhs, T2 const& rhs) {
|
template<typename T1, typename T2>
|
||||||
return bool( opCast( lhs ) == opCast( rhs ) );
|
static bool evaluate( T1& lhs, T2& rhs) {
|
||||||
|
return static_cast<bool>( lhs == rhs );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
template<typename T1, typename T2>
|
template<>
|
||||||
struct Evaluator<T1, T2, IsNotEqualTo> {
|
struct Evaluator<IsNotEqualTo> {
|
||||||
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
|
template<typename T1, typename T2>
|
||||||
return bool( opCast( lhs ) != opCast( rhs ) );
|
static bool evaluate( T1& lhs, T2& rhs ) {
|
||||||
|
return bool( lhs != rhs );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
template<typename T1, typename T2>
|
template<>
|
||||||
struct Evaluator<T1, T2, IsLessThan> {
|
struct Evaluator<IsLessThan> {
|
||||||
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
|
template<typename T1, typename T2>
|
||||||
return bool( opCast( lhs ) < opCast( rhs ) );
|
static bool evaluate( T1& lhs, T2& rhs ) {
|
||||||
|
return bool( lhs < rhs );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
template<typename T1, typename T2>
|
template<>
|
||||||
struct Evaluator<T1, T2, IsGreaterThan> {
|
struct Evaluator<IsGreaterThan> {
|
||||||
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
|
template<typename T1, typename T2>
|
||||||
return bool( opCast( lhs ) > opCast( rhs ) );
|
static bool evaluate( T1& lhs, T2& rhs ) {
|
||||||
|
return bool( lhs > rhs );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
template<typename T1, typename T2>
|
template<>
|
||||||
struct Evaluator<T1, T2, IsGreaterThanOrEqualTo> {
|
struct Evaluator<IsGreaterThanOrEqualTo> {
|
||||||
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
|
template<typename T1, typename T2>
|
||||||
return bool( opCast( lhs ) >= opCast( rhs ) );
|
static bool evaluate( T1& lhs, T2& rhs ) {
|
||||||
|
return bool( lhs >= rhs );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
template<typename T1, typename T2>
|
template<>
|
||||||
struct Evaluator<T1, T2, IsLessThanOrEqualTo> {
|
struct Evaluator<IsLessThanOrEqualTo> {
|
||||||
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
|
template<typename T1, typename T2>
|
||||||
return bool( opCast( lhs ) <= opCast( rhs ) );
|
static bool evaluate( T1& lhs, T2& rhs ) {
|
||||||
|
return bool( lhs <= rhs );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<Operator Op, typename T1, typename T2>
|
|
||||||
bool applyEvaluator( T1 const& lhs, T2 const& rhs ) {
|
|
||||||
return Evaluator<T1, T2, Op>::evaluate( lhs, rhs );
|
|
||||||
}
|
|
||||||
|
|
||||||
// This level of indirection allows us to specialise for integer types
|
// This level of indirection allows us to specialise for integer types
|
||||||
// to avoid signed/ unsigned warnings
|
// to avoid signed/ unsigned warnings
|
||||||
|
|
||||||
// "base" overload
|
// "base" overload
|
||||||
template<Operator Op, typename T1, typename T2>
|
template<Operator Op, typename T1, typename T2>
|
||||||
bool compare( T1 const& lhs, T2 const& rhs ) {
|
bool compare( T1 const& lhs, T2 const& rhs ) {
|
||||||
return Evaluator<T1, T2, Op>::evaluate( lhs, rhs );
|
return Evaluator<Op>::evaluate( opCast( lhs ), opCast( rhs ) );
|
||||||
}
|
|
||||||
|
|
||||||
// unsigned X to int
|
|
||||||
template<Operator Op> bool compare( unsigned int lhs, int rhs ) {
|
|
||||||
return applyEvaluator<Op>( lhs, static_cast<unsigned int>( rhs ) );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( unsigned long lhs, int rhs ) {
|
|
||||||
return applyEvaluator<Op>( lhs, static_cast<unsigned int>( rhs ) );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( unsigned char lhs, int rhs ) {
|
|
||||||
return applyEvaluator<Op>( lhs, static_cast<unsigned int>( rhs ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
// unsigned X to long
|
|
||||||
template<Operator Op> bool compare( unsigned int lhs, long rhs ) {
|
|
||||||
return applyEvaluator<Op>( lhs, static_cast<unsigned long>( rhs ) );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( unsigned long lhs, long rhs ) {
|
|
||||||
return applyEvaluator<Op>( lhs, static_cast<unsigned long>( rhs ) );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( unsigned char lhs, long rhs ) {
|
|
||||||
return applyEvaluator<Op>( lhs, static_cast<unsigned long>( rhs ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
// int to unsigned X
|
|
||||||
template<Operator Op> bool compare( int lhs, unsigned int rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<unsigned int>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( int lhs, unsigned long rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<unsigned int>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( int lhs, unsigned char rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<unsigned int>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
|
|
||||||
// long to unsigned X
|
|
||||||
template<Operator Op> bool compare( long lhs, unsigned int rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( long lhs, unsigned long rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( long lhs, unsigned char rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// pointer to long (when comparing against NULL)
|
// pointer to long (when comparing against NULL)
|
||||||
template<Operator Op, typename T> bool compare( long lhs, T* rhs ) {
|
template<Operator Op, typename T> bool compare( long lhs, T* rhs ) {
|
||||||
return Evaluator<T*, T*, Op>::evaluate( reinterpret_cast<T*>( lhs ), rhs );
|
return Evaluator<Op>::evaluate( opCast( reinterpret_cast<T*>( lhs ) ), opCast( rhs ) );
|
||||||
}
|
}
|
||||||
template<Operator Op, typename T> bool compare( T* lhs, long rhs ) {
|
template<Operator Op, typename T> bool compare( T* lhs, long rhs ) {
|
||||||
return Evaluator<T*, T*, Op>::evaluate( lhs, reinterpret_cast<T*>( rhs ) );
|
return Evaluator<Op>::evaluate( opCast( lhs ), opCast( reinterpret_cast<T*>( rhs ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
// pointer to int (when comparing against NULL)
|
// pointer to int (when comparing against NULL)
|
||||||
template<Operator Op, typename T> bool compare( int lhs, T* rhs ) {
|
template<Operator Op, typename T> bool compare( int lhs, T* rhs ) {
|
||||||
return Evaluator<T*, T*, Op>::evaluate( reinterpret_cast<T*>( lhs ), rhs );
|
return Evaluator<Op>::evaluate( opCast( reinterpret_cast<T*>( lhs ) ), opCast( rhs ) );
|
||||||
}
|
}
|
||||||
template<Operator Op, typename T> bool compare( T* lhs, int rhs ) {
|
template<Operator Op, typename T> bool compare( T* lhs, int rhs ) {
|
||||||
return Evaluator<T*, T*, Op>::evaluate( lhs, reinterpret_cast<T*>( rhs ) );
|
return Evaluator<Op>::evaluate( opCast( lhs ), opCast( reinterpret_cast<T*>( rhs ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CATCH_CONFIG_CPP11_LONG_LONG
|
// Needed?
|
||||||
// long long to unsigned X
|
|
||||||
template<Operator Op> bool compare( long long lhs, unsigned int rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( long long lhs, unsigned long rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( long long lhs, unsigned long long rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( long long lhs, unsigned char rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
|
|
||||||
// unsigned long long to X
|
|
||||||
template<Operator Op> bool compare( unsigned long long lhs, int rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<long>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( unsigned long long lhs, long rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<long>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( unsigned long long lhs, long long rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<long>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
template<Operator Op> bool compare( unsigned long long lhs, char rhs ) {
|
|
||||||
return applyEvaluator<Op>( static_cast<long>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
|
|
||||||
// pointer to long long (when comparing against NULL)
|
|
||||||
template<Operator Op, typename T> bool compare( long long lhs, T* rhs ) {
|
|
||||||
return Evaluator<T*, T*, Op>::evaluate( reinterpret_cast<T*>( lhs ), rhs );
|
|
||||||
}
|
|
||||||
template<Operator Op, typename T> bool compare( T* lhs, long long rhs ) {
|
|
||||||
return Evaluator<T*, T*, Op>::evaluate( lhs, reinterpret_cast<T*>( rhs ) );
|
|
||||||
}
|
|
||||||
#endif // CATCH_CONFIG_CPP11_LONG_LONG
|
|
||||||
|
|
||||||
#ifdef CATCH_CONFIG_CPP11_NULLPTR
|
#ifdef CATCH_CONFIG_CPP11_NULLPTR
|
||||||
// pointer to nullptr_t (when comparing against nullptr)
|
// // pointer to nullptr_t (when comparing against nullptr)
|
||||||
template<Operator Op, typename T> bool compare( std::nullptr_t, T* rhs ) {
|
// template<Operator Op, typename T> bool compare( std::nullptr_t, T* rhs ) {
|
||||||
return Evaluator<T*, T*, Op>::evaluate( nullptr, rhs );
|
// return Evaluator<T*, T*, Op>::evaluate( nullptr, rhs );
|
||||||
}
|
// }
|
||||||
template<Operator Op, typename T> bool compare( T* lhs, std::nullptr_t ) {
|
// template<Operator Op, typename T> bool compare( T* lhs, std::nullptr_t ) {
|
||||||
return Evaluator<T*, T*, Op>::evaluate( lhs, nullptr );
|
// return Evaluator<T*, T*, Op>::evaluate( lhs, nullptr );
|
||||||
}
|
// }
|
||||||
#endif // CATCH_CONFIG_CPP11_NULLPTR
|
#endif // CATCH_CONFIG_CPP11_NULLPTR
|
||||||
|
|
||||||
} // end of namespace Internal
|
} // end of namespace Internal
|
||||||
|
@ -78,8 +78,8 @@ public:
|
|||||||
.endExpression( *this );
|
.endExpression( *this );
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void reconstructExpression( std::string& dest ) const CATCH_OVERRIDE {
|
virtual std::string reconstructExpression() const CATCH_OVERRIDE {
|
||||||
dest = Catch::toString( m_truthy );
|
return Catch::toString( m_truthy );
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -115,22 +115,11 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void reconstructExpression( std::string& dest ) const CATCH_OVERRIDE {
|
virtual std::string reconstructExpression() const CATCH_OVERRIDE {
|
||||||
std::string lhs = Catch::toString( m_lhs );
|
return reconstructExpressionImpl
|
||||||
std::string rhs = Catch::toString( m_rhs );
|
(Catch::toString( m_lhs ),
|
||||||
char delim = lhs.size() + rhs.size() < 40 &&
|
Catch::toString( m_rhs ),
|
||||||
lhs.find('\n') == std::string::npos &&
|
Internal::OperatorTraits<Op>::getName() );
|
||||||
rhs.find('\n') == std::string::npos ? ' ' : '\n';
|
|
||||||
dest.reserve( 7 + lhs.size() + rhs.size() );
|
|
||||||
// 2 for spaces around operator
|
|
||||||
// 2 for operator
|
|
||||||
// 2 for parentheses (conditionally added later)
|
|
||||||
// 1 for negation (conditionally added later)
|
|
||||||
dest = lhs;
|
|
||||||
dest += delim;
|
|
||||||
dest += Internal::OperatorTraits<Op>::getName();
|
|
||||||
dest += delim;
|
|
||||||
dest += rhs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -149,14 +138,14 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void reconstructExpression( std::string& dest ) const CATCH_OVERRIDE {
|
virtual std::string reconstructExpression() const CATCH_OVERRIDE {
|
||||||
std::string matcherAsString = m_matcher.toString();
|
std::string matcherAsString = m_matcher.toString();
|
||||||
dest = Catch::toString( m_arg );
|
std::string dest = Catch::toString( m_arg ) + " ";
|
||||||
dest += ' ';
|
|
||||||
if( matcherAsString == Detail::unprintableString )
|
if( matcherAsString == Detail::unprintableString )
|
||||||
dest += m_matcherString;
|
dest += m_matcherString;
|
||||||
else
|
else
|
||||||
dest += matcherAsString;
|
dest += matcherAsString;
|
||||||
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -55,7 +55,7 @@ namespace Catch {
|
|||||||
|
|
||||||
void endExpression( DecomposedExpression const& expr );
|
void endExpression( DecomposedExpression const& expr );
|
||||||
|
|
||||||
virtual void reconstructExpression( std::string& dest ) const CATCH_OVERRIDE;
|
virtual std::string reconstructExpression() const CATCH_OVERRIDE;
|
||||||
|
|
||||||
AssertionResult build() const;
|
AssertionResult build() const;
|
||||||
AssertionResult build( DecomposedExpression const& expr ) const;
|
AssertionResult build( DecomposedExpression const& expr ) const;
|
||||||
|
@ -132,8 +132,8 @@ namespace Catch {
|
|||||||
return AssertionResult( m_assertionInfo, data );
|
return AssertionResult( m_assertionInfo, data );
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResultBuilder::reconstructExpression( std::string& dest ) const {
|
std::string ResultBuilder::reconstructExpression() const {
|
||||||
dest = m_assertionInfo.capturedExpression;
|
return m_assertionInfo.capturedExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end namespace Catch
|
} // end namespace Catch
|
||||||
|
Loading…
Reference in New Issue
Block a user