mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 20:27:11 +01:00 
			
		
		
		
	Folded ExpressionResultBuilder into ResultBuilder
(even more SRP violations!)
This commit is contained in:
		| @@ -39,7 +39,7 @@ | ||||
|             __catchResult.useActiveException( Catch::ResultDisposition::Normal ); \ | ||||
|         } \ | ||||
|         INTERNAL_CATCH_REACT( __catchResult ) \ | ||||
|     } while( Catch::alwaysFalse() && (expr) ) // expr here is never evaluated at runtime but it forces the compiler to give it a look | ||||
|     } while( Catch::isTrue( false && (expr) ) ) // expr here is never evaluated at runtime but it forces the compiler to give it a look | ||||
|  | ||||
| /////////////////////////////////////////////////////////////////////////////// | ||||
| #define INTERNAL_CATCH_IF( expr, resultDisposition, macroName ) \ | ||||
| @@ -104,7 +104,7 @@ | ||||
|     #define INTERNAL_CATCH_MSG( messageType, resultDisposition, macroName, ... ) \ | ||||
|         do { \ | ||||
|             Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \ | ||||
|             __catchResult.m_resultBuilder << __VA_ARGS__ + ::Catch::StreamEndStop(); \ | ||||
|             __catchResult << __VA_ARGS__ + ::Catch::StreamEndStop(); \ | ||||
|             __catchResult.captureResult( messageType ); \ | ||||
|             INTERNAL_CATCH_REACT( __catchResult ) \ | ||||
|         } while( Catch::alwaysFalse() ) | ||||
| @@ -112,7 +112,7 @@ | ||||
|     #define INTERNAL_CATCH_MSG( messageType, resultDisposition, macroName, log ) \ | ||||
|         do { \ | ||||
|             Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \ | ||||
|             __catchResult.m_resultBuilder << log + ::Catch::StreamEndStop(); \ | ||||
|             __catchResult << log + ::Catch::StreamEndStop(); \ | ||||
|             __catchResult.captureResult( messageType ); \ | ||||
|             INTERNAL_CATCH_REACT( __catchResult ) \ | ||||
|         } while( Catch::alwaysFalse() ) | ||||
| @@ -128,7 +128,7 @@ | ||||
|         Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #arg " " #matcher, resultDisposition ); \ | ||||
|         try { \ | ||||
|             std::string matcherAsString = ::Catch::Matchers::matcher.toString(); \ | ||||
|             __catchResult.m_resultBuilder \ | ||||
|             __catchResult \ | ||||
|                 .setLhs( Catch::toString( arg ) ) \ | ||||
|                 .setRhs( matcherAsString == "{?}" ? #matcher : matcherAsString ) \ | ||||
|                 .setOp( "matches" ) \ | ||||
|   | ||||
| @@ -9,20 +9,13 @@ | ||||
| #define TWOBLUECUBES_CATCH_EXPRESSION_LHS_HPP_INCLUDED | ||||
|  | ||||
| #include "catch_result_builder.h" | ||||
| #include "catch_expressionresult_builder.h" | ||||
| #include "catch_evaluate.hpp" | ||||
| #include "catch_tostring.h" | ||||
|  | ||||
| #include <assert.h> | ||||
|  | ||||
| namespace Catch { | ||||
|  | ||||
| struct ResultBuilder; | ||||
|  | ||||
| ExpressionResultBuilder& getResultBuilder( ResultBuilder* rb ); | ||||
|  | ||||
| // Wraps the LHS of an expression and captures the operator and RHS (if any) - | ||||
| // wrapping them all in an ExpressionResultBuilder object | ||||
| // wrapping them all in a ResultBuilder object | ||||
| template<typename T> | ||||
| class ExpressionLhs { | ||||
|     ExpressionLhs& operator = ( ExpressionLhs const& ); | ||||
| @@ -31,54 +24,53 @@ class ExpressionLhs { | ||||
| #  endif | ||||
|  | ||||
| public: | ||||
|     ExpressionLhs( ResultBuilder& rb, T lhs ) : m_rb( &rb ), m_lhs( lhs ) {} | ||||
|     ExpressionLhs( ResultBuilder& rb, T lhs ) : m_rb( rb ), m_lhs( lhs ) {} | ||||
| #  ifdef CATCH_CPP11_OR_GREATER | ||||
|     ExpressionLhs( ExpressionLhs const& ) = default; | ||||
|     ExpressionLhs( ExpressionLhs && )     = default; | ||||
| #  endif | ||||
|  | ||||
|     template<typename RhsT> | ||||
|     ExpressionResultBuilder& operator == ( RhsT const& rhs ) { | ||||
|     ResultBuilder& operator == ( RhsT const& rhs ) { | ||||
|         return captureExpression<Internal::IsEqualTo>( rhs ); | ||||
|     } | ||||
|  | ||||
|     template<typename RhsT> | ||||
|     ExpressionResultBuilder& operator != ( RhsT const& rhs ) { | ||||
|     ResultBuilder& operator != ( RhsT const& rhs ) { | ||||
|         return captureExpression<Internal::IsNotEqualTo>( rhs ); | ||||
|     } | ||||
|  | ||||
|     template<typename RhsT> | ||||
|     ExpressionResultBuilder& operator < ( RhsT const& rhs ) { | ||||
|     ResultBuilder& operator < ( RhsT const& rhs ) { | ||||
|         return captureExpression<Internal::IsLessThan>( rhs ); | ||||
|     } | ||||
|  | ||||
|     template<typename RhsT> | ||||
|     ExpressionResultBuilder& operator > ( RhsT const& rhs ) { | ||||
|     ResultBuilder& operator > ( RhsT const& rhs ) { | ||||
|         return captureExpression<Internal::IsGreaterThan>( rhs ); | ||||
|     } | ||||
|  | ||||
|     template<typename RhsT> | ||||
|     ExpressionResultBuilder& operator <= ( RhsT const& rhs ) { | ||||
|     ResultBuilder& operator <= ( RhsT const& rhs ) { | ||||
|         return captureExpression<Internal::IsLessThanOrEqualTo>( rhs ); | ||||
|     } | ||||
|  | ||||
|     template<typename RhsT> | ||||
|     ExpressionResultBuilder& operator >= ( RhsT const& rhs ) { | ||||
|     ResultBuilder& operator >= ( RhsT const& rhs ) { | ||||
|         return captureExpression<Internal::IsGreaterThanOrEqualTo>( rhs ); | ||||
|     } | ||||
|  | ||||
|     ExpressionResultBuilder& operator == ( bool rhs ) { | ||||
|     ResultBuilder& operator == ( bool rhs ) { | ||||
|         return captureExpression<Internal::IsEqualTo>( rhs ); | ||||
|     } | ||||
|  | ||||
|     ExpressionResultBuilder& operator != ( bool rhs ) { | ||||
|     ResultBuilder& operator != ( bool rhs ) { | ||||
|         return captureExpression<Internal::IsNotEqualTo>( rhs ); | ||||
|     } | ||||
|  | ||||
|     void endExpression() { | ||||
|         assert( m_rb ); | ||||
|         bool value = m_lhs ? true : false; | ||||
|         getResultBuilder( m_rb ) | ||||
|         m_rb | ||||
|             .setLhs( Catch::toString( value ) ) | ||||
|             .setResultType( value ) | ||||
|             .endExpression(); | ||||
| @@ -95,9 +87,8 @@ public: | ||||
|  | ||||
| private: | ||||
|     template<Internal::Operator Op, typename RhsT> | ||||
|     ExpressionResultBuilder& captureExpression( RhsT const& rhs ) { | ||||
|         assert( m_rb ); | ||||
|         return getResultBuilder( m_rb ) | ||||
|     ResultBuilder& captureExpression( RhsT const& rhs ) { | ||||
|         return m_rb | ||||
|             .setResultType( Internal::compare<Op>( m_lhs, rhs ) ) | ||||
|             .setLhs( Catch::toString( m_lhs ) ) | ||||
|             .setRhs( Catch::toString( rhs ) ) | ||||
| @@ -105,7 +96,7 @@ private: | ||||
|     } | ||||
|  | ||||
| private: | ||||
|     ResultBuilder* m_rb; | ||||
|     ResultBuilder& m_rb; | ||||
|     T m_lhs; | ||||
| }; | ||||
|  | ||||
|   | ||||
| @@ -1,65 +0,0 @@ | ||||
| /* | ||||
|  *  Created by Phil on 8/5/2012. | ||||
|  *  Copyright 2012 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_ASSERTIONRESULT_BUILDER_H_INCLUDED | ||||
| #define TWOBLUECUBES_CATCH_ASSERTIONRESULT_BUILDER_H_INCLUDED | ||||
|  | ||||
| #include "catch_tostring.h" | ||||
| #include "catch_assertionresult.h" | ||||
| #include "catch_result_type.h" | ||||
| #include "catch_evaluate.hpp" | ||||
| #include "catch_common.h" | ||||
|  | ||||
| namespace Catch { | ||||
|  | ||||
| struct STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison; | ||||
| struct ResultBuilder; | ||||
|  | ||||
| // Wraps the (stringised versions of) the lhs, operator and rhs of an expression - as well as | ||||
| // the result of evaluating it. This is used to build an AssertionResult object | ||||
| class ExpressionResultBuilder { | ||||
| public: | ||||
|  | ||||
|     ExpressionResultBuilder( ResultBuilder* rb, ResultWas::OfType resultType = ResultWas::Unknown ); | ||||
|     ExpressionResultBuilder( ExpressionResultBuilder const& other ); | ||||
|     ExpressionResultBuilder& operator=( ExpressionResultBuilder const& other ); | ||||
|  | ||||
|     ExpressionResultBuilder& setResultType( ResultWas::OfType result ); | ||||
|     ExpressionResultBuilder& setResultType( bool result ); | ||||
|     ExpressionResultBuilder& setLhs( std::string const& lhs ); | ||||
|     ExpressionResultBuilder& setRhs( std::string const& rhs ); | ||||
|     ExpressionResultBuilder& setOp( std::string const& op ); | ||||
|  | ||||
|     void endExpression(); | ||||
|  | ||||
|     template<typename T> | ||||
|     ExpressionResultBuilder& operator << ( T const& value ) { | ||||
|         m_stream << value; | ||||
|         return *this; | ||||
|     } | ||||
|  | ||||
|     std::string reconstructExpression( AssertionInfo const& info ) const; | ||||
|  | ||||
|     AssertionResult buildResult( AssertionInfo const& info ) const; | ||||
|  | ||||
|     template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator && ( RhsT const& ); | ||||
|     template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator || ( RhsT const& ); | ||||
|  | ||||
| private: | ||||
|     ResultBuilder* m_rb; | ||||
|     AssertionResultData m_data; | ||||
|     struct ExprComponents { | ||||
|         ExprComponents() : shouldNegate( false ) {} | ||||
|         bool shouldNegate; | ||||
|         std::string lhs, rhs, op; | ||||
|     } m_exprComponents; | ||||
|     std::ostringstream m_stream; | ||||
| }; | ||||
|  | ||||
| } // end namespace Catch | ||||
|  | ||||
| #endif // TWOBLUECUBES_CATCH_ASSERTIONRESULT_BUILDER_H_INCLUDED | ||||
| @@ -1,103 +0,0 @@ | ||||
| /* | ||||
|  *  Created by Phil on 8/8/2012. | ||||
|  *  Copyright 2012 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_EXPRESSIONRESULT_BUILDER_HPP_INCLUDED | ||||
| #define TWOBLUECUBES_CATCH_EXPRESSIONRESULT_BUILDER_HPP_INCLUDED | ||||
|  | ||||
| #include "catch_expressionresult_builder.h" | ||||
| #include "catch_result_builder.h" | ||||
|  | ||||
| #include <assert.h> | ||||
|  | ||||
| namespace Catch { | ||||
|  | ||||
|     ExpressionResultBuilder::ExpressionResultBuilder( ResultBuilder* rb, ResultWas::OfType resultType ) | ||||
|     : m_rb( rb ) { | ||||
|         m_data.resultType = resultType; | ||||
|     } | ||||
|     ExpressionResultBuilder::ExpressionResultBuilder( ExpressionResultBuilder const& other ) | ||||
|     :   m_rb( other.m_rb ), | ||||
|         m_data( other.m_data ), | ||||
|         m_exprComponents( other.m_exprComponents ) | ||||
|     { | ||||
|         m_stream << other.m_stream.str(); | ||||
|     } | ||||
|     ExpressionResultBuilder& ExpressionResultBuilder::operator=(ExpressionResultBuilder const& other ) { | ||||
|         m_rb = other.m_rb; | ||||
|         m_data = other.m_data; | ||||
|         m_exprComponents = other.m_exprComponents; | ||||
|         m_stream.str(""); | ||||
|         m_stream << other.m_stream.str(); | ||||
|         return *this; | ||||
|     } | ||||
|     ExpressionResultBuilder& ExpressionResultBuilder::setResultType( ResultWas::OfType result ) { | ||||
|         m_data.resultType = result; | ||||
|         return *this; | ||||
|     } | ||||
|     ExpressionResultBuilder& ExpressionResultBuilder::setResultType( bool result ) { | ||||
|         m_data.resultType = result ? ResultWas::Ok : ResultWas::ExpressionFailed; | ||||
|         return *this; | ||||
|     } | ||||
|     void ExpressionResultBuilder::endExpression() { | ||||
|         m_exprComponents.shouldNegate = shouldNegate( m_rb->m_assertionInfo.resultDisposition ); | ||||
|         m_rb->captureExpression(); | ||||
|     } | ||||
|     ExpressionResultBuilder& ExpressionResultBuilder::setLhs( std::string const& lhs ) { | ||||
|         m_exprComponents.lhs = lhs; | ||||
|         return *this; | ||||
|     } | ||||
|     ExpressionResultBuilder& ExpressionResultBuilder::setRhs( std::string const& rhs ) { | ||||
|         m_exprComponents.rhs = rhs; | ||||
|         return *this; | ||||
|     } | ||||
|     ExpressionResultBuilder& ExpressionResultBuilder::setOp( std::string const& op ) { | ||||
|         m_exprComponents.op = op; | ||||
|         return *this; | ||||
|     } | ||||
|  | ||||
|     AssertionResult ExpressionResultBuilder::buildResult( AssertionInfo const& info ) const | ||||
|     { | ||||
|         assert( m_data.resultType != ResultWas::Unknown ); | ||||
|  | ||||
|         AssertionResultData data = m_data; | ||||
|  | ||||
|         // Flip bool results if shouldNegate is set | ||||
|         if( m_exprComponents.shouldNegate && data.resultType == ResultWas::Ok ) | ||||
|             data.resultType = ResultWas::ExpressionFailed; | ||||
|         else if( m_exprComponents.shouldNegate && data.resultType == ResultWas::ExpressionFailed ) | ||||
|             data.resultType = ResultWas::Ok; | ||||
|  | ||||
|         data.message = m_stream.str(); | ||||
|         data.reconstructedExpression = reconstructExpression( info ); | ||||
|         if( m_exprComponents.shouldNegate ) { | ||||
|             if( m_exprComponents.op == "" ) | ||||
|                 data.reconstructedExpression = "!" + data.reconstructedExpression; | ||||
|             else | ||||
|                 data.reconstructedExpression = "!(" + data.reconstructedExpression + ")"; | ||||
|         } | ||||
|         return AssertionResult( info, data ); | ||||
|     } | ||||
|     std::string ExpressionResultBuilder::reconstructExpression( AssertionInfo const& info ) const { | ||||
|         if( m_exprComponents.op == "" ) | ||||
|             return m_exprComponents.lhs.empty() ? info.capturedExpression : m_exprComponents.op + m_exprComponents.lhs; | ||||
|         else if( m_exprComponents.op == "matches" ) | ||||
|             return m_exprComponents.lhs + " " + m_exprComponents.rhs; | ||||
|         else if( m_exprComponents.op != "!" ) { | ||||
|             if( m_exprComponents.lhs.size() + m_exprComponents.rhs.size() < 40 && | ||||
|                 m_exprComponents.lhs.find("\n") == std::string::npos && | ||||
|                 m_exprComponents.rhs.find("\n") == std::string::npos ) | ||||
|                 return m_exprComponents.lhs + " " + m_exprComponents.op + " " + m_exprComponents.rhs; | ||||
|             else | ||||
|                 return m_exprComponents.lhs + "\n" + m_exprComponents.op + "\n" + m_exprComponents.rhs; | ||||
|         } | ||||
|         else | ||||
|             return "{can't expand - use " + info.macroName + "_FALSE( " + info.capturedExpression.substr(1) + " ) instead of " + info.macroName + "( " + info.capturedExpression + " ) for better diagnostics}"; | ||||
|     } | ||||
|  | ||||
| } // end namespace Catch | ||||
|  | ||||
| #endif // TWOBLUECUBES_CATCH_EXPRESSIONRESULT_BUILDER_HPP_INCLUDED | ||||
| @@ -23,7 +23,6 @@ | ||||
| #include "catch_console_colour_impl.hpp" | ||||
| #include "catch_generators_impl.hpp" | ||||
| #include "catch_assertionresult.hpp" | ||||
| #include "catch_expressionresult_builder.hpp" | ||||
| #include "catch_test_case_info.hpp" | ||||
| #include "catch_test_spec.hpp" | ||||
| #include "catch_version.hpp" | ||||
|   | ||||
| @@ -15,7 +15,6 @@ | ||||
| namespace Catch { | ||||
|  | ||||
|     class TestCase; | ||||
|     class ExpressionResultBuilder; | ||||
|     class AssertionResult; | ||||
|     struct AssertionInfo; | ||||
|     struct SectionInfo; | ||||
|   | ||||
| @@ -46,10 +46,10 @@ namespace Catch | ||||
|                     it != itEnd; | ||||
|                     ++it ) { | ||||
|                 if( it->type == ResultWas::Info ) { | ||||
|                     ExpressionResultBuilder expressionBuilder( NULL, it->type ); | ||||
|                         expressionBuilder << it->message; | ||||
|                     AssertionInfo info( it->macroName, it->lineInfo, "", ResultDisposition::Normal ); | ||||
|                     AssertionResult result = expressionBuilder.buildResult( info ); | ||||
|                     ResultBuilder rb( it->macroName.c_str(), it->lineInfo, "", ResultDisposition::Normal ); | ||||
|                     rb << it->message; | ||||
|                     rb.setResultType( ResultWas::Info ); | ||||
|                     AssertionResult result = rb.build(); | ||||
|                     m_legacyReporter->Result( result ); | ||||
|                 } | ||||
|             } | ||||
|   | ||||
| @@ -8,28 +8,61 @@ | ||||
| #ifndef TWOBLUECUBES_CATCH_RESULT_BUILDER_H_INCLUDED | ||||
| #define TWOBLUECUBES_CATCH_RESULT_BUILDER_H_INCLUDED | ||||
|  | ||||
| #include "catch_expression_lhs.hpp" | ||||
| #include "catch_expressionresult_builder.h" | ||||
| #include "catch_result_type.h" | ||||
| #include "catch_assertionresult.h" | ||||
| #include "catch_common.h" | ||||
|  | ||||
| namespace Catch { | ||||
|  | ||||
|     struct TestFailureException{}; | ||||
|  | ||||
|     struct ResultBuilder { | ||||
|     template<typename T> class ExpressionLhs; | ||||
|  | ||||
|     struct STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison; | ||||
|  | ||||
|     struct CopyableStream { | ||||
|         CopyableStream() {} | ||||
|         CopyableStream( CopyableStream const& other ) { | ||||
|             oss << other.oss.str(); | ||||
|         } | ||||
|         CopyableStream& operator=( CopyableStream const& other ) { | ||||
|             oss.str(""); | ||||
|             oss << other.oss.str(); | ||||
|             return *this; | ||||
|         } | ||||
|         std::ostringstream oss; | ||||
|     }; | ||||
|  | ||||
|     class ResultBuilder { | ||||
|     public: | ||||
|         ResultBuilder(  char const* macroName, | ||||
|                         SourceLineInfo const& lineInfo, | ||||
|                         char const* capturedExpression, | ||||
|                         ResultDisposition::Flags resultDisposition ); | ||||
|  | ||||
|         template<typename T> | ||||
|         ExpressionLhs<T const&> operator->* ( T const& operand ) { | ||||
|             return ExpressionLhs<T const&>( *this, operand ); | ||||
|         ExpressionLhs<T const&> operator->* ( T const& operand ); | ||||
|         ExpressionLhs<bool> operator->* ( bool value ); | ||||
|  | ||||
|         template<typename T> | ||||
|         ResultBuilder& operator << ( T const& value ) { | ||||
|             m_stream.oss << value; | ||||
|             return *this; | ||||
|         } | ||||
|  | ||||
|         ExpressionLhs<bool> operator->* ( bool value ) { | ||||
|             return ExpressionLhs<bool>( *this, value ); | ||||
|         } | ||||
|         template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator && ( RhsT const& ); | ||||
|         template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator || ( RhsT const& ); | ||||
|  | ||||
|         ResultBuilder& setResultType( ResultWas::OfType result ); | ||||
|         ResultBuilder& setResultType( bool result ); | ||||
|         ResultBuilder& setLhs( std::string const& lhs ); | ||||
|         ResultBuilder& setRhs( std::string const& rhs ); | ||||
|         ResultBuilder& setOp( std::string const& op ); | ||||
|  | ||||
|         void endExpression(); | ||||
|  | ||||
|         std::string reconstructExpression() const; | ||||
|         AssertionResult build() const; | ||||
|  | ||||
|         void useActiveException( ResultDisposition::Flags resultDisposition = ResultDisposition::Normal ); | ||||
|         void captureResult( ResultWas::OfType resultType ); | ||||
| @@ -38,12 +71,36 @@ namespace Catch { | ||||
|         bool shouldDebugBreak() const; | ||||
|         bool allowThrows() const; | ||||
|  | ||||
|     private: | ||||
|         AssertionInfo m_assertionInfo; | ||||
|         ExpressionResultBuilder m_resultBuilder; | ||||
|         AssertionResultData m_data; | ||||
|         struct ExprComponents { | ||||
|             ExprComponents() : testFalse( false ) {} | ||||
|             bool testFalse; | ||||
|             std::string lhs, rhs, op; | ||||
|         } m_exprComponents; | ||||
|         CopyableStream m_stream; | ||||
|          | ||||
|         bool m_shouldDebugBreak; | ||||
|         bool m_shouldThrow; | ||||
|     }; | ||||
|  | ||||
| } // end namespace Catch | ||||
| } // namespace Catch | ||||
|  | ||||
| // Include after due to circular dependency: | ||||
| #include "catch_expression_lhs.hpp" | ||||
|  | ||||
| namespace Catch { | ||||
|  | ||||
|     template<typename T> | ||||
|     inline ExpressionLhs<T const&> ResultBuilder::operator->* ( T const& operand ) { | ||||
|         return ExpressionLhs<T const&>( *this, operand ); | ||||
|     } | ||||
|  | ||||
|     inline ExpressionLhs<bool> ResultBuilder::operator->* ( bool value ) { | ||||
|         return ExpressionLhs<bool>( *this, value ); | ||||
|     } | ||||
|  | ||||
| } // namespace Catch | ||||
|  | ||||
| #endif // TWOBLUECUBES_CATCH_RESULT_BUILDER_H_INCLUDED | ||||
|   | ||||
| @@ -9,9 +9,7 @@ | ||||
| #define TWOBLUECUBES_CATCH_RESULT_BUILDER_HPP_INCLUDED | ||||
|  | ||||
| #include "catch_result_builder.h" | ||||
| #include "catch_expressionresult_builder.h" | ||||
| #include "catch_context.h" | ||||
| #include "catch_common.h" | ||||
| #include "catch_interfaces_config.h" | ||||
| #include "catch_interfaces_runner.h" | ||||
| #include "catch_interfaces_capture.h" | ||||
| @@ -20,33 +18,58 @@ | ||||
|  | ||||
| namespace Catch { | ||||
|  | ||||
|     ExpressionResultBuilder& getResultBuilder( ResultBuilder* rb ) { | ||||
|         return rb->m_resultBuilder; | ||||
|     inline bool isFalseTest( int flags ) { | ||||
|         return ( flags & ResultDisposition::NegateResult ) != 0; | ||||
|     } | ||||
|  | ||||
|     ResultBuilder::ResultBuilder(   char const* macroName, | ||||
|                                     SourceLineInfo const& lineInfo, | ||||
|                                     char const* capturedExpression, | ||||
|                                     ResultDisposition::Flags resultDisposition ) | ||||
|         :   m_assertionInfo( macroName, lineInfo, capturedExpression, resultDisposition ), | ||||
|             m_resultBuilder( this ), | ||||
|             m_shouldDebugBreak( false ), | ||||
|             m_shouldThrow( false ) | ||||
|         {} | ||||
|     :   m_assertionInfo( macroName, lineInfo, capturedExpression, resultDisposition ), | ||||
|         m_shouldDebugBreak( false ), | ||||
|         m_shouldThrow( false ) | ||||
|     {} | ||||
|  | ||||
|     ResultBuilder& ResultBuilder::setResultType( ResultWas::OfType result ) { | ||||
|         m_data.resultType = result; | ||||
|         return *this; | ||||
|     } | ||||
|     ResultBuilder& ResultBuilder::setResultType( bool result ) { | ||||
|         m_data.resultType = result ? ResultWas::Ok : ResultWas::ExpressionFailed; | ||||
|         return *this; | ||||
|     } | ||||
|     ResultBuilder& ResultBuilder::setLhs( std::string const& lhs ) { | ||||
|         m_exprComponents.lhs = lhs; | ||||
|         return *this; | ||||
|     } | ||||
|     ResultBuilder& ResultBuilder::setRhs( std::string const& rhs ) { | ||||
|         m_exprComponents.rhs = rhs; | ||||
|         return *this; | ||||
|     } | ||||
|     ResultBuilder& ResultBuilder::setOp( std::string const& op ) { | ||||
|         m_exprComponents.op = op; | ||||
|         return *this; | ||||
|     } | ||||
|  | ||||
|     void ResultBuilder::endExpression() { | ||||
|         m_exprComponents.testFalse = isFalseTest( m_assertionInfo.resultDisposition ); | ||||
|         captureExpression(); | ||||
|     } | ||||
|  | ||||
|     void ResultBuilder::useActiveException( ResultDisposition::Flags resultDisposition ) { | ||||
|         m_assertionInfo.resultDisposition = resultDisposition; | ||||
|         m_resultBuilder << Catch::translateActiveException(); | ||||
|         m_stream.oss << Catch::translateActiveException(); | ||||
|         captureResult( ResultWas::ThrewException ); | ||||
|     } | ||||
|  | ||||
|     void ResultBuilder::captureResult( ResultWas::OfType resultType ) { | ||||
|         m_resultBuilder.setResultType( resultType ); | ||||
|         setResultType( resultType ); | ||||
|         captureExpression(); | ||||
|     } | ||||
|  | ||||
|     void ResultBuilder::captureExpression() { | ||||
|         AssertionResult result = m_resultBuilder.buildResult( m_assertionInfo ); | ||||
|         AssertionResult result = build(); | ||||
|         getResultCapture().assertionEnded( result ); | ||||
|  | ||||
|         if( !result.isOk() ) { | ||||
| @@ -64,6 +87,45 @@ namespace Catch { | ||||
|     bool ResultBuilder::shouldDebugBreak() const { return m_shouldDebugBreak; } | ||||
|     bool ResultBuilder::allowThrows() const { return getCurrentContext().getConfig()->allowThrows(); } | ||||
|  | ||||
|     AssertionResult ResultBuilder::build() const | ||||
|     { | ||||
|         assert( m_data.resultType != ResultWas::Unknown ); | ||||
|  | ||||
|         AssertionResultData data = m_data; | ||||
|  | ||||
|         // Flip bool results if testFalse is set | ||||
|         if( m_exprComponents.testFalse && data.resultType == ResultWas::Ok ) | ||||
|             data.resultType = ResultWas::ExpressionFailed; | ||||
|         else if( m_exprComponents.testFalse && data.resultType == ResultWas::ExpressionFailed ) | ||||
|             data.resultType = ResultWas::Ok; | ||||
|  | ||||
|         data.message = m_stream.oss.str(); | ||||
|         data.reconstructedExpression = reconstructExpression(); | ||||
|         if( m_exprComponents.testFalse ) { | ||||
|             if( m_exprComponents.op == "" ) | ||||
|                 data.reconstructedExpression = "!" + data.reconstructedExpression; | ||||
|             else | ||||
|                 data.reconstructedExpression = "!(" + data.reconstructedExpression + ")"; | ||||
|         } | ||||
|         return AssertionResult( m_assertionInfo, data ); | ||||
|     } | ||||
|     std::string ResultBuilder::reconstructExpression() const { | ||||
|         if( m_exprComponents.op == "" ) | ||||
|             return m_exprComponents.lhs.empty() ? m_assertionInfo.capturedExpression : m_exprComponents.op + m_exprComponents.lhs; | ||||
|         else if( m_exprComponents.op == "matches" ) | ||||
|             return m_exprComponents.lhs + " " + m_exprComponents.rhs; | ||||
|         else if( m_exprComponents.op != "!" ) { | ||||
|             if( m_exprComponents.lhs.size() + m_exprComponents.rhs.size() < 40 && | ||||
|                 m_exprComponents.lhs.find("\n") == std::string::npos && | ||||
|                 m_exprComponents.rhs.find("\n") == std::string::npos ) | ||||
|                 return m_exprComponents.lhs + " " + m_exprComponents.op + " " + m_exprComponents.rhs; | ||||
|             else | ||||
|                 return m_exprComponents.lhs + "\n" + m_exprComponents.op + "\n" + m_exprComponents.rhs; | ||||
|         } | ||||
|         else | ||||
|             return "{can't expand - use " + m_assertionInfo.macroName + "_FALSE( " + m_assertionInfo.capturedExpression.substr(1) + " ) instead of " + m_assertionInfo.macroName + "( " + m_assertionInfo.capturedExpression + " ) for better diagnostics}"; | ||||
|     } | ||||
|  | ||||
| } // end namespace Catch | ||||
|  | ||||
| #endif // TWOBLUECUBES_CATCH_RESULT_BUILDER_HPP_INCLUDED | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Phil Nash
					Phil Nash