From 48a70220a3cd092a3ebb5e7a48ac8b302ed706fb Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Thu, 11 Nov 2010 07:21:57 +0000 Subject: [PATCH] Detect and warn about overly complex expressions --- Test/TrickyTests.cpp | 4 ++-- internal/catch_capture.hpp | 2 +- internal/catch_resultinfo.hpp | 27 +++++++++++++++++++-------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Test/TrickyTests.cpp b/Test/TrickyTests.cpp index e2fbb6be..9cb544ad 100644 --- a/Test/TrickyTests.cpp +++ b/Test/TrickyTests.cpp @@ -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 ); } \ No newline at end of file diff --git a/internal/catch_capture.hpp b/internal/catch_capture.hpp index 4392c415..e7533cac 100644 --- a/internal/catch_capture.hpp +++ b/internal/catch_capture.hpp @@ -63,7 +63,7 @@ public: template MutableResultInfo& operator ||( const RhsT& rhs ) { - // !TBD: set message to say we haven't captured all parts + m_expressionIncomplete = true; return *this; } diff --git a/internal/catch_resultinfo.hpp b/internal/catch_resultinfo.hpp index 5eae176e..99ccc830 100644 --- a/internal/catch_resultinfo.hpp +++ b/internal/catch_resultinfo.hpp @@ -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 @@ -105,6 +104,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; @@ -114,6 +124,7 @@ namespace Catch std::string m_message; ResultWas::OfType m_result; bool m_isNot; + bool m_expressionIncomplete; }; } // end namespace Catch