Detect and warn about overly complex expressions

This commit is contained in:
Phil Nash 2010-11-11 07:21:57 +00:00
parent 3d0fed768a
commit 48a70220a3
3 changed files with 22 additions and 11 deletions

View File

@ -38,6 +38,6 @@ TEST_CASE( "succeeding/Tricky/complex lhs", "Where the LHS is not a simple value
int a = 1;
int b = 2;
// !TBD: This only captures part of the expression
EXPECT( a == 2 || b == 2 );
// This only captures part of the expression, but issues a warning about the rest
EXPECT( a == 2 || b == 1 );
}

View File

@ -63,7 +63,7 @@ public:
template<typename RhsT>
MutableResultInfo& operator ||( const RhsT& rhs )
{
// !TBD: set message to say we haven't captured all parts
m_expressionIncomplete = true;
return *this;
}

View File

@ -40,7 +40,8 @@ namespace Catch
ResultInfo()
: m_result( ResultWas::Unknown ),
m_isNot( false ),
m_line( 0 )
m_line( 0 ),
m_expressionIncomplete( false )
{}
ResultInfo( const std::string& expr, ResultWas::OfType result, bool isNot, const std::string& filename, size_t line, const std::string& macroName )
@ -50,7 +51,8 @@ namespace Catch
m_op( m_expr[0] == '!' ? "!" : "" ),
m_filename( filename ),
m_line( line ),
m_macroName( macroName )
m_macroName( macroName ),
m_expressionIncomplete( false )
{
}
@ -78,12 +80,9 @@ namespace Catch
}
std::string getExpandedExpression() const
{
if( m_op == "" || m_isNot )
return m_lhs.empty() ? m_expr : m_op + m_lhs;
else if( m_op != "!" )
return m_lhs + " " + m_op + " " + m_rhs;
else
return "{can't expand - use " + m_macroName + "_NOT( " + m_expr.substr(1) + " ) instead of " + m_macroName + "( " + m_expr + " ) for better diagnostics}";
return m_expressionIncomplete
? getExpandedExpressionInternal() + " {can't expand the rest of the expression - consider rewriting it}"
: getExpandedExpressionInternal();
}
std::string getMessage() const
@ -106,6 +105,17 @@ namespace Catch
return m_macroName;
}
protected:
std::string getExpandedExpressionInternal() const
{
if( m_op == "" || m_isNot )
return m_lhs.empty() ? m_expr : m_op + m_lhs;
else if( m_op != "!" )
return m_lhs + " " + m_op + " " + m_rhs;
else
return "{can't expand - use " + m_macroName + "_NOT( " + m_expr.substr(1) + " ) instead of " + m_macroName + "( " + m_expr + " ) for better diagnostics}";
}
protected:
std::string m_macroName;
std::string m_filename;
@ -114,6 +124,7 @@ namespace Catch
std::string m_message;
ResultWas::OfType m_result;
bool m_isNot;
bool m_expressionIncomplete;
};
} // end namespace Catch